Skip to content

Support symbolic values in torch.arange and other tensor operations #2738

@IvanYashchuk

Description

@IvanYashchuk

🚀 Feature

Thunder needs to support using symbolic scalar values as arguments to tensor creation operations, particularly torch.arange with symbolic start/end bounds.

Current Behavior

Likely requires concrete values for tensor operations.

Expected Behavior

Support symbolic values as arguments to:

  • torch.arange(symbolic_start, symbolic_end, ...)
  • torch.zeros(symbolic_size, ...)
  • .view(symbolic_shape, ...) and other reshaping ops
  • .index_copy_(dim, symbolic_indices, ...)

Example from torch.compile

# cache_position: "i64[1]" = torch.arange(cumulative_length, add, device=...)
cache_position: "i64[s50]" = torch.arange(
    l_kwargs_past_key_values_layers_0_cumulative_length, 
    add,  # add is Sym(s50 + s67)
    device=device(type='cuda', index=0)
)

The output tensor has symbolic shape "i64[s50]".

Minimal Reproduction Case

import torch
import thunder

@thunder.jit
def create_cache_position(cumulative_length: int, seq_len: int) -> torch.Tensor:
    """Create cache position indices using symbolic bounds."""
    new_cumulative_length = cumulative_length + seq_len
    
    # torch.arange with symbolic start and end
    cache_position = torch.arange(
        cumulative_length,
        new_cumulative_length,
        device='cuda'
    )
    
    return cache_position

result = create_cache_position(1024, 32)
assert result.shape == (32,)
assert result[0].item() == 1024
assert result[-1].item() == 1055

Related Operations

  • torch.arange(symbolic, symbolic)
  • torch.zeros(symbolic, *shape)
  • tensor.view(-1, symbolic)
  • tensor.scatter(1, symbolic_indices, value)

Success Criteria

  • torch.arange accepts symbolic start/end
  • Output tensor shape is correctly inferred as symbolic
  • Other tensor ops support symbolic size arguments
  • Generated code handles runtime dimensions correctly

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions