Views
No views yet
1#!/usr/bin/env python3
2"""Kimi-K2-Thinking W4A8 re-quantization via AMD Quark (File-to-File Quantization)."""
3
4import argparse
5import os
6from quark.torch.quantization.config.config import (
7 FP8E4M3PerTensorSpec, Int4PerChannelSpec, ProgressiveSpec,
8 QConfig, QLayerConfig,
9)
10from quark.torch.quantization.api import ModelQuantizer
11
12
13def get_config():
14 exclude_layers = [
15 "*self_attn*", "*mlp.gate", "*lm_head",
16 "*mlp.gate_proj", "*mlp.up_proj", "*mlp.down_proj",
17 "*shared_experts*",
18 ]
19
20 input_spec = FP8E4M3PerTensorSpec(
21 observer_method="min_max", scale_type="float32", is_dynamic=True,
22 ).to_quantization_spec()
23
24 weight_spec = ProgressiveSpec(
25 first_stage=FP8E4M3PerTensorSpec(
26 observer_method="min_max", scale_type="float32", is_dynamic=False,
27 ),
28 second_stage=Int4PerChannelSpec(
29 symmetric=True, scale_type="float32",
30 round_method="half_even", is_dynamic=False, ch_axis=0,
31 ),
32 ).to_quantization_spec()
33
34 return QConfig(
35 global_quant_config=QLayerConfig(input_tensors=input_spec, weight=weight_spec),
36 exclude=exclude_layers,
37 )
38
39
40if __name__ == "__main__":
41 parser = argparse.ArgumentParser()
42 parser.add_argument("--model-path", type=str, required=True,
43 help="Path to moonshotai/Kimi-K2-Thinking checkpoint")
44 parser.add_argument("--export-path", type=str, required=True,
45 help="Path to save quantized output")
46 args = parser.parse_args()
47
48 quantizer = ModelQuantizer(get_config())
49 quantizer.direct_quantize_checkpoint(
50 pretrained_model_path=args.model_path,
51 save_path=args.export_path,
52 )
53 print("[INFO] Quantization completed")| Benchmark | Kimi-K2-Thinking | Kimi-K2-Thinking-W4A8(this model) | Recovery |
| GSM8K | 93.93 | 93.4 | 99.4% |
MODEL_DIR=/data/amd/Kimi-K2-Thinking-W4A8
VLLM_ATTENTION_BACKEND="TRITON_MLA" VLLM_ROCM_USE_AITER=1 VLLM_ROCM_USE_AITER_FUSION_SHARED_EXPERTS=0 VLLM_ROCM_USE_AITER_FP4BMM=0 vllm serve $MODEL_DIR \
--port 8001 \
--trust-remote-code \
--gpu-memory-utilization 0.9 \
--tensor-parallel-size 8MODEL_ARGS="model=/data/amd/Kimi-K2-Thinking-W4A8,base_url=http://localhost:8001/v1/completions,num_concurrent=999999,timeout=999999,tokenized_requests=False,max_length=38768,temperature=0.6,top_p=0.95,add_bos_token=True,seed=$SEED,trust_remote_code=True"
lm_eval \
--model local-completions \
--model_args $MODEL_ARGS \
--tasks gsm8k \
--num_fewshot 8 \
--batch_size auto