pytorch - ✅(Solved) Fix [torch.compile] Better way to override the default backend [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#178930Fetched 2026-04-08 01:57:01
View on GitHub
Comments
1
Participants
2
Timeline
82
Reactions
0
Author
Participants
Timeline (top)
mentioned ×35subscribed ×35labeled ×7referenced ×3

Fix Action

Fix / Workaround

Today calling torch.compile with an out-of-tree backend requires explicitly setting backend="my_custom_backend". It'd be useful to have the ability to register/override the default backend in some way such that the user doesn't have to specify it explicitly. I've currently worked around this by monkey-patching the torch.compile method but that's gross. Dynamo already provides an API for registering a backend, maybe exposing a flag that could set it as the default would be best?

PR fix notes

PR #178944: [torch.compile] Add torch.compiler.set_default_backend

Description (problem / solution / changelog)

Addresses https://github.com/pytorch/pytorch/issues/178930

Allow overriding the default torch.compile backend globally so that out-of-tree backend authors don't need to pass backend= at every call site. Follows the same pattern as torch.set_default_dtype and torch.set_default_device. Explicit `backend= arguments to torch.compile still take precedence.

Usage:

import torch

torch.compiler.set_default_backend("my_custom_backend")
compiled_model = torch.compile(model)  # uses "my_custom_backend"

torch.compiler.set_default_backend(None)

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

Changed files

  • docs/source/torch.compiler_api.md (modified, +2/-0)
  • test/dynamo/test_backends.py (modified, +40/-0)
  • torch/__init__.py (modified, +6/-1)
  • torch/_dynamo/backends/registry.py (modified, +20/-0)
  • torch/compiler/__init__.py (modified, +35/-0)

Code Example

from torch._dynamo.backends import registry


registry.register_backend(compiler_fn=MyCustomBackend(), name="my_custom_backend", default=True)
RAW_BUFFERClick to expand / collapse

🚀 The feature, motivation and pitch

Today calling torch.compile with an out-of-tree backend requires explicitly setting backend="my_custom_backend". It'd be useful to have the ability to register/override the default backend in some way such that the user doesn't have to specify it explicitly. I've currently worked around this by monkey-patching the torch.compile method but that's gross. Dynamo already provides an API for registering a backend, maybe exposing a flag that could set it as the default would be best?

from torch._dynamo.backends import registry


registry.register_backend(compiler_fn=MyCustomBackend(), name="my_custom_backend", default=True)

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

Alternatives

No response

Additional context

No response

extent analysis

TL;DR

Exposing a flag to set a registered backend as the default in torch.compile may resolve the issue.

Guidance

  • Investigate the torch._dynamo.backends.registry API to understand how to properly register and override the default backend.
  • Consider adding a default parameter to the torch.compile function to allow users to specify the default backend.
  • Review the monkey-patching workaround to identify potential issues and areas for improvement.
  • Discuss with the maintainers and contributors (cc'd in the issue) to determine the best approach for implementing this feature.

Example

# Example of registering a backend with the default flag set to True
from torch._dynamo.backends import registry
registry.register_backend(compiler_fn=MyCustomBackend(), name="my_custom_backend", default=True)

Notes

The current workaround using monkey-patching is not ideal, and a more elegant solution is needed. The torch._dynamo.backends.registry API provides a way to register backends, but it's unclear if it can be used to set a default backend.

Recommendation

Apply workaround: Expose a flag to set a registered backend as the default in torch.compile, as this seems to be the most straightforward solution to the problem, allowing users to specify the default backend without having to explicitly pass it to torch.compile.

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