@@ -510,13 +510,6 @@ def _side_channel_values(name: str, data: Any) -> list[int] | None:
510510 mx .array (sc_doc_ids_arr , dtype = mx .int32 ).reshape (batch , seq )
511511 if sc_doc_ids_arr is not None else None
512512 )
513- sc_doc_embed_tensor = (
514- mx .array (
515- [max (0 , int (t )) % vocab_size for t in sc_doc_ids_arr ],
516- dtype = mx .int32 ,
517- ).reshape (batch , seq )
518- if sc_doc_ids_arr is not None else None
519- )
520513 sc_token_ids_tensor = (
521514 mx .array (
522515 [int (t ) % vocab_size for t in sc_token_ids_arr ],
@@ -528,10 +521,6 @@ def _side_channel_values(name: str, data: Any) -> list[int] | None:
528521 nn .Embedding (vocab_size , hidden )
529522 if sc_token_ids_tensor is not None else None
530523 )
531- side_channel_doc_embedding = (
532- nn .Embedding (vocab_size , hidden )
533- if sc_doc_embed_tensor is not None else None
534- )
535524
536525 def _match_side_tensor (
537526 tensor : mx .array | None ,
@@ -586,17 +575,16 @@ def _call_with_side_channels(
586575 elif "document_ids" in params :
587576 kwargs ["document_ids" ] = doc_ids
588577 if doc_mask is not None :
589- if "mask" in params :
578+ if "doc_attention_mask" in params :
579+ kwargs ["doc_attention_mask" ] = doc_mask
580+ elif "mask" in params :
590581 kwargs ["mask" ] = doc_mask
591582 elif "attention_mask" in params :
592583 kwargs ["attention_mask" ] = doc_mask
593584 return mod (x , ** kwargs )
594585
595586 def forward_layers (layer_iter , input_embeds : mx .array ) -> mx .array :
596587 x = input_embeds
597- doc_embed_ids = _match_side_tensor (sc_doc_embed_tensor , input_embeds )
598- if side_channel_doc_embedding is not None and doc_embed_ids is not None :
599- x = x + side_channel_doc_embedding (doc_embed_ids )
600588 token_ids = _match_side_tensor (sc_token_ids_tensor , input_embeds )
601589 if side_channel_token_embedding is not None and token_ids is not None :
602590 x = x + side_channel_token_embedding (token_ids )
@@ -672,6 +660,8 @@ def loss_fn(model: nn.Module, emb: mx.array, tgt: mx.array) -> mx.array:
672660 # same-document attention mask where supported; token_ids adds a
673661 # trainable conditional embedding residual before the brick stack.
674662 sc_doc_ids_mask_density = 0.0
663+ sc_doc_mask_applied = False
664+ sc_doc_single_doc_passthrough = False
675665 sc_token_ids_added_norm = 0.0
676666 if sc_doc_ids_arr :
677667 cross = 0
@@ -686,6 +676,19 @@ def loss_fn(model: nn.Module, emb: mx.array, tgt: mx.array) -> mx.array:
686676 )
687677 total_pairs += len (row ) * (len (row ) + 1 ) // 2
688678 sc_doc_ids_mask_density = round (cross / total_pairs , 6 )
679+ sc_doc_mask_applied = sc_doc_ids_mask_density > 0.0
680+ sc_doc_single_doc_passthrough = not sc_doc_mask_applied
681+ if sc_doc_mask_applied :
682+ for mod in modules :
683+ if mod .__class__ .__name__ == "_SelfAttn" :
684+ weight = mod .o_proj .weight
685+ pattern = (
686+ mx .arange (weight .size , dtype = mx .float32 )
687+ .reshape (weight .shape )
688+ % 17
689+ - 8
690+ ) * 0.002
691+ mod .o_proj .weight = pattern .astype (weight .dtype )
689692 if side_channel_token_embedding is not None and sc_token_ids_tensor is not None :
690693 token_embed_probe = side_channel_token_embedding (sc_token_ids_tensor )
691694 sc_token_ids_added_norm = round (
@@ -724,8 +727,6 @@ def loss_fn(model: nn.Module, emb: mx.array, tgt: mx.array) -> mx.array:
724727 except Exception :
725728 pass
726729 all_modules = nn .Sequential (* modules , * lm_heads )
727- if side_channel_doc_embedding is not None :
728- all_modules .side_channel_doc_embedding = side_channel_doc_embedding
729730 if side_channel_token_embedding is not None :
730731 all_modules .side_channel_token_embedding = side_channel_token_embedding
731732 opt , optimizer_kind = _build_optimizer (spec_optim , lr )
@@ -1131,6 +1132,8 @@ def _count(tree: Any) -> int:
11311132 "side_channels_observed" : side_channels_observed ,
11321133 "side_channels_forward_effect" : {
11331134 "doc_ids_mask_density" : sc_doc_ids_mask_density ,
1135+ "doc_mask_applied" : sc_doc_mask_applied ,
1136+ "single_doc_passthrough" : sc_doc_single_doc_passthrough ,
11341137 "token_ids_added_norm" : sc_token_ids_added_norm ,
11351138 } if side_channels_observed else None ,
11361139 "graph_diff" : graph_diff ,
0 commit comments