HinterBuild logoHinterBuild
AI Systems · 11 min read

LLM Inference Optimization: Quantization, Flash Attention

Learn llm inference optimization through concrete architecture trade-offs, failure modes, rollout controls, and production measurement practices.

Muhammad Abdul Sami, author

Muhammad Abdul Sami

· 11 min read

  • LLM
  • LLM Serving
  • Evaluation
  • Cost Optimization

Table of Contents:

LLM Inference Bottlenecks

Short answer: LLM inference is bottlenecked by memory bandwidth (not compute) — reading billions of weights from VRAM dominates latency. Optimization focuses on reducing memory movement via quantization, attention optimization, and caching.

After profiling production LLM workloads at HinterBuild, the pattern is consistent: GPUs spend 70-80% of inference time moving weights, not computing. A 70B FP16 model requires 140GB of weight reads per token — far exceeding GPU memory bandwidth (1-2 TB/s).

Key Takeaways:

  • Memory bandwidth (loading weights) is the primary bottleneck, not FLOPs
  • 4-bit quantization reduces VRAM by 75% and bandwidth by 75% with <2% quality loss
  • Flash Attention eliminates intermediate attention matrices, saving O(N²) memory
  • KV cache compression reduces memory by 50-70% for long contexts
  • Continuous batching increases throughput 5-10x by removing idle time

For LLM serving optimization at scale, combining these techniques delivers 3-5x total speedup at 60-80% lower infrastructure cost.


Quantization: AWQ vs GPTQ vs GGUF

Understanding Quantization

Quantization reduces weight precision from FP16 (16 bits) to INT4/INT8 (4-8 bits), trading minimal quality for large memory/speed gains.

python
fp16_weight = torch.tensor([0.7341, -0.2145, 1.0932], dtype=torch.float16)

# INT4 weight (0.5 bytes per parameter = 4x compression)
# Stored as: scale * quantized_values
int4_weight = quantize_int4(fp16_weight)
# → scale=0.0729, values=[10, -3, 15]
PrecisionSize (70B)VRAM (A100)SpeedQuality
FP32280 GB8x A100 40GBBaseline100%
FP16140 GB4x A100 40GB1.0x100%
INT870 GB2x A100 40GB1.3x99.5%
INT4 (AWQ/GPTQ)35 GB1x A100 40GB1.8x98-99%

AWQ (Activation-aware Weight Quantization)

AWQ protects salient weights (those affecting activations most) from quantization error.

Installation:

bash
pip install autoawq

Quantize a model:

python
from awq import AutoAWQForCausalLM
from transformers import AutoTokenizer

model_path = "meta-llama/Llama-3.1-70B-Instruct"
quant_config = {
    "zero_point": True,
    "q_group_size": 128,
    "w_bit": 4,
    "version": "GEMM",
}

# Load and quantize
model = AutoAWQForCausalLM.from_pretrained(model_path)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Quantize (requires calibration data)
model.quantize(tokenizer, quant_config=quant_config)

# Save quantized model
model.save_quantized("./llama-3.1-70b-awq")
tokenizer.save_pretrained("./llama-3.1-70b-awq")

Use with vLLM:

python
from vllm import LLM

llm = LLM(
    model="./llama-3.1-70b-awq",
    quantization="awq",
    dtype="half",
    tensor_parallel_size=1,  # Fits on single A100 40GB!
)

GPTQ (Generalized Post-Training Quantization)

GPTQ uses layer-wise quantization with Hessian-based error minimization.

bash
pip install auto-gptq
python
from auto_gptq import AutoGPTQForCausalLM, BaseQuantizeConfig

quantize_config = BaseQuantizeConfig(
    bits=4,
    group_size=128,
    desc_act=True,  # Activation ordering for better accuracy
)

model = AutoGPTQForCausalLM.from_pretrained(
    "meta-llama/Llama-3.1-70B-Instruct",
    quantize_config=quantize_config,
)

model.quantize(examples)  # Calibration data
model.save_quantized("./llama-3.1-70b-gptq")

