8000 torch.compile fails for some torch.script.jit wrapped class method with 'torch._C.ScriptFunction' object has no attribute '__name__' · Issue #153607 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

torch.compile fails for some torch.script.jit wrapped class method with 'torch._C.ScriptFunction' object has no attribute '__name__' #153607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
8000
alexfanqi opened this issue May 15, 2025 · 4 comments
Labels
module: dynamo oncall: jit Add this issue/PR to JIT oncall triage queue oncall: pt2 triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@alexfanqi
Copy link
alexfanqi commented May 15, 2025

🐛 Describe the bug

torch.compile fails for this common way of speeding up code with torch.script.jit, with error message
'torch._C.ScriptFunction' object has no attribute '__name__'

reproducer

import torch

x = torch.randint(2000, (1, 14), dtype=torch.long)

class A:
    @staticmethod
    @torch.jit.script
    def _g(a):
        return a ** 2
    
    def g(self, a):
        return self._g(a)

_a = A()
g_compiled = torch.compile(_a.g)
g_compiled(x)
Full Error
Traceback (most recent call last):
  File "/mnt/zpool-febdash/src/novolume/test/test_jit.py", line 36, in <module>
    g_compiled(x)
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/eval_frame.py", line 574, in _fn
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/convert_frame.py", line 1380, in __call__
    return self._torchdynamo_orig_callable(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/convert_frame.py", line 1164, in __call__
    result = self._inner_convert(
             ^^^^^^^^^^^^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/convert_frame.py", line 547, in __call__
    return _compile(
           ^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/convert_frame.py", line 1036, in _compile
    raise InternalTorchDynamoError(
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/convert_frame.py", line 986, in _compile
    guarded_code = compile_inner(code, one_graph, hooks, transform)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/convert_frame.py", line 715, in compile_inner
    return _compile_inner(code, one_graph, hooks, transform)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_utils_internal.py", line 95, in wrapper_function
    return function(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/convert_frame.py", line 750, in _compile_inner
    out_code = transform_code_object(code, transform)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/bytecode_transformation.py", line 1361, in transform_code_object
    transformations(instructions, code_options)
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/convert_frame.py", line 231, in _fn
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/convert_frame.py", line 662, in transform
    tracer.run()
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/symbolic_convert.py", line 2868, in run
    super().run()
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/symbolic_convert.py", line 1052, in run
    while self.step():
          ^^^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/symbolic_convert.py", line 962, in step
    self.dispatch_table[inst.opcode](self, inst)
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/symbolic_convert.py", line 1798, in LOAD_ATTR
    self.LOAD_METHOD(inst)
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/symbolic_convert.py", line 1766, in LOAD_METHOD
    self._load_attr(inst)
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/symbolic_convert.py", line 1790, in _load_attr
    result = BuiltinVariable(getattr).call_function(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/variables/builtin.py", line 1004, in call_function
    return handler(tx, args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/variables/builtin.py", line 717, in <lambda>
    return lambda tx, args, kwargs: obj.call_function(
                                    ^^^^^^^^^^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/variables/builtin.py", line 1004, in call_function
    return handler(tx, args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/variables/builtin.py", line 852, in builtin_dispatch
    rv = fn(tx, args, kwargs)
         ^^^^^^^^^^^^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/variables/builtin.py", line 772, in call_self_handler
    result = self_handler(tx, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/variables/builtin.py", line 1704, in call_getattr
    return obj.var_getattr(tx, name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/variables/user_defined.py", line 1109, in var_getattr
    return trace_rules.lookup(func).create_with_source(func, source=source)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/trace_rules.py", line 3603, in lookup
    return lookup_inner(obj)
           ^^^^^^^^^^^^^^^^^
  File "/home/alexfanqi/micromamba/envs/ml-py312/lib/python3.12/site-packages/torch/_dynamo/trace_rules.py", line 3652, in lookup_inner
    obj and obj.__name__ == "__torch_function__"
            ^^^^^^^^^^^^
torch._dynamo.exc.InternalTorchDynamoError: AttributeError: 'torch._C.ScriptFunction' object has no attribute '__name__'

from user code:
   File "/mnt/zpool-febdash/src/novolume/test/test_jit.py", line 26, in g
    return self._g(a)

Set TORCH_LOGS="+dynamo" and TORCHDYNAMO_VERBOSE=1 for more information


You can suppress this exception and fall back to eager by setting:
    import torch._dynamo
    torch._dynamo.config.suppress_errors = True

Versions

Collecting environment information...
PyTorch version: 2.6.0
Is debug build: False
CUDA used to build PyTorch: 12.6
ROCM used to build PyTorch: N/A

OS: Fedora Linux 41 (KDE Plasma) (x86_64)
GCC version: (conda-forge gcc 13.3.0-1) 13.3.0
Clang version: 19.1.7 (Fedora 19.1.7-3.fc41)
CMake version: version 3.30.8
Libc version: glibc-2.40

Python version: 3.12.8 | packaged by conda-forge | (main, Dec 5 2024, 14:24:40) [GCC 13.3.0] (64-bit runtime)
Python platform: Linux-6.14.5-200.fc41.x86_64-x86_64-with-glibc2.40
Is CUDA available: True
CUDA runtime version: 12.8.61
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce MX550
Nvidia driver version: 570.144
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 39 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 16
On-line CPU(s) list: 0-15
Vendor ID: GenuineIntel
Model name: 12th Gen Intel(R) Core(TM) i5-12500H
CPU family: 6
Model: 154
Thread(s) per core: 2
Core(s) per socket: 12
Socket(s): 1
Stepping: 3
CPU(s) scaling MHz: 37%
CPU max MHz: 4500.0000
CPU min MHz: 400.0000
BogoMIPS: 6220.80
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect user_shstk avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi vnmi umip pku ospke waitpkg gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize arch_lbr ibt flush_l1d arch_capabilities
Virtualization: VT-x
L1d cache: 448 KiB (12 instances)
L1i cache: 640 KiB (12 instances)
L2 cache: 9 MiB (6 instances)
L3 cache: 18 MiB (1 instance)
NUMA node(s): 1
NUMA node0 CPU(s): 0-15
Vulnerability Gather data sampling: Not affected
Vulnerability Ghostwrite: Not affected
Vulnerability Itlb multihit: Not affected
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Not affected
Vulnerability Reg file data sampling: Mitigation; Clear Register File
Vulnerability Retbleed: Not affected
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; PBRSB-eIBRS SW sequence; BHI BHI_DIS_S
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Not affected

Versions of relevant libraries:
[pip3] mypy_extensions==1.0.0
[pip3] numpy==1.26.4
[pip3] numpy-groupies==0.11.2
[pip3] numpydoc==1.8.0
[pip3] onnx==1.17.0
[pip3] optree==0.15.0
[pip3] torch==2.6.0
[pip3] torch_scatter==2.1.2
[pip3] torch_sparse==0.6.18
[pip3] torchvision==0.21.0
[pip3] torchvision-extra-decoders==0.0.2
[pip3] triton==3.2.0+git576374f8
[conda] Could not collect

cc @EikanWang @jgong5 @wenzhe-nrv @sanchitintel @chauhang @penguinwu @voznesenskym @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @jiayisunx @chenyang78 @kadeng @amjames

@alexfanqi alexfanqi changed the title torch.compile cannot hanle torch.script.jit for some class method torch.compile fails for some torch.script.jit wrapped class method with 'torch._C.ScriptFunction' object has no attribute '__name__' May 15, 2025
@malfet malfet added oncall: jit Add this issue/PR to JIT oncall triage queue oncall: pt2 labels May 15, 2025
@williamwen42
Copy link
Member

Can't repro on the nightly - this was probably fixed. Can you check if you can repro on your original code on the nightly?

@williamwen42 williamwen42 added triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module module: dynamo labels May 15, 2025
@alexfanqi
Copy link
Author

The error changed to a warning on nightly.

<frozen importlib._bootstrap>:488: RuntimeWarning: The global interpreter lock (GIL) has been enabled to load module 'triton._C.libtriton', which has not declared that it can run safely without the GIL. To override this behavior and keep the GIL disabled (at your own risk), run with PYTHON_GIL=0 or -Xgil=0.
/home/alexfanqi/micromamba/envs/pytorch-nightly/lib/python3.13t/site-packages/torch/_dynamo/variables/functions.py:1459: UserWarning: Dynamo does not know how to trace the builtin `torch.jit.torch.jit.ScriptFunction.` This function is either a Python builtin (e.g. _warnings.warn) or a third-party C/C++ Python extension (perhaps created with pybind).
If it is a Python builtin, please file an issue on GitHub so the PyTorch team can add support for it and see the next case for a workaround.
If it is a third-party C/C++ Python extension, please either wrap it into a PyTorch-understood custom operator (see https://pytorch.org/tutorials/advanced/custom_ops_landing_page.html for more details) or, if it is traceable, use `torch.compiler.allow_in_graph`.
  torch._dynamo.utils.warn_once(explanation + "\n" + "\n".join(hints))
<frozen importlib._bootstrap>:488: RuntimeWarning: The global interpreter lock (GIL) has been enabled to load module 'triton._C.libtriton', which has not declared that it can run safely without the GIL. To override this behavior and keep the GIL disabled (at your own risk), run with PYTHON_GIL=0 or -Xgil=0.
nighly env

Collecting environment information...
PyTorch version: 2.8.0.dev20250515+cu128
Is debug build: False
CUDA used to build PyTorch: 12.8
ROCM used to build PyTorch: N/A

OS: Fedora Linux 41 (KDE Plasma) (x86_64)
GCC version: (GCC) 14.2.1 20250110 (Red Hat 14.2.1-7)
Clang version: 19.1.7 (Fedora 19.1.7-3.fc41)
CMake version: version 3.30.8
Libc version: glibc-2.40

Python version: 3.13.3 experimental free-threading build | packaged by conda-forge | (main, Apr 14 2025, 20:49:24) [GCC 13.3.0] (64-bit runtime)
Python platform: Linux-6.14.5-200.fc41.x86_64-x86_64-with-glibc2.40
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce MX550
Nvidia driver version: 570.144
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 39 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 16
On-line CPU(s) list: 0-15
Vendor ID: GenuineIntel
Model name: 12th Gen Intel(R) Core(TM) i5-12500H
CPU family: 6
Model: 154
Thread(s) per core: 2
Core(s) per socket: 12
Socket(s): 1
Stepping: 3
CPU(s) scaling MHz: 25%
CPU max MHz: 4500.0000
CPU min MHz: 400.0000
BogoMIPS: 6220.80
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect user_shstk avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi vnmi umip pku ospke waitpkg gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize arch_lbr ibt flush_l1d arch_capabilities
Virtualization: VT-x
L1d cache: 448 KiB (12 instances)
L1i cache: 640 KiB (12 instances)
L2 cache: 9 MiB (6 instances)
L3 cache: 18 MiB (1 instance)
NUMA node(s): 1
NUMA node0 CPU(s): 0-15
Vulnerability Gather data sampling: Not affected
Vulnerability Ghostwrite: Not affected
Vulnerability Itlb multihit: Not affected
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Not affected
Vulnerability Reg file data sampling: Mitigation; Clear Register File
Vulnerability Retbleed: Not affected
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; PBRSB-eIBRS SW sequence; BHI BHI_DIS_S
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Not affected

Versions of relevant libraries:
[pip3] numpy==2.2.5
[pip3] nvidia-cublas-cu12==12.8.3.14
[pip3] nvidia-cuda-cupti-cu12==12.8.57
[pip3] nvidia-cuda-nvrtc-cu12==12.8.61
[pip3] nvidia-cuda-runtime-cu12==12.8.57
[pip3] nvidia-cudnn-cu12==9.8.0.87
[pip3] nvidia-cufft-cu12==11.3.3.41
[pip3] nvidia-curand-cu12==10.3.9.55
[pip3] nvidia-cusolver-cu12==11.7.2.55
[pip3] nvidia-cusparse-cu12==12.5.7.53
[pip3] nvidia-cusparselt-cu12==0.6.3
[pip3] nvidia-nccl-cu12==2.26.5
[pip3] nvidia-nvjitlink-cu12==12.8.61
[pip3] nvidia-nvtx-cu12==12.8.55
[pip3] pytorch-triton==3.3.0+git96316ce5
[pip3] torch==2.8.0.dev20250515+cu128
[conda] Could not collect

@williamwen42
Copy link
Member

This graph break is happening because we're doing some shortcuts in variable tracking during the attribute access:

elif isinstance(subobj, staticmethod):
.

As an immediate workaround, you can try moving the static method outside the class or changing the static method to a class method.

@williamwen42
Copy link
Member

cc @anijain2305 @StrongerXi another case where we should consider going through variable builder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: dynamo oncall: jit Add this issue/PR to JIT oncall triage queue oncall: pt2 triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

3 participants
0