-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathapply_rotation_transform.py
More file actions
957 lines (837 loc) · 39.3 KB
/
Copy pathapply_rotation_transform.py
File metadata and controls
957 lines (837 loc) · 39.3 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
# # Copyright (C) 2026 Intel Corporation
# # SPDX-License-Identifier: Apache-2.0
"""Hadamard inplace rotation — public API and rotation primitives.
Supports LLaMA-2, LLaMA-3, Qwen-3 (and any model with the same layout).
The entry point is :func:`apply_hadamard_rotation`.
"""
import gc
import typing
from typing import Dict, Union
import torch
import tqdm
from auto_round.experimental.rotation_inplace.model_config import (
MAPPING_REGISTRY,
RotationMapping,
_resolve,
infer_mapping_from_model,
)
from auto_round.experimental.rotation_inplace.special_model_handler import apply_special_overrides
from auto_round.experimental.rotation_inplace.utils import (
CrossHeadOnlineHadamardHook,
FullOnlineHadamardHook,
GroupOnlineHadamardHook,
_get_custom_had,
_normalize_rotation_matrix,
_resolve_compute_device,
_rotate_embedding_grouped,
_rotate_linear_grouped,
apply_cross_head_had_to_linear,
apply_exact_had_to_linear,
deterministic_hadamard_matrix,
get_hadK,
get_or_create_random_hadamard,
)
# ---------------------------------------------------------------------------
# Low-level primitives (model-agnostic via RotationMapping)
# ---------------------------------------------------------------------------
def _resolve_head_dim(mapping, config, hidden_size, num_heads):
"""Resolve the per-head attention dimension.
Resolution order:
1. ``mapping.attn_head_dim`` (explicit override on the RotationMapping).
2. ``config.head_dim`` if present (Qwen-3 and other models declare an
explicit ``head_dim`` that does not necessarily equal
``hidden_size // num_heads``; e.g. Qwen3-32B has hidden=5120,
heads=64, head_dim=128 → o_proj.in_features = 8192, not 5120).
3. ``hidden_size // num_heads`` as a last-resort default.
"""
if mapping.attn_head_dim:
return mapping.attn_head_dim
cfg_head_dim = getattr(config, "head_dim", None)
if isinstance(cfg_head_dim, int) and cfg_head_dim > 0:
return cfg_head_dim
return hidden_size // num_heads
def _fuse_ln_linear(
layernorm: torch.nn.Module,
linear_layers: typing.Iterable[torch.nn.Linear],
) -> None:
"""Fuse the linear operations in LayerNorm into adjacent linear blocks."""
for linear in linear_layers:
linear_dtype = linear.weight.dtype
dev = linear.weight.device
W_ = linear.weight.data.double()
ln_weight = layernorm.weight.double().to(dev)
linear.weight.data = (W_ * ln_weight).to(linear_dtype)
if hasattr(layernorm, "bias") and layernorm.bias is not None:
if linear.bias is None:
linear.bias = torch.nn.Parameter(torch.zeros(linear.out_features, dtype=torch.float64, device=dev))
ln_bias = layernorm.bias.double().to(dev)
linear.bias.data = linear.bias.data.double() + torch.matmul(W_, ln_bias)
linear.bias.data = linear.bias.data.to(linear_dtype)
def _reset_ln_params(layernorm: torch.nn.Module) -> None:
"""Reset LayerNorm to identity: weight=1, bias=0."""
layernorm.weight.data.fill_(1.0)
if hasattr(layernorm, "bias") and layernorm.bias is not None:
layernorm.bias.data.fill_(0.0)
def _rotate_weight_chunked(
weight: torch.Tensor,
Q: torch.Tensor,
side: str,
compute_device,
chunk: int = 4096,
) -> torch.Tensor:
"""Compute the rotated weight without ever materialising the full fp64 copy.
* ``side == 'input'`` → returns ``W @ Q`` (chunked over rows of ``W``).
* ``side == 'output'`` → returns ``Q^T @ W`` (chunked over columns of ``W``).
The output is pre-allocated in the **original** dtype on the **original**
device of ``weight``. At any moment only a single chunk lives in fp64 on
``compute_device``, so peak transient memory is roughly
``chunk * other_dim * 8`` bytes instead of ``W.numel() * 8``.
Embedding/lm_head on Qwen3-14B (151936 × 5120) drops from ~12 GB to a few
hundred MB transient.
"""
dtype = weight.dtype
dev = weight.device
out = torch.empty_like(weight)
Q_ = Q.to(device=compute_device, dtype=torch.float64)
try:
if side == "input":
# (R, C) @ (C, C) → (R, C); chunk over R.
R = weight.shape[0]
for i in range(0, R, chunk):
j = min(i + chunk, R)
blk = weight.data[i:j].to(device=compute_device, dtype=torch.float64)
rotated = (blk @ Q_).to(device=dev, dtype=dtype)
out[i:j].copy_(rotated)
del blk, rotated
elif side == "output":
# Q^T @ (R, C) → (R, C); chunk over C so each block is (R, chunk).
C = weight.shape[1]
Q_T = Q_.T.contiguous()
for i in range(0, C, chunk):
j = min(i + chunk, C)
blk = weight.data[:, i:j].to(device=compute_device, dtype=torch.float64)
rotated = (Q_T @ blk).to(device=dev, dtype=dtype)
out[:, i:j].copy_(rotated)
del blk, rotated
del Q_T
else:
raise ValueError(f"side must be 'input' or 'output', got {side!r}")
finally:
del Q_
return out
def _rotate_linear_by_Q(module: torch.nn.Linear, Q: torch.Tensor, side: str, compute_device=None) -> None:
"""Apply rotation *Q* to a Linear layer's weight (and bias if present).
Memory-efficient: never materialises the full fp64 weight at once.
Args:
side: ``'input'`` → W = W @ Q (rotate input side)
``'output'`` → W = Q^T @ W (rotate output side)
compute_device: Device to run computation on. If None, auto-detects GPU.
"""
cdev = _resolve_compute_device(compute_device)
module.weight.data = _rotate_weight_chunked(module.weight.data, Q, side, cdev)
if side == "output" and module.bias is not None:
dtype = module.bias.data.dtype
dev = module.bias.data.device
# Bias is a 1-D vector → small; safe to do in one shot.
b = module.bias.data.to(device=cdev, dtype=torch.float64)
Q_ = Q.to(device=cdev, dtype=torch.float64)
new_b = torch.matmul(Q_.T, b).to(device=dev, dtype=dtype)
del b, Q_
module.bias.data = new_b
def _untie_word_embeddings(model, mapping: RotationMapping) -> None:
"""Break tied weights between lm_head and embedding if they share the same tensor."""
embedding = _resolve(model, mapping.embedding)
lm_head = _resolve(model, mapping.lm_head)
if lm_head.weight.data_ptr() != embedding.weight.data_ptr():
return
lm_head.weight = torch.nn.Parameter(lm_head.weight.data.clone())
if hasattr(model.config, "tie_word_embeddings"):
model.config.tie_word_embeddings = False
def _uses_layernorm_with_mean(model, mapping: RotationMapping) -> bool:
"""Check whether the model uses standard LayerNorm (which subtracts mean)."""
layers = _resolve(model, mapping.layers_attr)
first_ln = _resolve(layers[0], mapping.attn_input_ln)
return isinstance(first_ln, torch.nn.LayerNorm)
def _bake_mean_into_linear(linear: torch.nn.Linear) -> None:
"""Subtract column-wise mean from a Linear layer's weight (and mean from bias)."""
linear_dtype = linear.weight.dtype
W_ = linear.weight.data.double()
linear.weight.data = (W_ - W_.mean(dim=-2, keepdim=True)).to(linear_dtype)
if linear.bias is not None:
b_ = linear.bias.data.double()
linear.bias.data = (b_ - b_.mean()).to(linear_dtype)
def _subtract_embedding_mean(model, mapping: RotationMapping) -> None:
"""Subtract per-row mean from the embedding weight matrix."""
W = _resolve(model, mapping.embedding)
dtype = W.weight.data.dtype
W_ = W.weight.data.to(dtype=torch.float64)
W.weight.data = (W_ - W_.mean(dim=-1, keepdim=True)).to(dtype=dtype)
if mapping.positional_embedding is not None:
P = _resolve(model, mapping.positional_embedding)
p_dtype = P.weight.data.dtype
P_ = P.weight.data.to(dtype=torch.float64)
P.weight.data = (P_ - P_.mean(dim=-1, keepdim=True)).to(dtype=p_dtype)
class _RMSNorm(torch.nn.Module):
"""RMS Normalization (no mean subtraction)."""
def __init__(self, dim: int, eps: float = 1e-5):
super().__init__()
self.eps = eps
self.register_buffer("weight", torch.ones(dim))
def forward(self, x: torch.Tensor) -> torch.Tensor:
rms = torch.sqrt(x.pow(2).mean(dim=-1, keepdim=True) + self.eps)
return x / rms * self.weight
def _replace_layernorms_with_rmsnorm(model) -> None:
"""Replace all ``nn.LayerNorm`` modules with ``_RMSNorm``."""
replacements = []
for name, module in model.named_modules():
if isinstance(module, torch.nn.LayerNorm):
replacements.append((name, module))
for name, module in replacements:
parts = name.rsplit(".", 1)
if len(parts) == 2:
parent = _resolve(model, parts[0])
attr = parts[1]
else:
parent = model
attr = parts[0]
rms = _RMSNorm(module.normalized_shape[0], eps=module.eps)
rms = rms.to(device=module.weight.device, dtype=module.weight.dtype)
setattr(parent, attr, rms)
# ---------------------------------------------------------------------------
# High-level steps driven by RotationMapping
# ---------------------------------------------------------------------------
def _fuse_layer_norms(model, mapping: RotationMapping) -> None:
"""Fuse all LayerNorm parameters into adjacent Linear layers."""
layers = _resolve(model, mapping.layers_attr)
for layer in layers:
mlp_ln = _resolve(layer, mapping.mlp_input_ln)
mlp_linears = [_resolve(layer, p) for p in mapping.mlp_in]
_fuse_ln_linear(mlp_ln, mlp_linears)
_reset_ln_params(mlp_ln)
attn_ln = _resolve(layer, mapping.attn_input_ln)
attn_linears = [
_resolve(layer, mapping.attn_q),
_resolve(layer, mapping.attn_k),
_resolve(layer, mapping.attn_v),
]
_fuse_ln_linear(attn_ln, attn_linears)
_reset_ln_params(attn_ln)
pre_head_ln = _resolve(model, mapping.pre_head_ln)
lm_head = _resolve(model, mapping.lm_head)
_fuse_ln_linear(pre_head_ln, [lm_head])
_reset_ln_params(pre_head_ln)
# ---------------------------------------------------------------------------
# Unified weight rotation (full or grouped)
# ---------------------------------------------------------------------------
@torch.inference_mode()
def _rotate_weights(
model,
mapping: RotationMapping,
use_fast_had: bool = True,
group_size: int = None,
compute_device: torch.device = None,
had_dict: dict = None,
preset: str = None,
fuse_online_to_weight: bool = True,
) -> None:
"""Apply Hadamard rotation to all weights.
Args:
group_size: ``None`` → full Hadamard rotation.
``int`` → block-diagonal rotation with this block size.
compute_device: Device to run Hadamard computation on (e.g. ``"cuda:0"``).
Weights are moved there temporarily and moved back afterwards.
If ``None``, auto-detects GPU availability.
allow_online_rotation: If ``True`` (default), apply extra input-side
Hadamard rotations on ``down_proj`` and the OV pair (``v_proj``
output + ``o_proj`` input) that require compensating online hooks
at inference time. If ``False``, skip those extra rotations so
that **no** online hooks are needed.
had_dict: Normalized ``dict[int, Tensor]`` of custom Hadamard matrices
(keyed by dimension). Only used in grouped mode.
preset: Rotation preset name (``"quarot_hadamard"``, ``"hadamard"``,
``"random_hadamard"``, or ``None``).
* ``"quarot_hadamard"``: fusable (residual-stream) rotations use
``fast_hadamard_transform`` / random Hadamard; non-fusable
(online-paired) rotations and their weight-side counterparts use
deterministic ``get_hadK``/``matmul_hadU`` so that the online
hook at inference produces the exact same transform.
* ``"hadamard"``: all rotations use deterministic ``get_hadK`` /
``matmul_hadU``. Full-mode Q is a deterministic Hadamard matrix.
* ``"random_hadamard"``: all rotations use random Hadamard matrices
from the global cache (``get_or_create_random_hadamard``).
Same dimension → same matrix everywhere.
* ``None``: same behaviour as ``"hadamard"`` (built-in butterfly).
"""
compute_device = _resolve_compute_device(compute_device)
config = model.config
hidden_size = getattr(config, mapping.hidden_size_attr)
intermediate_size = getattr(config, mapping.intermediate_size_attr)
num_heads = getattr(config, mapping.num_heads_attr)
head_dim = _resolve_head_dim(mapping, config, hidden_size, num_heads)
is_grouped = group_size is not None and group_size > 0
desc = f"Rotating (group_size={group_size})" if is_grouped else "Rotating"
# ----- Resolve per-operation Hadamard sources -----
fused_fast = use_fast_had
online_fast = False
if preset == "random_hadamard":
fused_fast = False
# -- Matrix resolution --
had_matrix, _found = _get_custom_had(had_dict, group_size) if is_grouped else (None, False)
online_had_matrix = had_matrix
if preset == "random_hadamard" and had_matrix is None:
had_matrix = get_or_create_random_hadamard(group_size if is_grouped else hidden_size, compute_device)
online_had_matrix = had_matrix
if preset == "quarot_hadamard" and is_grouped:
online_had_matrix = None # force deterministic for online-paired
# -- Helper: look up cached random matrix for online-paired ops --
def _online_had(dim):
"""Return cached random matrix for *dim* under random_hadamard, else None."""
if preset == "random_hadamard":
return get_or_create_random_hadamard(dim, compute_device)
return None
if is_grouped:
assert hidden_size % group_size == 0, f"group_size={group_size} must divide hidden_size={hidden_size}"
assert (
intermediate_size % group_size == 0
), f"group_size={group_size} must divide intermediate_size={intermediate_size}"
# --- Full mode: build Hadamard matrix Q ---
Q = None
if not is_grouped:
if preset == "hadamard":
Q = deterministic_hadamard_matrix(hidden_size, compute_device)
else:
# "random_hadamard", "quarot_hadamard", None — same shape → same matrix
Q = get_or_create_random_hadamard(hidden_size, compute_device)
# ---- Top-level: embedding / lm_head ----
# When fuse_online_to_weight=False, skip embedding and lm_head rotation:
# each layer is self-contained (weight rotation + online hook cancel out).
if fuse_online_to_weight:
embedding = _resolve(model, mapping.embedding)
if is_grouped:
_rotate_embedding_grouped(
embedding, group_size, use_fast_had=fused_fast, compute_device=compute_device, had_matrix=had_matrix
)
else:
# Chunked: avoids a full fp64 copy of the (vocab, hidden) embedding,
# which on Qwen3-14B is ~6 GB on its own.
embedding.weight.data = _rotate_weight_chunked(
embedding.weight.data, Q, side="input", compute_device=compute_device
)
if mapping.positional_embedding is not None:
pos_emb = _resolve(model, mapping.positional_embedding)
if is_grouped:
_rotate_embedding_grouped(
pos_emb, group_size, use_fast_had=fused_fast, compute_device=compute_device, had_matrix=had_matrix
)
else:
pos_emb.weight.data = _rotate_weight_chunked(
pos_emb.weight.data, Q, side="input", compute_device=compute_device
)
# ---- Top-level: lm_head ----
lm_head = _resolve(model, mapping.lm_head)
if is_grouped:
_rotate_linear_grouped(
lm_head,
group_size,
side="input",
use_fast_had=fused_fast,
compute_device=compute_device,
had_matrix=had_matrix,
)
else:
_rotate_linear_by_Q(lm_head, Q, side="input", compute_device=compute_device)
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
# ---- Per-layer rotation ----
layers = _resolve(model, mapping.layers_attr)
for layer in tqdm.tqdm(layers, unit="layer", desc=desc):
if fuse_online_to_weight:
# ---- fuse mode: QuaRot-style residual stream rotation ----
# Q/K/V: only residual Q on input (no online Had stacking, no hook).
# When Q == online Had (e.g. preset="hadamard"), Q @ Q = I cancels
# the rotation entirely, destroying quantization benefit.
# gate/up: only residual Q on input (no online Had stacking, no hook).
# down_proj: residual Q^T on output + online Had on input (+ hook).
# v_proj/o_proj: per-head/cross-head Had below (+ hook on o_proj).
for attr in (mapping.attn_q, mapping.attn_k, mapping.attn_v):
mod = _resolve(layer, attr)
if is_grouped:
_rotate_linear_grouped(
mod,
group_size,
side="input",
use_fast_had=fused_fast,
compute_device=compute_device,
had_matrix=had_matrix,
)
else:
_rotate_linear_by_Q(mod, Q, side="input", compute_device=compute_device)
# o_proj: residual stream output rotation
if is_grouped:
_rotate_linear_grouped(
_resolve(layer, mapping.attn_o),
group_size,
side="output",
use_fast_had=fused_fast,
compute_device=compute_device,
had_matrix=had_matrix,
)
else:
_rotate_linear_by_Q(_resolve(layer, mapping.attn_o), Q, side="output", compute_device=compute_device)
# gate/up: only residual Q on input
for attr in mapping.mlp_in:
mod = _resolve(layer, attr)
if is_grouped:
_rotate_linear_grouped(
mod,
group_size,
side="input",
use_fast_had=fused_fast,
compute_device=compute_device,
had_matrix=had_matrix,
)
else:
_rotate_linear_by_Q(mod, Q, side="input", compute_device=compute_device)
# down_proj: residual output + online input Had
down_proj = _resolve(layer, mapping.mlp_out)
if is_grouped:
_rotate_linear_grouped(
down_proj,
group_size,
side="output",
use_fast_had=fused_fast,
compute_device=compute_device,
had_matrix=had_matrix,
)
_rotate_linear_grouped(
down_proj,
group_size,
side="input",
use_fast_had=online_fast,
compute_device=compute_device,
had_matrix=online_had_matrix,
)
else:
_rotate_linear_by_Q(down_proj, Q, side="output", compute_device=compute_device)
apply_exact_had_to_linear(
down_proj,
had_dim=-1,
output=False,
use_fast_had=online_fast,
compute_device=compute_device,
had_matrix=_online_had(intermediate_size),
)
# OV projection: v_proj per-head output + o_proj decomposed input
#
# The online hook on o_proj applies (H_cross ⊗ I_head)⁻¹ at
# runtime, so the weight-side rotation must equal exactly
# (H_cross ⊗ I_head)(I_heads ⊗ H_head) = H_cross ⊗ H_head.
#
# IMPORTANT: we must NOT use a single full-dimension Hadamard
# (``had_dim=-1``) on o_proj, because the butterfly construction
# ``matmul_hadU(hidden_size)`` does NOT satisfy the Kronecker
# decomposition ``H_hidden = H_num_heads ⊗ H_head_dim`` when
# ``num_heads`` is not a power of 2 (e.g. Qwen3-14B, num_heads=40).
# Instead we always apply per-head + cross-head separately.
v_proj = _resolve(layer, mapping.attn_v)
o_proj = _resolve(layer, mapping.attn_o)
if is_grouped:
pass
else:
online_head_had = _online_had(head_dim)
apply_exact_had_to_linear(
v_proj,
had_dim=head_dim,
output=True,
use_fast_had=online_fast,
compute_device=compute_device,
had_matrix=online_head_had,
)
apply_exact_had_to_linear(
o_proj,
had_dim=head_dim,
output=False,
use_fast_had=online_fast,
compute_device=compute_device,
had_matrix=online_head_had,
)
apply_cross_head_had_to_linear(
o_proj,
num_heads,
head_dim,
use_fast_had=online_fast,
compute_device=compute_device,
had_matrix=_online_had(num_heads),
)
else:
# ---- unfused mode: no residual rotation, only input-side Had ----
# Each layer gets Had fused on input side + compensating hook → equivalent.
# No embedding/lm_head rotation. No self-cancelling pair.
# v_proj treated same as Q/K (input Had only, no per-head/cross-head).
# Q/K/V: input-side Had on hidden_size
for attr in (mapping.attn_q, mapping.attn_k, mapping.attn_v):
mod = _resolve(layer, attr)
if is_grouped:
_rotate_linear_grouped(
mod,
group_size,
side="input",
use_fast_had=online_fast,
compute_device=compute_device,
had_matrix=online_had_matrix,
)
else:
apply_exact_had_to_linear(
mod,
had_dim=-1,
output=False,
use_fast_had=online_fast,
compute_device=compute_device,
had_matrix=_online_had(hidden_size),
)
# o_proj: input-side Had on hidden_size (full Had, not cross-head)
o_proj = _resolve(layer, mapping.attn_o)
if is_grouped:
_rotate_linear_grouped(
o_proj,
group_size,
side="input",
use_fast_had=online_fast,
compute_device=compute_device,
had_matrix=online_had_matrix,
)
else:
apply_exact_had_to_linear(
o_proj,
had_dim=-1,
output=False,
use_fast_had=online_fast,
compute_device=compute_device,
had_matrix=_online_had(hidden_size),
)
# gate/up: input-side Had on hidden_size
for attr in mapping.mlp_in:
mod = _resolve(layer, attr)
if is_grouped:
_rotate_linear_grouped(
mod,
group_size,
side="input",
use_fast_had=online_fast,
compute_device=compute_device,
had_matrix=online_had_matrix,
)
else:
apply_exact_had_to_linear(
mod,
had_dim=-1,
output=False,
use_fast_had=online_fast,
compute_device=compute_device,
had_matrix=_online_had(hidden_size),
)
# down_proj: input-side Had on intermediate_size
down_proj = _resolve(layer, mapping.mlp_out)
if is_grouped:
_rotate_linear_grouped(
down_proj,
group_size,
side="input",
use_fast_had=online_fast,
compute_device=compute_device,
had_matrix=online_had_matrix,
)
else:
apply_exact_had_to_linear(
down_proj,
had_dim=-1,
output=False,
use_fast_had=online_fast,
compute_device=compute_device,
had_matrix=_online_had(intermediate_size),
)
# Per-layer cleanup: drop fp64 temporaries and CUDA caching allocator
# blocks so peak memory stays at ~1 layer's worth instead of accumulating
# across all 32+ decoder layers (was the main cause of 33 GB RAM on 8B).
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
# ---------------------------------------------------------------------------
# Unified online hook registration
# ---------------------------------------------------------------------------
def _register_online_hooks(
model,
mapping: RotationMapping,
fp32_had: bool = False,
use_fast_had: bool = True,
group_size: int = None,
had_dict: dict = None,
preset: str = None,
fuse_online_to_weight: bool = True,
):
"""Register online Hadamard pre-forward hooks on ``down_proj`` and ``o_proj``.
Online hooks must use the **same** Hadamard matrix that was applied to the
weight-side counterpart during ``_rotate_weights``. For ``quarot_hadamard``
this is always the deterministic ``get_hadK``/``matmul_hadU`` path
(``use_fast_had=False``). For ``"random_hadamard"`` it is the random matrix that
was generated once and stored in ``had_dict``.
Args:
group_size: ``None`` → full Hadamard hooks (original QuaRot).
``int`` → per-group Hadamard hooks.
had_dict: Normalized ``dict[int, Tensor]`` of custom Hadamard matrices.
preset: Rotation preset name.
Returns:
list of hook handles.
"""
config = model.config
num_heads = getattr(config, mapping.num_heads_attr)
hidden_size = getattr(config, mapping.hidden_size_attr)
intermediate_size = getattr(config, mapping.intermediate_size_attr)
head_dim = _resolve_head_dim(mapping, config, hidden_size, num_heads)
is_grouped = group_size is not None and group_size > 0
# Online hooks always use deterministic (fixed) Hadamard — never fast_had
# for quarot_hadamard; for "random_hadamard" they use the same random matrix
# that was cached in had_dict by _rotate_weights.
online_fast = False
# -- Matrix resolution (must match the *online-paired* matrix used by
# _rotate_weights for down_proj input / OV pair). Variable name kept in
# sync with _rotate_weights to make any future drift obvious.
online_had_matrix, _ = _get_custom_had(had_dict, group_size) if is_grouped else (None, False)
if preset == "random_hadamard" and online_had_matrix is None:
online_had_matrix = get_or_create_random_hadamard(group_size if is_grouped else hidden_size)
if preset == "quarot_hadamard" and is_grouped:
online_had_matrix = None
# -- Helper: look up cached random matrix for online-paired hooks --
def _online_had(dim):
if preset == "random_hadamard":
return get_or_create_random_hadamard(dim)
return None
mlp_out_suffix = mapping.mlp_out.split(".")[-1]
attn_o_suffix = mapping.attn_o.split(".")[-1]
# Suffixes for Q/K/V and gate/up (for online input Had hooks)
attn_qkv_suffixes = set(attr.split(".")[-1] for attr in (mapping.attn_q, mapping.attn_k, mapping.attn_v))
mlp_in_suffixes = set(attr.split(".")[-1] for attr in mapping.mlp_in)
# --- Build hook factories ---
def _make_down_proj_hook():
if is_grouped:
return GroupOnlineHadamardHook(
group_size=group_size, fp32_had=fp32_had, use_fast_had=online_fast, had_matrix=online_had_matrix
)
online_mat = _online_had(intermediate_size)
if online_mat is not None:
return FullOnlineHadamardHook(
had_K=None, K=None, fp32_had=fp32_had, use_fast_had=online_fast, had_matrix=online_mat
)
had_K, K = get_hadK(intermediate_size)
return FullOnlineHadamardHook(had_K=had_K, K=K, fp32_had=fp32_had, use_fast_had=online_fast)
def _make_hidden_had_hook():
"""Full Had hook on hidden_size (for Q/K/V and gate/up input)."""
if is_grouped:
return GroupOnlineHadamardHook(
group_size=group_size, fp32_had=fp32_had, use_fast_had=online_fast, had_matrix=online_had_matrix
)
online_mat = _online_had(hidden_size)
if online_mat is not None:
return FullOnlineHadamardHook(
had_K=None, K=None, fp32_had=fp32_had, use_fast_had=online_fast, had_matrix=online_mat
)
had_K, K = get_hadK(hidden_size)
return FullOnlineHadamardHook(had_K=had_K, K=K, fp32_had=fp32_had, use_fast_had=online_fast)
def _make_o_proj_hook():
online_mat = _online_had(num_heads)
if online_mat is not None:
return CrossHeadOnlineHadamardHook(
had_K=None,
K=None,
head_dim=head_dim,
fp32_had=fp32_had,
use_fast_had=online_fast,
had_matrix=online_mat,
)
had_K, K = get_hadK(num_heads)
return CrossHeadOnlineHadamardHook(
had_K=had_K,
K=K,
head_dim=head_dim,
fp32_had=fp32_had,
use_fast_had=online_fast,
)
# --- Register ---
handles = []
for name, module in model.named_modules():
if not isinstance(module, torch.nn.Linear):
continue
suffix = name.split(".")[-1]
if name.endswith(mlp_out_suffix):
# down_proj: full Had on intermediate_size input
h = module.register_forward_pre_hook(_make_down_proj_hook())
handles.append(h)
elif name.endswith(attn_o_suffix):
if fuse_online_to_weight and not is_grouped:
# o_proj: cross-head Had on input (fused mode, full only)
h = module.register_forward_pre_hook(_make_o_proj_hook())
handles.append(h)
elif not fuse_online_to_weight:
# o_proj: full Had on hidden_size input (unfused mode, matches weight rotation)
h = module.register_forward_pre_hook(_make_hidden_had_hook())
handles.append(h)
elif suffix in attn_qkv_suffixes:
if not fuse_online_to_weight:
# Q/K/V: full Had on hidden_size input (unfused mode only).
# In fused mode Q/K/V only have residual Q on weight (no online Had),
# and activations come pre-rotated from residual stream → no hook needed.
h = module.register_forward_pre_hook(_make_hidden_had_hook())
handles.append(h)
elif suffix in mlp_in_suffixes:
if not fuse_online_to_weight:
# gate/up: full Had on hidden_size input (unfused mode only).
# Same reasoning as Q/K/V above.
h = module.register_forward_pre_hook(_make_hidden_had_hook())
handles.append(h)
return handles
# ---------------------------------------------------------------------------
# Public API
# ---------------------------------------------------------------------------
def apply_rotation_transform(
model,
group_size: int = None,
allow_online_rotation: bool = True,
rotation_matrix: Union[str, torch.Tensor, Dict[int, torch.Tensor], None] = None,
compute_device: torch.device | str = None,
fp32_had: bool = False,
fuse_online_to_weight: bool = None,
):
"""Fuse layer norms, rotate weights, and register online Hadamard hooks.
This is the single entry point for applying Hadamard inplace rotation.
The model architecture is auto-detected via ``model.config.model_type``.
Args:
model: A HuggingFace CausalLM model (LLaMA-2/3, Qwen-3, etc.).
fp32_had: Whether to compute the online Hadamard transform in fp32.
group_size: If ``None`` (default), use full-dimension Hadamard rotation.
compute_device: Device to run Hadamard computation on.
allow_online_rotation: If ``True`` (default), apply online Hadamard
rotations on ``down_proj`` input and the OV pair.
rotation_matrix: Rotation matrix selection (``"hadamard"``,
``"random_hadamard"``, ``"quarot_hadamard"``, Tensor, dict, or None).
fuse_online_to_weight: If ``True`` (default), fuse online Hadamard
rotation into weights (down_proj input, v_proj output, o_proj input)
and register compensating online hooks. If ``False``, skip
embedding/lm_head rotation; each linear layer is self-contained
with input-side Had on weight + compensating online hook on
activation. No v_proj cross-head or inner-head rotation.
Returns:
list of hook handles."""
if fuse_online_to_weight is None:
if model.config.model_type in MAPPING_REGISTRY or model.__class__.__name__ in MAPPING_REGISTRY:
fuse_online_to_weight = True
else:
fuse_online_to_weight = False
# ---- Model-specific overrides ----
# Some models require a specific rotation configuration to preserve
# accuracy or to run correctly. The mapping lives in
# ``special_model_handler.SPECIAL_MODEL_REGISTRY`` so that adding a new
# special-cased model is a one-liner there instead of a code change here.
_override_kwargs = {
"rotation_matrix": rotation_matrix,
"fuse_online_to_weight": fuse_online_to_weight,
"group_size": group_size,
"allow_online_rotation": allow_online_rotation,
}
apply_special_overrides(model, _override_kwargs)
rotation_matrix = _override_kwargs["rotation_matrix"]
fuse_online_to_weight = _override_kwargs["fuse_online_to_weight"]
group_size = _override_kwargs["group_size"]
allow_online_rotation = _override_kwargs["allow_online_rotation"]
had_dict, use_fast_had, preset = _normalize_rotation_matrix(rotation_matrix, group_size)
compute_device = _resolve_compute_device(compute_device)
if use_fast_had:
from auto_round.utils import logger
try:
import fast_hadamard_transform # noqa: F401
if group_size is None:
logger.warning(
"fast_hadamard_transform uses a different Hadamard matrix than the "
"default implementation. Please ensure consistency between training "
"and inference. This will be refined later."
)
except ImportError:
logger.warning("Importing fast_hadamard_transform failed, falling back to default implementation.")
use_fast_had = False
mapping = infer_mapping_from_model(model)
_untie_word_embeddings(model, mapping)
if _uses_layernorm_with_mean(model, mapping):
_subtract_embedding_mean(model, mapping)
_fuse_layer_norms(model, mapping)
if _uses_layernorm_with_mean(model, mapping):
layers = _resolve(model, mapping.layers_attr)
for layer in layers:
_bake_mean_into_linear(_resolve(layer, mapping.attn_o))
_bake_mean_into_linear(_resolve(layer, mapping.mlp_out))
_replace_layernorms_with_rmsnorm(model)
_rotate_weights(
model,
mapping,
use_fast_had=use_fast_had,
group_size=group_size,
compute_device=compute_device,
had_dict=had_dict,
preset=preset,
fuse_online_to_weight=fuse_online_to_weight,
)
handles = []
if fuse_online_to_weight or allow_online_rotation:
handles = _register_online_hooks(
model,
mapping,
fp32_had=fp32_had,
use_fast_had=use_fast_had,
group_size=group_size,
had_dict=had_dict,
preset=preset,
fuse_online_to_weight=fuse_online_to_weight,
)
return model, handles
# ---------------------------------------------------------------------------
# Quick smoke test
# ---------------------------------------------------------------------------
if __name__ == "__main__":
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "/models/Qwen3-14B"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
apply_rotation_transform(
model, group_size=128, allow_online_rotation=True, rotation_matrix="hadamard", fuse_online_to_weight=True
)
text = "There is a girl who likes adventure,"
inputs = tokenizer(text, return_tensors="pt").to(model.device)
print(tokenizer.decode(model.generate(**inputs, max_new_tokens=50)[0]))
#
# model_name = "/models/Qwen3-8B"
# tokenizer = AutoTokenizer.from_pretrained(model_name)
# model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
# apply_rotation_transform(model, group_size=-1, allow_online_rotation=True, fuse_online_to_weight=True)
# model.to("cuda")
# text = "There is a girl who likes adventure,"
# inputs = tokenizer(text, return_tensors="pt").to(model.device)
# print(tokenizer.decode(model.generate(**inputs, max_new_tokens=50)[0]))
#
# from transformers import AutoModelForCausalLM, AutoTokenizer
#
# model_name = "/models/Meta-Llama-3.1-8B-Instruct"
# tokenizer = AutoTokenizer.from_pretrained(model_name)
# model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
# apply_rotation_transform(model, fuse_online_to_weight=True, group_size=32)
# model.to("cuda")
# text = "There is a girl who likes adventure,"
# inputs = tokenizer(text, return_tensors="pt").to(model.device)
# print(tokenizer.decode(model.generate(**inputs, max_new_tokens=50)[0]))
#
# model_name = "/models/Llama-2-7b-chat-hf"
# tokenizer = AutoTokenizer.from_pretrained(model_name)
# model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
# apply_hadamard_rotation(model)
# model.to("cuda")
# text = "There is a girl who likes adventure,"
# inputs = tokenizer(text, return_tensors="pt").to(model.device)
# print(tokenizer.decode(model.generate(**inputs, max_new_tokens=50)[0]))