Add support for MegaBake
Feature request
Add support for MegaBake as an optional compilation backend in transformers serve, allowing users to opt into megakernel-based inference directly through the Transformers CLI.
Motivation
MegaBake is a PyTorch-native automated megakernel compiler that captures PyTorch FX graphs and performs region fusion, execution scheduling, and CuTe DSL based megakernel generation . It aims to reduce kernel launch overhead and intermediate memory traffic while retaining the existing PyTorch model and runtime interfaces. It’s extendable by design and can support megakernels for multiple hardwares like TPUs and AMD devices in future.
MegaBake already integrates with torch.compile through its custom backend and can run any transformers based model by converting it into a megakernel. It supports SDPA, Flex attention etc.
Having native support within transformers serve would allow users to experiment with megakernel-based inference on supported models without maintaining a separate serving implementation or modifying model definitions.
Your contribution
@ayushsatyam146 and I will be working on the following:
Introduce an optional CLI flag to select MegaBake as the compilation backend:
transformers serve <model_id> --compile-backend megabake
When enabled, the serving implementation would leverage megabake.make_backend() through torch.compile for supported inference paths.
The integration should:
- Preserve the existing model loading and serving APIs.
- Keep MegaBake an optional dependency.
- Provide meaningful errors for unsupported models, graphs, or hardware.
- Leave the default serving behavior unchanged.
Hi @sumantro93 @ayushsatyam146, this looks like a great addition, and I'd be glad to help.
Are you already working on the implementation?