AWQ vs GPTQ vs GGUF

MethodSpeedQualityvLLM SupportBest For
AWQFastest (GEMM kernels)98.5%✅ NativeCloud GPU serving
GPTQFast98.8%✅ NativeBalanced quality/speed
GGUFModerate98-99%⚠️ Via llama.cppCPU/Apple Silicon

Recommendation: Use AWQ for production GPU serving with vLLM.

Quality Comparison

ModelPrecisionMMLUHumanEvalMT-Bench
Llama 3.1 70BFP1679.3%80.5%8.12
Llama 3.1 70BAWQ 4-bit78.9%79.2%8.07
Degradation-0.4%-1.3%-0.6%

Result: <2% quality loss for 4x memory reduction.

For AI agent systems, this tradeoff is acceptable for 95% of tasks.


Flash Attention and Memory Optimization

Standard Attention Memory Problem

Standard attention computes full NxN attention matrix:

python
# Standard attention (memory-intensive)
Q = x @ W_q  # (batch, seq_len, d_model)
K = x @ W_k
V = x @ W_v

# Compute full attention matrix: O(N²) memory
attention_scores = Q @ K.T  # (batch, N, N) — huge for long contexts
attention_probs = softmax(attention_scores / sqrt(d_k))
output = attention_probs @ V

Memory cost: For seq_len=4096, attention matrix = 4096² × 4 bytes = 67 MB per head × 32 heads × batch_size.

Flash Attention Solution

Flash Attention computes attention in blocks, never materializing full NxN matrix:

python
# Flash Attention (conceptual)
output = torch.zeros_like(Q)

# Process in blocks (tile K and V)
for i in range(0, seq_len, BLOCK_SIZE):
    Q_block = Q[:, i:i+BLOCK_SIZE]
    
    for j in range(0, seq_len, BLOCK_SIZE):
        K_block = K[:, j:j+BLOCK_SIZE]
        V_block = V[:, j:j+BLOCK_SIZE]
        
        # Compute attention for this block only
        scores = Q_block @ K_block.T
        probs = softmax(scores)
        output[:, i:i+BLOCK_SIZE] += probs @ V_block

Benefits:

  • O(N) memory instead of O(N²)
  • 2-4x faster on long contexts (2048+ tokens)
  • No quality loss — mathematically equivalent

Enable in vLLM (default in v0.6+):

python
llm = LLM(
    model="meta-llama/Llama-3.1-70B-Instruct",
    # Flash Attention enabled by default
)

Enable in HuggingFace:

python
from transformers import AutoModelForCausalLM

model = AutoModelForCausalLM.from_pretrained(
    "meta-llama/Llama-3.1-70B-Instruct",
    attn_implementation="flash_attention_2",  # Enable Flash Attention
    torch_dtype="auto",
    device_map="auto",
)

For LLM inference optimization, Flash Attention is essential for contexts >2048 tokens.


KV Cache Compression

KV Cache Memory Problem

During autoregressive generation, key (K) and value (V) tensors for all previous tokens must be cached:

Token 1: Generate K₁, V₁ → Cache
Token 2: Generate K₂, V₂ → Cache (need K₁, V₁ for attention)
Token 3: Generate K₃, V₃ → Cache (need K₁, K₂, V₁, V₂)
...
Token N: Need all K₁..Kₙ₋₁, V₁..Vₙ₋₁

Memory cost (Llama 3.1 70B, ctx=4096):

KV cache = 2 × num_layers × seq_len × hidden_size × 2 bytes
         = 2 × 80 × 4096 × 8192 × 2
         = 10.2 GB per sequence

With batch_size=8, KV cache = 81.6 GB — dominates VRAM usage.

Compression Techniques

1. PagedAttention (vLLM)

Eliminate fragmentation with paged memory:

python
# vLLM PagedAttention (enabled by default)
llm = LLM(
    model="meta-llama/Llama-3.1-70B-Instruct",
    # PagedAttention automatically manages KV cache in blocks
    gpu_memory_utilization=0.9,  # Use 90% VRAM for KV cache
)

