Skip to content

Tag nn.Linear for the Linear hub kernel - #2

Draft
kashif wants to merge 3 commits into
vasqu:propogate-kernelsfrom
kashif:add-linear-kernel
Draft

kashif wants to merge 3 commits into
vasqu:propogate-kernelsfrom
kashif:add-linear-kernel

Conversation

@kashif

@kashif kashif commented Jun 29, 2026 •

Copy link
Copy Markdown

Stacked on top of huggingface#46575 (base is your propogate-kernels branch, so the diff is just the two commits).

This wires up the generic Linear hub kernel you sketched in the huggingface#46575 description. Since nn.Linear is a builtin we cannot decorate, so set_use_kernels tags it with replace_kernel_forward_from_hub(nn.Linear, "Linear"). After that, kernelize swaps every nn.Linear to the Hub LigerLinear (a tiled matmul that recomputes in the backward) in Mode.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 any nn.Linear nested inside an already-kernelized non-Linear layer. kernelize swaps the instance forward, so a del child.forward reverts 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.

# 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")

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@kashif

kashif commented Jun 30, 2026 •

Copy link
Copy Markdown
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.

# 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():

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants