Skip to content

ENH: testing.lazy_xp_function: torch.compile support - #668

Draft
lucascolley wants to merge 5 commits into
data-apis:mainfrom
lucascolley:torch-autojit
Draft

ENH: testing.lazy_xp_function: torch.compile support#668
lucascolley wants to merge 5 commits into
data-apis:mainfrom
lucascolley:torch-autojit

Conversation

@lucascolley

@lucascolley lucascolley commented Apr 3, 2026

Copy link
Copy Markdown
Member

Closes gh-664

@rgommers this was easier than I expected. Is there someone familiar with Dynamo that we could ping? A few things that came up:

  • The following deprecation warning with a confusing message given that we are already using torch.compile:
E           torch._dynamo.exc.BackendCompilerFailed: backend='inductor' raised:
E           DeprecationWarning: `torch.jit.script_method` is not supported in Python 3.14+ and may break. Please switch to `torch.compile` or `torch.export`.
  • Some failing tests here with:
E                       torch._dynamo.exc.FailOnRecompileLimitHit: recompile_limit reached with fullgraph=True. Excessive recompilations can degrade performance due to the compilation overhead of each recompilation. To monitor recompilations, enable TORCH_LOGS=recompiles. If recompilations are expected, consider increasing torch._dynamo.config.cache_size_limit to an appropriate value.

(Draft PR as this will need docs updates before merge.)

Comment on lines +534 to 536
if jit_library is JitLibrary.jax and isinstance(obj, Iterator):
self._obj = list(obj)
self._is_iter = True

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

claude reckoned that, unlike JAX, we needn't treat iterables with this special case for torch.compile

Comment thread src/array_api_extra/testing.py Outdated
Comment on lines +424 to +436
wrapped = autojit(func, JitLibrary.jax)
# If we're dealing with a staticmethod or classmethod, make
# sure things stay that way.
if isinstance(attr, staticmethod):
wrapped = staticmethod(wrapped)
elif isinstance(attr, classmethod):
wrapped = classmethod(wrapped)
temp_setattr(target, name, wrapped)

elif is_torch_namespace(xp):
for target, name, attr, func, tags in iter_tagged():
if tags["torch_compile"]:
wrapped = autojit(func, JitLibrary.torch)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(minor: could reduce some LoC perhaps)

@lucascolley

Copy link
Copy Markdown
Member Author

@ev-br FYI

@lucascolley
lucascolley requested a review from rgommers April 3, 2026 12:03
Comment thread src/array_api_extra/_lib/_helpers.py

@lucascolley lucascolley left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the warnings about to flood CI should be fixed by data-apis/array-api-compat#411 (comment).

Comment thread pyproject.toml Outdated
@lucascolley
lucascolley force-pushed the torch-autojit branch 2 times, most recently from 393dd61 to cc4a106 Compare April 3, 2026 13:46
@lucascolley

Copy link
Copy Markdown
Member Author

ready for review

@j-bowhay

Copy link
Copy Markdown
Contributor

@lucascolley what's the current status here? Is it just review bandwidth preventing this from moving forward?

@lucascolley

Copy link
Copy Markdown
Member Author

@lucascolley what's the current status here? Is it just review bandwidth preventing this from moving forward?

Yes, pretty much. I had also noted in the linked issue:

Currently unclear how to deal with accepting a variable amount of graph breaks / recompilations.

@ev-br

ev-br commented Aug 16, 2026

Copy link
Copy Markdown
Member

Currently unclear how to deal with accepting a variable amount of graph breaks / recompilations.

While I haven't written a single LOC, a reasonable design looks similar what is done for dask: an extra num_graphbreaks : int parameter (a made-up name), where a zero valueal maps onto fullgraph=True.

Also we definitely want to test things twice: in the eager mode and under torch dynamo. Not sure if it's already done for Jax (and if yes, where) or if it needs implementing.

@j-bowhay

Copy link
Copy Markdown
Contributor

@lucascolley what's the current status here? Is it just review bandwidth preventing this from moving forward?

Yes, pretty much. I had also noted in the linked issue:

Currently unclear how to deal with accepting a variable amount of graph breaks / recompilations.

Do we need that level of granularity? Could we not have 3 options:

  • No support for torch.compile
  • Support for torch.compile but with full_graph=False
  • Support for torch.compile but with full_graph=True

@lucascolley

Copy link
Copy Markdown
Member Author

@lucascolley what's the current status here? Is it just review bandwidth preventing this from moving forward?

Yes, pretty much. I had also noted in the linked issue:

Currently unclear how to deal with accepting a variable amount of graph breaks / recompilations.

Do we need that level of granularity? Could we not have 3 options:

* No support for `torch.compile`

* Support for `torch.compile` but with `full_graph=False`

* Support for `torch.compile` but with `full_graph=True`

Would be happy to start with that at least if we can get that working.

@j-bowhay

Copy link
Copy Markdown
Contributor

Do you have a preference for what that api would look like? Would it be acceptable to have both a torch_compile and torch_full_graph argument?

@lucascolley

Copy link
Copy Markdown
Member Author

Do you have a preference for what that api would look like? Would it be acceptable to have both a torch_compile and torch_full_graph argument?

not sure yet what I think is nicest... see also #917

@j-bowhay

Copy link
Copy Markdown
Contributor

Do you have a preference for what that api would look like? Would it be acceptable to have both a torch_compile and torch_full_graph argument?

not sure yet what I think is nicest... see also #917

I guess the one difference being we also want to be able to turn compile on/off and control if graph breaks are allowed

@lucascolley
lucascolley force-pushed the torch-autojit branch 2 times, most recently from 03c9eb8 to b6cc704 Compare August 16, 2026 14:44
@lucascolley

Copy link
Copy Markdown
Member Author

CI is happy now, with fullgraph=False. TODOs:

  • ENH: fix is_jax_array under torch.compile array-api-compat#464
  • investigate the warnings that are ignored here
  • figure out the API for fullgraph=True
  • add all three modes (eager, no fullgraph, fullgraph) to array-api-extra's CI matrix
  • investigate recompilations / graph breaks in array-api-extra and perhaps remove some of them
  • add docs
  • try to plumb this into SciPy

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENH: testing.lazy_xp_function: torch.compile support?

3 participants