Today use_kernel_forward_from_hub decides whether to replace a layer's forward based only on the hardware. In some cases the kernel is only valid for a specific module configuration, not just a specific device.
Concrete motivation: adding SwiGLU / GeGLU kernels to transformers models. Whether the kernel is applicable depends on the config value behind the activation function — a SwiGLUMLP kernel is only correct when module.config.hidden_act == "silu".
Proposed design — an optional predicate (working name condition) evaluated against the module the decorator is applied to:
@use_kernel_forward_from_hub(
"SwiGLUMLP",
condition=lambda module: module.config.hidden_act == "silu",
)
class MyMLP(nn.Module):
...
The kernel forward is only used when the predicate returns True; otherwise the original forward is kept. config here is an attribute of the layer the decorator is applied to.
This could be implemented in transformers, but it seems generally useful and a better fit in kernels itself.
Discussed in Slack between @danieldk and Anton; naming suggestion condition over guard came from that thread.
Requested by Sayak Paul - Slack thread - Agent trace
Today
use_kernel_forward_from_hubdecides whether to replace a layer's forward based only on the hardware. In some cases the kernel is only valid for a specific module configuration, not just a specific device.Concrete motivation: adding SwiGLU / GeGLU kernels to transformers models. Whether the kernel is applicable depends on the config value behind the activation function — a
SwiGLUMLPkernel is only correct whenmodule.config.hidden_act == "silu".Proposed design — an optional predicate (working name
condition) evaluated against the module the decorator is applied to:The kernel forward is only used when the predicate returns True; otherwise the original forward is kept.
confighere is an attribute of the layer the decorator is applied to.This could be implemented in transformers, but it seems generally useful and a better fit in
kernelsitself.Discussed in Slack between @danieldk and Anton; naming suggestion
conditionoverguardcame from that thread.Requested by Sayak Paul - Slack thread - Agent trace