Repository navigation
Conversation
vasqu
reviewed
Jun 30, 2026
| # builtin, so it is tagged via replace_kernel_forward_from_hub instead of a decorator. | ||
| from .integrations import replace_kernel_forward_from_hub | ||
|
|
||
| replace_kernel_forward_from_hub(nn.Linear, "Linear") |
Owner
There was a problem hiding this comment.
Yep I think this is valid. My only concern is the interaction between fused tiled MLP classes - so e.g. SwiGLU doing the tiling twice ig? Would be nice if we get a proper workaround that
Author
|
You're right, kernelize doesn't touch train. I misread that, so ignore that part. The real issue is just the recursion, the inner linears get kernelized and computed twice. A recurse=False default in kernels sounds like the clean fix. Happy to drop this workaround once that lands. |
vasqu
reviewed
Jul 1, 2026
| # as an instance `forward` plus an instance `train` that re-assigns `forward` on every mode | ||
| # switch, so both are deleted to revert to the class methods; deleting only `forward` would let | ||
| # the next `model.train()` re-kernelize the child. This also keeps PEFT adapters and hooks alive. | ||
| for _, parent in self.named_modules(): |
Owner
There was a problem hiding this comment.
cc @danieldk would it make sense to have some sort of functionality within kernels that disallows kernels on nested structures?
Imo it is a solution but not super elegant imo
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Stacked on top of huggingface#46575 (base is your
propogate-kernelsbranch, so the diff is just the two commits).This wires up the generic
Linearhub kernel you sketched in the huggingface#46575 description. Sincenn.Linearis a builtin we cannot decorate, soset_use_kernelstags it withreplace_kernel_forward_from_hub(nn.Linear, "Linear"). After that,kernelizeswaps everynn.Linearto the HubLigerLinear(a tiled matmul that recomputes in the backward) inMode.TRAINING | Mode.TORCH_COMPILE.Second commit handles the nesting you flagged: an MLP kernel (e.g. tiled SwiGLU) calls its own
gate/up/down_proj, so kernelize swapping both the MLP and those linears would tile twice. After kernelize, we restore the original forward of anynn.Linearnested inside an already-kernelized non-Linear layer. kernelize swaps the instance forward, so adel child.forwardreverts it. Keeping the real module (rather than tiling the matmul in the parent kernel) also preserves PEFT adapters and forward hooks on those projections, which a weight-only path would drop.Verified locally: the tag sets
nn.Linear.kernel_layer_name = "Linear", and the restore loop reverts a nested linear's swapped forward.Note: the tiled kernel's FSDP2 and DeepSpeed ZeRO-3 fixes landed in kernels-community huggingface#1000 (merged, rebuilt on the Hub), verified on 2x H100.