@@ -196,139 +196,6 @@ def test_hybridep_dispatcher_type_is_preserved(self):
196196 self .assertTrue (config .moe_use_fusion_node )
197197
198198
199- class TestMagicInit (unittest .TestCase ):
200- """Tests for the magic_init functionality in TransformerConfig."""
201-
202- def test_magic_init_false_default_behavior (self ):
203- """When magic_init is False (default), normal init methods should be used."""
204- config = TransformerConfig (
205- num_hidden_layers = 12 ,
206- hidden_size = 768 ,
207- magic_init = False ,
208- )
209- # When False, init_method should be set but not the magic init
210- self .assertIsNotNone (config .init_method )
211- self .assertIsNotNone (config .output_layer_init_method )
212-
213- def test_magic_init_true_sigma_calculation (self ):
214- """When magic_init is True, sigma should be sqrt(0.3333 / hidden_size)."""
215- import math
216-
217- hidden_size = 768
218- config = TransformerConfig (
219- num_hidden_layers = 12 ,
220- hidden_size = hidden_size ,
221- magic_init = True ,
222- )
223- expected_sigma = math .sqrt (0.3333 / hidden_size )
224- self .assertAlmostEqual (config .init_method_std , expected_sigma , places = 6 )
225-
226- def test_magic_init_true_all_methods_same (self ):
227- """When magic_init is True, all init methods should be the same."""
228- config = TransformerConfig (
229- num_hidden_layers = 12 ,
230- hidden_size = 768 ,
231- magic_init = True ,
232- )
233- # All init methods should be the same function
234- self .assertIs (config .init_method , config .output_layer_init_method )
235- self .assertIs (config .init_method , config .embedding_init_method )
236-
237- def test_magic_init_true_different_hidden_sizes (self ):
238- """Test sigma calculation with different hidden sizes."""
239- import math
240-
241- for hidden_size in [512 , 768 , 1024 , 2048 , 4096 ]:
242- config = TransformerConfig (
243- num_hidden_layers = 12 ,
244- hidden_size = hidden_size ,
245- magic_init = True ,
246- )
247- expected_sigma = math .sqrt (0.3333 / hidden_size )
248- self .assertAlmostEqual (
249- config .init_method_std , expected_sigma , places = 6
250- )
251-
252- def test_magic_init_true_init_method_matches_get_magic_init_method (self ):
253- """When magic_init is True, init method should match get_magic_init_method."""
254- import math
255-
256- from paddlefleet .utils import get_magic_init_method
257-
258- hidden_size = 768
259- config = TransformerConfig (
260- num_hidden_layers = 12 ,
261- hidden_size = hidden_size ,
262- magic_init = True ,
263- )
264-
265- # Create test weight
266- weight = paddle .randn ([100 , 100 ])
267-
268- # Apply config's init method
269- config .init_method (weight )
270-
271- # Calculate expected using get_magic_init_method
272- expected_sigma = math .sqrt (0.3333 / hidden_size )
273- magic_init = get_magic_init_method (expected_sigma )
274- expected_weight = paddle .randn ([100 , 100 ])
275- magic_init (expected_weight )
276-
277- # Compare results using same random seed
278- paddle .seed (1234 )
279- weight1 = paddle .randn ([100 , 100 ])
280- config .init_method (weight1 )
281-
282- paddle .seed (1234 )
283- weight2 = paddle .randn ([100 , 100 ])
284- magic_init (weight2 )
285-
286- paddle .testing .assert_close (weight1 , weight2 , rtol = 1e-6 , atol = 1e-6 )
287-
288- def test_magic_init_false_uses_normal_init (self ):
289- """When magic_init is False, normal init methods should be used."""
290- config = TransformerConfig (
291- num_hidden_layers = 12 ,
292- hidden_size = 768 ,
293- magic_init = False ,
294- )
295- # Should have init_method_std set to normal value
296- self .assertIsNotNone (config .init_method_std )
297- # Should be a reasonable value for normal init (not the magic init value)
298- import math
299-
300- magic_sigma = math .sqrt (0.3333 / 768 )
301- self .assertNotAlmostEqual (config .init_method_std , magic_sigma , places = 6 )
302-
303- def test_magic_init_true_with_moe (self ):
304- """Test magic_init works correctly with MoE models."""
305- import math
306-
307- config = TransformerConfig (
308- num_hidden_layers = 12 ,
309- hidden_size = 768 ,
310- n_routed_experts = 8 ,
311- magic_init = True ,
312- )
313- expected_sigma = math .sqrt (0.3333 / 768 )
314- self .assertAlmostEqual (config .init_method_std , expected_sigma , places = 6 )
315- # All init methods should still be the same
316- self .assertIs (config .init_method , config .output_layer_init_method )
317- self .assertIs (config .init_method , config .embedding_init_method )
318-
319- def test_magic_init_true_raises_on_zero_hidden_size (self ):
320- """When magic_init is True and hidden_size is 0, should raise ValueError."""
321- with self .assertRaises (
322- ValueError ,
323- msg = "hidden_size must be non-zero when magic_init is True." ,
324- ):
325- TransformerConfig (
326- num_hidden_layers = 12 ,
327- hidden_size = 0 ,
328- magic_init = True ,
329- )
330-
331-
332199class TestTruncateNormInit (unittest .TestCase ):
333200 """Tests for the default truncated normal initialization in TransformerConfig."""
334201
@@ -360,19 +227,22 @@ def test_truncate_norm_is_default_and_reused(self):
360227 self .assertIs (config .init_method , config .output_layer_init_method )
361228 self .assertIs (config .init_method , config .embedding_init_method )
362229
363- def test_magic_init_takes_precedence_over_default (self ):
230+ def test_magic_init_is_forced_to_truncate_norm_default (self ):
364231 hidden_size = 1024
365232 config = TransformerConfig (
366233 num_hidden_layers = 12 ,
367234 hidden_size = hidden_size ,
368235 magic_init = True ,
369236 )
370237
238+ self .assertFalse (config .magic_init )
371239 self .assertAlmostEqual (
372240 config .init_method_std ,
373- math . sqrt ( 0.3333 / hidden_size ),
241+ 0.5 / math . sqrt ( hidden_size ),
374242 places = 6 ,
375243 )
244+ self .assertIs (config .init_method , config .output_layer_init_method )
245+ self .assertIs (config .init_method , config .embedding_init_method )
376246
377247 def test_explicit_init_method_disables_default (self ):
378248 import paddle
0 commit comments