Symbolic trace failed when using HFTracer with Qwen3Model. AttributeError: 'Tensor' object has no attribute 'get'.
Author: tongl2
Pull request: no
Labels: name: bug

### System Info

- `transformers` version: 4.57.3
- Platform: Linux-6.8.0-71-generic-x86_64-with-glibc2.39
- Python version: 3.11.14
- Huggingface_hub version: 0.36.0
- Safetensors version: 0.7.0
- Accelerate version: not installed
- Accelerate config: not found
- DeepSpeed version: not installed
- PyTorch version (accelerator?): 2.8.0+cu129 (CUDA)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using distributed or parallel set-up in script?: <fill in>
- Using GPU in script?: <fill in>
- GPU type: NVIDIA H20


### Who can help?

@Cyrilvallez I'm not quite sure with the maintainer of utils.fx module, correct me pls if I tag the wrong maintainer.

### Information

- [ ] The official example scripts
- [x] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [x] My own task or dataset (give details below)

### Reproduction

1. Run script
```from transformers import AutoModel, AutoConfig
from transformers.utils.fx import HFTracer, symbolic_trace
from transformers.modeling_utils import no_init_weights
import torch
from torch.fx import GraphModule

cfg = AutoConfig.from_pretrained('qwen3_8b_config.json')
with no_init_weights():
    model = AutoModel.from_config(cfg).eval()

dummy_input = {
    "input_ids": torch.randint(0, 1000, (1, 64)),
    "attention_mask": torch.ones(1, 64, dtype=torch.long),
    'position_ids': None,
    'past_key_values': None,
    'inputs_embeds': None,
    'use_cache': None,
    'cache_position': None,
}

# For debug:
import torch
import traceback
def _tensor_get(self, key, default=None):
    print("\n⚠️  A Tensor was mistakenly called with .get()!")
    print(f'{type(self)=}')
    print(f'{self=}')
    print("Tensor shape:", self.shape, "dtype:", self.dtype)
    print("Call stack:")
    traceback.print_stack()
    raise AttributeError(f"'Tensor' object has no attribute 'get'. This tensor was used at the above location.")
torch.Tensor.get = _tensor_get

tracer = HFTracer()
graph = tracer.trace(model, concrete_args=dummy_input)
graph_module = GraphModule(model, graph)

graph_module.graph.print_tabular()
```
2. Got Result
```
/***/opt/miniconda3/envs/torch-primer-311/lib/python3.11/site-packages/torch/fx/_symbolic_trace.py:924: UserWarning: Was not able to add assertion to guarantee correct input input_ids to specialized function. It is up to the user to make sure that your inputs match the inputs you specialized the function with.
  warnings.warn(
/***/opt/miniconda3/envs/torch-primer-311/lib/python3.11/site-packages/torch/fx/_symbolic_trace.py:924: UserWarning: Was not able to add assertion to guarantee correct input attention_mask to specialized function. It is up to the user to make sure that your inputs match the inputs you specialized the function with.
  warnings.warn(

⚠️  A Tensor was mistakenly called with .get()!
type(self)=<class 'torch.Tensor'>
self=tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
Tensor shape: torch.Size([1, 64]) dtype: torch.int64
Call stack:
  File "/***/workspace/torch_primer/code/fx/fx_qwen3_next_new.py", line 35, in <module>
    graph = tracer.trace(model, concrete_args=dummy_input)
  File "/***/opt/miniconda3/envs/torch-primer-311/lib/python3.11/site-packages/transformers/utils/fx.py", line 1316, in trace
    self.graph = super().trace(root, concrete_args=concrete_args)
  File "/***/opt/miniconda3/envs/torch-primer-311/lib/python3.11/site-packages/torch/fx/_symbolic_trace.py", line 851, in trace
    (self.create_arg(fn(*args)),),
  File "/***/opt/miniconda3/envs/torch-primer-311/lib/python3.11/site-packages/transformers/utils/generic.py", line 970, in wrapper
    )
  File "/***/workspace/torch_primer/code/fx/fx_qwen3_next_new.py", line 30, in _tensor_get
    traceback.print_stack()
Traceback (most recent call last):
  File "/***/workspace/torch_primer/code/fx/fx_qwen3_next_new.py", line 35, in <module>
    graph = tracer.trace(model, concrete_args=dummy_input)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/***/opt/miniconda3/envs/torch-primer-311/lib/python3.11/site-packages/transformers/utils/fx.py", line 1316, in trace
    self.graph = super().trace(root, concrete_args=concrete_args)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/***/opt/miniconda3/envs/torch-primer-311/lib/python3.11/site-packages/torch/fx/_symbolic_trace.py", line 851, in trace
    (self.create_arg(fn(*args)),),
                     ^^^^^^^^^
  File "/***/opt/miniconda3/envs/torch-primer-311/lib/python3.11/site-packages/transformers/utils/generic.py", line 970, in wrapper
    )
^^^^^
  File "/***/workspace/torch_primer/code/fx/fx_qwen3_next_new.py", line 31, in _tensor_get
    raise AttributeError(f"'Tensor' object has no attribute 'get'. This tensor was used at the above location.")
AttributeError: 'Tensor' object has no attribute 'get'. This tensor was used at the above location.

```


