@@ -460,92 +460,6 @@ def test_zero_byte_safetensors_file_raises(self, tmp_path):
460460 safetensors .torch .load_file (str (empty ))
461461
462462
463- # ===========================================================================
464- # (6b) Weight tying — genuine ONNX initializer sharing
465- # ===========================================================================
466-
467-
468- class TestWeightTying :
469- """Verify that tied weights (lm_head = embed_tokens) share one ONNX initializer."""
470-
471- def _build_tied_model (self ) -> tuple [ir .Model , CausalLMModel ]:
472- config = make_config (tie_word_embeddings = True )
473- module = CausalLMModel (config )
474- pkg = build_from_module (module , config )
475- return pkg ["model" ], module
476-
477- def test_tied_weights_share_single_initializer (self ):
478- """When tie_word_embeddings=True, lm_head.weight must NOT be a separate initializer."""
479- model , module = self ._build_tied_model ()
480- weight = torch .zeros (make_config ().vocab_size , make_config ().hidden_size )
481- sd = module .preprocess_weights ({"model.embed_tokens.weight" : weight })
482- assert sd ["model.embed_tokens.weight" ] is sd ["lm_head.weight" ], (
483- "preprocess_weights must produce the same tensor object for tied weights"
484- )
485-
486- apply_weights (model , sd )
487-
488- assert "lm_head.weight" not in model .graph .initializers , (
489- "lm_head.weight initializer should be removed — merged into embed_tokens"
490- )
491- assert "model.embed_tokens.weight" in model .graph .initializers , (
492- "embed_tokens initializer must remain as the canonical shared weight"
493- )
494-
495- def test_untied_weights_have_separate_initializers (self ):
496- """When tie_word_embeddings=False, both initializers remain independent."""
497- config = make_config (tie_word_embeddings = False )
498- module = CausalLMModel (config )
499- pkg = build_from_module (module , config )
500- model = pkg ["model" ]
501-
502- vocab_size = config .vocab_size
503- hidden_size = config .hidden_size
504- sd = {
505- "model.embed_tokens.weight" : torch .zeros (vocab_size , hidden_size ),
506- "lm_head.weight" : torch .zeros (vocab_size , hidden_size ),
507- }
508- apply_weights (model , sd )
509-
510- assert "model.embed_tokens.weight" in model .graph .initializers
511- assert "lm_head.weight" in model .graph .initializers
512-
513- def test_tied_model_initializer_count_lower_than_untied (self ):
514- """Tied model must have fewer initializers than untied (one embed weight, not two)."""
515- from mobius ._testing import make_config as mc
516-
517- config_tied = mc (tie_word_embeddings = True )
518- module_tied = CausalLMModel (config_tied )
519- pkg_tied = build_from_module (module_tied , config_tied )
520- model_tied = pkg_tied ["model" ]
521- sd_tied = module_tied .preprocess_weights (
522- {
523- "model.embed_tokens.weight" : torch .zeros (
524- config_tied .vocab_size , config_tied .hidden_size
525- )
526- }
527- )
528- apply_weights (model_tied , sd_tied )
529-
530- config_untied = mc (tie_word_embeddings = False )
531- module_untied = CausalLMModel (config_untied )
532- pkg_untied = build_from_module (module_untied , config_untied )
533- model_untied = pkg_untied ["model" ]
534- sd_untied = {
535- "model.embed_tokens.weight" : torch .zeros (
536- config_untied .vocab_size , config_untied .hidden_size
537- ),
538- "lm_head.weight" : torch .zeros (config_untied .vocab_size , config_untied .hidden_size ),
539- }
540- apply_weights (model_untied , sd_untied )
541-
542- n_tied = len (model_tied .graph .initializers )
543- n_untied = len (model_untied .graph .initializers )
544- assert n_tied < n_untied , (
545- f"Tied model should have fewer initializers ({ n_tied } ) than untied ({ n_untied } )"
546- )
547-
548-
549463# ===========================================================================
550464# (6) build_from_module contract preservation
551465# ===========================================================================
0 commit comments