pytorch - 💡(How to fix) Fix DISABLED test_disable_enable_torch_function_ctx (__main__.TestTorchFunctionMode) [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#176965Fetched 2026-04-08 00:23:38
View on GitHub
Comments
2
Participants
2
Timeline
30
Reactions
0
Timeline (top)
mentioned ×12subscribed ×12labeled ×4commented ×2

Error Message

Traceback (most recent call last): File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/testing/_internal/common_utils.py", line 3370, in wrapper method(*args, **kwargs) ~~~~~~^^^^^^^^^^^^^^^^^ File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 2340, in call result = self._torchdynamo_orig_backend( frame, cache_entry, self.hooks, frame_state, skip=1 ) File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 2076, in call result = self._inner_convert( frame, cache_entry, hooks, frame_state, skip=skip + 1 ) File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 727, in call result = _compile( frame.f_code, ...<16 lines>... convert_frame_box=self._box, ) File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 1851, in _compile guarded_code, tracer_output = compile_inner(code, one_graph, hooks) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_utils_internal.py", line 96, in wrapper_function return function(*args, **kwargs) File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 1495, in compile_inner return _compile_inner(code, one_graph, hooks) File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 1540, in _compile_inner dynamo_output = compile_frame( code, ...<11 lines>... package=package, ) File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 1403, in compile_frame bytecode, tracer_output = transform_code_object(code, transform) ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^ File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/bytecode_transformation.py", line 1626, in transform_code_object tracer_output = transformations(instructions, code_options) File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 1375, in transform tracer_output = trace_frame( code, ...<14 lines>... package=package, ) File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 368, in _fn assert guards.check(), ( ~~~~~~~~~~~~^^ AssertionError: Global torch_function state changed while dynamo tracing, please report a bug

To execute this test, run the following from the base repo dir: PYTORCH_TEST_WITH_DYNAMO=1 python test/test_overrides.py TestTorchFunctionMode.test_disable_enable_torch_function_ctx

This message can be suppressed by setting PYTORCH_PRINT_REPRO_ON_FAILURE=0

Root Cause

This test was disabled because it is failing in CI. See recent examples and the most recent trunk workflow logs.

Code Example

Traceback (most recent call last):
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/testing/_internal/common_utils.py", line 3370, in wrapper
    method(*args, **kwargs)
    ~~~~~~^^^^^^^^^^^^^^^^^
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 2340, in __call__
    result = self._torchdynamo_orig_backend(
        frame, cache_entry, self.hooks, frame_state, skip=1
    )
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 2076, in __call__
    result = self._inner_convert(
        frame, cache_entry, hooks, frame_state, skip=skip + 1
    )
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 727, in __call__
    result = _compile(
        frame.f_code,
    ...<16 lines>...
        convert_frame_box=self._box,
    )
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 1851, in _compile
    guarded_code, tracer_output = compile_inner(code, one_graph, hooks)
                                  ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_utils_internal.py", line 96, in wrapper_function
    return function(*args, **kwargs)
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 1495, in compile_inner
    return _compile_inner(code, one_graph, hooks)
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 1540, in _compile_inner
    dynamo_output = compile_frame(
        code,
    ...<11 lines>...
        package=package,
    )
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 1403, in compile_frame
    bytecode, tracer_output = transform_code_object(code, transform)
                              ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/bytecode_transformation.py", line 1626, in transform_code_object
    tracer_output = transformations(instructions, code_options)
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 1375, in transform
    tracer_output = trace_frame(
        code,
    ...<14 lines>...
        package=package,
    )
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 368, in _fn
    assert guards.check(), (
           ~~~~~~~~~~~~^^
AssertionError: Global torch_function state changed while dynamo tracing, please report a bug


To execute this test, run the following from the base repo dir:
    PYTORCH_TEST_WITH_DYNAMO=1 python test/test_overrides.py TestTorchFunctionMode.test_disable_enable_torch_function_ctx

This message can be suppressed by setting PYTORCH_PRINT_REPRO_ON_FAILURE=0
RAW_BUFFERClick to expand / collapse

Platforms: dynamo

This test was disabled because it is failing in CI. See recent examples and the most recent trunk workflow logs.

Over the past 6 hours, it has been determined flaky in 7 workflow(s) with 7 failures and 7 successes.

Debugging instructions (after clicking on the recent samples link): DO NOT ASSUME THINGS ARE OKAY IF THE CI IS GREEN. We now shield flaky tests from developers so CI will thus be green but it will be harder to parse the logs. To find relevant log snippets:

  1. Click on the workflow logs linked above
  2. Click on the Test step of the job so that it is expanded. Otherwise, the grepping will not work.
  3. Grep for test_disable_enable_torch_function_ctx
  4. There should be several instances run (as flaky tests are rerun in CI) from which you can study the logs.
<details><summary>Sample error message</summary>
Traceback (most recent call last):
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/testing/_internal/common_utils.py", line 3370, in wrapper
    method(*args, **kwargs)
    ~~~~~~^^^^^^^^^^^^^^^^^
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 2340, in __call__
    result = self._torchdynamo_orig_backend(
        frame, cache_entry, self.hooks, frame_state, skip=1
    )
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 2076, in __call__
    result = self._inner_convert(
        frame, cache_entry, hooks, frame_state, skip=skip + 1
    )
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 727, in __call__
    result = _compile(
        frame.f_code,
    ...<16 lines>...
        convert_frame_box=self._box,
    )
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 1851, in _compile
    guarded_code, tracer_output = compile_inner(code, one_graph, hooks)
                                  ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_utils_internal.py", line 96, in wrapper_function
    return function(*args, **kwargs)
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 1495, in compile_inner
    return _compile_inner(code, one_graph, hooks)
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 1540, in _compile_inner
    dynamo_output = compile_frame(
        code,
    ...<11 lines>...
        package=package,
    )
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 1403, in compile_frame
    bytecode, tracer_output = transform_code_object(code, transform)
                              ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/bytecode_transformation.py", line 1626, in transform_code_object
    tracer_output = transformations(instructions, code_options)
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 1375, in transform
    tracer_output = trace_frame(
        code,
    ...<14 lines>...
        package=package,
    )
  File "/opt/conda/envs/py_3.14/lib/python3.14/site-packages/torch/_dynamo/convert_frame.py", line 368, in _fn
    assert guards.check(), (
           ~~~~~~~~~~~~^^
AssertionError: Global torch_function state changed while dynamo tracing, please report a bug


To execute this test, run the following from the base repo dir:
    PYTORCH_TEST_WITH_DYNAMO=1 python test/test_overrides.py TestTorchFunctionMode.test_disable_enable_torch_function_ctx

This message can be suppressed by setting PYTORCH_PRINT_REPRO_ON_FAILURE=0
</details>

Test file path: test_overrides.py

For all disabled tests (by GitHub issue), see https://hud.pytorch.org/disabled.

cc @hameerabbasi @rgommers @ezyang @chauhang @penguinwu

extent analysis

Fix Plan

1. Update test_disable_enable_torch_function_ctx to use torch.dynamo.freeze instead of torch.dynamo.convert

The issue is caused by the test modifying the global torch_function state while dynamo tracing is active. We can fix this by using torch.dynamo.freeze to freeze the tracing state before modifying the global state.

import torch
from torch.dynamo import freeze

def test_disable_enable_torch_function_ctx():
    # Freeze the tracing state
    with freeze():
        # Modify the global torch_function state
        torch._C._set_torch_function_state('new_state')

2. Update the test to use PYTORCH_TEST_WITH_DYNAMO=0 to disable dynamo tracing

We can also update the test to use PYTORCH_TEST_WITH_DYNAMO=0 to disable dynamo tracing, which will prevent the test from modifying the global torch_function state.

import os

def test_disable_enable_torch_function_ctx():
    # Disable dynamo tracing
    os.environ['PYTORCH_TEST_WITH_DYNAMO'] = '0'
    # Modify the global torch_function state
    torch._C._set_torch_function_state('new_state')

3. Update the test to use torch.dynamo.reset to reset the tracing state

Alternatively, we can update the test to use torch.dynamo.reset to reset the tracing state before modifying the global torch_function state.

import torch
from torch.dynamo import reset

def test_disable_enable_torch_function_ctx():
    # Reset the tracing state
    reset()
    # Modify the global torch_function state
    torch._C._set_torch_function_state('new_state')

Verification

To verify that the fix worked, you can run the test with the updated code

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 - 💡(How to fix) Fix DISABLED test_disable_enable_torch_function_ctx (__main__.TestTorchFunctionMode) [2 comments, 2 participants]