### Expected behavior

`graph_module.graph.print_tabular()` is supposed to work correctly.

Comments:
I further dived into the problem. It seems like the core issue is about **check_model_inputs** wrapper.
Here in torch Tracer.trace method (_symbolic_trace.py:848), a wrapped forward method is called: `fn(*args)`, where fn is `fn=<function check_model_inputs.<locals>.wrapped_fn.<locals>.wrapper at 0x7a0b34d6a020>`, and `args` is a tuple `args[1:]=(tensor([[437, 190, 962, 641, 535, 899, 603, 225, 794,  54, 151, 848, 881, 323,
         189, 511, 708, 194, 642, 357, 903, 168, 348, 803, 114, 393, 135, 211,
         186, 476, 348, 861, 139, 873, 888, 211, 385, 781, 166,  88, 735, 341,
         973,  49, 952, 944, 753, 194, 877,  95, 616, 658, 909, 503,  69, 380,
           5, 694, 624,  40, 939, 945,  28, 353]]), tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]), None, None, None, None, None, Proxy(_kwargs))
`

I then printed args and kwargs inside `check_model_inputs`'s `wrapper` function (generic.py:962). I got `args=tensor([[437, 190, 962, 641, 535, 899, 603, 225, 794,  54, 151, 848, 881, 323,
         189, 511, 708, 194, 642, 357, 903, 168, 348, 803, 114, 393, 135, 211,
         186, 476, 348, 861, 139, 873, 888, 211, 385, 781, 166,  88, 735, 341,
         973,  49, 952, 944, 753, 194, 877,  95, 616, 658, 909, 503,  69, 380,
           5, 694, 624,  40, 939, 945,  28, 353]])`, and `kwargs=tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])`. The caller unpacked args, but the callee(wrapper) failed to pack args and kwargs. So the args and kwargs kept unpacked values. I have no clue why is that.

I can take a stab at it. will update here if I find something.

Hey! We do not really support `torch.fx`, as it's somewhat deprecated in favor of `dynamo` tracing instead (compile and export).
Feel free to dive in and open a PR if you find a simple enough fix, but we will otherwise not put too much efforts to make it work!

I see. I will update here if I find something interesting.

Okay, it seems like when the traced `forward` has arbitrary arguments (`*args `or `**kwargs`), torch.fx.Tracer will go into a `_patch_function` function to refactor the function. `_patch_function` seems fail to process wrapped functions properly, and leads to this exception. So far I see no bugs in transformers repo, so I started a seperate issue in pytorch repo (https://github.com/pytorch/pytorch/issues/171981). I will close this issue as done. Great thanks for your replies @vaibhav-research @Cyrilvallez