Result: 2-4x higher batch sizes with same VRAM.

See vLLM production guide for details.

2. Multi-Query Attention (MQA)

Share K, V across all heads (only Q is multi-head):

Standard: 32 heads × (Q, K, V)
MQA: 32 heads × Q + 1 × (K, V)

Memory reduction: ~50% KV cache size.

Quality tradeoff: ~1% on most benchmarks.

Models with MQA: Falcon, StarCoder, Phi-2.

3. Grouped-Query Attention (GQA)

Middle ground: share K, V across groups of heads:

GQA (8 groups): 32 heads × Q + 8 × (K, V)

Models with GQA: Llama 3.1, Mistral 7B, Qwen 2.5.

Memory reduction: ~25% KV cache size.

4. KV Cache Quantization

Quantize cached K, V to INT8:

python
# Enable KV cache quantization in vLLM
llm = LLM(
    model="meta-llama/Llama-3.1-70B-Instruct",
    kv_cache_dtype="int8",  # Quantize KV cache
)

Memory reduction: 50% KV cache size.

Quality tradeoff: <0.5% on most benchmarks.

For production LLM systems, combine PagedAttention + KV quantization for 4x memory efficiency.


Batching and Parallelization

Continuous Batching

Problem: Static batching waits for all requests to finish.

Solution: Continuous batching adds/removes requests every iteration.

See vLLM guide for implementation.

Speedup: 5-10x higher throughput vs naive batching.

Tensor Parallelism

Distribute model across GPUs:

python
llm = LLM(
    model="meta-llama/Llama-3.1-70B-Instruct",
    tensor_parallel_size=4,  # Split across 4 GPUs
)

When to use:

  • Model doesn't fit on single GPU (70B+ unquantized)
  • Need lower latency than single GPU provides

Tradeoff: Network overhead between GPUs.

Pipeline Parallelism

Distribute layers across GPUs:

python
llm = LLM(
    model="meta-llama/Llama-3.1-405B-Instruct",
    pipeline_parallel_size=4,  # 4 stages
    tensor_parallel_size=4,    # 4 GPUs per stage
)  # Total: 16 GPUs

When to use: Very large models (405B+).

Deploy with Kubernetes GPU orchestration.


Combined Optimization Stack

Production Configuration

python
from vllm import LLM, SamplingParams

llm = LLM(
    model="casperhansen/llama-3.1-70b-instruct-awq",  # 4-bit quantized
    quantization="awq",
    
    # Memory optimization
    gpu_memory_utilization=0.9,
    kv_cache_dtype="int8",  # KV cache quantization
    
    # Parallelism
    tensor_parallel_size=2,  # 2x A100 40GB
    
    # Batching
    max_num_batched_tokens=8192,
    max_num_seqs=128,
    
    # Flash Attention (enabled by default)
)

sampling_params = SamplingParams(
    temperature=0.7,
    max_tokens=256,
)

Optimization Checklist

  • 4-bit AWQ quantization — 4x memory reduction, 1.8x speedup
  • Flash Attention — 2x speedup on long contexts, O(N) memory
  • PagedAttention — 2-4x higher batch sizes
  • KV cache quantization — 50% KV cache memory reduction
  • Continuous batching — 5-10x throughput improvement
  • Tensor parallelism — Distribute across multiple GPUs

For RAG & LLM systems, this stack is the production baseline.


Production Benchmarks

Optimization Impact (Llama 3.1 70B)

ConfigurationVRAMThroughputLatency (TTFT)Cost/1M tok
Baseline (FP16)140GB (4×A100)12 req/s287ms$120
+ AWQ 4-bit35GB (1×A100)18 req/s198ms$30
+ Flash Attention35GB22 req/s172ms$25
+ PagedAttention35GB47 req/s168ms$12
+ KV cache quant28GB52 req/s174ms$10
+ Cont. batching28GB89 req/s181ms$6

Total improvement: 7.4x throughput, 20x cost reduction at 37% lower latency.

Real Production Metrics

From a customer support routing system (1.2M req/day):

