Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 13 min read How-to mediumAdept

Make your ZeroGPU Spaces go brrr with ahead-of-time compilation

PyTorch ahead-of-time compilation (torch.export plus AOTInductor) suits ZeroGPU Spaces, where each GPU task runs in a short-lived forked process and torch.compile's just-in-time caching costs tens of seconds to minutes per task. The workflow captures example inputs with spaces.aoti_capture, exports and compiles the transformer inside a @spaces.GPU function, then patches it into the pipeline with spaces.aoti_apply, giving 1.3–1.8× speedups on Flux, Wan and LTX (1.7× on FLUX.1-dev). Further gains come from FP8 dynamic quantization via torchao (about 1.2× more on H200), dynamic shapes via torch.export.Dim, per-resolution multi-compile with shared weights, and prebuilt FlashAttention-3 kernels from the kernels library.

Assumes: PyTorch basicsdiffusion model pipelinesCUDA GPU inference

Related posts

  • Fast LoRA inference for Flux with Diffusers and PEFT

    Hugging Face hardMaster

    Combining Flash Attention 3, torch.compile and TorchAO FP8 quantization with PEFT's LoRA hotswapping speeds up Flux.1-Dev LoRA inference by 2.23x on an H100, and swapping adapters does not trigger recompilation. Hotswapping avoids recompiles by turning LoRA scaling factors into tensors and zero-padding adapter weights to a declared max rank. The limits are that later LoRAs must target the same layers as the first one, or a subset of them, and text encoder LoRAs are not supported. On a 24GB RTX 4090, FA3 is unavailable, so the recipe adds NF4 quantization of the T5 text encoder to fit FP8 without CPU offloading, which gives a 2.04x speedup.

  • Exploring simple optimizations for SDXL

    Hugging Face mediumAdept

    Running SDXL out of the box in fp32 on an A100 takes about 28GB and 72 seconds for 4 images. A series of Diffusers and PyTorch 2 optimizations, each benchmarked on that setup, cuts both. Switching to fp16 brings latency to 14.8s, SDPA attention (on by default) to 11.4s, and torch.compile to 10.3s. Memory drops with model or sequential CPU offloading (sequential is much slower), VAE slicing (15.4GB, or 11.5GB combined with sequential offload), precomputed text embeddings, and the Tiny Autoencoder (15.5GB and faster, at some cost in fine detail). The gains depend on hardware; torch.compile, for example, only appears to help modern GPUs.

  • Learn the Hugging Face Kernel Hub in 5 Minutes

    Hugging Face mediumAdept

    Hugging Face's Kernel Hub and `kernels` library let PyTorch code download pre-compiled, optimized GPU kernels such as FlashAttention, activations, RMSNorm and quantization kernels with a single `get_kernel` call. The library matches the binary to the installed Python, PyTorch and CUDA versions, which avoids source builds that for FlashAttention can need about 96 GB of RAM and hours of compilation. The walkthrough checks a fast GELU kernel against PyTorch and swaps a Triton LlamaRMSNorm into a simple model with the `use_kernel_forward_from_hub` decorator. It then benchmarks the kernel against a plain PyTorch RMSNorm using CUDA events.

  • The science behind TorchNeuron, Amazon’s new PyTorch backend for AWS Trainium chips

    Amazon Science mediumAdept

    TorchNeuron is an open-source PyTorch backend that runs existing PyTorch code on AWS Trainium with little or no change. It adds an opt-in adaptive eager mode that batches dispatched ops into small subgraphs and compiles them in parallel. The subgraphs are sized so compile time overlaps with execution and stays hidden, while source-line correlation is preserved for debugging. Compiled NEFF executables are cached in memory, on disk, and through a per-machine shared compilation service, which cut time-to-first-step by about a third for a 64-process LLM run. Validation used AI agents to mine Hugging Face down to 841 representative models plus a registry of tens of thousands of tensor shapes, feeding a staged pipeline of fast unit tests, then representative models, then the full model set.

  • Custom Kernels for All from Codex and Claude

    Hugging Face mediumAdept

    Hugging Face packaged CUDA kernel expertise (GPU architecture guides for H100/A100/T4, diffusers and transformers integration patterns, vectorized kernel templates, benchmark scripts) into an agent skill that lets Claude and Codex generate complete kernel-builder projects with PyTorch bindings. Agent-written RMSNorm kernels averaged 1.88x over PyTorch for LTX-Video and 1.94x for Qwen3-8B on H100 in BF16, with Qwen3 speedups growing from 1.58x at 128 tokens to 2.47x at 8192. End to end, LTX-Video gained only 6% (1.43x with torch.compile), consistent with RMSNorm taking about 5% of compute. The post also walks through building all PyTorch/CUDA variants with Nix and publishing to the Kernel Hub for one-line loading via get_kernel.

The quest for software engineering knowledge.

Look:
Layout: