archon-final-backup / m10_int8_quant.py
jescy525's picture
Upload folder using huggingface_hub
612c58c verified
Raw
History Blame Contribute Delete
3.25 kB
"""M10 — Quantization for ARCHON inference acceleration.
Primary path: NVFP4 PTQ via TensorRT-LLM 0.17+ on Blackwell sm_120 (RTX PRO 6000).
Fallback path: INT8 dynamic torch.quantization on sm_70 V100.
Both achieve memory reduction; speedup depends on hardware tensor cores.
Spec NVFP4 (NVIDIA blog 2026):
- 0.1% MMLU drop FP4 vs FP8
- 2-3x speedup throughput
- Blackwell-exclusive (B200, B100, RTX PRO 6000 sm_120)
V100 fallback INT8 dynamic:
- ~2x speedup on Linear layers via fbgemm INT8
- Tensor cores INT8 (sm_70+) accelerate matmul
- Slight accuracy drop ~0.5-1% MMLU
"""
from __future__ import annotations
import torch
import torch.nn as nn
def quantize_int8_dynamic(model: nn.Module) -> nn.Module:
"""Apply INT8 dynamic quantization to Linear layers.
Compatible V100 sm_70+. Wraps every nn.Linear in DynamicallyQuantizedLinear.
Returns: quantized model (replaces original in-place semantically).
"""
quantized = torch.quantization.quantize_dynamic(
model.cpu(),
{nn.Linear},
dtype=torch.qint8,
)
return quantized
def quantize_nvfp4_blackwell_ptq(model: nn.Module, calib_data, save_path: str):
"""NVFP4 PTQ for Blackwell. Requires TensorRT-LLM 0.17+ + ModelOpt.
Stub: actual implementation uses nvidia-ammo / modelopt toolkit.
Steps:
1. Calibrate with ~256 representative samples
2. Layer-wise sensitivity analysis (skip MTP heads — keep FP8)
3. Export to TensorRT engine .plan
4. Serve via TRT-LLM Python runtime
"""
# Pseudo-code for documentation
code = """
import modelopt.torch.quantization as mtq
cfg = mtq.NVFP4_DEFAULT_CFG
# Keep MTP heads at FP8 (sensitive)
cfg["quant_cfg"]["*mtp_heads*"] = mtq.FP8_DEFAULT_CFG
cfg["quant_cfg"]["*lm_head*"] = mtq.FP8_DEFAULT_CFG
model_q = mtq.quantize(model, cfg, forward_loop=lambda m: [m(b) for b in calib_data])
mtq.print_quant_summary(model_q)
# Export TRT engine
import tensorrt_llm
# ... build engine
"""
raise NotImplementedError(f"NVFP4 PTQ requires Blackwell GPU + modelopt. Pseudo-code:\n{code}")
def measure_quant_speedup(model_fp16: nn.Module, model_int8: nn.Module,
input_ids: torch.Tensor, n_runs: int = 10) -> dict:
"""Compare forward latency FP16 vs INT8."""
import time
# FP16 baseline
model_fp16.eval()
with torch.no_grad():
# warmup
_ = model_fp16(input_ids)
if input_ids.is_cuda:
torch.cuda.synchronize()
t0 = time.time()
for _ in range(n_runs):
_ = model_fp16(input_ids)
if input_ids.is_cuda:
torch.cuda.synchronize()
t1 = time.time()
fp16_ms = (t1 - t0) / n_runs * 1000
# INT8
model_int8.eval()
cpu_in = input_ids.cpu()
with torch.no_grad():
_ = model_int8(cpu_in)
t0 = time.time()
for _ in range(n_runs):
_ = model_int8(cpu_in)
t1 = time.time()
int8_ms = (t1 - t0) / n_runs * 1000
return {
"fp16_ms": fp16_ms,
"int8_ms": int8_ms,
"speedup": fp16_ms / int8_ms,
}
if __name__ == "__main__":
print("[M10 quant] module ready (INT8 V100 fallback + NVFP4 Blackwell stub)")