2026-05-09 06:07:22 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
"""
|
2026-05-09 06:31:08 +00:00
|
|
|
DeepSeek V4 Pro → NVFP4 quantization — defensive edition.
|
2026-05-09 06:07:22 +00:00
|
|
|
|
2026-05-09 06:31:08 +00:00
|
|
|
Runs the full ModelOpt PTQ pipeline with maximum protection against GPU tensor
|
|
|
|
|
corruption that crashes the export after 6 hours of calibration.
|
|
|
|
|
|
|
|
|
|
Key defense: immediately after calibration, every quantizer _amax tensor is
|
|
|
|
|
snapshotted to CPU. Then the model state is saved to disk. If export crashes,
|
|
|
|
|
the state can be reloaded and export retried without re-calibrating.
|
|
|
|
|
|
|
|
|
|
The _amax tensors are tiny (scalars and small vectors). Snapshotting ~49K of them
|
|
|
|
|
to CPU costs almost nothing in memory and guarantees we have valid calibration
|
|
|
|
|
data regardless of what CUDA does to the GPU copies afterward.
|
2026-05-09 06:07:22 +00:00
|
|
|
|
2026-05-09 06:08:35 +00:00
|
|
|
Must be run from the modelopt example directory for imports:
|
|
|
|
|
cd /root/nvidia-meeting/modelopt-repo/examples/llm_ptq
|
|
|
|
|
python3 /root/nvidia-meeting/deepseek-v4-quant/scripts/quantize_nvfp4.py
|
|
|
|
|
|
2026-05-09 06:07:22 +00:00
|
|
|
Usage:
|
|
|
|
|
# Full run (calibrate + export):
|
2026-05-09 06:08:35 +00:00
|
|
|
python3 /root/nvidia-meeting/deepseek-v4-quant/scripts/quantize_nvfp4.py
|
2026-05-09 06:07:22 +00:00
|
|
|
|
|
|
|
|
# Re-run export only (after a calibration save exists):
|
2026-05-09 06:08:35 +00:00
|
|
|
python3 /root/nvidia-meeting/deepseek-v4-quant/scripts/quantize_nvfp4.py --export-only
|
2026-05-09 06:31:08 +00:00
|
|
|
|
|
|
|
|
# Validate saved calibration state (check amax values):
|
|
|
|
|
python3 /root/nvidia-meeting/deepseek-v4-quant/scripts/quantize_nvfp4.py --validate-only
|
2026-05-09 06:07:22 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
import copy
|
2026-05-09 06:31:08 +00:00
|
|
|
import gc
|
2026-05-09 06:07:22 +00:00
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
import time
|
|
|
|
|
import warnings
|
|
|
|
|
|
|
|
|
|
import torch
|
|
|
|
|
|
|
|
|
|
# ── Config ──────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
MODEL = "/root/nvidia-meeting/DeepSeek-V4-Pro-BF16"
|
|
|
|
|
QUANT = "nvfp4"
|
|
|
|
|
TP = 8
|
|
|
|
|
CALIB_SIZE = 128
|
|
|
|
|
CALIB_SEQ = 512
|
|
|
|
|
KV_CACHE_QUANT = "fp8_cast"
|
|
|
|
|
GPU_MEM_PCT = 0.7
|
|
|
|
|
|
|
|
|
|
HF_TOKEN = "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"
|
|
|
|
|
|
2026-05-09 06:08:35 +00:00
|
|
|
# Paths
|
|
|
|
|
EXAMPLE_DIR = "/root/nvidia-meeting/modelopt-repo/examples/llm_ptq"
|
2026-05-09 06:07:22 +00:00
|
|
|
EXPORT_DIR = "/root/nvidia-meeting/DeepSeek-V4-Pro-NVFP4"
|
|
|
|
|
CALIB_SAVE_PATH = "/root/nvidia-meeting/v4_nvfp4_calibrated_state.pt"
|
2026-05-09 06:31:08 +00:00
|
|
|
AMAX_SNAPSHOT_PATH = "/root/nvidia-meeting/v4_nvfp4_amax_snapshots.pt"
|
|
|
|
|
|
2026-05-09 06:07:22 +00:00
|
|
|
|
|
|
|
|
def apply_patches():
|
2026-05-09 06:31:08 +00:00
|
|
|
"""Apply runtime patches for V4 compatibility and GPU tensor safety."""
|
2026-05-09 06:07:22 +00:00
|
|
|
|
2026-05-09 06:10:18 +00:00
|
|
|
from modelopt.torch.quantization.nn.modules import tensor_quantizer as tq_module
|
2026-05-09 09:26:23 +00:00
|
|
|
from modelopt.torch.quantization.qtensor import nvfp4_tensor
|
2026-05-09 06:07:22 +00:00
|
|
|
|
2026-05-09 08:04:03 +00:00
|
|
|
# ── Patch 1: load_calib_amax — force _amax to CPU after calibration ──
|
2026-05-09 06:31:08 +00:00
|
|
|
#
|
2026-05-09 08:04:03 +00:00
|
|
|
# load_calib_amax() is called by max_calibrate() after the forward loop
|
|
|
|
|
# finishes. It writes _amax to GPU by default. We patch it so _amax
|
2026-05-09 09:26:23 +00:00
|
|
|
# goes to CPU immediately, preventing GPU corruption during the long
|
|
|
|
|
# wait before export.
|
2026-05-09 08:04:03 +00:00
|
|
|
orig_load_calib_amax = tq_module.TensorQuantizer.load_calib_amax
|
|
|
|
|
|
|
|
|
|
def patched_load_calib_amax(self, *args, **kwargs):
|
|
|
|
|
orig_load_calib_amax(self, *args, **kwargs)
|
|
|
|
|
if hasattr(self, '_amax') and self._amax is not None:
|
|
|
|
|
self._amax = self._amax.cpu()
|
|
|
|
|
|
|
|
|
|
tq_module.TensorQuantizer.load_calib_amax = patched_load_calib_amax
|
|
|
|
|
print("✓ Patched TensorQuantizer.load_calib_amax (force _amax to CPU)")
|
2026-05-09 06:31:08 +00:00
|
|
|
|
|
|
|
|
# ── Patch 2: export_amax — CPU safety ──
|
|
|
|
|
# If any _amax is still on GPU at export time, move it before reading.
|
2026-05-09 06:08:35 +00:00
|
|
|
orig_export_amax = tq_module.TensorQuantizer.export_amax
|
|
|
|
|
|
|
|
|
|
def patched_export_amax(self):
|
|
|
|
|
if self.amax is not None and self.amax.is_cuda:
|
|
|
|
|
self._amax = self._amax.cpu()
|
|
|
|
|
return orig_export_amax(self)
|
|
|
|
|
|
|
|
|
|
tq_module.TensorQuantizer.export_amax = patched_export_amax
|
2026-05-09 06:31:08 +00:00
|
|
|
print("✓ Patched TensorQuantizer.export_amax (CPU fallback)")
|
2026-05-09 06:08:35 +00:00
|
|
|
|
2026-05-09 06:31:08 +00:00
|
|
|
# ── Patch 3: NVFP4QTensor.get_activation_scaling_factor — graceful degradation ──
|
2026-05-09 06:07:22 +00:00
|
|
|
@classmethod
|
|
|
|
|
def patched_get_activation_scaling_factor(cls, quantizer):
|
|
|
|
|
if not quantizer.is_enabled:
|
|
|
|
|
return None
|
|
|
|
|
try:
|
|
|
|
|
amax = quantizer.export_amax()
|
|
|
|
|
except (torch.cuda.CudaError, RuntimeError) as e:
|
|
|
|
|
print(f" WARNING: export_amax() failed ({e}), attempting CPU recovery...")
|
|
|
|
|
if hasattr(quantizer, '_amax') and quantizer._amax is not None:
|
|
|
|
|
quantizer._amax = quantizer._amax.cpu()
|
|
|
|
|
amax = quantizer.export_amax()
|
|
|
|
|
|
|
|
|
|
if amax is None:
|
|
|
|
|
return None
|
|
|
|
|
amax = amax.cpu()
|
|
|
|
|
activation_scaling_factor = amax.float() / (quantizer.maxbound * 448.0)
|
|
|
|
|
|
|
|
|
|
if not torch.all(activation_scaling_factor > 0):
|
|
|
|
|
n_bad = (activation_scaling_factor <= 0).sum().item()
|
|
|
|
|
n_total = activation_scaling_factor.numel()
|
2026-05-09 06:31:08 +00:00
|
|
|
print(f" WARNING: {n_bad}/{n_total} activation scaling factors <= 0, clamping")
|
2026-05-09 06:07:22 +00:00
|
|
|
activation_scaling_factor = activation_scaling_factor.clamp(min=torch.finfo(torch.float32).tiny)
|
|
|
|
|
|
|
|
|
|
return activation_scaling_factor
|
|
|
|
|
|
|
|
|
|
nvfp4_tensor.NVFP4QTensor.get_activation_scaling_factor = patched_get_activation_scaling_factor
|
2026-05-09 06:31:08 +00:00
|
|
|
print("✓ Patched NVFP4QTensor.get_activation_scaling_factor (CPU + clamp)")
|
|
|
|
|
|
2026-05-09 06:07:22 +00:00
|
|
|
|
2026-05-09 06:31:08 +00:00
|
|
|
def snapshot_amax_to_cpu(model, snapshot_path):
|
|
|
|
|
"""Walk all quantizers, copy their _amax to CPU, save to disk.
|
2026-05-09 06:07:22 +00:00
|
|
|
|
2026-05-09 09:26:23 +00:00
|
|
|
After calibration completes, the _amax tensors are fresh and valid on GPU.
|
|
|
|
|
We copy them to CPU immediately and save to disk. This costs almost nothing
|
|
|
|
|
(~50MB for ~49K quantizers) but guarantees we have valid calibration data
|
|
|
|
|
even if CUDA corrupts the GPU copies later.
|
2026-05-09 06:31:08 +00:00
|
|
|
"""
|
2026-05-09 06:08:35 +00:00
|
|
|
from modelopt.torch.quantization.nn.modules.tensor_quantizer import TensorQuantizer
|
2026-05-09 06:31:08 +00:00
|
|
|
|
|
|
|
|
print(f"\nSnapshotting quantizer _amax to CPU...")
|
|
|
|
|
t0 = time.time()
|
|
|
|
|
snapshots = {}
|
|
|
|
|
n_moved = 0
|
|
|
|
|
|
|
|
|
|
for name, module in model.named_modules():
|
|
|
|
|
if not isinstance(module, TensorQuantizer):
|
|
|
|
|
continue
|
|
|
|
|
if hasattr(module, '_amax') and module._amax is not None:
|
|
|
|
|
amax_cpu = module._amax.detach().cpu().clone()
|
|
|
|
|
snapshots[name] = amax_cpu
|
|
|
|
|
module._amax.data.copy_(amax_cpu)
|
|
|
|
|
n_moved += 1
|
|
|
|
|
|
|
|
|
|
torch.save(snapshots, snapshot_path)
|
|
|
|
|
size_mb = os.path.getsize(snapshot_path) / (1024**2)
|
|
|
|
|
print(f"✓ Snapshotted {n_moved} quantizer _amax tensors to CPU ({time.time()-t0:.1f}s)")
|
|
|
|
|
print(f" Saved to: {snapshot_path} ({size_mb:.1f} MB)")
|
|
|
|
|
|
|
|
|
|
return snapshots
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def restore_amax_from_snapshot(model, snapshot_path):
|
2026-05-09 09:26:23 +00:00
|
|
|
"""Restore _amax from a previously saved CPU snapshot."""
|
2026-05-09 06:31:08 +00:00
|
|
|
from modelopt.torch.quantization.nn.modules.tensor_quantizer import TensorQuantizer
|
|
|
|
|
|
|
|
|
|
print(f"Restoring _amax from snapshot: {snapshot_path}")
|
|
|
|
|
snapshots = torch.load(snapshot_path, map_location='cpu')
|
|
|
|
|
n_restored = 0
|
|
|
|
|
|
|
|
|
|
for name, module in model.named_modules():
|
|
|
|
|
if not isinstance(module, TensorQuantizer):
|
|
|
|
|
continue
|
|
|
|
|
if name in snapshots and hasattr(module, '_amax'):
|
|
|
|
|
module._amax.data.copy_(snapshots[name].to(module._amax.device))
|
|
|
|
|
n_restored += 1
|
|
|
|
|
|
|
|
|
|
print(f"✓ Restored {n_restored} _amax tensors from snapshot")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def force_all_amax_to_cpu(model):
|
2026-05-09 09:26:23 +00:00
|
|
|
"""Force ALL quantizer tensors to CPU. Nuclear option after calibration."""
|
2026-05-09 06:31:08 +00:00
|
|
|
from modelopt.torch.quantization.nn.modules.tensor_quantizer import TensorQuantizer
|
|
|
|
|
|
2026-05-09 06:07:22 +00:00
|
|
|
count = 0
|
|
|
|
|
for name, module in model.named_modules():
|
2026-05-09 06:31:08 +00:00
|
|
|
if not isinstance(module, TensorQuantizer):
|
|
|
|
|
continue
|
|
|
|
|
for attr in ['_amax', '_pre_quant_scale', '_global_amax']:
|
|
|
|
|
if hasattr(module, attr):
|
|
|
|
|
val = getattr(module, attr)
|
|
|
|
|
if val is not None and isinstance(val, torch.Tensor) and val.is_cuda:
|
|
|
|
|
setattr(module, attr, val.cpu())
|
2026-05-09 06:08:35 +00:00
|
|
|
count += 1
|
2026-05-09 06:31:08 +00:00
|
|
|
print(f"✓ Forced {count} quantizer tensors to CPU")
|
2026-05-09 06:07:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def save_calibrated_state(model, path):
|
2026-05-09 09:26:23 +00:00
|
|
|
"""Save model state dict after calibration."""
|
2026-05-09 06:07:22 +00:00
|
|
|
print(f"\n{'='*60}")
|
|
|
|
|
print(f"SAVING CALIBRATED STATE → {path}")
|
|
|
|
|
print(f"{'='*60}")
|
|
|
|
|
|
|
|
|
|
start = time.time()
|
|
|
|
|
|
|
|
|
|
state = {
|
|
|
|
|
'model_state_dict': model.state_dict(),
|
|
|
|
|
'timestamp': time.strftime('%Y-%m-%d %H:%M:%S'),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
torch.save(state, path)
|
|
|
|
|
size_gb = os.path.getsize(path) / (1024**3)
|
|
|
|
|
print(f"✓ Saved calibrated state: {size_gb:.1f} GB ({time.time()-start:.0f}s)")
|
|
|
|
|
print(f" Path: {path}")
|
2026-05-09 06:31:08 +00:00
|
|
|
print(f" Re-run with --export-only to retry export.\n")
|
|
|
|
|
|
2026-05-09 06:07:22 +00:00
|
|
|
|
2026-05-09 06:31:08 +00:00
|
|
|
def run_calibration(model_path, export_dir, calib_save_path, amax_snapshot_path, calib_size, calib_seq):
|
|
|
|
|
"""Full pipeline: load → quantize → calibrate → snapshot → save → export."""
|
2026-05-09 06:07:22 +00:00
|
|
|
|
2026-05-09 06:08:35 +00:00
|
|
|
os.chdir(EXAMPLE_DIR)
|
|
|
|
|
sys.path.insert(0, EXAMPLE_DIR)
|
2026-05-09 06:07:22 +00:00
|
|
|
|
2026-05-09 06:08:35 +00:00
|
|
|
os.environ["HF_TOKEN"] = HF_TOKEN
|
|
|
|
|
os.environ["HUGGING_FACE_HUB_TOKEN"] = HF_TOKEN
|
|
|
|
|
|
2026-05-09 09:26:23 +00:00
|
|
|
# Import from hf_ptq and modelopt — all verified against the example script
|
|
|
|
|
from example_utils import get_model, get_tokenizer
|
2026-05-09 06:08:35 +00:00
|
|
|
from hf_ptq import (
|
2026-05-09 09:26:23 +00:00
|
|
|
make_calib_dataloader,
|
|
|
|
|
build_quant_cfg,
|
|
|
|
|
load_mtp_weights,
|
|
|
|
|
copy_custom_model_files,
|
|
|
|
|
QUANT_CFG_CHOICES,
|
|
|
|
|
KV_QUANT_CFG_CHOICES,
|
2026-05-09 06:08:35 +00:00
|
|
|
)
|
2026-05-09 06:07:22 +00:00
|
|
|
from modelopt.torch import quantization as mtq
|
2026-05-09 06:08:35 +00:00
|
|
|
from modelopt.torch.quantization.config import need_calibration
|
2026-05-09 06:07:22 +00:00
|
|
|
from modelopt.torch.utils.dataset_utils import get_max_batch_size
|
2026-05-09 06:08:35 +00:00
|
|
|
from modelopt.torch.export import export_hf_checkpoint
|
2026-05-09 06:07:22 +00:00
|
|
|
|
|
|
|
|
apply_patches()
|
|
|
|
|
|
|
|
|
|
# ── Load model ──
|
2026-05-09 09:26:23 +00:00
|
|
|
# Use modelopt's get_model() — handles max_memory properly for 3TB model.
|
|
|
|
|
# Raw AutoModelForCausalLM.from_pretrained OOMs during expert weight conversion.
|
2026-05-09 06:07:22 +00:00
|
|
|
print(f"\nLoading model from {model_path}...")
|
|
|
|
|
t0 = time.time()
|
|
|
|
|
|
2026-05-09 09:26:23 +00:00
|
|
|
model = get_model(
|
2026-05-09 06:07:22 +00:00
|
|
|
model_path,
|
2026-05-09 08:00:50 +00:00
|
|
|
gpu_mem_percentage=GPU_MEM_PCT,
|
2026-05-09 06:07:22 +00:00
|
|
|
trust_remote_code=True,
|
2026-05-09 08:00:50 +00:00
|
|
|
use_seq_device_map=True,
|
2026-05-09 06:07:22 +00:00
|
|
|
)
|
2026-05-09 08:00:50 +00:00
|
|
|
tokenizer = get_tokenizer(model_path, trust_remote_code=True)
|
2026-05-09 06:08:35 +00:00
|
|
|
print(f"✓ Model loaded in {time.time()-t0:.0f}s")
|
2026-05-09 06:07:22 +00:00
|
|
|
|
|
|
|
|
# ── Setup quantization config ──
|
2026-05-09 09:26:23 +00:00
|
|
|
# Same flow as hf_ptq's quantize_main()
|
2026-05-09 06:07:22 +00:00
|
|
|
quant_cfg = copy.deepcopy(QUANT_CFG_CHOICES[QUANT])
|
|
|
|
|
quant_cfg = build_quant_cfg(QUANT, quant_cfg, None, None, None)
|
|
|
|
|
|
|
|
|
|
if KV_CACHE_QUANT != "none":
|
2026-05-09 09:26:23 +00:00
|
|
|
enable_quant_kv_cache = True
|
|
|
|
|
print(f"✓ KV cache quantization: {KV_CACHE_QUANT}")
|
2026-05-09 06:07:22 +00:00
|
|
|
quant_cfg = mtq.update_quant_cfg_with_kv_cache_quant(
|
|
|
|
|
quant_cfg,
|
2026-05-09 09:17:12 +00:00
|
|
|
getattr(mtq, KV_QUANT_CFG_CHOICES[KV_CACHE_QUANT])["quant_cfg"],
|
2026-05-09 06:07:22 +00:00
|
|
|
)
|
2026-05-09 09:26:23 +00:00
|
|
|
else:
|
|
|
|
|
enable_quant_kv_cache = False
|
2026-05-09 06:07:22 +00:00
|
|
|
|
|
|
|
|
# ── Detect batch size ──
|
2026-05-09 09:26:23 +00:00
|
|
|
# Same as hf_ptq's quantize_main()
|
2026-05-09 06:07:22 +00:00
|
|
|
print("\nDetecting max calibration batch size...")
|
|
|
|
|
batch_size = get_max_batch_size(
|
|
|
|
|
model,
|
2026-05-09 06:08:35 +00:00
|
|
|
max_sample_length=calib_seq,
|
2026-05-09 06:07:22 +00:00
|
|
|
sample_memory_usage_ratio=1.1,
|
|
|
|
|
)
|
2026-05-09 06:08:35 +00:00
|
|
|
batch_size = min(batch_size, calib_size)
|
2026-05-09 06:07:22 +00:00
|
|
|
print(f"✓ Using calibration batch_size={batch_size}")
|
|
|
|
|
|
|
|
|
|
# ── Prepare dataloader ──
|
2026-05-09 09:26:23 +00:00
|
|
|
# Same args structure as hf_ptq
|
2026-05-09 06:08:35 +00:00
|
|
|
args = argparse.Namespace(
|
|
|
|
|
calib_size=[calib_size],
|
|
|
|
|
calib_seq=calib_seq,
|
|
|
|
|
calib_dataset="",
|
2026-05-09 13:37:24 +00:00
|
|
|
dataset=None, # None triggers default: ["cnn_dailymail", "nemotron-post-training-dataset-v2"]
|
2026-05-09 06:08:35 +00:00
|
|
|
batch_size=batch_size,
|
|
|
|
|
calib_batch_size=0,
|
2026-05-09 13:37:24 +00:00
|
|
|
calib_with_images=False,
|
|
|
|
|
auto_quantize_bits=None,
|
|
|
|
|
auto_quantize_method=None,
|
|
|
|
|
specdec_offline_dataset=None,
|
|
|
|
|
inference_pipeline_parallel=1,
|
2026-05-09 06:08:35 +00:00
|
|
|
)
|
2026-05-09 06:07:22 +00:00
|
|
|
calib_dataloader, _ = make_calib_dataloader(
|
2026-05-09 06:08:35 +00:00
|
|
|
args, model, None, tokenizer, torch.device("cuda"), None,
|
2026-05-09 06:07:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# ── Quantize + Calibrate ──
|
|
|
|
|
print(f"\n{'='*60}")
|
2026-05-09 06:08:35 +00:00
|
|
|
print(f"QUANTIZING: {QUANT} with {calib_size} calibration samples")
|
2026-05-09 06:07:22 +00:00
|
|
|
print(f"{'='*60}")
|
|
|
|
|
t0 = time.time()
|
|
|
|
|
|
|
|
|
|
model = mtq.quantize(model, quant_cfg, forward_loop=calib_dataloader)
|
|
|
|
|
|
|
|
|
|
print(f"✓ Quantization + calibration complete in {time.time()-t0:.0f}s")
|
|
|
|
|
|
2026-05-09 06:31:08 +00:00
|
|
|
# ── IMMEDIATELY snapshot all _amax to CPU ──
|
|
|
|
|
snapshots = snapshot_amax_to_cpu(model, amax_snapshot_path)
|
|
|
|
|
|
|
|
|
|
# ── Force ALL quantizer state to CPU ──
|
|
|
|
|
force_all_amax_to_cpu(model)
|
|
|
|
|
|
2026-05-09 09:26:23 +00:00
|
|
|
# ── Free GPU memory ──
|
2026-05-09 06:31:08 +00:00
|
|
|
torch.cuda.empty_cache()
|
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
|
|
# ── SAVE STATE ──
|
2026-05-09 06:07:22 +00:00
|
|
|
save_calibrated_state(model, calib_save_path)
|
|
|
|
|
|
|
|
|
|
# ── Export ──
|
2026-05-09 06:31:08 +00:00
|
|
|
run_export(model, tokenizer, model_path, export_dir, amax_snapshot_path)
|
2026-05-09 06:07:22 +00:00
|
|
|
|
|
|
|
|
|
2026-05-09 06:31:08 +00:00
|
|
|
def run_export(model, tokenizer, model_path, export_dir, amax_snapshot_path=None):
|
2026-05-09 06:07:22 +00:00
|
|
|
"""Export the quantized model to HF safetensors format."""
|
|
|
|
|
from modelopt.torch.export import export_hf_checkpoint
|
2026-05-09 06:08:35 +00:00
|
|
|
from hf_ptq import load_mtp_weights, copy_custom_model_files
|
2026-05-09 06:07:22 +00:00
|
|
|
|
|
|
|
|
print(f"\n{'='*60}")
|
|
|
|
|
print(f"EXPORTING → {export_dir}")
|
|
|
|
|
print(f"{'='*60}")
|
|
|
|
|
|
2026-05-09 06:31:08 +00:00
|
|
|
force_all_amax_to_cpu(model)
|
|
|
|
|
if amax_snapshot_path and os.path.exists(amax_snapshot_path):
|
|
|
|
|
restore_amax_from_snapshot(model, amax_snapshot_path)
|
|
|
|
|
|
|
|
|
|
torch.cuda.empty_cache()
|
|
|
|
|
gc.collect()
|
2026-05-09 06:07:22 +00:00
|
|
|
|
|
|
|
|
t0 = time.time()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
mtp_layer_prefixes, mtp_state_dict = load_mtp_weights(model, model_path)
|
|
|
|
|
if mtp_layer_prefixes:
|
|
|
|
|
model._mtp_layer_prefixes = mtp_layer_prefixes
|
|
|
|
|
|
|
|
|
|
export_hf_checkpoint(
|
|
|
|
|
model,
|
|
|
|
|
export_dir=export_dir,
|
|
|
|
|
extra_state_dict=mtp_state_dict,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
tokenizer.save_pretrained(export_dir)
|
|
|
|
|
copy_custom_model_files(model_path, export_dir, True)
|
|
|
|
|
|
2026-05-09 06:08:35 +00:00
|
|
|
print(f"\n✓ Export complete in {time.time()-t0:.0f}s → {export_dir}")
|
2026-05-09 06:07:22 +00:00
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"\n✗ EXPORT FAILED: {e}")
|
2026-05-09 06:31:08 +00:00
|
|
|
print(f" Calibrated state: {CALIB_SAVE_PATH}")
|
|
|
|
|
print(f" Amax snapshots: {AMAX_SNAPSHOT_PATH}")
|
|
|
|
|
print(f" Re-run with --export-only to retry")
|
2026-05-09 06:07:22 +00:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
2026-05-09 06:31:08 +00:00
|
|
|
def run_export_only(calib_save_path, amax_snapshot_path, model_path, export_dir):
|
|
|
|
|
"""Load saved calibration state and run export only."""
|
2026-05-09 09:26:23 +00:00
|
|
|
|
2026-05-09 06:08:35 +00:00
|
|
|
os.chdir(EXAMPLE_DIR)
|
|
|
|
|
sys.path.insert(0, EXAMPLE_DIR)
|
|
|
|
|
|
|
|
|
|
os.environ["HF_TOKEN"] = HF_TOKEN
|
|
|
|
|
os.environ["HUGGING_FACE_HUB_TOKEN"] = HF_TOKEN
|
2026-05-09 06:07:22 +00:00
|
|
|
|
|
|
|
|
apply_patches()
|
|
|
|
|
|
2026-05-09 09:26:23 +00:00
|
|
|
from example_utils import get_model, get_tokenizer
|
2026-05-09 06:07:22 +00:00
|
|
|
|
2026-05-09 09:26:23 +00:00
|
|
|
print(f"Loading model from {model_path}...")
|
|
|
|
|
model = get_model(
|
2026-05-09 06:07:22 +00:00
|
|
|
model_path,
|
2026-05-09 08:00:50 +00:00
|
|
|
device="cpu",
|
2026-05-09 06:07:22 +00:00
|
|
|
trust_remote_code=True,
|
|
|
|
|
)
|
2026-05-09 08:00:50 +00:00
|
|
|
tokenizer = get_tokenizer(model_path, trust_remote_code=True)
|
2026-05-09 06:07:22 +00:00
|
|
|
|
2026-05-09 06:08:35 +00:00
|
|
|
print(f"Loading calibrated state from {calib_save_path}...")
|
|
|
|
|
state = torch.load(calib_save_path, map_location='cpu')
|
|
|
|
|
model.load_state_dict(state['model_state_dict'])
|
|
|
|
|
print(f"✓ Loaded calibrated state (saved at {state['timestamp']})")
|
2026-05-09 06:07:22 +00:00
|
|
|
|
2026-05-09 06:31:08 +00:00
|
|
|
run_export(model, tokenizer, model_path, export_dir, amax_snapshot_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_validate(calib_save_path, amax_snapshot_path):
|
|
|
|
|
"""Validate saved calibration state — check amax values are valid."""
|
|
|
|
|
print(f"\nValidating calibration state...")
|
|
|
|
|
|
|
|
|
|
if os.path.exists(amax_snapshot_path):
|
|
|
|
|
snapshots = torch.load(amax_snapshot_path, map_location='cpu')
|
|
|
|
|
n_total = len(snapshots)
|
|
|
|
|
n_valid = 0
|
|
|
|
|
n_zero = 0
|
|
|
|
|
n_nan = 0
|
|
|
|
|
n_neg = 0
|
|
|
|
|
|
|
|
|
|
for name, amax in snapshots.items():
|
|
|
|
|
if torch.any(torch.isnan(amax)):
|
|
|
|
|
n_nan += 1
|
|
|
|
|
elif torch.any(amax < 0):
|
|
|
|
|
n_neg += 1
|
|
|
|
|
elif torch.all(amax == 0):
|
|
|
|
|
n_zero += 1
|
|
|
|
|
else:
|
|
|
|
|
n_valid += 1
|
|
|
|
|
|
|
|
|
|
print(f"\nAmax snapshot validation:")
|
|
|
|
|
print(f" Total quantizers: {n_total}")
|
|
|
|
|
print(f" Valid: {n_valid}")
|
|
|
|
|
print(f" All zeros: {n_zero}")
|
|
|
|
|
print(f" Negative: {n_neg}")
|
|
|
|
|
print(f" NaN: {n_nan}")
|
|
|
|
|
|
|
|
|
|
if n_valid == n_total:
|
|
|
|
|
print(f"\n✓ All {n_total} amax snapshots are valid!")
|
|
|
|
|
else:
|
|
|
|
|
print(f"\n✗ {n_total - n_valid} quantizers have invalid amax!")
|
|
|
|
|
else:
|
|
|
|
|
print(f"✗ No amax snapshot found at {amax_snapshot_path}")
|
|
|
|
|
|
|
|
|
|
if os.path.exists(calib_save_path):
|
|
|
|
|
size_gb = os.path.getsize(calib_save_path) / (1024**3)
|
|
|
|
|
print(f"\nCalibrated state: {calib_save_path} ({size_gb:.1f} GB)")
|
|
|
|
|
else:
|
|
|
|
|
print(f"\n✗ No calibrated state found at {calib_save_path}")
|
2026-05-09 06:07:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
parser = argparse.ArgumentParser(description="DeepSeek V4 Pro NVFP4 Quantization")
|
|
|
|
|
parser.add_argument("--export-only", action="store_true",
|
|
|
|
|
help="Skip calibration, load saved state and run export only")
|
2026-05-09 08:02:09 +00:00
|
|
|
parser.add_argument("--validate-only", action="store_true",
|
2026-05-09 06:31:08 +00:00
|
|
|
help="Validate saved calibration state without running anything")
|
2026-05-09 06:07:22 +00:00
|
|
|
parser.add_argument("--model", default=MODEL, help="Path to BF16 model")
|
|
|
|
|
parser.add_argument("--export-dir", default=EXPORT_DIR, help="Export output directory")
|
|
|
|
|
parser.add_argument("--calib-save", default=CALIB_SAVE_PATH, help="Calibration state save path")
|
2026-05-09 06:31:08 +00:00
|
|
|
parser.add_argument("--amax-snapshot", default=AMAX_SNAPSHOT_PATH, help="Amax snapshot path")
|
2026-05-09 06:07:22 +00:00
|
|
|
parser.add_argument("--calib-size", type=int, default=CALIB_SIZE, help="Calibration samples")
|
|
|
|
|
parser.add_argument("--calib-seq", type=int, default=CALIB_SEQ, help="Calibration sequence length")
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
2026-05-09 06:31:08 +00:00
|
|
|
if args.validate_only:
|
|
|
|
|
run_validate(args.calib_save, args.amax_snapshot)
|
|
|
|
|
elif args.export_only:
|
2026-05-09 06:07:22 +00:00
|
|
|
if not os.path.exists(args.calib_save):
|
|
|
|
|
print(f"ERROR: No calibration state found at {args.calib_save}")
|
|
|
|
|
sys.exit(1)
|
2026-05-09 06:31:08 +00:00
|
|
|
run_export_only(args.calib_save, args.amax_snapshot, args.model, args.export_dir)
|
2026-05-09 06:07:22 +00:00
|
|
|
else:
|
2026-05-09 06:08:35 +00:00
|
|
|
run_calibration(args.model, args.export_dir, args.calib_save,
|
2026-05-09 06:31:08 +00:00
|
|
|
args.amax_snapshot, args.calib_size, args.calib_seq)
|
2026-05-09 06:07:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|