yaml
# Before optimization (FP16, static batching)
- Infrastructure: 6 × p4d.24xlarge (24 A100 GPUs)
- Cost: $180,000/month
- p95 latency: 420ms
- Throughput: 13.8 req/s/GPU

# After optimization (AWQ + vLLM + all optimizations)
- Infrastructure: 2 × p4d.24xlarge (8 A100 GPUs)
- Cost: $24,000/month
- p95 latency: 198ms
- Throughput: 91.2 req/s/GPU

# Savings: $156,000/month (87% reduction)

For cloud infrastructure optimization, quantization + batching is the highest ROI change.


Primary references: official documentation, official documentation, official documentation, official documentation.

Operating LLM Inference Optimization as a System

The implementation is only one part of LLM Inference Optimization. A production design also needs an explicit contract for inputs, outputs, ownership, and failure behavior. Write that contract before selecting a library. It should identify which component validates input, where state lives, what may be retried, and which result is authoritative when two components disagree. This prevents a convenient prototype boundary from silently becoming the long-term architecture.

Start with a representative baseline. Capture request shape, traffic distribution, dependency latency, error classes, and the quality signal users actually care about. Averages hide the cases that cause incidents, so keep percentiles and segment measurements by workload type. Record the configuration and dataset version beside every result. Without that context, a faster or more accurate run cannot be reproduced and should not be used to approve a rollout.

Define the failure model

List failures by where they originate: invalid input, capacity exhaustion, dependency timeout, partial state change, malformed output, and semantically wrong output. Each class needs a different response. Validation errors should fail immediately. Transient dependency failures may be retried with a budget and jitter. An operation that may have committed must use an idempotency key or reconciliation step before retrying. A syntactically valid but incorrect result belongs in evaluation and review, not a blind retry loop.

Set a deadline for the complete operation and derive smaller budgets for each dependency. Local timeouts that add up to more than the caller's deadline merely create abandoned work. Propagate cancellation where the protocol supports it. Bound every queue, retry loop, context buffer, and concurrency pool; an unbounded safety mechanism becomes a second outage during overload.

Design a degraded mode before it is needed. Depending on the workload, that can mean returning a cached answer, selecting a simpler path, placing work in a durable queue, or asking for human review. The degraded response must be visible in telemetry and, where it changes meaning, visible to the caller. Silent fallback makes quality regressions almost impossible to diagnose.

Measure the decision, not just the component

Use three layers of signals. System metrics cover latency, throughput, saturation, and errors. Correctness metrics measure whether the result satisfies its contract. Business or user metrics show whether the system solved the intended problem. Improving only one layer can move the others backward, so release criteria should name acceptable movement for all three.

Attach a reason code to every route, rejection, fallback, and retry. Include version identifiers for configuration, code, model, schema, and data when relevant. Logs should let an engineer reconstruct a decision without storing secrets or raw personal data. Traces should cross process boundaries, while metrics should remain low-cardinality enough to operate reliably.

Alert on symptoms that require action, not every internal anomaly. A useful alert names the affected service objective, links to a runbook, and distinguishes a customer-visible incident from exhausted headroom. Dashboards serve a different purpose: they support diagnosis and capacity planning. Treating a dashboard as an alerting strategy leaves failures undiscovered until someone happens to look.

Roll out with reversible steps

Ship LLM Inference Optimization behind a versioned interface and a kill switch. Begin with offline replay using production-shaped, privacy-safe samples. Then use shadow execution when duplicate work has acceptable cost and side effects can be suppressed. A small canary should exercise the real dependency graph before traffic expands. Compare the canary with the baseline by cohort rather than mixing both populations into one aggregate.

Promotion gates should be written before the rollout. Include a minimum sample size or observation window, maximum regression in tail latency and error rate, and a correctness threshold. Roll back automatically when a hard safety boundary is crossed; use manual review for ambiguous quality movement. Preserve enough evidence from both paths to explain why the gate passed or failed.

