Skip to content

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

Hugging Face 8 min read Research hardMaster

Making Knowledge Distillation Cheap Enough to Run at Scale

Two systems changes make LLM knowledge distillation far cheaper: caching the teacher's top-100 logits offline so the teacher never shares memory with the student, and a fused chunked KL loss that folds the output projection into the loss and recomputes chunks in the backward pass, so the full vocabulary × sequence logits grid is never materialized. On a single H200 at 8K context, offline top-100 distillation matches online training loss while cutting peak memory from 102.8 GB to 58.3 GB. In an isolated loss benchmark at 32K tokens, peak memory drops 15.6× (85.2 to 5.45 GiB), and distilling GPT-OSS 20B at 32K context shrank from four nodes to one with about 5× faster steps. The tradeoff is a repeated output projection that costs some speed at short contexts; the implementation is open-sourced.

Assumes: knowledge distillationKL divergenceLLM training memory (activations, gradients)PyTorch autograd

Related posts

  • Speeding up language models for travel applications

    Amazon Science mediumAdept

    Combining knowledge distillation with Medusa multi-token decoding let Booking.com replace large LLMs with a 1.1B-parameter TinyLlama student that matches a Llama-3.1 70B teacher while running 10 to 20 times as fast. The student is trained on human-labeled data plus teacher-generated labels, with Medusa heads attached and trained in the same stage. Distillation closed over 90% of the quality gap and gave roughly a 6x speedup. Medusa added another 2 to 3.6x, and the single-stage Medusa-2 variant was preferred for its simpler production pipeline. Across trip planning, smart filters and listing description generation, results included 15x faster p99 responses and descriptions at under one-third the cost of GPT-4.

  • Gisting: Compressing LLM Agent context to ↑ throughput and ↓ cost

    Shopify Engineering hardMaster

    Shopify compressed the ~6,000-token system prompt of its Sidekick GraphQL agent into ~1,500 learned gist tokens (4:1) without losing prediction quality. The gist embeddings are trained by knowledge distillation (KL divergence between full-prompt teacher logits and gist-prompt student logits) with model weights frozen, then written into the embedding matrix so serving needs no special path. Gisting complements prefix caching because decode still streams the whole KV cache per generated token. At 350 RPM, median TTFT fell 19%, end-to-end latency about 38%, and throughput rose 16%, allowing 14% fewer GPUs. Key recipe findings from an autoresearch loop: initialize gists from mean prompt-chunk embeddings, batch-level loss normalization, and precomputed teacher logits cutting training from 30 to 6 hours.

  • Consistency diffusion language models: Up to 14x faster inference without sacrificing quality

    Together AI hardMaster

    Consistency diffusion language models (CDLM) speed up diffusion LLM inference by post-training a block-wise causal student that can finalize multiple tokens per step and use exact block-wise KV caching. Training distills from teacher decoding trajectories using three losses: distillation on newly unmasked tokens, a consistency loss on still-masked tokens, and an auxiliary masked-denoising loss. On Dream-7B-Instruct, refinement steps drop by about 4.1x–7.7x with minor accuracy changes, and latency improves by up to 11.2x on GSM8K-CoT and 14.5x on MBPP. Naively truncating steps without this training sharply degrades accuracy. A roofline analysis shows block-wise DLMs sit between memory-bound AR decoding and compute-bound full-attention DLMs, which makes them efficient at small batch sizes.

  • Mastering Long Contexts in LLMs with KVPress

    Hugging Face mediumAdept

    KV cache memory grows linearly with context length: Llama 3-70B at 1M tokens in bfloat16 needs about 330GB of KV cache, roughly 70% of total memory. NVIDIA's KVPress is a Python toolkit of "presses" that prune low-importance KV pairs during prefill via forward hooks on attention layers. Examples include KnormPress, SnapKVPress and ExpectedAttentionPress, each tuned with a compression_ratio and usable through a custom transformers pipeline. On Llama 3.1 8B at 128k context, 50% compression cuts peak memory from 45GB to 37GB and raises decoding from 11 to 17 tokens/s on an A100, while RULER benchmarks show accuracy dropping as compression rises.

  • Continuous batching from first principles

    Hugging Face mediumAdept

    Continuous batching falls out naturally once you optimize LLM serving for throughput, and the post derives it step by step from attention, causal masking, KV caching and chunked prefill. The KV cache cuts the cost of generating each new token from O(n²) to O(n) at O(n) memory, about 16 KB per token per layer for Llama-2-7B in float16. Chunked prefill splits long prompts across forward passes. Padded batched generation wastes compute when prompts finish at different times, and dynamic batching needs heavy padding because newly inserted prompts must prefill while others decode, which motivates continuous batching's mixing of prefill and decode tokens in one forward pass.

The quest for software engineering knowledge.

Look:
Layout: