pytorch - ✅(Solved) Fix Cannot JIT a deprecated class [1 pull requests, 2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#183520Fetched 2026-05-14 03:28:34
View on GitHub
Comments
2
Participants
2
Timeline
13
Reactions
0
Author
Participants
Timeline (top)
mentioned ×4subscribed ×4commented ×2cross-referenced ×1

Error Message

TypeError: module, class, method, function, traceback, frame, or code object was expected, got builtin_function_or_method

Fix Action

Fixed

PR fix notes

PR #183522: Fix using torch.cuda.amp. autocast with JIT on Python 3.13

Description (problem / solution / changelog)

The @deprecated wrapper causes a wrapper method __new__ to be injected. This causes it to no longer be treated as a builtin by inspect.isbuiltin but e.g inspect.getsource looks at the unwrapped function, which is still the object.__new__ builtin and fails with

TypeError: module, class, method, function, traceback, frame, or code object was expected, got builtin_function_or_method

In Python < 3.13 the custom __new__ is not defined as a wrapper so it happens to work. Fix by using the consistenly in all Python versions until JIT is fixed to handle those cases, see #183520

@fffrog

Changed files

  • torch/cuda/amp/autocast_mode.py (modified, +18/-15)

Code Example

from typing_extensions import deprecated
import torch

@deprecated("!")
class Foo:
    def __init__(self):
        pass

@torch.jit.script
def fn():
    f=Foo()
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

This turned up after https://github.com/pytorch/pytorch/pull/163654 was merged and included in PyTorch 2.10: Some tests use the now deprecated class in a JIT context which does not work. MWE:

from typing_extensions import deprecated
import torch

@deprecated("!")
class Foo:
    def __init__(self):
        pass

@torch.jit.script
def fn():
    f=Foo()

TypeError: module, class, method, function, traceback, frame, or code object was expected, got builtin_function_or_method

Cause: The __new__ method wrapper added by @deprecated causes it to be picked up by the JIT passes which then fails in 2 places:

  1. https://github.com/pytorch/pytorch/blob/24cdfbdd4ce3d3b080e64e6a8b03a8ecda547bd0/torch/_jit_internal.py#L593 doesn't detect it as builtin anymore which makes inspect.getsource in inspect.getsource fail
  2. It now appears in cls.__dict__ so it is not filtered anymore at https://github.com/pytorch/pytorch/blob/f605bd7dcc16ff97328db88b1005a9aecff2fed4/torch/jit/frontend.py#L263 which makes it fail in get_source_lines_and_files on calling inspect.getsourcefile(obj)

Test failure is reproducible by running python test/test_jit_autocast.py TestAutocast.test_callees on a CUDA build with Python 3.13, the snipped above is a reduced sample. This suggests there is no CI using Python 3.13 or this would have been detected earlier.

Partial fix is using inspect.unwrap before further processing which would yield the underlying method, e.g. object.__new__ in this case. But I'd say that the wrapped method is virtually never the one meant to be inspected: A signature of *args, **kwargs as usually used by wrappers is too generic to be helpful for JIT

Versions

Test failures introduced in PyTorch 2.10, still present in 2.12-rc9

The actual non-support was already present in 2.5, haven't tested even older versions.

cc @EikanWang @jgong5 @wenzhe-nrv @sanchitintel

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - ✅(Solved) Fix Cannot JIT a deprecated class [1 pull requests, 2 comments, 2 participants]