-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathquantize.py
More file actions
1172 lines (1019 loc) · 45.7 KB
/
Copy pathquantize.py
File metadata and controls
1172 lines (1019 loc) · 45.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import copy
import gc
import json
import os
import re
import sys
import tomllib
from collections import defaultdict
from collections.abc import Mapping
from pathlib import Path
import torch
from torch import nn
from transformers import AutoTokenizer
from safetensors.torch import save_file
import modelopt.torch.quantization as mtq
import logging
from models import load_config, AVAILABLE_MODELS
from models.mimo_v25_visual import (
build_mimo_processor,
precompute_mimo_visual_embeds_for_batches,
replace_mimo_image_pixels,
)
from models.mimo_v25_media import (
audio_codes_from_mels,
audio_placeholder_count_from_codes,
ensure_mimo_audio_tokenizer,
expand_audio_placeholders,
load_audio_mel,
mimo_video_processor_kwargs,
normalize_processor_inputs,
pad_audio_codes_to_group_boundary,
video_audio_source,
video_uses_audio,
)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
parser = argparse.ArgumentParser()
parser.add_argument("--model", required=True, choices=AVAILABLE_MODELS,
help="Model config to use.")
parser.add_argument("--model-id", default=None,
help="Override the default HuggingFace model ID or local path.")
parser.add_argument("--export-dir", required=True)
parser.add_argument("--calib-config", default=None,
help="TOML file describing calibration datasets and parameters.")
parser.add_argument("--data-dir", default="data",
help="Base directory for relative dataset paths in the TOML.")
parser.add_argument("--calib-jsonl", default=None,
help="Single calibration JSONL (shorthand; ignored if --calib-config given).")
parser.add_argument("--calib-limit", type=int, default=192)
parser.add_argument("--batch-size", type=int, default=48)
parser.add_argument("--batch-tokens", type=int, default=128 * 1024,
help="Token budget for auto-computing batch_size when a dataset sets max_len.")
parser.add_argument("--max-len", type=int, default=4096)
parser.add_argument("--cpu-capacity", type=str, default="200GiB")
parser.add_argument("--streaming-gpu-capacity", type=str, default=None,
help="Streaming planner capacity for each storage GPU 1-N. Default: physical GPU memory.")
parser.add_argument("--streaming-gpu0-storage-capacity", type=str, default="0GiB",
help="Optional streaming layer-storage budget on execution GPU 0, e.g. 48GiB.")
parser.add_argument("--save-amax", type=str, default=None,
help="Save calibration amax values to this safetensors file.")
parser.add_argument("--skip-export", action="store_true",
help="Skip model export (amax-only calibration run).")
streaming_group = parser.add_mutually_exclusive_group()
streaming_group.add_argument("--streaming", dest="streaming", action="store_true",
help="Force streaming loader. Default: use model config.")
streaming_group.add_argument("--no-streaming", dest="streaming", action="store_false",
help="Disable streaming loader, overriding the model config.")
parser.add_argument("--device-map", default=None,
help="Transformers device_map for non-streaming loads. Defaults to sequential for MiniMax-M3, auto otherwise.")
parser.add_argument("--max-memory-per-gpu", default=None,
help="Optional Accelerate max_memory cap for every visible GPU, e.g. 88GiB.")
parser.add_argument("--max-memory-cpu", default=None,
help="Optional Accelerate max_memory cap for CPU offload, e.g. 256GiB.")
parser.add_argument("--gpu-headroom-gib", type=float, default=None,
help="GPU memory to leave free when deriving max_memory. Defaults to 7 GiB for MiniMax-M3.")
parser.add_argument("--host-staged-device-moves", choices=["auto", "yes", "no"], default="auto",
help="Stage cross-GPU moves through CPU. For streaming, auto enables this only with more than 9 visible GPUs.")
parser.add_argument("--cuda-staging-device", default=None,
help="For streaming, CUDA trampoline device used by --cuda-staging-source-device transfers to/from cuda:0, e.g. cuda:8.")
parser.add_argument("--cuda-staging-source-device", default=None,
help="Storage CUDA device whose transfers to/from cuda:0 should relay through --cuda-staging-device, e.g. cuda:9.")
parser.add_argument("--cuda-staging-reserve", default="0GiB",
help="Reduce the streaming planner capacity on --cuda-staging-device by this amount.")
parser.add_argument("--attn-implementation", default="auto",
help="Transformers attention backend. For MiniMax-M3, auto resolves to sdpa; use minimax_m3_flex to try the custom FlexAttention path.")
parser.add_argument("--floor-amaxes", action="store_true",
help="Floor sparse expert amaxes to median/10 of their peer group.")
parser.add_argument("--resume-amax", type=str, default=None,
help="Load amax checkpoint and resume calibration from where it left off.")
parser.add_argument("--resume-batch", type=int, default=0,
help="Skip batches before this number (1-indexed). Use with --resume-amax.")
parser.add_argument("--calib-method", default="max", choices=["max", "quantile"],
help="Calibration algorithm. 'quantile' uses P2 streaming quantile estimation.")
parser.add_argument("--save-quantiles", type=str, default=None,
help="Save quantile estimates to this JSON file (quantile calibration only).")
parser.set_defaults(streaming=None)
args = parser.parse_args()
# ---------------------------------------------------------------------------
# Resolve calibration datasets.
# ---------------------------------------------------------------------------
def load_calib_datasets(args):
"""Return list of dicts with keys: path, limit, batch_size, optional max_len.
Also applies [calibration] overrides from the TOML to args if present.
"""
if args.calib_config:
with open(args.calib_config, "rb") as f:
toml_cfg = tomllib.load(f)
datasets = toml_cfg.get("dataset", [])
if not datasets:
parser.error(f"No [[dataset]] entries in {args.calib_config}")
batch_tokens = args.batch_tokens
for i, ds in enumerate(datasets):
if "path" not in ds:
parser.error(f"dataset[{i}] missing 'path' in {args.calib_config}")
if not os.path.isabs(ds["path"]):
ds["path"] = os.path.join(args.data_dir, ds["path"])
if "batch_size" not in ds:
if "max_len" in ds:
ds["batch_size"] = max(1, batch_tokens // ds["max_len"])
else:
ds["batch_size"] = args.batch_size
# [calibration] section overrides CLI defaults.
calib_sec = toml_cfg.get("calibration", {})
if "method" in calib_sec:
args.calib_method = calib_sec["method"]
if "quantiles" in calib_sec:
args.quantiles = calib_sec["quantiles"]
return datasets
if not args.calib_jsonl:
parser.error("Provide either --calib-config or --calib-jsonl")
return [{
"path": args.calib_jsonl,
"limit": args.calib_limit,
"batch_size": args.batch_size,
"max_len": args.max_len,
}]
calib_datasets = load_calib_datasets(args)
print(f"\nCalibration plan: {len(calib_datasets)} dataset(s)")
for i, ds in enumerate(calib_datasets):
lim = ds.get('limit', 'all')
max_len = ds.get("max_len")
max_len_str = max_len if max_len is not None else "dynamic"
print(f" [{i+1}] {ds['path']} (limit={lim}, batch={ds['batch_size']}, maxlen={max_len_str})")
# ---------------------------------------------------------------------------
# Load model config and model.
# ---------------------------------------------------------------------------
cfg = load_config(args.model)
MODEL_ID = args.model_id or cfg.model_id
TRUST_REMOTE = cfg.trust_remote_code
PROCESSOR_TRUST_REMOTE = cfg.get_processor_trust_remote_code()
use_streaming = args.streaming if args.streaming is not None else cfg.streaming
ATTN_IMPLEMENTATION = None
if args.attn_implementation != "auto":
ATTN_IMPLEMENTATION = args.attn_implementation
elif args.model == "minimax_m3":
ATTN_IMPLEMENTATION = "sdpa"
if ATTN_IMPLEMENTATION == "minimax_m3_flex":
from models.minimax_m3_flex import register_minimax_m3_flex_attention
register_minimax_m3_flex_attention()
if ATTN_IMPLEMENTATION is not None:
print(f"Using attention implementation: {ATTN_IMPLEMENTATION}")
cfg.register_moe()
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=TRUST_REMOTE)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
has_mm = any(ds.get("multimodal", False) for ds in calib_datasets)
processor = None
if has_mm:
from transformers import AutoProcessor
from PIL import Image
try:
processor = AutoProcessor.from_pretrained(
MODEL_ID,
trust_remote_code=PROCESSOR_TRUST_REMOTE,
)
print(f"Loaded multimodal processor for {MODEL_ID}")
except OSError:
if args.model != "mimo_v25":
raise
print(f"Deferring MiMo processor construction until model config is loaded for {MODEL_ID}")
def _parse_gib(s):
s = s.strip()
for suffix in ("GiB", "gib"):
if s.endswith(suffix):
return float(s[: -len(suffix)])
for suffix in ("MiB", "mib"):
if s.endswith(suffix):
return float(s[: -len(suffix)]) / 1024
for suffix in ("GB", "gb"):
if s.endswith(suffix):
return float(s[: -len(suffix)]) * 1000**3 / 1024**3
for suffix in ("MB", "mb"):
if s.endswith(suffix):
return float(s[: -len(suffix)]) * 1000**2 / 1024**3
return float(s)
def _max_memory_map(per_gpu: str | None, cpu: str | None) -> dict | None:
max_memory = {}
if per_gpu is not None:
for idx in range(torch.cuda.device_count()):
max_memory[idx] = per_gpu
if cpu is not None:
max_memory["cpu"] = cpu
return max_memory or None
def _default_per_gpu_memory(headroom_gib: float | None) -> str | None:
if args.model != "minimax_m3":
return None
if headroom_gib is None:
headroom_gib = 7.0
if not torch.cuda.is_available() or headroom_gib <= 0:
return None
total_gib = torch.cuda.get_device_properties(0).total_memory / 1024**3
usable_gib = max(1, int(total_gib - headroom_gib))
return f"{usable_gib}GiB"
def _patch_accelerate_host_staged_device_moves():
try:
import accelerate.hooks as accelerate_hooks
from accelerate.utils import operations as accelerate_ops
except ImportError:
return
if getattr(accelerate_hooks, "_quantize_host_staged_send", False):
return
original_send_to_device = accelerate_ops.send_to_device
def host_staged_send_to_device(value, device, non_blocking=False, skip_keys=None):
target = torch.device(device) if not isinstance(device, torch.device) else device
if torch.is_tensor(value):
if value.device.type == "cuda" and target.type == "cuda" and value.device != target:
return value.to("cpu").to(target)
return original_send_to_device(value, device, non_blocking=non_blocking, skip_keys=skip_keys)
if isinstance(value, tuple):
return tuple(
host_staged_send_to_device(item, device, non_blocking=non_blocking, skip_keys=skip_keys)
for item in value
)
if isinstance(value, list):
return [
host_staged_send_to_device(item, device, non_blocking=non_blocking, skip_keys=skip_keys)
for item in value
]
if isinstance(value, Mapping):
if isinstance(skip_keys, str):
skip = {skip_keys}
else:
skip = set(skip_keys or [])
return type(value)(
{
key: item
if key in skip
else host_staged_send_to_device(item, device, non_blocking=non_blocking, skip_keys=skip_keys)
for key, item in value.items()
}
)
return value
accelerate_ops.send_to_device = host_staged_send_to_device
accelerate_hooks.send_to_device = host_staged_send_to_device
accelerate_hooks._quantize_host_staged_send = True
model_cls = cfg.get_model_cls()
if use_streaming:
from streaming_loader import StreamingModelLoader
print(f"Loading model from {MODEL_ID} with streaming loader...")
streaming_cuda_staging_device = args.cuda_staging_device
streaming_host_stage_moves = args.host_staged_device_moves == "yes" or (
args.host_staged_device_moves == "auto"
and torch.cuda.device_count() > 9
and streaming_cuda_staging_device is None
)
if streaming_cuda_staging_device is not None:
if args.cuda_staging_source_device:
print(
"Using CUDA-staged streaming layer moves "
f"{args.cuda_staging_source_device} -> {streaming_cuda_staging_device} -> cuda:0"
)
else:
print(f"Using CUDA-staged streaming layer moves through {streaming_cuda_staging_device}")
if streaming_host_stage_moves:
print("Using host-staged streaming layer moves")
loader = StreamingModelLoader(
model_id=MODEL_ID,
dtype=torch.bfloat16,
trust_remote_code=TRUST_REMOTE,
gpu_capacity_gib=(
_parse_gib(args.streaming_gpu_capacity)
if args.streaming_gpu_capacity is not None else None
),
gpu0_storage_capacity_gib=_parse_gib(args.streaming_gpu0_storage_capacity),
cpu_capacity_gib=_parse_gib(args.cpu_capacity),
host_staged_device_moves=streaming_host_stage_moves,
attention_implementation=ATTN_IMPLEMENTATION,
cuda_staging_device=streaming_cuda_staging_device,
cuda_staging_source_device=args.cuda_staging_source_device,
cuda_staging_reserve_gib=_parse_gib(args.cuda_staging_reserve),
)
model = loader.load_model(model_cls=model_cls)
else:
from transformers import AutoModelForCausalLM
print(f"Loading model from {MODEL_ID} onto GPUs...")
loader = None
cls = model_cls or AutoModelForCausalLM
device_map = args.device_map or ("sequential" if args.model == "minimax_m3" else "auto")
model_kwargs = {
"torch_dtype": torch.bfloat16,
"trust_remote_code": TRUST_REMOTE,
"device_map": device_map,
}
if ATTN_IMPLEMENTATION is not None:
model_kwargs["attn_implementation"] = ATTN_IMPLEMENTATION
per_gpu_memory = args.max_memory_per_gpu or _default_per_gpu_memory(args.gpu_headroom_gib)
max_memory = _max_memory_map(per_gpu_memory, args.max_memory_cpu)
if max_memory is not None:
model_kwargs["max_memory"] = max_memory
print(f"Using max_memory={max_memory}")
host_stage_moves = args.host_staged_device_moves == "yes" or (
args.host_staged_device_moves == "auto" and args.model == "minimax_m3"
)
if host_stage_moves:
_patch_accelerate_host_staged_device_moves()
print("Using host-staged cross-GPU activation moves")
print(f"Using device_map={device_map}")
model = cls.from_pretrained(
MODEL_ID,
**model_kwargs,
)
if args.model == "minimax_m3" and ATTN_IMPLEMENTATION is not None:
from models.minimax_m3_flex import assert_attention_implementation
assert_attention_implementation(model, ATTN_IMPLEMENTATION)
if has_mm and processor is None and getattr(getattr(model, "config", None), "model_type", None) == "mimo_v2":
processor = build_mimo_processor(tokenizer, model.config)
print(f"Built MiMo multimodal processor for {MODEL_ID}")
if has_mm and processor is not None and getattr(processor, "chat_template", None) is None:
processor.chat_template = tokenizer.chat_template
# Dtype distribution before quantization.
print(f"\n{'='*60}")
print("Data type distribution BEFORE quantization:")
dtype_stats = {}
total_params = 0
for name, param in model.named_parameters():
dtype = str(param.dtype)
if dtype not in dtype_stats:
dtype_stats[dtype] = {"count": 0, "size_bytes": 0}
dtype_stats[dtype]["count"] += 1
dtype_stats[dtype]["size_bytes"] += param.numel() * param.element_size()
total_params += param.numel()
for dtype, stats in sorted(dtype_stats.items()):
print(f" {dtype:<20} {stats['count']:>6} tensors, {stats['size_bytes']/1e9:>8.2f} GB")
print(
f" {'TOTAL':<20} {sum(s['count'] for s in dtype_stats.values()):>6} tensors, "
f"{sum(s['size_bytes'] for s in dtype_stats.values())/1e9:>8.2f} GB"
)
print(f"{'='*60}")
# ---------------------------------------------------------------------------
# Build calibration batches from all datasets.
# ---------------------------------------------------------------------------
text_chat_template_counts = defaultdict(int)
def _apply_text_chat_template(messages):
final_role = messages[-1].get("role") if messages else None
if final_role == "assistant":
text_chat_template_counts["assistant_continuation"] += 1
return tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=False,
continue_final_message=True,
)
text_chat_template_counts["generation_prompt"] += 1
return tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
)
def iter_prompts(path, limit=None):
with open(path) as f:
for i, line in enumerate(f):
if limit is not None and i >= limit:
break
j = json.loads(line)
if "messages" in j:
try:
yield _apply_text_chat_template(j["messages"])
except Exception:
texts = [m["content"] for m in j["messages"] if m.get("role") == "user"]
if texts:
text_chat_template_counts["fallback_user_text"] += 1
yield " ".join(texts)
elif "prompt" in j or "text" in j:
text_chat_template_counts["raw_prompt"] += 1
yield j.get("prompt") or j.get("text")
def _tokenize_batch(texts, max_len):
"""Tokenize a batch of text, using processor if available (VL models)."""
kwargs = {
"text": texts,
"padding": True,
"return_tensors": "pt",
}
if max_len is not None:
kwargs.update({
"truncation": True,
"max_length": max_len,
})
if processor is not None:
batch = processor(**kwargs)
batch = _normalize_processor_batch(batch)
# Text-only batches need explicit position_ids to bypass the VL
# model's compute_3d_position_ids, which fails without image tokens.
if not _has_visual_inputs(batch):
seq_len = batch["input_ids"].shape[1]
batch["position_ids"] = torch.arange(seq_len).unsqueeze(0).expand_as(batch["input_ids"])
return batch
return tokenizer(**kwargs)
def build_batches(prompts, max_len, batch_size):
buf = []
for p in prompts:
buf.append(p)
if len(buf) == batch_size:
yield _tokenize_batch(buf, max_len)
buf = []
if buf:
yield _tokenize_batch(buf, max_len)
def _messages_with_answer_prefix(messages):
messages = copy.deepcopy(messages)
if messages and messages[-1].get("role") == "assistant":
content = messages[-1].get("content")
if not content:
messages[-1]["content"] = "Answer:"
elif isinstance(content, str) and not content.startswith("Answer:"):
messages[-1]["content"] = f"Answer: {content}"
else:
messages.append({"role": "assistant", "content": "Answer:"})
return messages
def _apply_mm_chat_template(messages):
kwargs = {
"tokenize": False,
"add_generation_prompt": False,
"continue_final_message": True,
}
if _is_mimo_v2_model():
kwargs["enable_thinking"] = False
try:
return processor.apply_chat_template(messages, **kwargs)
except TypeError:
kwargs.pop("enable_thinking", None)
return processor.apply_chat_template(messages, **kwargs)
def _has_visual_inputs(batch):
return any(
key in batch
for key in (
"pixel_values",
"image_embeds",
"pixel_values_videos",
"video_pixel_values",
"video_embeds",
)
)
def _is_mimo_v2_model():
return getattr(getattr(model, "config", None), "model_type", None) == "mimo_v2"
def _is_minimax_m3_vl_model():
return getattr(getattr(model, "config", None), "model_type", None) == "minimax_m3_vl"
def _normalize_processor_batch(batch):
if _is_mimo_v2_model():
return normalize_processor_inputs(batch)
return batch
def _video_processor_kwargs():
if _is_mimo_v2_model():
return mimo_video_processor_kwargs()
if _is_minimax_m3_vl_model():
# The published M3 processor merges video kwargs with do_resize=False,
# but its patch packing requires H/W to be aligned to patch*merge.
# It also uses every frame for path inputs unless sampling is enabled,
# which can create huge video prompts that are easy to truncate.
return {
"do_resize": True,
"do_sample_frames": True,
"fps": 0.5,
"max_pixels": 224 * 224,
}
return {}
def _config_value(name, default=None):
value = getattr(model.config, name, None)
if value is not None:
return value
processor_config = getattr(model.config, "processor_config", None) or {}
if isinstance(processor_config, dict):
return processor_config.get(name, default)
return getattr(processor_config, name, default)
def _media_ref(part, media_type):
return part.get(media_type) or part.get("path") or part.get("file") or part.get("url")
def _resolve_media_path(ref, dataset_path):
path = Path(ref).expanduser()
if path.is_absolute():
return str(path)
candidates = [
Path(args.data_dir) / path,
Path(dataset_path).parent / path,
Path.cwd() / path,
]
for candidate in candidates:
if candidate.exists():
return str(candidate)
return str(candidates[0])
def _load_tensor_value(value, dataset_path, tensor_name=None):
if isinstance(value, torch.Tensor):
return value.detach().cpu()
if isinstance(value, list):
return torch.tensor(value)
if not isinstance(value, str):
raise TypeError(f"Unsupported tensor value {type(value).__name__}")
path = Path(_resolve_media_path(value, dataset_path))
suffix = path.suffix.lower()
if suffix in {".pt", ".pth"}:
obj = torch.load(path, map_location="cpu")
if isinstance(obj, torch.Tensor):
return obj
if isinstance(obj, dict):
if tensor_name and tensor_name in obj:
return obj[tensor_name]
tensors = [v for v in obj.values() if isinstance(v, torch.Tensor)]
if tensors:
return tensors[0]
raise ValueError(f"No tensor found in {path}")
if suffix == ".safetensors":
from safetensors.torch import load_file
tensors = load_file(path, device="cpu")
if tensor_name and tensor_name in tensors:
return tensors[tensor_name]
return tensors[sorted(tensors)[0]]
if suffix == ".npy":
import numpy as np
return torch.from_numpy(np.load(path))
if suffix == ".json":
with open(path) as f:
return torch.tensor(json.load(f))
raise ValueError(f"Unsupported tensor file type: {path}")
def _ensure_mimo_audio_tokenizer():
return ensure_mimo_audio_tokenizer(
model=model,
model_id=MODEL_ID,
fallback_model_id=cfg.model_id,
log_file=sys.stdout,
)
def _load_audio_mel(audio_path):
return load_audio_mel(audio_path, _ensure_mimo_audio_tokenizer())
def _audio_codes_from_mels(mels):
_ensure_mimo_audio_tokenizer()
return audio_codes_from_mels(model, mels)
def _prepare_audio_part(part, dataset_path):
if "audio_embeds" in part:
embeds = _load_tensor_value(part["audio_embeds"], dataset_path, tensor_name=part.get("tensor_name"))
if embeds.dim() != 2:
raise ValueError(f"audio_embeds must be 2D [N, H], got {tuple(embeds.shape)}")
return {"embeds": embeds, "placeholder_count": embeds.shape[0]}
if "audio_codes" in part:
codes = _load_tensor_value(part["audio_codes"], dataset_path, tensor_name=part.get("tensor_name")).long()
if codes.dim() == 1:
codes = codes.unsqueeze(-1)
if codes.dim() != 2:
raise ValueError(f"audio_codes must be 2D [T, C], got {tuple(codes.shape)}")
codes = pad_audio_codes_to_group_boundary(codes, model.config)
return {"codes": codes, "placeholder_count": audio_placeholder_count_from_codes(codes, model.config)}
if "audio_mels" in part:
mel = _load_tensor_value(part["audio_mels"], dataset_path, tensor_name=part.get("tensor_name")).float()
codes = pad_audio_codes_to_group_boundary(_audio_codes_from_mels([mel])[0], model.config)
return {"codes": codes, "placeholder_count": audio_placeholder_count_from_codes(codes, model.config)}
audio_ref = _media_ref(part, "audio")
if audio_ref:
mel = _load_audio_mel(_resolve_media_path(audio_ref, dataset_path))
codes = pad_audio_codes_to_group_boundary(_audio_codes_from_mels([mel])[0], model.config)
return {"codes": codes, "placeholder_count": audio_placeholder_count_from_codes(codes, model.config)}
return None
def iter_mm_samples(path, limit=None):
"""Yield multimodal samples from JSONL with image, video, and audio parts."""
with open(path) as f:
for i, line in enumerate(f):
if limit is not None and i >= limit:
break
j = json.loads(line)
messages = j.get("messages", [])
processed_messages = copy.deepcopy(messages)
images = []
videos = []
audios = []
for msg in processed_messages:
content = msg.get("content", [])
if isinstance(content, str):
continue
new_content = []
for part in content:
new_content.append(part)
if part.get("type") == "image":
img_path = _media_ref(part, "image")
if img_path:
images.append(Image.open(_resolve_media_path(img_path, path)).convert("RGB"))
elif part.get("type") == "video":
video_path = _media_ref(part, "video")
if video_path:
resolved_video_path = _resolve_media_path(video_path, path)
videos.append(resolved_video_path)
if _is_mimo_v2_model() and video_uses_audio(part, resolved_video_path):
audio_ref = video_audio_source(part, resolved_video_path)
audio_part = {"type": "audio", "audio": audio_ref}
audio = _prepare_audio_part(audio_part, path)
if audio is not None:
audios.append(audio)
# SGLang turns video-with-audio into both VIDEO and AUDIO
# modalities. Keep the prompt modal tokens aligned with
# that by adding an audio part next to the video part.
new_content.append(audio_part)
elif part.get("type") == "audio":
audio = _prepare_audio_part(part, path)
if audio is not None:
audios.append(audio)
msg["content"] = new_content
if images or videos or audios:
yield {
"messages": processed_messages,
"images": images,
"videos": videos,
"audios": audios,
}
def build_mm_batches(samples, max_len, batch_size):
"""Build multimodal batches using the processor."""
buf_texts = []
buf_images = []
buf_videos = []
buf_audio_codes = []
buf_audio_embeds = []
buf_audio_counts = []
def flush():
if not buf_texts:
return None
if buf_audio_codes and buf_audio_embeds:
raise ValueError("A single multimodal batch cannot mix audio_codes and audio_embeds")
kwargs = {
"text": buf_texts,
"padding": True,
"return_tensors": "pt",
}
if max_len is not None:
kwargs.update({
"truncation": True,
"max_length": max_len,
})
if buf_images:
kwargs["images"] = buf_images
if buf_videos:
kwargs["videos"] = buf_videos
kwargs.update(_video_processor_kwargs())
batch = processor(**kwargs)
batch = _normalize_processor_batch(batch)
if _is_mimo_v2_model() and buf_images:
replace_mimo_image_pixels(batch, buf_images, model.config)
if buf_audio_codes:
batch["audio_codes"] = torch.cat(buf_audio_codes, dim=0).long()
if buf_audio_embeds:
batch["audio_embeds"] = torch.cat(buf_audio_embeds, dim=0)
if buf_audio_counts:
audio_token_id = _config_value("audio_token_id")
if audio_token_id is None:
raise ValueError("Audio calibration data requires model.config.audio_token_id")
expected = sum(buf_audio_counts)
actual = int((batch["input_ids"] == int(audio_token_id)).sum().item())
if actual != expected:
raise ValueError(
"Audio placeholder mismatch: "
f"tokenized prompt has {actual} audio token(s), but audio payload expects {expected}"
)
if not _has_visual_inputs(batch):
seq_len = batch["input_ids"].shape[1]
batch["position_ids"] = torch.arange(seq_len).unsqueeze(0).expand_as(batch["input_ids"])
return batch
for sample in samples:
text = _apply_mm_chat_template(_messages_with_answer_prefix(sample["messages"]))
audio_counts = [audio["placeholder_count"] for audio in sample["audios"]]
if audio_counts:
text = expand_audio_placeholders(text, audio_counts)
buf_texts.append(text)
buf_images.extend(sample["images"])
buf_videos.extend(sample["videos"])
buf_audio_counts.extend(audio_counts)
for audio in sample["audios"]:
if "codes" in audio:
buf_audio_codes.append(audio["codes"])
elif "embeds" in audio:
buf_audio_embeds.append(audio["embeds"])
if len(buf_texts) == batch_size:
yield flush()
buf_texts = []
buf_images = []
buf_videos = []
buf_audio_codes = []
buf_audio_embeds = []
buf_audio_counts = []
if buf_texts:
yield flush()
# Pre-build all batches, tagged with dataset index for logging.
all_batches = []
for ds_idx, ds in enumerate(calib_datasets):
if ds.get("multimodal", False):
ds_batches = list(build_mm_batches(
iter_mm_samples(ds["path"], limit=ds.get("limit")),
max_len=ds.get("max_len"),
batch_size=ds["batch_size"],
))
else:
ds_batches = list(build_batches(
iter_prompts(ds["path"], limit=ds.get("limit")),
max_len=ds.get("max_len"),
batch_size=ds["batch_size"],
))
print(f" Dataset [{ds_idx+1}]: {len(ds_batches)} batches")
all_batches.extend((ds_idx, b) for b in ds_batches)
print(f" Total: {len(all_batches)} batches across {len(calib_datasets)} dataset(s)")
if text_chat_template_counts:
counts = ", ".join(f"{key}={value}" for key, value in sorted(text_chat_template_counts.items()))
print(f" Text prompt formats: {counts}")
if _is_mimo_v2_model():
converted = precompute_mimo_visual_embeds_for_batches(
MODEL_ID,
model.config,
all_batches,
)
if converted:
print(f"Precomputed MiMo visual embeddings for {converted} calibration batch tensor(s)")
if getattr(model, "audio_tokenizer", None) is not None:
# Audio media has already been converted to compact codes in all_batches.
# Keep the tokenizer sidecar out of ModelOpt wrapping and checkpoint export.
model.audio_tokenizer = None
gc.collect()
torch.cuda.empty_cache()
model.eval()
for p in model.parameters():
p.requires_grad_(False)
if hasattr(model, "gradient_checkpointing_disable"):
model.gradient_checkpointing_disable()
gc.collect()
torch.cuda.empty_cache()
torch.cuda.reset_peak_memory_stats()
# ---------------------------------------------------------------------------
# Calibration forward loop.
# ---------------------------------------------------------------------------
amax_ckpt_path = os.path.join(args.export_dir, "amax_checkpoint.safetensors")
quantile_ckpt_path = os.path.join(args.export_dir, "quantile_checkpoint.json")
os.makedirs(args.export_dir, exist_ok=True)
def _save_amax_checkpoint(m, batch_num):
from modelopt.torch.quantization.nn import TensorQuantizer
amaxes = {}
for name, mod in m.named_modules():
if isinstance(mod, TensorQuantizer):
cal = getattr(mod, "_calibrator", None)
if cal is not None and getattr(cal, "_calib_amax", None) is not None:
v = cal._calib_amax.detach().clone().cpu()
amaxes[name] = v.reshape(1) if v.dim() == 0 else v
save_file(amaxes, amax_ckpt_path)
print(f" [checkpoint] {len(amaxes)} amaxes saved after batch {batch_num}")
def _save_quantile_checkpoint(m, batch_num):
from modelopt.torch.quantization.calib.quantile import save_quantile_data
n_saved = save_quantile_data(m, quantile_ckpt_path)
print(f" [checkpoint] {n_saved} quantile estimates saved after batch {batch_num}")
def _restore_amax(m, path):
from safetensors.torch import load_file
from modelopt.torch.quantization.nn import TensorQuantizer
saved = load_file(path)
restored = 0
for name, mod in m.named_modules():
if name in saved and isinstance(mod, TensorQuantizer):
cal = getattr(mod, "_calibrator", None)
if cal is not None:
v = saved[name]
if v.shape == torch.Size([1]):
v = v.squeeze(0)
device = cal._calib_amax.device if cal._calib_amax is not None else "cuda:0"
cal._calib_amax = v.to(device)
restored += 1
print(f"Restored {restored}/{len(saved)} calibrator amaxes from {path}")
_MIMO_ALLOWED_QUANTIZER_RE = re.compile(
r"^model\.layers\.\d+\.mlp\.experts\.\d+\."
r"(?:gate_proj|up_proj|down_proj)\.(?:weight_quantizer|input_quantizer)$"
)
_MINIMAX_M3_ALLOWED_QUANTIZER_RE = re.compile(
r"^model\.language_model\.layers\.\d+\.mlp\.experts\."
r"(?:gate_proj|up_proj|down_proj)\.\d+\."
r"(?:weight_quantizer|input_quantizer)$"
)
def _validate_enabled_quantizers(m):
allowed_re = None
label = None
if args.model == "mimo_v25":
allowed_re = _MIMO_ALLOWED_QUANTIZER_RE
label = "MiMo"
elif args.model == "minimax_m3":
allowed_re = _MINIMAX_M3_ALLOWED_QUANTIZER_RE
label = "MiniMax M3"
if allowed_re is None:
return
enabled = []
bad = []
for name, mod in m.named_modules():
is_enabled = getattr(mod, "is_enabled", None)
if is_enabled is None:
continue
if bool(is_enabled):
enabled.append(name)
if not allowed_re.match(name):
bad.append(name)
if bad:
sample = "\n ".join(bad[:32])
raise RuntimeError(
f"{label} quantization is configured for routed expert MLPs only, "
f"but found {len(bad)} enabled non-expert quantizer(s):\n {sample}"
)
if not enabled:
raise RuntimeError(f"{label} quantization has no enabled routed expert MLP quantizers")
print(f" {label} enabled quantizers: {len(enabled)} routed expert MLP quantizer(s)")
def _override_quantile_levels(m):
"""Override quantile levels on all QuantileCalibrators if specified in config."""
if not hasattr(args, "quantiles"):
return
from modelopt.torch.quantization.calib.quantile import QuantileCalibrator, P2QuantileEstimator
count = 0
for name, mod in m.named_modules():
cal = getattr(mod, "_calibrator", None)
if isinstance(cal, QuantileCalibrator):
cal._quantile_probs = list(args.quantiles)
cal._estimators = {p: P2QuantileEstimator(p) for p in cal._quantile_probs}
count += 1
if count:
print(f" Overrode quantile levels to {args.quantiles} on {count} calibrators")
def forward_loop(m):
input_device = next(m.parameters()).device
resume = args.resume_batch
_validate_enabled_quantizers(m)
_override_quantile_levels(m)
if args.resume_amax:
_restore_amax(m, args.resume_amax)
print(f"Resuming from batch {resume + 1}")
print(f"\nCalibration: {len(all_batches)} batches across {len(calib_datasets)} dataset(s)...")
cur_ds = -1
for i, (ds_idx, batch) in enumerate(all_batches, 1):
if i <= resume:
continue
if ds_idx != cur_ds:
cur_ds = ds_idx
ds = calib_datasets[ds_idx]
print(f"\n --- Dataset [{ds_idx+1}]: {os.path.basename(ds['path'])} "
f"(batch={ds['batch_size']}, maxlen={ds.get('max_len', 'dynamic')}) ---")
print(f" Batch {i}/{len(all_batches)}...")
kwargs = {
k: v.to(input_device, non_blocking=True)
for k, v in batch.items() if isinstance(v, torch.Tensor)
}
with torch.no_grad():
outputs = m(**kwargs, use_cache=False)
del outputs, kwargs
gc.collect()
torch.cuda.empty_cache()
_save_amax_checkpoint(m, i)
if args.calib_method == "quantile":
_save_quantile_checkpoint(m, i)
print("Calibration complete.")
# ---------------------------------------------------------------------------
# Quantize.
# ---------------------------------------------------------------------------
base_qcfg = copy.deepcopy(mtq.NVFP4_DEFAULT_CFG)
qcfg = copy.deepcopy(base_qcfg)
for pattern, override in cfg.get_all_quant_overrides().items():
if override == {"enable": True}:
if pattern.endswith("weight_quantizer"):
override = copy.deepcopy(base_qcfg["quant_cfg"]["*weight_quantizer"])
elif pattern.endswith("input_quantizer"):
override = copy.deepcopy(base_qcfg["quant_cfg"]["*input_quantizer"])