---
title: "Make your ZeroGPU Spaces go brrr with ahead-of-time compilation"
description: "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."
url: "https://swe.quest/articles/make-your-zerogpu-spaces-go-brrr-with-ahead-of-time-compilation-51092e3d8eed121c"
canonical: "https://huggingface.co/blog/zerogpu-aoti"
source: "Hugging Face"
published: "2025-09-02"
difficulty: "medium"
kind: "How-to"
reading_minutes: 13
tags: ["inference","performance","compilers","python"]
---

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

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2025-09-02, 13 min read, How-to. Original: [https://huggingface.co/blog/zerogpu-aoti](https://huggingface.co/blog/zerogpu-aoti)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Inference & serving](https://swe.quest/tags/inference.md) (AI & ML)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [Compilers](https://swe.quest/tags/compilers.md) (Compilers & runtimes)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- PyTorch basics
- diffusion model pipelines
- CUDA GPU inference ([easy posts on Hardware](https://swe.quest/tags/hardware.md?difficulty=easy))

## Keywords

ZeroGPU, Hugging Face Spaces, torch.export, AOTInductor, torchao, FlashAttention-3, FLUX.1-dev, diffusers

## Related posts

- [Fast LoRA inference for Flux with Diffusers and PEFT](https://swe.quest/articles/fast-lora-inference-for-flux-with-diffusers-and-peft-adf61d95e12cb0a4.md): Hugging Face · 2025-07-23 · Hard · 9 min read · Inference & serving, Performance engineering, Computer vision, Python
  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.
- [Exploring simple optimizations for SDXL](https://swe.quest/articles/exploring-simple-optimizations-for-sdxl-b199204d28422c69.md): Hugging Face · 2023-10-24 · Medium · 8 min read · Inference & serving, Performance engineering, Computer vision, Python
  Running SDXL out of the box in fp32 on an A100 takes about 28GB and 72 seconds for 4 images.
- [Learn the Hugging Face Kernel Hub in 5 Minutes](https://swe.quest/articles/learn-the-hugging-face-kernel-hub-in-5-minutes-049114b1e85677a7.md): Hugging Face · 2025-06-12 · Medium · 13 min read · Inference & serving, Performance engineering, Python, Hardware
  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 science behind TorchNeuron, Amazon’s new PyTorch backend for AWS Trainium chips](https://swe.quest/articles/the-science-behind-torchneuron-amazon-s-new-pytorch-backend-for-aws-trainium-c2048563f9b5b6ed.md): Amazon Science · 2026-10-22 · Medium · 5 min read · Compilers, Hardware, Testing, Model training, Python
  TorchNeuron is an open-source PyTorch backend that runs existing PyTorch code on AWS Trainium with little or no change.
- [Custom Kernels for All from Codex and Claude](https://swe.quest/articles/custom-kernels-for-all-from-codex-and-claude-9d652cbc33fa9001.md): Hugging Face · 2026-02-13 · Medium · 9 min read · AI for coding, Performance engineering, Inference & serving, Hardware, Python
  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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
