-
Notifications
You must be signed in to change notification settings - Fork 13
Add graph-safe ops and Inductor heuristics #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,14 @@ def _patch_something(): | |
| return func | ||
|
|
||
|
|
||
| @patch_function | ||
| def _patch_visible_devices_env(): | ||
| if "MUSA_VISIBLE_DEVICES" in os.environ: | ||
| os.environ["CUDA_VISIBLE_DEVICES"] = os.environ["MUSA_VISIBLE_DEVICES"] | ||
| else: | ||
| os.environ.pop("CUDA_VISIBLE_DEVICES", None) | ||
|
|
||
|
|
||
| def requires_import(*module_names: str) -> Callable[[Callable], Callable]: | ||
| """ | ||
| Decorator to guard a patch function with import checks. | ||
|
|
@@ -108,6 +116,39 @@ def wrapper(*args, **kwargs): | |
| return decorator | ||
|
|
||
|
|
||
| @patch_function | ||
| @requires_import("torch._inductor.template_heuristics.registry") | ||
| def _patch_inductor_template_heuristics(): | ||
|
froststeam marked this conversation as resolved.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This copies the current CUDA heuristic registry only once during import. That will miss lazy/future registrations, and it relies on private registry/cache names and key shape. Also, copying a CUDA heuristic class under a musa key does not establish that its lowering/template/autotune path is MUSA-compatible. Could we move this compatibility to the registration/lookup boundary (or use an explicit versioned allowlist) and add a real torch.compile + Inductor/Triton MUSA smoke after lazy imports? Unsupported templates/torch versions should fail closed or fall back rather than silently appearing supported. |
||
| """Reuse CUDA Inductor template heuristics for CUDA-compatible MUSA templates.""" | ||
| if not is_musa_platform(): | ||
| return | ||
|
|
||
| import torch._inductor.template_heuristics.registry as registry | ||
|
|
||
| heuristic_registry = getattr(registry, "_TEMPLATE_HEURISTIC_REGISTRY", None) | ||
| if not isinstance(heuristic_registry, dict): | ||
| return | ||
|
|
||
| changed = False | ||
| for key, heuristic_class in list(heuristic_registry.items()): | ||
| if not isinstance(key, tuple) or len(key) != 3: | ||
| continue | ||
| template_name, device_type, op_name = key | ||
| if device_type != "cuda": | ||
| continue | ||
| if not isinstance(template_name, str) or not template_name.startswith("triton::"): | ||
| continue | ||
| musa_key = (template_name, "musa", op_name) | ||
| if musa_key not in heuristic_registry: | ||
| heuristic_registry[musa_key] = heuristic_class | ||
| changed = True | ||
|
|
||
| if changed: | ||
| heuristic_cache = getattr(registry, "_HEURISTIC_CACHE", None) | ||
| if isinstance(heuristic_cache, dict): | ||
| heuristic_cache.clear() | ||
|
|
||
|
|
||
| # Cache for translated device strings - avoids repeated string operations | ||
| _device_str_cache = {} | ||
|
|
||
|
|
@@ -2122,6 +2163,7 @@ def apply_patches(): | |
| - torch.cuda.nccl -> torch.musa.mccl | ||
| - torch.amp.autocast(device_type='cuda') -> 'musa' | ||
| - torch.utils.cpp_extension (CUDAExtension, BuildExtension) -> MUSA versions | ||
| - CUDA_VISIBLE_DEVICES -> MUSA_VISIBLE_DEVICES environment fallback | ||
| - torch._inductor.autotune_process.CUDA_VISIBLE_DEVICES -> MUSA_VISIBLE_DEVICES | ||
| - torch.accelerator.synchronize() -> torch.musa.synchronize() | ||
| - torch.accelerator context managers (device_index, stream) for forward compatibility | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.