Extreme quantization

From j4flmao/agent-skills · View interactive version

Extreme quantization is an AI agent skill from the GitHub repository j4flmao/agent-skills. Use when the user asks about FP8, INT4, NVFP4, MXFP4, GPTQ, AWQ, SmoothQuant, W4A16, W8A8, NF4 or bitsandbytes for LLMs on NVIDIA GPUs. Produces a quantization recipe, code and accuracy-eval plan matched to the GPU and serving engine. Not for format/GGUF basics (use llm-model-quantization) or Tensor

How to use this skill

Skill file

Extreme Quantization (sub-16-bit LLMs)

Purpose

Shrink LLM weights, activations and KV cache to 8, 4 or fewer bits so models fit in less memory and run faster. Weight-only quantization speeds up memory-bound decoding; weight+activation quantization speeds up compute-bound prefill and large batches. Every scheme must be validated on task evals, not just perplexity.

Agent Protocol

Trigger

"quantize", "int4", "int8", "fp8", "fp4", "nvfp4", "mxfp4", "gptq", "awq", "smoothquant", "w4a16", "w8a8", "nf4", "bitsandbytes", "kv cache fp8".

Input Context

Output Artifact

Quantization recipe + command/code + evaluation plan. No file unless requested.

Response Format

Scheme: {W4A16|W8A8-INT8|W8A8-FP8|NVFP4|...} group={size}
Method: {GPTQ|AWQ|SmoothQuant|RTN|dynamic FP8}
Hardware path: {kernel/tensor-core type}
Memory: {before} -> {after}
Eval: {tasks, max allowed drop}

Completion Criteria

Formats

| Format | Bits | Notes |

|--------|------|-------|

| BF16/FP16 | 16 | Baseline. |

| INT8 | 8 | Per-channel weights, per-token activations. Tensor cores since Turing. |

| FP8 E4M3 | 8 | 4 exp / 3 mantissa, max 448. Weights + activations (forward). Tensor cores on Ada and Hopper and later. |

| FP8 E5M2 | 8 | 5 exp / 2 mantissa, max 57344. Wider range; used for gradients in FP8 training. |

| INT4 (W4A16) | 4 | Weight-only, group-wise scales (group size 128 typical). Dequantized in the GEMM kernel (e.g. Marlin/Machete). |

| MXFP4 | 4 | OCP microscaling: E2M1 values, one E8M0 (power-of-two) scale per 32 elements. |

| NVFP4 | 4 | E2M1 values, one FP8 E4M3 scale per 16 elements plus a per-tensor FP32 scale. Native on Blackwell tensor cores. |

| NF4 | 4 | bitsandbytes NormalFloat for QLoRA; storage format, not a fast inference path. |

Methods

Decision Rules

GPU is Hopper/Blackwell and engine supports FP8? → W8A8-FP8 first (lowest risk)
Memory-bound decoding, small batch, need max compression → W4A16 (AWQ or GPTQ)
High-throughput serving, large batches on Ampere → W8A8-INT8 (SmoothQuant + GPTQ)
Blackwell, aggressive throughput → NVFP4 (validate carefully; consider QAT)
Fine-tuning on limited memory → QLoRA (NF4 base + BF16 adapters)

Weight-only 4-bit can be slower than BF16 at large batch sizes because dequantization adds work to a compute-bound GEMM.

Example: GPTQ W4A16 with llm-compressor (vLLM ecosystem)

from llmcompressor import oneshot
from llmcompressor.modifiers.quantization import GPTQModifier

recipe = GPTQModifier(targets="Linear", scheme="W4A16", ignore=["lm_head"])
oneshot(
    model="meta-llama/Llama-3.1-8B-Instruct",
    dataset="open_platypus",
    recipe=recipe,
    max_seq_length=2048,
    num_calibration_samples=512,
    output_dir="Llama-3.1-8B-Instruct-W4A16",
)
vllm serve ./Llama-3.1-8B-Instruct-W4A16          # format detected from the saved config
vllm serve meta-llama/Llama-3.1-8B-Instruct --quantization fp8 --kv-cache-dtype fp8   # dynamic FP8

Example: 4-bit loading for QLoRA (transformers + bitsandbytes)

import torch
from transformers import AutoModelForCausalLM, BitsAndBytesConfig

bnb = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.bfloat16,
    bnb_4bit_use_double_quant=True,
)
model = AutoModelForCausalLM.from_pretrained(
    "meta-llama/Llama-3.1-8B", quantization_config=bnb, device_map="auto"
)

FP8 Training

NVIDIA Transformer Engine's hybrid recipe uses E4M3 in the forward pass and E5M2 for gradients, with delayed scaling (scale from an amax history). Newer recipes use finer-grained (block/MX) scaling. Keep master weights and optimizer state in higher precision.

Memory Arithmetic

Weights ≈ params x bits / 8. A 70B model: ~140 GB in BF16, ~70 GB in FP8, ~35-40 GB in 4-bit (scales/zeros add overhead). KV cache and activations come on top and often dominate at long context.

Anti-Patterns

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.