Skip to content

Support conditional kernelization beyond hardware (e.g. based on module config) #789

Description

@moon-bot-app

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions