[Feat] Support dataclass & automatic triton_kernel wrapping for triton_op registration in magi_register_custom_op#33
Open
themistbeforedawn wants to merge 2 commits into
Conversation
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
What's new
@magi_register_custom_opnow auto-detects Triton kernels in the decorated function (and its helpers) and routes the registration throughtorch.library.triton_opinstead ofcustom_op, so Inductor can trace through the op and fuse around the kernel rather than treating it as an opaque barrier. In the common case the user surface doesn't change:Internally a single AST pass walks
fn+ its helpers (BFS, depth-capped) and collects every Triton kernel reference and everytorch.ops.<ns>.<op>call. Based on what it finds, the decorator picks the appropriate registration path —triton_op(with bare kernels shadowed viawrap_triton),custom_op, or skipping registration entirely so Inductor can inlinefnand fuse across the nested ops. See the decision matrix on_decide_registration_pathfor the full rules.Three new keyword args cover the cases AST can't reach or where the user wants to override:
extra_triton_kernels=[...]— escape hatch for kernels the AST scanner can't statically resolve (e.g.self.kernel[grid](...)— subscript-on-attribute, factory-built kernels, runtime imports). Listed kernels are treated as bare and forcetriton_opmode.force_register_mode="triton_op" | "custom_op"— pin the path explicitly; useful when you want a deliberate fusion barrier around a Triton body, or when introspection misses your kernel.max_introspect_depth=5— how deep the helper-recursion goes (doesn't bound flat scanning offnitself).Friendly up-front errors at decoration time, instead of opaque schema-fingerprint failures later: duplicate / mis-namespaced op names,
@triton.heuristicsas the outermost decorator (whichwrap_tritondoesn't accept — the message tells you the right stacking order), and obvious mis-uses like mixing a bare kernel with a nestedcustom_opin the same body.triton_opregistration failure falls back tocustom_op + register_fakewith a warning.Architecture
The existing 4-slot pipeline is unchanged; this PR only changes what fills slot 2 (
torch.library.triton_opinstead ofcustom_opwhen appropriate) and adds amode="none"short-circuit that returnsfndirectly.Tests
50 new tests in
tests/api_tests/test_register_triton_op.pycover the full kernel-discovery surface (closures, helpers, multi-level nesting, factory kernels,self.kernelonnn.Module, cross-module launchers, third-party.fnwrappers,staticmethod/classmethod, runtime imports, dedup,wrap_tritonidempotence), all three registration paths +force_register_modeoverrides,@triton.autotune(incl. multiple per op) and@triton.heuristicsrejection, autograd + dataclass interactions through the Triton path, and an Inductor-AOT-graph check confirming the kernel is actually visible post-compile.