[feat] Smart magi-register-op#28
Open
themistbeforedawn wants to merge 2 commits into
Open
Conversation
23 tasks
23 tasks
23 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🗂️ PR Category
📝 Description
Summary
This PR introduces major enhancements to the
@magi_register_custom_opdecorator, enabling seamless support for structured data (nested frozen Dataclasses, Optionals, Literals) and ensuring that internal Triton kernels become fully transparent to the Inductor compiler (torch.compile).The implementation acts as a robust bridge between the user-friendly Pythonic space and
torch.library's restrictive flat-schema requirements without compromising performance.Key Features & Changes
1. Deep Dataclass Flattening & Schema Bridging
@dataclass(frozen=True)as inputs. The decorator recursively flattens them into primitives for thetorch.libraryschema and seamlessly reconstitutes the original dataclass struct during runtime execution.Optional[Tensor/scalar], PEP-604 (Tensor | None), and lists (list[int/bool/Tensor]).Literal[str, ...]and string-valuedEnumannotations tostrto appease the PyTorch schema while passing the exact user string to the underlying op implementation.2. Dataclass-Aware Autograd
setup_context_fnandbackward_fnare now bridged to directly support the original dataclass signature rather than flat internal parameters.None(for dropping gradients across entire dataclasses), dictionary mappings, or reconstructed dataclass shapes for accurate partial gradients out ofbackward_fn.3. Advanced Triton Kernel Auto-Detection (
triton_op)kernel[grid](...)calls using AST introspection, overriding the module__globals__to emitwrap_triton(kernel)under the hood. Inductor correctly traces into the kernels rather than treating the op as opaque.wrap_triton(...). Excludes these IDs from the shadow-rewriter, avoidingwrap_triton(wrap_triton(kernel))exceptions.@triton.heuristicsat the top level of a kernel (which PyTorch currently cannot wrap).4. Friendly Error Boundaries & Fallbacks
namespace::name), and duplicate operation names.5. Massive Testing Refactor
tests/api_tests/test_register_custom_op.py(79 tests) andtest_register_triton_op.py(46 tests) away from procedural naming into grouped, theme-driven logic (Core Registration, Type Support, Autograd Bridge, Compositions). Added a detailed Markdown Coverage Matrix bounding the top of each file.