-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstreaming_loader.py
More file actions
1360 lines (1187 loc) · 57.4 KB
/
Copy pathstreaming_loader.py
File metadata and controls
1360 lines (1187 loc) · 57.4 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
"""Streaming model loader for large MoE models.
Replaces accelerate's device_map="auto" with a simple, predictable system:
- GPU 0 is the sole execution device; it may optionally store overflow layers
within an explicit capacity budget.
- GPUs 1-N and CPU are dumb weight storage
- Disk-resident layers load directly from safetensors on demand (no re-serialization)
Every decoder layer runs on GPU 0 via forward hooks that copy weights in,
execute, then free. Activations stay on GPU 0 throughout.
"""
import gc
import json
import os
import re
from collections import defaultdict
import torch
import torch.nn as nn
from accelerate import init_empty_weights
from accelerate.utils import set_module_tensor_to_device
from safetensors import safe_open
from transformers import AutoConfig, AutoModelForCausalLM
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 _extract_layer_idx(key):
"""Extract layer index from a parameter key containing 'layers.N.'."""
m = re.search(r"\.layers\.(\d+)\.", key)
return int(m.group(1)) if m else None
def _detect_layer_prefix(weight_map):
"""Detect the key prefix before 'layers.N.' from checkpoint keys.
Returns e.g. 'model.layers.' or 'model.language_model.layers.' depending
on whether the checkpoint is a text-only or VL model.
"""
candidates = defaultdict(set)
for key in weight_map:
m = re.match(r"^(.*?layers\.)\d+\.", key)
if m:
candidates[m.group(1)].add(int(key[m.end(1):].split(".", 1)[0]))
if candidates:
# Some multimodal side towers also contain modules named layers.N
# (MiMo audio does this). The main decoder has by far the largest
# layer-index set, so use that as the streaming layer prefix.
return max(candidates.items(), key=lambda item: len(item[1]))[0]
return "model.layers."
def _checkpoint_key_to_model_key(key: str) -> str:
"""Apply the checkpoint-to-model key renames needed by streaming loads.
Transformers handles these through conversion_mapping.py during normal
from_pretrained loads. The streaming loader reads safetensors directly, so
it needs the relevant MiniMax M3-VL conversion rules locally.
"""
if key.startswith("language_model.lm_head"):
key = "lm_head" + key[len("language_model.lm_head"):]
elif key.startswith("language_model.model."):
key = "model.language_model." + key[len("language_model.model."):]
elif key.startswith("vision_tower.vision_model.embeddings.patch_embedding."):
key = "model.vision_tower.embeddings.proj." + key[
len("vision_tower.vision_model.embeddings.patch_embedding.") :
]
elif key.startswith("vision_tower.vision_model.encoder.layers."):
key = "model.vision_tower.layers." + key[len("vision_tower.vision_model.encoder.layers.") :]
elif key.startswith("vision_tower.vision_model.pre_layrnorm."):
key = "model.vision_tower.pre_layrnorm." + key[
len("vision_tower.vision_model.pre_layrnorm.") :
]
elif key.startswith("multi_modal_projector.linear_1."):
key = "model.multi_modal_projector.linear_1." + key[len("multi_modal_projector.linear_1.") :]
elif key.startswith("multi_modal_projector.linear_2."):
key = "model.multi_modal_projector.linear_2." + key[len("multi_modal_projector.linear_2.") :]
elif key.startswith("patch_merge_mlp.linear_1."):
key = "model.multi_modal_projector.merge_linear_1." + key[len("patch_merge_mlp.linear_1.") :]
elif key.startswith("patch_merge_mlp.linear_2."):
key = "model.multi_modal_projector.merge_linear_2." + key[len("patch_merge_mlp.linear_2.") :]
key = re.sub(
r"\.language_model\.layers\.(\d+)\.block_sparse_moe\.experts\.",
r".language_model.layers.\1.mlp.experts.",
key,
)
key = re.sub(
r"\.language_model\.layers\.(\d+)\.block_sparse_moe\.shared_experts\.",
r".language_model.layers.\1.mlp.shared_experts.",
key,
)
key = re.sub(
r"\.language_model\.layers\.(\d+)\.block_sparse_moe\.gate\.weight$",
r".language_model.layers.\1.mlp.gate.weight",
key,
)
key = re.sub(
r"\.language_model\.layers\.(\d+)\.block_sparse_moe\.e_score_correction_bias$",
r".language_model.layers.\1.mlp.gate.e_score_correction_bias",
key,
)
key = key.replace(".self_attn.index_q_proj.", ".self_attn.indexer.q_proj.")
key = key.replace(".self_attn.index_k_proj.", ".self_attn.indexer.k_proj.")
key = key.replace(".self_attn.index_q_norm.", ".self_attn.indexer.q_norm.")
key = key.replace(".self_attn.index_k_norm.", ".self_attn.indexer.k_norm.")
key = re.sub(r"\.mlp\.experts\.(\d+)\.w1\.weight$", r".mlp.experts.\1.gate_proj.weight", key)
key = re.sub(r"\.mlp\.experts\.(\d+)\.w3\.weight$", r".mlp.experts.\1.up_proj.weight", key)
key = re.sub(r"\.mlp\.experts\.(\d+)\.w2\.weight$", r".mlp.experts.\1.down_proj.weight", key)
return key
def _dense_gate_up_target_key(key: str) -> tuple[str, str] | None:
"""Return (target_key, part) for dense gate/up weights that need concat."""
m = re.match(r"^(.*\.mlp(?:\.shared_experts)?)\.(gate_proj|up_proj)\.weight$", key)
if not m:
return None
part = "gate" if m.group(2) == "gate_proj" else "up"
return f"{m.group(1)}.gate_up_proj.weight", part
def _expert_first_to_projection_first_key(key: str) -> str | None:
m = re.match(
r"^(.*\.mlp\.experts)\.(\d+)\.(gate_proj|up_proj|down_proj)\.weight$",
key,
)
if not m:
return None
prefix, idx, proj = m.group(1), m.group(2), m.group(3)
return f"{prefix}.{proj}.{idx}.weight"
def _load_checkpoint_tensors(snapshot_dir, weight_map, raw_keys, device):
tensors = {}
by_shard = defaultdict(list)
for raw_key in raw_keys:
by_shard[weight_map[raw_key]].append(raw_key)
for shard_file, keys in by_shard.items():
shard_path = os.path.join(snapshot_dir, shard_file)
with safe_open(shard_path, framework="pt", device=str(device)) as f:
available = set(f.keys())
for raw_key in keys:
if raw_key in available:
tensors[raw_key] = f.get_tensor(raw_key)
return tensors
def _resolve_snapshot_dir(model_id):
"""Resolve a HF model ID to the local snapshot directory."""
if os.path.isdir(model_id):
return model_id
# Standard HF cache layout
cache_dir = os.path.expanduser("~/.cache/huggingface/hub")
repo_dir = os.path.join(cache_dir, f"models--{model_id.replace('/', '--')}")
snapshots = os.path.join(repo_dir, "snapshots")
if os.path.isdir(snapshots):
# Use the most recent snapshot
entries = sorted(os.listdir(snapshots))
if entries:
return os.path.join(snapshots, entries[-1])
raise FileNotFoundError(
f"Cannot resolve snapshot dir for {model_id}. "
f"Pass a local path or ensure the model is cached."
)
def _iter_safetensors_files(snapshot_dir):
for name in sorted(os.listdir(snapshot_dir)):
if name.endswith(".safetensors") and name != "model_mtp.safetensors":
yield name
def _build_weight_map_from_shards(snapshot_dir):
"""Build a weight map from safetensors metadata.
Some repos publish an index whose shard names differ from the actual files
in cache. Scanning safetensors keys is metadata-only and avoids depending
on those stale shard filenames.
"""
weight_map = {}
for shard_file in _iter_safetensors_files(snapshot_dir):
shard_path = os.path.join(snapshot_dir, shard_file)
with safe_open(shard_path, framework="pt", device="cpu") as f:
for key in f.keys():
weight_map[key] = shard_file
return weight_map
def _device_sort_key(device):
device = str(device)
if device.startswith("cuda:"):
return (0, int(device.split(":", 1)[1]))
if device == "cpu":
return (1, 0)
if device == "meta":
return (2, 0)
return (3, device)
def _format_indices(indices):
indices = sorted(indices)
if not indices:
return ""
ranges = []
start = prev = indices[0]
for idx in indices[1:]:
if idx == prev + 1:
prev = idx
continue
ranges.append(f"{start}-{prev}" if start != prev else str(start))
start = prev = idx
ranges.append(f"{start}-{prev}" if start != prev else str(start))
return ", ".join(ranges)
class StreamingModelLoader:
"""Load and manage a large model with streaming execution on GPU 0."""
def __init__(
self,
model_id,
dtype=torch.bfloat16,
trust_remote_code=True,
gpu_capacity_gib=None,
gpu0_storage_capacity_gib=0,
cpu_capacity_gib=200,
host_staged_device_moves=False,
attention_implementation=None,
cuda_staging_device=None,
cuda_staging_source_device=None,
cuda_staging_reserve_gib=0,
):
self.model_id = model_id
self.dtype = dtype
self.trust_remote_code = trust_remote_code
self.gpu0_storage_capacity_gib = gpu0_storage_capacity_gib
self.cpu_capacity_gib = cpu_capacity_gib
self.host_staged_device_moves = host_staged_device_moves
self.attention_implementation = attention_implementation
self.num_gpus = torch.cuda.device_count()
self.cuda_staging_device = self._normalize_cuda_staging_device(cuda_staging_device)
self.cuda_staging_source_device = self._normalize_cuda_staging_source_device(cuda_staging_source_device)
self.cuda_staging_reserve_gib = cuda_staging_reserve_gib
if gpu_capacity_gib is None:
# Auto-detect from first GPU
self.gpu_capacity_gib = (
torch.cuda.get_device_properties(0).total_memory / 1024**3
)
else:
self.gpu_capacity_gib = gpu_capacity_gib
self.snapshot_dir = _resolve_snapshot_dir(model_id)
index_path = os.path.join(self.snapshot_dir, "model.safetensors.index.json")
with open(index_path) as f:
index = json.load(f)
self.weight_map = index["weight_map"]
missing_shards = {
shard
for shard in set(self.weight_map.values())
if not os.path.exists(os.path.join(self.snapshot_dir, shard))
}
if missing_shards:
print(
" Rebuilding safetensors weight map from local shards "
f"({len(missing_shards)} indexed shard names missing)"
)
self.weight_map = _build_weight_map_from_shards(self.snapshot_dir)
self._layer_prefix = _detect_layer_prefix(self.weight_map)
# Will be populated during load_model
self.storage_map = {} # layer_idx -> device string
self.layer_tensor_sizes = {} # layer_idx -> {relative tensor name -> bytes}
self.layer_tensor_storage = {} # layer_idx -> {relative tensor name -> device string}
self.layer_split_primary_device = {} # layer_idx -> fallback storage device
self._hook_handles = []
self._model_state_keys = None
def _normalize_cuda_staging_device(self, device):
if device is None:
return None
staging = torch.device(device)
if staging.type != "cuda" or staging.index is None:
raise ValueError(f"cuda_staging_device must be explicit CUDA device like 'cuda:1', got {device!r}")
if staging.index == 0:
raise ValueError("cuda_staging_device must not be cuda:0; GPU 0 is the streaming execution device")
if staging.index >= self.num_gpus:
raise ValueError(
f"cuda_staging_device {staging} is outside the visible CUDA device range "
f"0-{self.num_gpus - 1}"
)
return staging
def _normalize_cuda_staging_source_device(self, device):
if device is None:
return None
source = torch.device(device)
if source.type != "cuda" or source.index is None:
raise ValueError(f"cuda_staging_source_device must be explicit CUDA device like 'cuda:9', got {device!r}")
if source.index == 0:
raise ValueError("cuda_staging_source_device must not be cuda:0; GPU 0 is the streaming execution device")
if source.index >= self.num_gpus:
raise ValueError(
f"cuda_staging_source_device {source} is outside the visible CUDA device range "
f"0-{self.num_gpus - 1}"
)
if self.cuda_staging_device is not None and source == self.cuda_staging_device:
raise ValueError("cuda_staging_source_device must differ from cuda_staging_device")
return source
def load_model(self, register_moe_fn=None, model_cls=None):
"""Load the model with streaming hooks. Returns the model.
Args:
register_moe_fn: Optional callable to register MoE quantization
patches before model creation (e.g. register_glm5_moe_for_quantization).
model_cls: Optional explicit model class (e.g. Qwen3_5MoeForCausalLM).
When provided and the config is a VL composite with text_config,
the text_config is extracted automatically.
"""
if register_moe_fn is not None:
register_moe_fn()
cls = model_cls or AutoModelForCausalLM
print(f"Loading config from {self.model_id}...")
if cls is AutoModelForCausalLM:
config = AutoConfig.from_pretrained(
self.model_id, trust_remote_code=self.trust_remote_code
)
else:
config = cls.config_class.from_pretrained(
self.model_id, trust_remote_code=self.trust_remote_code
)
if self.attention_implementation is not None:
if self.attention_implementation == "minimax_m3_flex":
from models.minimax_m3_flex import register_minimax_m3_flex_attention
register_minimax_m3_flex_attention()
config._attn_implementation = self.attention_implementation
print(f"Creating model on meta device via {cls.__name__}...")
config_kwargs = {}
if self.attention_implementation is not None:
config_kwargs["attn_implementation"] = self.attention_implementation
if cls is AutoModelForCausalLM:
with init_empty_weights():
model = cls.from_config(
config,
torch_dtype=self.dtype,
trust_remote_code=self.trust_remote_code,
**config_kwargs,
)
else:
with init_empty_weights():
model = cls._from_config(config, dtype=self.dtype, **config_kwargs)
# Compute per-layer sizes from meta model
self.layer_tensor_sizes = self._compute_layer_tensor_sizes(model)
layer_sizes = {
layer_idx: sum(tensor_sizes.values())
for layer_idx, tensor_sizes in self.layer_tensor_sizes.items()
}
self.storage_map = self._compute_storage_map(layer_sizes)
self._print_storage_summary(layer_sizes)
# Materialize permanent modules on GPU 0
print("\nLoading permanent modules to GPU 0 (embed, norm, lm_head)...")
self._materialize_permanent_modules(model)
# Materialize GPU/CPU-assigned layers
print("Loading layers to storage devices...")
for layer_idx, device in sorted(self.storage_map.items()):
if device == "meta":
continue
print(f" Layer {layer_idx} -> {device}")
if device == "split":
self._materialize_layer_split(model, layer_idx)
else:
self._materialize_layer(model, layer_idx, device)
meta_count = sum(1 for d in self.storage_map.values() if d == "meta")
if meta_count:
print(f" {meta_count} layers remain on meta (will load from safetensors on demand)")
# Monkey-patch _QuantFusedExperts._setup for lazy meta handling
self._patch_quant_fused_experts_setup()
# Install streaming hooks
self._install_hooks(model)
# Report VRAM usage
print(f"\nVRAM after loading:")
for i in range(self.num_gpus):
alloc = torch.cuda.memory_allocated(i) / 1024**3
total = torch.cuda.get_device_properties(i).total_memory / 1024**3
print(f" GPU {i}: {alloc:.1f} / {total:.1f} GiB ({total - alloc:.1f} GiB free)")
return model
@staticmethod
def _get_layers(model):
"""Find the decoder layer list, handling VL model nesting."""
m = model.model
if hasattr(m, "layers"):
return m.layers
if hasattr(m, "language_model") and hasattr(m.language_model, "layers"):
return m.language_model.layers
raise AttributeError(
f"Cannot find decoder layers on {type(m).__name__}. "
f"Expected .layers or .language_model.layers"
)
def _compute_layer_tensor_sizes(self, model):
"""Compute tensor storage sizes in bytes for each decoder layer."""
layer_tensor_sizes = {}
for i, layer in enumerate(self._get_layers(model)):
tensor_sizes = {}
for name, p in layer.named_parameters():
tensor_sizes[name] = p.numel() * p.element_size()
for name, b in layer.named_buffers():
tensor_sizes[name] = b.numel() * b.element_size()
layer_tensor_sizes[i] = tensor_sizes
return layer_tensor_sizes
def _compute_storage_map(self, layer_sizes):
"""Assign layers to storage devices, splitting a layer when useful.
Whole-layer placement is preferred because it is simpler and keeps most
transfers contiguous. If a whole layer cannot fit on any single storage
GPU, try placing its individual tensors into the aggregate slack across
GPUs 1-N before spilling to GPU 0, CPU, or disk.
"""
storage_map = {}
self.layer_tensor_storage = {}
self.layer_split_primary_device = {}
# Reserve headroom per storage GPU for CUDA context, driver memory,
# and PyTorch allocator fragmentation from loading many small tensors.
gpu_headroom = 0
default_gpu_capacity = self.gpu_capacity_gib * 1024**3 - gpu_headroom
gpu0_capacity = self.gpu0_storage_capacity_gib * 1024**3
cpu_capacity = self.cpu_capacity_gib * 1024**3
gpu_capacities = {
gpu_idx: self._storage_gpu_capacity_bytes(gpu_idx, default_gpu_capacity)
for gpu_idx in range(1, self.num_gpus)
}
# GPU 0 is reserved for execution. Pack GPUs 1-N first, then use the
# explicit GPU-0 overflow budget if one was provided.
current_gpu = 1
gpu_used = {gpu_idx: 0 for gpu_idx in range(1, self.num_gpus)}
gpu0_used = 0
cpu_used = 0
for i in sorted(layer_sizes.keys()):
size = layer_sizes[i]
placed = False
while current_gpu < self.num_gpus:
gpu_capacity = gpu_capacities[current_gpu]
if gpu_used[current_gpu] + size <= gpu_capacity:
storage_map[i] = f"cuda:{current_gpu}"
gpu_used[current_gpu] += size
placed = True
break
else:
current_gpu += 1
if not placed:
split_storage = self._try_split_layer_across_storage_gpus(
i, gpu_used, gpu_capacities
)
if split_storage is not None:
storage_map[i] = "split"
self.layer_tensor_storage[i] = split_storage
self.layer_split_primary_device[i] = self._primary_split_device(i)
placed = True
if not placed:
if gpu0_used + size <= gpu0_capacity:
storage_map[i] = "cuda:0"
gpu0_used += size
placed = True
if not placed:
if cpu_used + size <= cpu_capacity:
storage_map[i] = "cpu"
cpu_used += size
else:
storage_map[i] = "meta"
return storage_map
def _storage_gpu_capacity_bytes(self, gpu_idx, default_capacity):
if (
self.cuda_staging_device is not None
and gpu_idx == self.cuda_staging_device.index
):
return max(
0,
default_capacity - self.cuda_staging_reserve_gib * 1024**3,
)
return default_capacity
def _try_split_layer_across_storage_gpus(self, layer_idx, gpu_used, gpu_capacities):
"""Balanced tensor placement into existing storage GPU slack.
Large tensors start on the least-used viable GPUs to avoid creating a
near-OOM storage device. Small tensors then prefer devices already used
by this layer, keeping the runtime copy fan-in low when possible.
"""
tensor_sizes = self.layer_tensor_sizes.get(layer_idx, {})
if not tensor_sizes or self.num_gpus <= 1:
return None
trial_used = dict(gpu_used)
placement = {}
layer_gpus = []
for name, size in sorted(tensor_sizes.items(), key=lambda item: item[1], reverse=True):
candidates = []
if layer_gpus:
candidates = [
gpu_idx
for gpu_idx in layer_gpus
if gpu_capacities[gpu_idx] - trial_used[gpu_idx] >= size
]
if not candidates:
candidates = [
gpu_idx
for gpu_idx in range(1, self.num_gpus)
if gpu_capacities[gpu_idx] - trial_used[gpu_idx] >= size
]
if not candidates:
return None
if any(gpu_idx in layer_gpus for gpu_idx in candidates):
# Pack later/smaller tensors into this layer's existing shards.
best_gpu = min(
candidates,
key=lambda gpu_idx: gpu_capacities[gpu_idx] - trial_used[gpu_idx] - size,
)
else:
# Balance new large shards across the storage GPUs.
best_gpu = max(
candidates,
key=lambda gpu_idx: gpu_capacities[gpu_idx] - trial_used[gpu_idx],
)
if best_gpu is None:
return None
if best_gpu not in layer_gpus:
layer_gpus.append(best_gpu)
placement[name] = f"cuda:{best_gpu}"
trial_used[best_gpu] += size
gpu_used.update(trial_used)
return placement
def _primary_split_device(self, layer_idx):
by_device = defaultdict(int)
for name, device in self.layer_tensor_storage.get(layer_idx, {}).items():
by_device[device] += self.layer_tensor_sizes.get(layer_idx, {}).get(name, 0)
if not by_device:
return "cpu"
return max(by_device.items(), key=lambda item: item[1])[0]
def _print_storage_summary(self, layer_sizes):
"""Print a summary of layer placement."""
by_device_layers = defaultdict(list)
by_device_split_layers = defaultdict(set)
by_device_bytes = defaultdict(int)
split_layers = []
for idx, device in sorted(self.storage_map.items()):
if device == "split":
split_layers.append(idx)
for name, tensor_device in self.layer_tensor_storage.get(idx, {}).items():
by_device_split_layers[tensor_device].add(idx)
by_device_bytes[tensor_device] += self.layer_tensor_sizes[idx].get(name, 0)
continue
by_device_layers[device].append(idx)
by_device_bytes[device] += layer_sizes[idx]
print(f"\nStorage map ({len(self.storage_map)} layers):")
all_devices = sorted(by_device_bytes.keys(), key=_device_sort_key)
for device in all_devices:
parts = []
whole = by_device_layers.get(device, [])
split = sorted(by_device_split_layers.get(device, []))
if whole:
parts.append(f"layers [{_format_indices(whole)}]")
if split:
parts.append(f"split [{_format_indices(split)}]")
total_gib = by_device_bytes[device] / 1024**3
print(f" {device}: {', '.join(parts)} ({total_gib:.1f} GiB)")
if split_layers:
print(" split layer details:")
for idx in split_layers:
by_device = defaultdict(int)
for name, device in self.layer_tensor_storage.get(idx, {}).items():
by_device[device] += self.layer_tensor_sizes[idx].get(name, 0)
parts = [
f"{device} {size / 1024**3:.1f} GiB"
for device, size in sorted(by_device.items(), key=lambda item: _device_sort_key(item[0]))
]
print(f" layer {idx}: {', '.join(parts)}")
def _get_layer_param_keys(self, layer_idx):
"""Get all checkpoint keys belonging to a specific layer."""
prefix = f"{self._layer_prefix}{layer_idx}."
return [k for k in self.weight_map if k.startswith(prefix)]
def _get_permanent_param_keys(self):
"""Get checkpoint keys for non-layer modules (embed, norm, lm_head)."""
decoder_layer_re = re.compile(rf"^{re.escape(self._layer_prefix)}\d+\.")
return [k for k in self.weight_map if not decoder_layer_re.match(k)]
def _materialize_permanent_modules(self, model):
"""Load embed_tokens, norm, rotary_emb, lm_head to GPU 0.
Checkpoint keys that don't exist on the model (e.g. MTP weights in
VL checkpoints) are silently skipped.
"""
model_keys = set(dict(model.named_parameters()).keys()) | set(dict(model.named_buffers()).keys())
all_keys = self._get_permanent_param_keys()
keys = all_keys
direct_or_mapped = sum(
1
for k in all_keys
if k in model_keys or _checkpoint_key_to_model_key(k) in model_keys
)
skipped = len(all_keys) - direct_or_mapped
if skipped:
print(f" Skipping {skipped} checkpoint keys not in model (e.g. MTP)")
self._materialize_params(model, keys, "cuda:0")
# .to(cuda:0) on all non-layer submodules to catch registered buffers
# created during __init__ (rotary inv_freq, vision pos embeds, etc.)
# that aren't in the checkpoint and are still on meta/cpu.
m = model.model if hasattr(model, "model") else model
for name, child in m.named_children():
if name == "layers":
continue
if hasattr(m, "language_model") and name == "language_model":
for lm_name, lm_child in child.named_children():
if lm_name != "layers":
_zero_materialize_meta_tensors(
lm_child, "cuda:0", f"{name}.{lm_name}"
)
lm_child.to("cuda:0")
continue
_zero_materialize_meta_tensors(child, "cuda:0", name)
child.to("cuda:0")
for name, child in model.named_children():
if name == "model":
continue
_zero_materialize_meta_tensors(child, "cuda:0", name)
child.to("cuda:0")
def _materialize_layer(self, model, layer_idx, device):
"""Load all parameters for a layer from safetensors to the given device."""
keys = self._get_layer_param_keys(layer_idx)
self._materialize_params(model, keys, device)
def _materialize_layer_split(self, model, layer_idx):
"""Load one layer with individual tensors placed on storage GPUs."""
keys = self._get_layer_param_keys(layer_idx)
raw_prefix = f"{self._layer_prefix}{layer_idx}."
model_prefix = _checkpoint_key_to_model_key(raw_prefix)
target_devices_by_key = {
f"{model_prefix}{relative_name}": device
for relative_name, device in self.layer_tensor_storage[layer_idx].items()
}
self._materialize_params(
model,
keys,
"cpu",
target_devices_by_key=target_devices_by_key,
)
def _materialize_params(self, model, param_keys, device, target_devices_by_key=None):
"""Load parameters from safetensors and place into model.
Handles the checkpoint-to-model key mismatch for MoE experts:
checkpoint has per-expert keys (experts.{i}.gate_proj.weight) that must
be fused into 3D parameters (experts.gate_up_proj, experts.down_proj).
"""
target_devices_by_key = target_devices_by_key or {}
def target_device_for(model_key):
return target_devices_by_key.get(model_key, device)
model_keys = self._get_model_state_keys(model)
# Separate expert keys from regular keys. Only fuse per-expert
# checkpoint weights when the in-memory model actually has fused
# parameters. MiMo already uses experts.{i}.{proj}.weight modules;
# quantized streaming layers use projection-first experts.{proj}.{i}.
expert_pattern = re.compile(
r"^(.*\.mlp\.experts)\.(\d+)\.(gate_proj|up_proj|down_proj)\.weight$"
)
regular_entries = []
dense_gate_up_groups = defaultdict(dict)
# expert_groups[prefix] = {expert_idx: {proj_name: checkpoint_key}}
expert_groups = defaultdict(lambda: defaultdict(dict))
for raw_key in param_keys:
key = _checkpoint_key_to_model_key(raw_key)
dense_gate_up = _dense_gate_up_target_key(key)
if dense_gate_up is not None:
target_key, part = dense_gate_up
if target_key in model_keys:
dense_gate_up_groups[target_key][part] = raw_key
continue
m = expert_pattern.match(key)
projection_first_key = _expert_first_to_projection_first_key(key)
if key not in model_keys and not m and projection_first_key not in model_keys:
continue
if m:
prefix, idx, proj = m.group(1), int(m.group(2)), m.group(3)
if (
f"{prefix}.gate_up_proj" in model_keys
or f"{prefix}.down_proj" in model_keys
):
expert_groups[prefix][idx][proj] = raw_key
elif projection_first_key in model_keys:
regular_entries.append((raw_key, projection_first_key))
elif key in model_keys:
regular_entries.append((raw_key, key))
else:
regular_entries.append((raw_key, key))
# Load regular (non-expert) keys directly
by_shard = defaultdict(list)
for raw_key, model_key in regular_entries:
by_shard[self.weight_map[raw_key]].append((raw_key, model_key))
for shard_file, entries in by_shard.items():
shard_path = os.path.join(self.snapshot_dir, shard_file)
load_device = "cpu" if target_devices_by_key else str(device)
with safe_open(shard_path, framework="pt", device=load_device) as f:
available = set(f.keys())
for raw_key, model_key in entries:
if raw_key in available:
tensor = f.get_tensor(raw_key)
target_device = target_device_for(model_key)
set_module_tensor_to_device(
model, model_key, target_device, value=tensor
)
# Fuse dense MiniMax M3 gate/up weights into gate_up_proj.
for target_key, parts in dense_gate_up_groups.items():
if "gate" not in parts or "up" not in parts:
raise RuntimeError(
f"Missing dense gate/up source weight for {target_key}: {sorted(parts)}"
)
tensors = _load_checkpoint_tensors(
self.snapshot_dir,
self.weight_map,
[parts["gate"], parts["up"]],
"cpu",
)
gate = tensors[parts["gate"]]
up = tensors[parts["up"]]
gate_up = torch.cat([gate, up], dim=0)
target_device = target_device_for(target_key)
set_module_tensor_to_device(
model,
target_key,
target_device,
value=gate_up,
)
del gate, up, gate_up, tensors
# Fuse per-expert keys into 3D parameters.
# Always fuse on CPU to avoid massive temporary GPU memory spikes —
# accumulating 768 individual expert tensors + stacked + fused can be
# 4-5x the final layer size, causing OOM on the target GPU.
for prefix, experts_dict in expert_groups.items():
num_experts = max(experts_dict.keys()) + 1
# Collect all expert shard files we'll need
expert_shard_keys = defaultdict(list)
for idx in range(num_experts):
for proj in ("gate_proj", "up_proj", "down_proj"):
ck = experts_dict.get(idx, {}).get(proj)
if ck:
expert_shard_keys[self.weight_map[ck]].append((idx, proj, ck))
# Load expert tensors to CPU regardless of target device
gate_tensors = [None] * num_experts
up_tensors = [None] * num_experts
down_tensors = [None] * num_experts
for shard_file, entries in expert_shard_keys.items():
shard_path = os.path.join(self.snapshot_dir, shard_file)
with safe_open(shard_path, framework="pt", device="cpu") as f:
for idx, proj, ck in entries:
t = f.get_tensor(ck)
if proj == "gate_proj":
gate_tensors[idx] = t
elif proj == "up_proj":
up_tensors[idx] = t
else:
down_tensors[idx] = t
# Fuse on CPU: gate [N,I,H] + up [N,I,H] -> gate_up [N,2I,H]
gate_stacked = torch.stack(gate_tensors) # [N, I, H]
up_stacked = torch.stack(up_tensors) # [N, I, H]
gate_up = torch.cat([gate_stacked, up_stacked], dim=1) # [N, 2I, H]
del gate_stacked, up_stacked, gate_tensors, up_tensors
# Move fused results to their target devices. Split storage may
# place the large gate/up and down expert tensors on different GPUs.
gate_up_key = f"{prefix}.gate_up_proj"
gate_up_device = target_device_for(gate_up_key)
set_module_tensor_to_device(
model, gate_up_key, gate_up_device, value=gate_up
)
del gate_up
# Fuse down on CPU: [N, H, I]
down_stacked = torch.stack(down_tensors)
del down_tensors
down_key = f"{prefix}.down_proj"
down_device = target_device_for(down_key)
set_module_tensor_to_device(
model, down_key, down_device, value=down_stacked
)
del down_stacked
gc.collect()
def _patch_quant_fused_experts_setup(self):
"""Monkey-patch _QuantFusedExperts._setup to handle meta tensors lazily."""
from moe_registry import _QuantFusedExperts
if getattr(_QuantFusedExperts, "_streaming_patch_applied", False):
return
original_setup = _QuantFusedExperts._setup
def patched_setup(self_expert):
if self_expert.gate_up_proj.device == torch.device("meta"):
# Meta device — create per-expert Linear structure on meta,
# actual data will be loaded by the streaming hook on demand.
I = self_expert.intermediate_dim
H = self_expert.hidden_dim
with init_empty_weights():
gate_proj = nn.ModuleList(
[nn.Linear(H, I, bias=False) for _ in range(self_expert.num_experts)]
)
up_proj = nn.ModuleList(
[nn.Linear(H, I, bias=False) for _ in range(self_expert.num_experts)]
)
down_proj = nn.ModuleList(
[nn.Linear(I, H, bias=False) for _ in range(self_expert.num_experts)]
)
delattr(self_expert, "gate_up_proj")
delattr(self_expert, "down_proj")
self_expert.gate_proj = gate_proj
self_expert.up_proj = up_proj
self_expert.down_proj = down_proj
self_expert._needs_lazy_unfuse = True
else:
original_setup(self_expert)
self_expert._needs_lazy_unfuse = False
_QuantFusedExperts._setup = patched_setup
_QuantFusedExperts._streaming_patch_applied = True
def _install_hooks(self, model):
"""Register streaming forward hooks on each decoder layer."""
layers = self._get_layers(model)
for i, layer in enumerate(layers):
device = self.storage_map[i]
hook = LayerStreamingHook(
layer_idx=i,
storage_device=device,
loader=self,
)
h1 = layer.register_forward_pre_hook(hook.pre_forward)
h2 = layer.register_forward_hook(hook.post_forward)
self._hook_handles.extend([h1, h2])
print(f"Installed streaming hooks on {len(layers)} layers")
def remove_hooks(self):
"""Remove all streaming hooks."""
for h in self._hook_handles:
h.remove()
self._hook_handles.clear()
def prepare_export(self, model):
"""Prepare model for export by removing streaming hooks and installing
export-mode materialization callbacks on meta layers."""
self.remove_hooks()
for i, layer in enumerate(self._get_layers(model)):
device = self.storage_map[i]
if device == "meta":
# Attach a callback that export_hf will check before .to("cpu")
layer._streaming_materialize = lambda mod, idx=i: (
self._materialize_layer_for_export(model, mod, idx)
)
def _materialize_layer_for_export(self, model, module, layer_idx):
"""Load a meta layer's weights for export (to CPU, with expert unfusing)."""
del model
self._materialize_layer_module(module, layer_idx, "cpu")
def _materialize_layer_module(self, module, layer_idx, device):
"""Load one decoder layer into an already-created module tree."""
keys = self._get_layer_param_keys(layer_idx)
raw_prefix = f"{self._layer_prefix}{layer_idx}."
model_prefix = _checkpoint_key_to_model_key(raw_prefix)
module_keys = set(dict(module.named_parameters()).keys()) | set(dict(module.named_buffers()).keys())
regular_entries = []
dense_gate_up_groups = defaultdict(dict)
for raw_key in keys:
model_key = _checkpoint_key_to_model_key(raw_key)
if not model_key.startswith(model_prefix):
continue
dense_gate_up = _dense_gate_up_target_key(model_key)
if dense_gate_up is not None:
target_key, part = dense_gate_up
if target_key.startswith(model_prefix):
target_relative = target_key[len(model_prefix):]
if target_relative in module_keys:
dense_gate_up_groups[target_relative][part] = raw_key
continue
relative = model_key[len(model_prefix):]
projection_first_key = _expert_first_to_projection_first_key(model_key)
if projection_first_key is not None and projection_first_key.startswith(model_prefix):
projection_first_relative = projection_first_key[len(model_prefix):]
if projection_first_relative in module_keys:
relative = projection_first_relative
if relative not in module_keys:
continue
regular_entries.append((raw_key, relative))
by_shard = defaultdict(list)
for raw_key, relative in regular_entries:
by_shard[self.weight_map[raw_key]].append((raw_key, relative))
for shard_file, entries in by_shard.items():
shard_path = os.path.join(self.snapshot_dir, shard_file)
with safe_open(shard_path, framework="pt", device=str(device)) as f:
available = set(f.keys())
for raw_key, relative in entries:
if raw_key not in available:
continue
tensor = f.get_tensor(raw_key)
_assign_tensor_to_module(module, relative, tensor, device)
for target_relative, parts in dense_gate_up_groups.items():
if "gate" not in parts or "up" not in parts:
raise RuntimeError(
f"Missing dense gate/up source weight for {target_relative}: {sorted(parts)}"
)
tensors = _load_checkpoint_tensors(
self.snapshot_dir,
self.weight_map,
[parts["gate"], parts["up"]],
device,
)
gate_up = torch.cat([tensors[parts["gate"]], tensors[parts["up"]]], dim=0)
_assign_tensor_to_module(module, target_relative, gate_up, device)
del gate_up, tensors
def _get_model_state_keys(self, model):
if self._model_state_keys is None:
self._model_state_keys = (
set(dict(model.named_parameters()).keys())
| set(dict(model.named_buffers()).keys())