Lightning-AI / Lightning-AI/lightning-thunder

Support symbolic tensor shapes and dynamic dimensions

Open
#2,741 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement symbolic values
Dominant language
Python
Stars
1.5k
Forks
121
PR merge metrics
No merged PRs in 30d

Description

🚀 Feature

Thunder needs to support tensors with symbolic dimensions in their shapes, where one or more dimensions are not known at compile time but are tracked symbolically.

Current Behavior

Thunder likely concretizes shapes at compile/trace time.

Expected Behavior

Support tensor shapes with symbolic dimensions:

  • "bf16[1, s0, 5120]" - batch size concrete, seq_len symbolic
  • "i64[s0]" - fully symbolic shape
  • Operations preserve symbolic dimensions through computation

Example from torch.compile

to: "i64[1, s50]" = l_args_0_.to(device(type='cuda', index=0))
inputs_embeds: "bf16[1, s50, 5120]" = torch.nn.functional.embedding(to, ...)
batched_outputs_2: "b8[s50, 1056]" = torch._functorch.predispatch._remove_batch_dim(...)

Minimal Reproduction Case

import torch
import thunder

@thunder.jit
def process_variable_length(input_ids: torch.Tensor) -> torch.Tensor:
    """
    Process input with variable sequence length.
    input_ids: [1, seq_len] where seq_len is symbolic (s50)
    """
    batch_size, seq_len = input_ids.shape  # seq_len should be symbolic
    
    # Embedding lookup - output shape should be [1, s50, 5120]
    embedding_weight = torch.randn(50000, 5120, device='cuda', dtype=torch.bfloat16)
    embeds = torch.nn.functional.embedding(input_ids, embedding_weight)
    
    # Operations should preserve symbolic dimension
    # embeds.shape = [1, s50, 5120]
    normed = embeds / embeds.norm(dim=-1, keepdim=True)
    
    return normed

for seq_len in [32, 128, 256]:
    input_ids = torch.randint(0, 50000, (1, seq_len), device='cuda')
    output = process_variable_length(input_ids)
    assert output.shape == (1, seq_len, 5120)

print(process_variable_length._lc_cs.last_epilogue_traces[-1])
# def epilogue(normed):
#   # normed: "cuda:0 bf16[1, 256, 5120]" <---- concrete shape but should be symbolic!
#   return normed

Shape Propagation Rules

  1. Input tensors can have symbolic dimensions
  2. Operations propagate symbolic dimensions:
    • [s0, 5120] @ [5120, 128][s0, 128]
    • [1, s0, 5120].mean(-1)[1, s0]
  3. Reshaping with symbolic dims: .view(-1, s0, 128)

Success Criteria

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal reproduction in the issue and inspect the generated epilogue for inputs with sequence lengths 32, 128, and 256. Trace how symbolic dimensions would propagate through embedding, normalization, matrix multiplication, reduction, and reshape operations; done means runtime-generated code preserves symbolic shapes and integrates with issue #2735.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
compilers, machine-learning
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.