-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathdelta_loss.py
More file actions
1477 lines (1300 loc) · 58.1 KB
/
Copy pathdelta_loss.py
File metadata and controls
1477 lines (1300 loc) · 58.1 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
# Copyright (c) 2025 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
from dataclasses import asdict
from functools import wraps
from typing import Iterable, Optional, Union
import torch
from accelerate import dispatch_model
from tqdm import tqdm
from auto_round.auto_scheme.gen_auto_scheme import AutoScheme
from auto_round.auto_scheme.register import register_scheme_methods
from auto_round.auto_scheme.utils import (
apply_quant_scheme,
compute_avg_bits_for_scheme,
compute_layer_bits,
parse_shared_layers,
remove_quant_scheme,
)
from auto_round.calib_dataset import get_dataloader
from auto_round.data_type.gguf import (
quant_tensor_gguf_asym_dq,
quant_tensor_gguf_sym_dq,
search_gguf_scale_min_asym,
search_gguf_scale_min_sym,
)
from auto_round.data_type.utils import reshape_pad_tensor_by_group_size, revert_tensor_by_pad
from auto_round.logger import logger
from auto_round.schemes import QuantizationScheme, preset_name_to_scheme
from auto_round.utils import (
SUPPORTED_LAYER_TYPES,
check_to_quantized,
clear_memory,
dispatch_model_by_all_available_devices,
get_block_names,
get_major_device,
get_module,
is_mllm_model,
llm_load_model,
mllm_load_model,
parse_available_devices,
set_avg_auto_device_map,
set_module,
set_non_auto_device_map,
to_device,
to_dtype,
)
from auto_round.utils.device import MemoryMonitor
from auto_round.utils.device_manager import get_current_device_manager
from auto_round.utils.offload import OffloadManager
from auto_round.wrapper import WrapperLinear
__all__ = ["gen_layer_config"]
class AutoSchemeWrapperLinear(WrapperLinear):
def __init__(
self,
orig_layer,
enable_minmax_tuning=True,
enable_norm_bias_tuning=False,
device="cpu",
enable_round_tuning=True,
need_weight_grad=False,
enable_torch_compile=False,
**kwargs,
):
super().__init__(
orig_layer,
enable_minmax_tuning,
enable_norm_bias_tuning,
device,
enable_round_tuning,
enable_torch_compile=enable_torch_compile,
**kwargs,
)
self.total_act_score = 0.0
self.act_score = 0.0
self.avg_act_score = 0.0
self.act_cnt = 0.0
self.weight_score = 0.0
self.mix_score = 0.0
self.super_qdq_func = super()._qdq_weight
self.act_qdq_func = super()._qdq_act
# self.device = device
self.max_act_value = 0
self.need_weight_grad = need_weight_grad
self.grad_mode = False
if self.need_weight_grad:
self.orig_layer.weight.requires_grad = True
def _qdq_act(self, x, act_min_scale=1.0, act_max_scale=1.0, act_max=None):
if hasattr(self.orig_layer, "act_bits") and self.orig_layer.act_bits > 8:
return x, 1.0, None
qdq_x, scale, zp = self.act_qdq_func(x, act_min_scale, act_max_scale, act_max)
if self.grad_mode:
with torch.no_grad():
self.max_act_value = torch.abs(x).max()
if torch.abs(x).max() != 0:
self.act_cnt += 1
x_diff = x - qdq_x
self.x_diff = x_diff.to("cpu")
def save_grad(grad):
if self.max_act_value == 0:
if torch.abs(grad).max() != 0:
raise ValueError
"""
this ut will cause NAN issue sometimes, need to investigate
@multi_card
def test_multi_card(self):
model_name = "/models/Qwen3-8B"
"""
if torch.isnan(grad).any() or torch.isnan(self.x_diff).any():
self.act_cnt -= 1
return None
self.total_act_score += torch.abs((grad * self.x_diff.to(grad.device))).sum().item()
self.act_score = 0.0 if self.act_cnt <= 0 else self.total_act_score / self.act_cnt
self.mix_score = self.weight_score + self.act_score
self.x_diff = None
return None
qdq_x.register_hook(save_grad)
return qdq_x, scale, zp
def _qdq_weight(self, value, min_scale, max_scale):
device = self.device
if self.orig_layer.bits > 8 or not self.need_weight_grad:
qdq_w, scale, zp = super()._qdq_weight(
torch.tensor(0, device=device), torch.tensor(1.0, device=device), torch.tensor(1.0, device=device)
)
return qdq_w, 1.0, None
qdq_w, scale, zp = super()._qdq_weight(
torch.tensor(0, device=device), torch.tensor(1.0, device=device), torch.tensor(1.0, device=device)
)
if self.grad_mode:
def save_grad(grad):
qdq_w, scale, zp = self.super_qdq_func(
torch.tensor(0, device=device), torch.tensor(1.0, device=device), torch.tensor(1.0, device=device)
)
w_diff = self.orig_layer.weight - qdq_w.to(self.orig_layer.weight.device)
self.weight_score += torch.abs((grad.to(w_diff.device) * w_diff)).sum().item()
act_score = 0.0 if self.act_cnt <= 0 else self.total_act_score / self.act_cnt
self.mix_score = self.weight_score + act_score
return None
qdq_w.register_hook(save_grad)
return qdq_w, 1.0, None
class AutoSchemeWrapperLinearForGGUFK(AutoSchemeWrapperLinear):
def __init__(
self,
orig_layer,
enable_minmax_tuning=True,
enable_norm_bias_tuning=False,
device="cpu",
enable_round_tuning=True,
need_weight_grad=False,
**kwargs,
):
super().__init__(
orig_layer,
enable_minmax_tuning,
enable_norm_bias_tuning,
device,
enable_round_tuning,
need_weight_grad,
**kwargs,
)
with torch.no_grad():
qdq_w, scale, zp = self.super_qdq_func(
torch.tensor(0).to(device), torch.tensor(1.0).to(device), torch.tensor(1.0).to(device)
)
self.register_buffer("qdq_w", qdq_w.detach().clone().to(self.orig_layer.weight.device))
if self.need_weight_grad:
def save_grad(grad):
w_diff = self.orig_layer.weight - self.qdq_w.to(self.orig_layer.weight.device)
# TODO strange, grad could be in CPU
self.weight_score += torch.abs((grad.to(w_diff.device) * w_diff)).sum().item() # TODO add 2nd order
act_score = 0.0 if self.act_cnt <= 0 else self.total_act_score / self.act_cnt
self.mix_score = self.weight_score + act_score
return None
self.qdq_w.requires_grad_(True)
self.orig_layer.weight.requires_grad_(False)
self.qdq_w.register_hook(save_grad)
def _qdq_weight(self, value, min_scale, max_scale):
return self.qdq_w, 1.0, None
class AutoSchemeWrapperLinearForGGUFKImatrix(AutoSchemeWrapperLinear):
def __init__(
self,
orig_layer,
enable_minmax_tuning=True,
enable_norm_bias_tuning=False,
device="cpu",
enable_round_tuning=True,
need_weight_grad=False,
enable_torch_compile=False,
**kwargs,
):
super().__init__(
orig_layer,
enable_minmax_tuning,
enable_norm_bias_tuning,
device,
enable_round_tuning,
need_weight_grad,
enable_torch_compile=enable_torch_compile,
**kwargs,
)
with torch.no_grad():
qdq_w = self._init_scale().detach()
self.register_buffer("qdq_w", qdq_w.detach().clone().to(self.orig_layer.weight.device))
if self.need_weight_grad:
def save_grad(grad):
w_diff = self.orig_layer.weight - self.qdq_w.to(self.orig_layer.weight.device)
self.weight_score += torch.abs((grad * w_diff.to(grad.device))).sum().item()
act_score = 0.0 if self.act_cnt <= 0 else self.total_act_score / self.act_cnt
self.mix_score = self.weight_score + act_score
return None
self.qdq_w.requires_grad_(True)
self.orig_layer.weight.requires_grad_(False)
self.qdq_w.register_hook(save_grad)
@torch.no_grad()
def _init_scale(self):
tensor = self.orig_layer.weight.data
bits = self.orig_layer.bits
scale_dtype = self.orig_layer.scale_dtype
imatrix = self.orig_layer.imatrix
orig_dtype = tensor.dtype
if self.orig_layer.bits in [2, 4, 5]:
group_size = 16 if bits == 2 else 32
tensor, orig_shape, pad_len = reshape_pad_tensor_by_group_size(tensor, group_size)
scale, wmin, d_scale, d_wmin = search_gguf_scale_min_asym(tensor, bits, scale_dtype, imatrix)
tensor = revert_tensor_by_pad(tensor, orig_shape=orig_shape, pad_len=pad_len)
qdq_w, _, _ = quant_tensor_gguf_asym_dq(
tensor=tensor,
bits=bits,
scale_dtype=scale_dtype,
imatrix=imatrix,
scale=scale,
wmin=wmin,
d_scale=d_scale,
d_wmin=d_wmin,
)
elif bits in [3, 6]:
group_size = 16
tensor, orig_shape, pad_len = reshape_pad_tensor_by_group_size(tensor, group_size)
scale, d_scale = search_gguf_scale_min_sym(tensor, bits, imatrix, scale_dtype, split_num=1)
tensor = revert_tensor_by_pad(tensor, orig_shape=orig_shape, pad_len=pad_len)
qdq_w, _, _ = quant_tensor_gguf_sym_dq(
tensor=tensor, bits=bits, scale_dtype=scale_dtype, imatrix=imatrix, scale=scale, d_scale=d_scale
)
else:
raise ValueError("bits must be in [2,3,4,5,6]")
return qdq_w.to(orig_dtype)
def _qdq_weight(self, value, min_scale, max_scale):
return self.qdq_w, 1.0, None
@torch.no_grad()
def cal_imatrix(model, dataloader):
def register_act_hook(model):
"""Registers hooks to accumulate activation squared norms into `imatrix`."""
def get_imatrix_hook(module, input, output):
input = input[0] if isinstance(input, (tuple, list)) else input
flattened = input.reshape(-1, input.shape[-1]).to(torch.float32)
squared = torch.sum(torch.pow(flattened, 2), dim=0).to(torch.float32)
if not hasattr(module, "imatrix"):
module.imatrix = squared.to("cpu")
else:
module.imatrix += squared.to(module.imatrix.device).to("cpu")
hook_handles = []
for name, module in model.named_modules():
if isinstance(module, SUPPORTED_LAYER_TYPES):
hook = module.register_forward_hook(get_imatrix_hook)
hook_handles.append(hook)
return hook_handles
hooks = register_act_hook(model)
for data in dataloader:
model.forward(**to_device(data, model.device))
for hook in hooks:
hook.remove()
class MyCustomError(Exception):
def __init__(self, message):
super().__init__(message)
last_grad_input = None
def prepare_model_low_gpu(model, block_inputs: dict = None, pbar=None, major_device="cpu"):
block_inputs.clear()
for n, m in model.named_modules():
if hasattr(m, "grad_mode"):
m.grad_mode = False
block_names = get_block_names(model)[0]
def wrap_forward(module, module_name):
original_forward = module.forward
@wraps(original_forward)
def new_forward(*args, **kwargs):
move_module_to_tuning_device(module, major_device=major_device)
# Call the original forward
with torch.no_grad():
result = original_forward(*args, **kwargs)
# Save input information and ensure tensors are on CPU
input_info = {
"args": [arg.detach().clone().to("cpu") if isinstance(arg, torch.Tensor) else arg for arg in args],
"kwargs": {
k: v.detach().clone().to("cpu") if isinstance(v, torch.Tensor) else v for k, v in kwargs.items()
},
}
block_inputs[module_name] = input_info
module.to("cpu")
clear_memory()
# Enable gradients for the output of the last block
if module.tmp_name == block_names[-1]:
if isinstance(result, torch.Tensor):
result = result.requires_grad_(True)
elif isinstance(result, tuple):
result = tuple(r.requires_grad_(True) if isinstance(r, torch.Tensor) else r for r in result)
pbar.update(1)
return result
return new_forward
# Assign a temporary name to each module
for n, m in model.named_modules():
m.tmp_name = n
# Wrap the forward method of each block
for block_name in block_names:
module = get_module(model, block_name)
module.forward = wrap_forward(module, block_name)
def _prepare_mllm_inputs(data, model):
"""Normalize one batch from a (possibly mllm) dataloader into a form the
model can be called on.
Crucially this casts ``images`` / ``pixel_values`` / ``pixel_values_videos``
to ``model.dtype`` — without it, vision tensors arrive as float32 while
the vision tower is bf16/fp16, and several HF VLM implementations silently
bypass the vision branch on dtype mismatch (which manifests downstream as
"vision grad = 0").
Returns ``(prepared, kind)`` where ``kind`` is ``"tensor" | "seq" | "dict"``
so the caller knows whether to use ``model(x)`` / ``model(*x)`` /
``model(**x)``.
"""
_img_keys = ("images", "image", "pixel_values", "pixel_values_videos", "pixel_values_images", "image_pixel_values")
if isinstance(data, torch.Tensor):
return data.to(model.device), "tensor"
if isinstance(data, (tuple, list)):
return to_device(data, model.device), "seq"
# Plain dict (the common path: HF VLM ``data_collator`` outputs).
new = {}
for key, value in data.items():
t = to_device(value, model.device)
if key in _img_keys:
t = to_dtype(t, model.dtype)
new[key] = t
return new, "dict"
def mllm_model_forward(model, data, **forward_kwargs):
"""Single entry point for "run a (possibly multimodal) batch through the
model". Used by both AutoScheme paths so that ``pixel_values`` / ``images``
are cast to ``model.dtype`` (otherwise VLMs silently skip the vision tower
→ vision grad = 0)."""
prepared, kind = _prepare_mllm_inputs(data, model)
if kind == "tensor":
return model(prepared, **forward_kwargs), prepared
if kind == "seq":
return model(*prepared, **forward_kwargs), prepared
return model(**prepared, **forward_kwargs), prepared
def model_forward_low_gpu(model, dataloader, major_device="cuda", pbar=None):
block_inputs = {}
block_names = get_block_names(model)[0]
for name in block_names:
module = get_module(model, name)
module.orig_forward = module.forward
def backward_pre_hook(module, grad_input):
"""Hook executed before backward propagation."""
global last_grad_input
last_grad_input = grad_input
get_current_device_manager().synchronize()
raise MyCustomError("Interrupt backward pass")
for data in dataloader:
prepare_model_low_gpu(model, block_inputs, major_device=major_device, pbar=pbar)
# Register backward hook on the last block
last_block = get_module(model, block_names[-1])
last_block_backward_hook = last_block.register_full_backward_pre_hook(backward_pre_hook)
data = to_device(data, model.device)
# VLM datasets often already include ``labels``; LLM ones don't. Strip
# any pre-existing ``labels`` from kwargs so we don't pass it twice.
labels = data["labels"] if isinstance(data, dict) and "labels" in data else data["input_ids"]
if isinstance(data, dict):
data_for_forward = {k: v for k, v in data.items() if k != "labels"}
else:
data_for_forward = data
# Route through the unified mllm forward so ``pixel_values`` /
# ``images`` get cast to ``model.dtype`` (otherwise the vision tower
# is silently bypassed on dtype mismatch and vision grad stays 0).
output, _prepared = mllm_model_forward(model, data_for_forward, labels=labels, use_cache=False)
try:
# Backward pass (will be interrupted by the hook)
output.loss.backward()
except MyCustomError:
pass
current_grad = last_grad_input
del output, data
clear_memory()
# Manually compute gradients block by block
last_block_backward_hook.remove()
for name in block_names:
module = get_module(model, name)
module.forward = module.orig_forward
for block_name in reversed(block_names):
# Retrieve stored inputs for the block
block_input_info = block_inputs.get(block_name, {})
block_input_args = to_device(block_input_info.get("args", []), major_device)
block_input_kwargs = to_device(block_input_info.get("kwargs", {}), major_device)
block_input_args[0].requires_grad_(True)
# Move the block module to GPU
block_module = get_module(model, block_name)
for n, m in block_module.named_modules():
if hasattr(m, "grad_mode"):
m.grad_mode = True
move_module_to_tuning_device(block_module, major_device=major_device)
# Set the block to eval mode while enabling gradient computation
block_module.eval()
# Recompute the block output
block_output = block_module(*block_input_args, **block_input_kwargs)
# Ensure the output requires gradients
if isinstance(block_output, tuple):
# For tuple outputs, we usually care about the first element (hidden states)
main_output = block_output[0]
main_output = main_output.requires_grad_(True)
else:
main_output = block_output.requires_grad_(True)
# Backward pass for the current block
torch.autograd.backward(
tensors=main_output,
# inputs=block_input_args,
grad_tensors=current_grad,
retain_graph=True, # False may lead to zero gradients for some cases (e.g., MXFP4)
)
# Extract gradients w.r.t. the block input
if block_input_args and isinstance(block_input_args[0], torch.Tensor):
current_grad = block_input_args[0].grad.detach().clone()
else:
print(f"Warning: No suitable input gradient found for {block_name}")
break
del block_output, main_output, block_input_args, block_input_kwargs
block_module.to("cpu")
clear_memory()
pbar.update(1)
def get_score_for_scheme(
model,
tokenizer,
quant_layer_names,
fixed_layer_scheme,
dataset,
ignore_scale_zp_bits=False,
nsamples=16,
seqlen=256,
pbar=None,
shared_layers=None,
need_weight_grad=False,
enable_torch_compile=False,
low_gpu_mem_usage=True,
major_device="cpu",
batch_size=1,
disable_opt_rtn=True,
offload_context: Optional[OffloadManager] = None,
processor=None,
is_vlm: bool = False,
force_mllm: bool = False,
model_name: Optional[str] = None,
):
scores_dict = {} # Key=name,Val=[quant_total_bits, loss]
for n, m in model.named_modules():
if type(m) in SUPPORTED_LAYER_TYPES:
m.weight.requires_grad = False
if hasattr(m, "bias") and m.bias is not None:
m.bias.requires_grad = False
has_imatrix = False
for name in quant_layer_names:
if name in fixed_layer_scheme.keys():
continue
m = get_module(model, name)
if hasattr(m, "imatrix") and m.imatrix is not None:
has_imatrix = True
break
for name in quant_layer_names:
if offload_context is not None:
offload_context.ensure_loaded(model, name)
if name in fixed_layer_scheme.keys():
continue
m = get_module(model, name)
if not check_to_quantized(m):
layer_bits, _ = compute_layer_bits(m, ignore_scale_zp_bits)
scores_dict[name] = [layer_bits, 0.0]
continue
if m.act_bits > 8 and m.super_bits is not None:
m.scale_dtype = torch.float32 # TODO set this via API
elif m.act_bits > 8:
m.scale_dtype = torch.float16
else:
m.scale_dtype = torch.bfloat16
WrapperLayer = AutoSchemeWrapperLinear
if hasattr(m, "super_group_size") and m.super_group_size is not None:
if has_imatrix:
WrapperLayer = AutoSchemeWrapperLinearForGGUFKImatrix
else:
WrapperLayer = AutoSchemeWrapperLinearForGGUFK
with torch.no_grad():
if low_gpu_mem_usage:
device = m.tuning_device if hasattr(m, "tuning_device") else major_device
# Any non-CPU device (cuda/xpu/hpu/...) is consolidated to the major device.
if str(device).split(":")[0] not in ("cpu", "meta", "disk"):
device = major_device
else:
device = m.weight.device
m.tuning_device = m.weight.device
new_m = WrapperLayer(
m,
device=device,
enable_minmax_tuning=False, # TODO this should be change
enable_norm_bias_tuning=False,
enable_round_tuning=False,
need_weight_grad=need_weight_grad,
enable_torch_compile=enable_torch_compile,
disable_opt_rtn=disable_opt_rtn,
)
set_module(model, name, new_m)
if offload_context is not None:
offload_context.flush_loaded(model)
# ---- Memory: only wrapper.orig_layer.weight needs ``requires_grad`` ---- #
# AutoScheme scoring uses ``iters=0`` (RTN), so we never UPDATE any
# parameter. The only parameters that need to participate in autograd
# are the wrappers' ``orig_layer.weight`` (so qdq_w in the backward
# graph can trace back to them and the weight-grad hook fires).
# All other parameters (norms, non-wrapped linears, vision-tower
# layers when ``--quant_nontext_module`` is off, …) just waste a full
# ``.grad`` buffer (~one model-worth of VRAM) during ``loss.backward()``.
# This is the biggest single VRAM win for the non-low_gpu_mem_usage
# path used to score VLMs.
wrapper_weight_ids = set()
for _, _m in model.named_modules():
if hasattr(_m, "orig_layer") and hasattr(_m.orig_layer, "weight") and _m.orig_layer.weight is not None:
wrapper_weight_ids.add(id(_m.orig_layer.weight))
_trimmed = 0
for _p in model.parameters():
if id(_p) not in wrapper_weight_ids and _p.requires_grad:
_p.requires_grad_(False)
_trimmed += 1
if _trimmed:
logger.info(
"AutoScheme: disabled requires_grad on %d non-wrapper parameters "
"(only wrapper.orig_layer.weight needs grad for scoring; saves "
"~one model-worth of grad buffer during backward).",
_trimmed,
)
# When scoring vision-tower layers, keep the autograd chain alive end-to-end:
# (1) every wrapper's orig weight must require grad — otherwise the STE
# output of W-only-low-bit wrappers has no grad path and act-score
# hooks never fire.
# (2) every vision sub-tree leaf param must require grad — so the very
# first vision op (patch_embed / first conv) enters autograd; its
# input ``pixel_values`` is a plain tensor with no grad.
if force_mllm:
_re_enabled_w = 0
for _, _m in model.named_modules():
if (
hasattr(_m, "orig_layer")
and hasattr(_m.orig_layer, "weight")
and _m.orig_layer.weight is not None
and not _m.orig_layer.weight.requires_grad
):
_m.orig_layer.weight.requires_grad_(True)
_re_enabled_w += 1
_vision_markers = ("vision", "visual", "image_encoder", "img_encoder", "patch_embed")
_re_enabled_v = 0
_seen = set()
for _mod_name, _mod in model.named_modules():
if not any(mk in _mod_name.lower() for mk in _vision_markers):
continue
for _p in _mod.parameters(recurse=False):
if id(_p) in _seen:
continue
_seen.add(id(_p))
if not _p.requires_grad:
_p.requires_grad_(True)
_re_enabled_v += 1
logger.info(
"AutoScheme(force_mllm): kept requires_grad on %d wrapper weights, " "%d vision-side params.",
_re_enabled_w,
_re_enabled_v,
)
def _build_calib_dataloader():
"""Pick the calibration dataloader.
Since AutoScheme only scores the language tower (``get_block_names``
already skips the vision/audio sub-trees on VLMs), a pure-text
calibration dataset is sufficient and far cheaper for VLMs too — most
VLMs accept a text-only forward and simply skip the vision encoder.
We therefore use ``get_dataloader`` (text-only) by default and only
fall back to the multimodal ``get_mllm_dataloader`` if a VLM truly
rejects text-only inputs (caller can detect that in the calling loop).
"""
return get_dataloader(tokenizer, seqlen, dataset_name=dataset, seed=42, bs=batch_size, nsamples=nsamples)
def _build_mllm_calib_dataloader():
"""Build the multimodal calibration dataloader (image + text).
Returns ``None`` if we can't build one (no processor / template /
dataset issue) so the caller can surface a clearer error.
"""
if processor is None:
return None
import os as _os
from auto_round.compressors.mllm.dataset import MLLM_DATASET, get_mllm_dataloader
template = None
if hasattr(model, "config") and hasattr(model.config, "model_type"):
template = model.config.model_type
# Decide the effective dataset.
# ``get_mllm_dataloader`` only treats ``dataset`` as multimodal when it
# is either a local file OR a key registered in ``MLLM_DATASET``.
# Otherwise it silently falls back to ``get_dataloader`` (text-only),
# which produces batches with NO ``pixel_values`` -> the vision tower
# is never invoked -> every vision score / grad collapses to 0. We
# explicitly catch that case here and override to a known-good
# multimodal dataset so the user doesn't end up with silent garbage.
ds = dataset
_is_real_mllm = isinstance(ds, str) and (_os.path.isfile(ds) or ds in MLLM_DATASET.keys())
if not _is_real_mllm:
_fallback = "liuhaotian/llava_conv_58k"
logger.warning_once(
"AutoScheme(force_mllm): dataset=%r is text-only, " "overriding to %r.",
ds,
_fallback,
)
ds = _fallback
try:
loader, _, _, _ = get_mllm_dataloader(
template=template,
model=model,
tokenizer=tokenizer,
processor=processor,
dataset=ds,
seqlen=seqlen,
bs=batch_size,
nsamples=nsamples,
# If, for any reason, get_mllm_dataloader still falls back to
# text-only, force it to hard-error rather than silently
# producing image-less batches.
quant_nontext_module=True,
)
return loader
except Exception as exc: # noqa: BLE001
logger.warning(f"Failed to build mllm calibration dataloader: {exc}")
return None
if low_gpu_mem_usage:
if force_mllm:
mllm_loader = _build_mllm_calib_dataloader()
if mllm_loader is None:
raise RuntimeError(
"AutoScheme(force_mllm): cannot build mllm dataloader. "
"Provide a `processor` and a multimodal `dataset`."
)
model_forward_low_gpu(model, mllm_loader, major_device=major_device, pbar=pbar)
else:
try:
dataloader = _build_calib_dataloader()
model_forward_low_gpu(model, dataloader, major_device=major_device, pbar=pbar)
except Exception as exc: # noqa: BLE001
if not is_vlm:
raise
logger.warning(
f"Text-only calibration failed on VLM ({exc}); "
f"falling back to multimodal calibration dataloader."
)
mllm_loader = _build_mllm_calib_dataloader()
if mllm_loader is None:
raise
model_forward_low_gpu(model, mllm_loader, major_device=major_device, pbar=pbar)
else:
for n, m in model.named_modules():
if hasattr(m, "grad_mode"):
m.grad_mode = True
def _run_forward_loop(loader):
_checked_pixel = False
_pixel_keys = (
"pixel_values",
"pixel_values_videos",
"pixel_values_images",
"image_pixel_values",
"images",
"image",
)
for data in loader:
# Pull labels out of the batch (VLM datasets often carry them;
# LLM ones don't) before mllm_model_forward casts dtypes.
_src = data if isinstance(data, dict) else None
labels = (
_src["labels"]
if _src is not None and "labels" in _src
else (_src["input_ids"] if _src is not None and "input_ids" in _src else None)
)
if _src is not None and "labels" in _src:
data_for_forward = {k: v for k, v in _src.items() if k != "labels"}
else:
data_for_forward = data
# Unified mllm-aware forward (casts pixel_values/images to
# model.dtype, handles dict-with-text/str/tuple paths the same
# way AutoRoundMLLM.calib does).
output, _prepared = mllm_model_forward(model, data_for_forward, labels=labels, use_cache=False)
output.loss.backward()
# One-shot sanity check: when scoring vision layers, the batch
# MUST carry image data, otherwise the vision tower is bypassed
# by the model and every vision score is silently 0.
if not _checked_pixel and force_mllm:
_checked_pixel = True
_has_pixel = isinstance(_prepared, dict) and any(k in _prepared for k in _pixel_keys)
if not _has_pixel:
_keys = list(_prepared.keys()) if isinstance(_prepared, dict) else type(_prepared).__name__
raise RuntimeError(
f"AutoScheme(force_mllm) batch has no pixel_values "
f"(keys: {_keys}). Vision scores would all be 0. "
f"Use a real multimodal dataset (e.g. "
f"liuhaotian/llava_conv_58k) and pass a processor."
)
for _, m in model.named_parameters(): # zero grads to keep VRAM low
m.grad = None
if pbar is not None:
pbar.update(1)
if force_mllm:
mllm_loader = _build_mllm_calib_dataloader()
if mllm_loader is None:
raise RuntimeError(
"AutoScheme(force_mllm): cannot build mllm dataloader. "
"Provide a `processor` and a multimodal `dataset`."
)
_run_forward_loop(mllm_loader)
else:
try:
_run_forward_loop(_build_calib_dataloader())
except Exception as exc: # noqa: BLE001
if not is_vlm:
raise
logger.warning(
f"Text-only calibration failed on VLM ({exc}); "
f"falling back to multimodal calibration dataloader."
)
mllm_loader = _build_mllm_calib_dataloader()
if mllm_loader is None:
raise
_run_forward_loop(mllm_loader)
for n, m in model.named_parameters():
m.grad = None
scores_dict = {}
for n, m in model.named_modules():
if hasattr(m, "mix_score"):
if m.orig_layer.act_bits <= 8:
if m.act_cnt == 0:
logger.warning_once(
"layer{n} max abs activation is 0, please use more data to improve the accuracy"
)
layer_bits, _ = compute_layer_bits(m.orig_layer, ignore_scale_zp_bits=ignore_scale_zp_bits)
scores_dict[n] = [layer_bits, m.mix_score]
for n, m in model.named_modules():
if hasattr(m, "orig_layer"):
set_module(model, n, m.orig_layer)
return scores_dict
def choose_bits_per_layer_with_path(layers: dict, P: int):
"""
Args:
layers: A dict mapping each layer name to a list of candidate options.
Each option is a tuple of (scheme, bits_cost, loss_cost, layer_names).
P: Upper bound on the total parameter (bit) budget.
Returns:
(min_loss, best_path), where best_path is a list of
(layer_names, scheme) for each layer, or (None, None) if no feasible
solution exists.
"""
# dp: total_params -> (accumulated_loss, chosen_path)
# The path explicitly stores the selected options.
dp: dict[int, tuple[float, list]] = {0: (0.0, [])}
for layer_name, opts in layers.items():
new_dp: dict[int, tuple[float, list]] = {}
for cur_params, (cur_loss, cur_path) in dp.items():
for opt in opts:
scheme, bits_cost, loss_cost, layer_names = opt
np_total = cur_params + bits_cost
if np_total > P:
continue
new_loss = cur_loss + loss_cost
new_path = cur_path + [(layer_names, scheme)]
# Keep the path with smaller loss for the same parameter budget
if np_total not in new_dp or new_loss < new_dp[np_total][0]:
new_dp[np_total] = (new_loss, new_path)
if not new_dp:
return None, None
# Pareto pruning: remove dominated (params, loss) states
items = sorted(new_dp.items(), key=lambda x: x[0]) # (params, (loss, path))
pruned: dict[int, tuple[float, list]] = {}
best_loss_so_far = float("inf")
for params_val, (loss_val, path_val) in items:
if loss_val < best_loss_so_far:
pruned[params_val] = (loss_val, path_val)
best_loss_so_far = loss_val
dp = pruned
# Select the solution with the minimum loss
best_params = min(dp.keys(), key=lambda k: dp[k][0])
best_loss, best_path = dp[best_params]
return best_loss, best_path
def move_module_to_tuning_device(module, major_device="cpu"):
def _normalize(dev):
return dev if isinstance(dev, torch.device) else torch.device(dev)
def _move_own_tensors(m, device):
# Cover non-leaf modules that directly hold nn.Parameter / buffers
# (e.g. Mamba/GDN linear_attn with A_log & dt_bias). Also relocate
# p.grad together with p.data — otherwise the next backward's grad
# accumulation hits a cuda/cpu device mismatch.
target = _normalize(device)
for p in m.parameters(recurse=False):
if p.device != target:
p.data = p.data.to(target)
if p.grad is not None and p.grad.device != target:
p.grad.data = p.grad.data.to(target)
for b_name, b in list(m.named_buffers(recurse=False)):
if b is None:
continue
if b.device != target:
m._buffers[b_name] = b.to(target)
for n, m in module.named_modules():
if hasattr(m, "orig_layer"):
target = m.orig_layer.tuning_device
m.to(target)
_move_own_tensors(m, target)
elif hasattr(m, "tuning_device"):
target = m.tuning_device
m.to(target)
_move_own_tensors(m, target)
elif len(list(m.children())) == 0:
m.to(major_device)
_move_own_tensors(m, major_device)
else:
_move_own_tensors(m, major_device)
def _gen_layer_config(
auto_scheme: AutoScheme,
model: Union[str, torch.nn.Module],
quant_layer_names: Iterable[str],
fixed_layer_scheme: dict[str, dict],
min_avg_bit_scheme,
dataset: str = "pile-10k",
tokenizer=None,
device_map=None,
enable_torch_compile=False,
disable_opt_rtn=True,
model_name=None,
major_device="cpu",
device_list=None,
processor=None,
is_vlm: bool = False,
):
# Initialize memory tracking for AutoScheme
memory_monitor = MemoryMonitor()
memory_monitor.reset()
memory_monitor.update_cpu()
# Create offload context for CPU RAM optimization
# Note: low_cpu_mem_usage only works when low_gpu_mem_usage is also enabled,
# because it requires layer-by-layer processing
offload_context = None
if auto_scheme.low_cpu_mem_usage and auto_scheme.low_gpu_mem_usage:
_model_dir = model_name
if _model_dir is None and hasattr(model, "config"):
_model_dir = getattr(model.config, "_name_or_path", None)
offload_mode = "clean"
offload_kwargs = {"model_dir": _model_dir}
# Rotation mutates weights in memory before AutoScheme starts. Clean-mode