Exporting Compute Graphs, LLM Shape Dynamics
Exporting Compute Graphs
Exporting a model to ONNX or StableHLO typically requires a sample input so the framework can trace and fix:
- input shape and dtype
- input/output structure
- which control-flow path is taken
flowchart LR
subgraph CNN["CNN Export (Static)"]
direction LR
CIn["Fixed-shape Image"] --> CGraph["Single Exported Graph"] --> COut["One-shot Output"]
end
subgraph LLM["LLM Export (Dynamic + Stateful)"]
direction LR
Prompt["Variable Prompt"] --> Prefill["Prefill Graph"]
Prefill -->|"Initial KV Cache"| Decode["Decode Graph (Loop)"]
NewTok["New Token + Position"] --> Decode
Decode -->|"Updated KV Cache + Logits"| Decode
end
CNNs Usually Have One Graph; LLMs Often Have Many
Why CNNs are simpler
A CNN typically has fixed-rank inputs, fixed (or a small set of) image sizes, no persistent decode-time state, and one-shot forward execution—so one exported graph is usually enough.
Why LLMs are harder
LLMs introduce dynamic dimensions and cross-step state:
- variable sequence length and batch size
- distinct prefill and decode phases
- a KV cache carried across steps
- autoregressive generation loops with runtime stopping criteria
Prefill vs. decode
Prefill and decode differ in tensor shapes, attention behavior, memory access patterns, and latency/throughput targets, so they are usually exported or compiled as separate programs:
- Prefill processes the full prompt and populates the initial KV cache.
- Decode processes one (or a few) new tokens while reading and updating the KV cache.
KV cache and functionalization
Export IRs such as ONNX and StableHLO expect pure tensor functions, making hidden in-place mutation awkward. Instead of logits = f(tokens), the exported signature is functionalized into:
(logits, new_cache) = f(tokens, old_cache, position, ...)
To avoid retracing on every decode step, systems typically allocate a fixed-capacity cache tensor and pass an explicit write position.
How Hugging Face Hides This Complexity with Eager Execution
Calling model.generate(...) in Hugging Face Transformers hides symbolic dimensions, graph variants, compiler IRs, and functionalized state signatures because it runs on PyTorch eager execution.
flowchart TD
Tok["1. Tokenize prompt"] --> Pre["2. Run prefill on full prompt"]
Pre --> KV["3. Receive past_key_values (KV cache)"]
KV --> Dec["4. Run decode with newest token + cache"]
Dec --> Sample["5. Sample / select next token"]
Sample -->|"Stop criterion not met"| Dec
Sample -->|"EOS / max_new_tokens"| Done["6. Return generated sequence"]
PyTorch eager execution natively accommodates changing sequence lengths, explicit Python cache objects, and runtime sampling/stopping control flow.
Tradeoff
Eager execution delivers strong flexibility and developer ergonomics, at the cost of lower peak throughput and higher per-token overhead under concurrent load. For production serving, specialized inference runtimes address these bottlenecks—see Hugging Face Models and LLM Inference Engines: llama.cpp, Ollama, and vLLM.