Configuration deserves the same discipline as code. Review changes, validate them before activation, keep an immutable history, and make rollback a single operation. If a deployment changes code and configuration together, record both versions. Otherwise an incident responder may roll back the binary while leaving the triggering configuration active.

Capacity and cost controls

Model capacity in units the bottleneck understands: concurrent connections, tokens, queue jobs, database transactions, GPU memory, or bytes in flight. Convert the expected traffic distribution into those units and include burst behavior. Then load-test the first constrained dependency, not merely the public endpoint. A system that accepts more work than it can finish within its deadline is overloaded even if CPU utilization looks comfortable.

Cost is also a reliability limit. Add per-request attribution, tenant or workflow budgets, and a global circuit breaker for unexpectedly expensive paths. Review unit economics at the same granularity as performance; a cheap median can conceal a small class of requests responsible for most spend. Optimize only after measuring, because reducing context, replicas, validation, or redundancy can trade visible cost for less visible risk.

Production readiness review

Before launch, ask an engineer who did not build the feature to follow the runbook through one simulated failure. Verify backups or checkpoints by restoring them, not by checking that a job reported success. Exercise credential rotation, dependency unavailability, bad configuration, and rollback. Assign an owner for each alarm and a date for reviewing thresholds after real traffic arrives.

The final architecture document should be short enough to remain current. Keep the decision, rejected alternatives, invariants, dependency contracts, dashboards, and rollback procedure. Link detailed experiments rather than pasting them into the document. Teams that need help turning this review into an operable service can use our LLM Inference Optimization engineering support.

Frequently Asked Questions

What's the single biggest LLM inference optimization?

4-bit quantization (AWQ) — 4x memory reduction, 1.8x speedup, <2% quality loss. Deploy in 30 minutes with vLLM.

Does quantization hurt model quality?

4-bit: <2% degradation on benchmarks, imperceptible in most production tasks.
8-bit: <0.5% degradation, safe for 100% of tasks.

What's the difference between AWQ and GPTQ?

Both are 4-bit quantization methods. AWQ is faster (better GPU kernels) and easier to deploy with vLLM. GPTQ has slightly higher quality (~0.3%) but slower inference.

Should I use Flash Attention?

Yes, always. It's enabled by default in vLLM and HuggingFace (with attn_implementation="flash_attention_2"). No downsides, 2-4x speedup on long contexts.

How much does KV cache quantization help?

50% memory reduction with <0.5% quality loss. Essential for long contexts (4K+ tokens) or high batch sizes.

Can I combine all optimizations?

Yes. Typical stack: AWQ 4-bit + Flash Attention + PagedAttention + KV cache quant + continuous batching = 5-10x total speedup.

How do I benchmark my optimizations?

Use vLLM benchmarking tools:

bash
python benchmarks/benchmark_serving.py \
  --model casperhansen/llama-3.1-70b-instruct-awq \
  --dataset-name random \
  --num-prompts 1000

What's the best GPU for quantized LLM serving?

  • A10G 24GB: Best cost/performance for 7-13B models
  • A100 40GB: Best for 70B quantized or 13B unquantized
  • H100 80GB: Best for 70B+ with highest throughput needs

Deploy with Kubernetes GPU management.


Conclusion

LLM inference optimization in 2026 is well-understood: quantization reduces memory, Flash Attention optimizes computation, PagedAttention eliminates fragmentation, and continuous batching maximizes throughput. Combined, these deliver 5-10x speedup at 60-80% lower cost with <2% quality loss.

The optimization playbook:

  1. Start with 4-bit AWQ quantization — biggest single win
  2. Enable Flash Attention — free speedup on long contexts
  3. Deploy on vLLM — PagedAttention + continuous batching
  4. Add KV cache quantization for long contexts or high batch sizes
  5. Benchmark and iterate — measure TTFT, throughput, quality

At HinterBuild, we optimize LLM serving infrastructure for production:

Contact us to optimize your LLM inference stack.

Free consultation

Book a free consultation call on LLM quantization & inference

30-minute call with the HinterBuild team. Discuss your project, architecture questions, or next steps — no obligation.

Book a meeting

Keep reading