Skip to content

array_namespace incompatible with torch.compile due to using modules as dictionary key #411

Description

@lucascolley
E       torch._dynamo.exc.UncapturedHigherOrderOpError: while_loop doesn't work unless it is captured completely with torch.compile. Got Dynamo cannot determine whether the underlying object is hashable
E         Explanation: Dynamo does not know whether the underlying python object for PythonModuleVariable(<module 'scipy._external.array_api_compat.torch' from '/Users/lucascolley/ghq/github.com/scipy/scipy/build-cpu-install/usr/lib/python3.13/site-packages/scipy/_external/array_api_compat/torch/__init__.py'>) is hashable
E         Hint: Consider using a different type of object as the dictionary key instead of <class 'module'>.
E         Hint: It may be possible to write Dynamo tracing rules for this code. Please report an issue to PyTorch if you encounter this graph break often and it is causing performance issues.
E
E         Developer debug context: is_python_hashable PythonModuleVariable(<module 'scipy._external.array_api_compat.torch' from '/Users/lucascolley/ghq/github.com/scipy/scipy/build-cpu-install/usr/lib/python3.13/site-packages/scipy/_external/array_api_compat/torch/__init__.py'>)
E
E        For more details about this graph break, please visit: https://meta-pytorch.github.io/compile-graph-break-site/gb/gb0365.html
E
E       from user code:
E          File "/Users/lucascolley/ghq/github.com/scipy/scipy/.pixi/envs/torch-cpu/lib/python3.13/site-packages/torch/_higher_order_ops/while_loop.py", line 244, in _while_loop_op_wrapper
E           return while_loop_op(*args, **kwargs)
E         File "/Users/lucascolley/ghq/github.com/scipy/scipy/.pixi/envs/torch-cpu/lib/python3.13/site-packages/torch/utils/_device.py", line 109, in __torch_function__
E           return func(*args, **kwargs)
E         File "/Users/lucascolley/ghq/github.com/scipy/scipy/.pixi/envs/torch-cpu/lib/python3.13/site-packages/torch/_higher_order_ops/while_loop.py", line 218, in flat_body_fn
E           return body_fn(*carried, *additional)
E         File "/Users/lucascolley/ghq/github.com/scipy/scipy/build-cpu-install/usr/lib/python3.13/site-packages/scipy/sparse/linalg/_isolve/iterative.py", line 436, in body_fun
E           converged = xp_vector_norm(r, axis=-1) <= atol
E         File "/Users/lucascolley/ghq/github.com/scipy/scipy/build-cpu-install/usr/lib/python3.13/site-packages/scipy/_lib/_array_api.py", line 458, in xp_vector_norm
E           xp = array_namespace(x) if xp is None else xp
E         File "/Users/lucascolley/ghq/github.com/scipy/scipy/build-cpu-install/usr/lib/python3.13/site-packages/scipy/_lib/_array_api_override.py", line 156, in array_namespace
E           return array_api_compat.array_namespace(*numpy_arrays, *api_arrays)
E         File "/Users/lucascolley/ghq/github.com/scipy/scipy/build-cpu-install/usr/lib/python3.13/site-packages/scipy/_external/array_api_compat/common/_helpers.py", line 666, in array_namespace
E           namespaces.add(xp)

The hint seems to be saying we should avoid using modules as dictionary keys, but it seems that this actually arises from using a set of modules:

namespaces: set[Namespace] = set()
for x in xs:
xp, info = _cls_to_namespace(cast(Hashable, type(x)), api_version, use_compat)
if info is _ClsToXPInfo.SCALAR:
continue
if (
info is _ClsToXPInfo.MAYBE_JAX_ZERO_GRADIENT
and _is_jax_zero_gradient_array(x)
):
xp = _jax_namespace(api_version, use_compat)
if xp is None:
get_ns = getattr(x, "__array_namespace__", None)
if get_ns is None:
raise TypeError(f"{type(x).__name__} is not a supported array type")
if use_compat:
raise ValueError(
"The given array does not have an array-api-compat wrapper"
)
xp = get_ns(api_version=api_version)
namespaces.add(xp)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions