Lightning-AI / Lightning-AI/lightning-thunder

HF "deepseek-ai/deepseek-v3" TransformerLayer fails to run with thunder.jit and thunderfx

Open
#2,005 0 comments 0 reactions 1 assignee View on GitHub

@kshitij12345 is already working on this.

Since Apr 29, 2025.

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

Description

Repro
```python
import nvfuser
import pytest
import transformers
import torch
import torch.distributed as dist
from contextlib import contextmanager
from torch.distributed.tensor import DTensor
from torch.distributed.tensor.parallel import (
parallelize_module,
RowwiseParallel,
ColwiseParallel,
)

@contextmanager
def default_tensor_type(dtype=torch.float32, device="cpu"):
# Save
prev_dtype = torch.get_default_dtype()
prev_device = torch.get_default_device()

# Set
torch.set_default_dtype(dtype)
torch.set_default_device(device)

yield

# Restore
torch.set_default_dtype(prev_dtype)
torch.set_default_device(prev_device)

config = transformers.AutoConfig.from_pretrained(
"deepseek-ai/deepseek-v3", trust_remote_code=True
)

# Create only one layer which is sufficient for the test.
config.num_hidden_layers = 1
# Without this, the first and only layer will have a dense MLP instead of MoE.
config.first_k_dense_replace = 0
# Disable quantization so the test can run on A100 and is made easier for nvFuser.
delattr(config, "quantization_config")

torch.manual_seed(0)

with default_tensor_type(dtype=config.torch_dtype, device="cuda"):
model = transformers.AutoModel.from_config(config, trust_remote_code=True)
# Training is unavailable (cf. https://huggingface.co/deepseek-ai/DeepSeek-V3/blob/main/modeling_deepseek.py#L439)
model.eval()

transformer_layer = model.layers[0]

batch_size = 1
seq_len = 2048
inp = torch.randn(batch_size, seq_len, config.hidden_size)
mask = transformers.modeling_attn_mask_utils._prepare_4d_causal_attention_mask(
None, [batch_size, seq_len], inp, past_key_values_length=0
)
import thunder
from thunder.dynamo import thunderfx

# AssertionError: A symbol [Symbol name=_set_grad_enabled_with_warning] was called while processing a primitive
# tfn = thunder.jit(transformer_layer)

# torch._dynamo.exc.TorchRuntimeError: Failed running call_function >(*(FakeTensor(..., size=(), dtype=torch.int64), 0), **{}): 'ndarray' object has no attribute 'add'
tfn = thunderfx(transformer_layer)

# Error: torch._dynamo.exc.TorchRuntimeError: Failed running call_function >(*(FakeTensor(..., size=(), dtype=torch.int64), 0), **{}): 'ndarray' object has no attribute 'add'
# tfn = torch.compile(transformer_layer)

# Eager Works
# tfn = transformer_layer
(out,) = tfn(inp, attention_mask=mask)

# thunder.last_traces(tfn)[-1].save_trace("deepseek_tfm_layer_fwd.py")
# thunder.last_backward_traces(tfn)[-1].save_trace("deepseek_tfm_layer_bwd.py")
# Finish all computation and communication. Otherwise,
# destroy_process_group may deadlock.
torch.cuda.synchronize()

assert out.size() == (batch_size, seq_len, config.hidden_size)
assert out.dtype == config.torch_dtype
assert out.is_cuda

```

In case of `thunderfx`, the error comes from torch-dynamo (torch.compile)

Assigning myself for investigation.

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.