pytorch - ✅(Solved) Fix torch.compile: Bad import result for types.ModuleType subclass in sys.modules [1 pull requests, 1 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#177682Fetched 2026-04-08 00:52:37
View on GitHub
Comments
1
Participants
2
Timeline
42
Reactions
0
Author
Participants
Timeline (top)
mentioned ×15subscribed ×15labeled ×6referenced ×3

Error Message

torch._dynamo.exc.Unsupported: Bad import result

Fix Action

Fixed

PR fix notes

PR #177824: fixed:torch.compile Bad import result for types.ModuleType

Description (problem / solution / changelog)

Fixes #177682 torch.compile: Bad import result for types.ModuleType subclass in sys.modules. user define "_ConfigModule" is a subclass of types.ModuleType so change from "istype(value, types.ModuleType)" to "isinstance(value, types.ModuleType)"

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

Changed files

  • test/dynamo/test_misc.py (modified, +20/-0)
  • torch/_dynamo/symbolic_convert.py (modified, +1/-1)

Code Example

torch._dynamo.exc.Unsupported: Bad import result

---

import sys
import types
import torch

class _ConfigModule(types.ModuleType):
    x = 1

_ConfigModule.__module__ = __name__
sys.modules["my_config"] = _ConfigModule("my_config")

@torch.compile(fullgraph=True)
def f():
    import my_config  # noqa: F401
    return torch.tensor(1)

f()
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

torch.compile raises torch._dynamo.exc.Unsupported: Bad import result when importing a module that is a types.ModuleType subclass inserted into sys.modules. Dynamo doesn't recognize the subclass as a valid Python module.

This is the same pattern used by torch._dynamo.config (which is a _ConfigModule, a types.ModuleType subclass) and _SpmdTypesModule.

Error

torch._dynamo.exc.Unsupported: Bad import result

To Reproduce

import sys
import types
import torch

class _ConfigModule(types.ModuleType):
    x = 1

_ConfigModule.__module__ = __name__
sys.modules["my_config"] = _ConfigModule("my_config")

@torch.compile(fullgraph=True)
def f():
    import my_config  # noqa: F401
    return torch.tensor(1)

f()

Expected behavior

Dynamo should recognize types.ModuleType subclasses as valid Python modules, since isinstance(obj, types.ModuleType) returns True for them. The import should succeed without a graph break.

Versions

trunk

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

extent analysis

Fix Plan

To fix the torch._dynamo.exc.Unsupported: Bad import result error, we need to modify the torch.compile function to recognize types.ModuleType subclasses as valid Python modules.

Here are the concrete steps:

  • Modify the torch._dynamo module to check if the imported module is an instance of types.ModuleType using isinstance.
  • If it is, proceed with the import without raising an error.

Example code:

import sys
import types
import torch

class _ConfigModule(types.ModuleType):
    x = 1

_ConfigModule.__module__ = __name__
sys.modules["my_config"] = _ConfigModule("my_config")

# Patch torch._dynamo to recognize types.ModuleType subclasses
import torch._dynamo as dynamo
original_import = dynamo.import_module
def patched_import(name, *args, **kwargs):
    module = original_import(name, *args, **kwargs)
    if isinstance(module, types.ModuleType):
        return module
    raise dynamo.exc.Unsupported(f"Bad import result: {name}")
dynamo.import_module = patched_import

@torch.compile(fullgraph=True)
def f():
    import my_config  # noqa: F401
    return torch.tensor(1)

f()

Verification

To verify that the fix worked, run the modified code and check that the torch._dynamo.exc.Unsupported: Bad import result error is no longer raised.

Extra Tips

Note that this fix is a temporary workaround and may need to be revisited in future versions of PyTorch. Additionally, be cautious when modifying internal PyTorch modules, as this can have unintended consequences.

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…

FAQ

Expected behavior

Dynamo should recognize types.ModuleType subclasses as valid Python modules, since isinstance(obj, types.ModuleType) returns True for them. The import should succeed without a graph break.

Still need to ship something?

×6

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

Back to top recommendations

TRENDING