55forward: same outputs, same gradients, same state_dict layout, and graceful
66fallback everywhere the reformulation doesn't apply.
77"""
8+
89import copy
910from functools import partial
1011
@@ -64,7 +65,9 @@ def randomize_lora(model, seed=1, magnitude_jitter=0.3):
6465 for name in ("magnitude" ,):
6566 if hasattr (mod , name ):
6667 p = getattr (mod , name )
67- p .mul_ (1.0 + magnitude_jitter * torch .rand (p .shape , generator = g ).to (p ))
68+ p .mul_ (
69+ 1.0 + magnitude_jitter * torch .rand (p .shape , generator = g ).to (p )
70+ )
6871
6972
7073def lora_trainables (model ):
@@ -147,7 +150,9 @@ def test_gradient_parity_mps_fp16_autocast(adapter):
147150 y_naive , g_naive = run_fwd_bwd (naive , x , proj )
148151 out_err = rel_err (y_fast .float (), y_naive .float ())
149152 worst = max (rel_err (g_fast [k ].float (), g_naive [k ].float ()) for k in g_naive )
150- print (f"[autocast fp16 { adapter } ] output rel err = { out_err :.3e} , worst grad rel err = { worst :.3e} " )
153+ print (
154+ f"[autocast fp16 { adapter } ] output rel err = { out_err :.3e} , worst grad rel err = { worst :.3e} "
155+ )
151156 # fp16 GEMM noise dominates; ~1e-2-class is the expected scale, this bound is a regression guard
152157 assert out_err <= 3e-2
153158 assert worst <= 5e-2
@@ -166,7 +171,9 @@ def test_strength_buffer(adapter, device):
166171 base = make_model (bias = True , device = device ) # same seed -> identical base weights
167172 y_fast = fast (x )
168173 y_base = base (x )
169- assert torch .equal (y_fast , y_base ), f"strength=0 must be bit-exact to the base ({ adapter } /{ device } )"
174+ assert torch .equal (y_fast , y_base ), (
175+ f"strength=0 must be bit-exact to the base ({ adapter } /{ device } )"
176+ )
170177
171178
172179@pytest .mark .parametrize ("device" , DEVICES )
@@ -179,9 +186,12 @@ def test_stacked_parametrization_falls_back(device):
179186 for m in (fast , naive ):
180187 torch .manual_seed (7 )
181188 for mod in m .modules ():
182- if isinstance (mod , nn .Linear ) and parametrize .is_parametrized (mod , "weight" ):
189+ if isinstance (mod , nn .Linear ) and parametrize .is_parametrized (
190+ mod , "weight"
191+ ):
183192 p2 = LoRAParametrization .from_linear (
184- mod , rank = 4 , lora_alpha = 8 , adapter_type = "dora-rows" , lora_index = 1 )
193+ mod , rank = 4 , lora_alpha = 8 , adapter_type = "dora-rows" , lora_index = 1
194+ )
185195 with torch .no_grad ():
186196 p2 .lora_B .copy_ (torch .randn_like (p2 .lora_B ) * 0.05 )
187197 parametrize .register_parametrization (mod , "weight" , p2 , unsafe = True )
@@ -192,7 +202,7 @@ def test_stacked_parametrization_falls_back(device):
192202 assert len (mod .parametrizations ["weight" ]) == 2
193203
194204 x = torch .randn (2 , 9 , 64 , device = device )
195- y_fast = fast (x ) # wrapper must detect the stack and fall back per-forward
205+ y_fast = fast (x ) # wrapper must detect the stack and fall back per-forward
196206 y_naive = naive (x )
197207 assert torch .equal (y_fast , y_naive ), "stacked modules must use the exact naive path"
198208
@@ -229,7 +239,9 @@ def test_state_dict_layout_unchanged(adapter, monkeypatch):
229239 monkeypatch .setenv ("SA3_FAST_LORA" , "0" )
230240 naive = make_model ()
231241 add_lora (naive , lora_config (adapter ))
232- assert not naive [0 ].__dict__ .get ("_fast_lora_wrapped" , False ), "SA3_FAST_LORA=0 must disable wrapping"
242+ assert not naive [0 ].__dict__ .get ("_fast_lora_wrapped" , False ), (
243+ "SA3_FAST_LORA=0 must disable wrapping"
244+ )
233245
234246 # populate the norm-constant cache before snapshotting
235247 fast (torch .randn (2 , 4 , 64 ))
@@ -249,8 +261,13 @@ def test_dropout_shared_between_direction_and_norm(device):
249261 forward; under a fixed RNG seed both paths must consume the same draws."""
250262 cfg = {
251263 nn .Linear : {
252- "weight" : partial (LoRAParametrization .from_linear , rank = 8 , lora_alpha = 16 ,
253- adapter_type = "dora-rows" , lora_dropout_p = 0.5 ),
264+ "weight" : partial (
265+ LoRAParametrization .from_linear ,
266+ rank = 8 ,
267+ lora_alpha = 16 ,
268+ adapter_type = "dora-rows" ,
269+ lora_dropout_p = 0.5 ,
270+ ),
254271 },
255272 }
256273 model = make_model (device = device )
0 commit comments