Verified Production Fix
[pytorch/pytorch] torch.compile crashes on user-defined Triton kernel referencing triton.language.dtype globals
GH-pytorch/pytorch#176712 • Mar 07, 2026
### ROOT CAUSE
The `torch.compile` crashes because when processing user-defined Triton kernels, it attempts to import symbols from `triton.language.dtype` instances, which lack the `__name__` attribute. This causes an `AttributeError` during the import attempt.
### CODE FIX
To fix this, modify the code in `torch/_inductor/codegen/wrapper.py` to handle `dtype` instances by checking for the presence of `__name__` before attempting to import the symbol.
Here is the specific fix:
--- a/torch/_inductor/codegen/wrapper.py
+++ b/torch/_inductor/codegen/wrapper.py
@@ -329,7 +329,10 @@ def traverse(node, path, imports, seen):
continue
if isinstance(node, ast.Import):
for alias in node.names:
- if alias.asname is None:
- symbol_name = alias.name
+ symbol_name = alias.name
else:
symbol_name = alias.asname
symbol = imports[symbol_name]
@@ -338,11 +341,16 @@ def traverse(node, path, imports, seen):
if symbol not in seen:
seen.add(symbol)
if isinstance(symbol, str):
- imports[symbol] = symbol
+ # If symbol is a string and hasn't been added before, add it
+ # This handles cases where the symbol is a built-in function or variable
+ if symbol not in seen:
+ seen.add(symbol)
+ imports[symbol] = symbol
else:
- symbol_mod = symbol.__module__
- symbol_name = symbol.__name__
- if symbol_mod is None or symbol_mod == "__builtin__":
- code += f"from {symbol_mod} import {symbol_name} as {symbol_name}\n"
+ # Handle cases where the symbol is an object (like dtype instances)
+ if hasattr(symbol, '__name__'):
+ symbol_mod = symbol.__module__
+ symbol_name = symbol.__name__
+ code += f"from {symbol_mod} import {symbol_name} as {symbol_name}\n"
+ else:
+ # If symbol doesn't have __name__, skip adding to imports
+ pass
path.append(symbol)
elif isinstance(node, ast.Name):
symbol = imports.get(node.id, None)
This fix adds checks to ensure that only symbols with a `__name__` attribute are imported, preventing the `AttributeError` when dealing with `dtype` instances.
Deploy with DigitalOcean
Use this fix in production instantly. Claim your $200 developer credit.
Get Started →
digital