2323 "FullAttention" ,
2424 "FullDecoderLayer" ,
2525 "L2Clip" ,
26+ "MessageGate" ,
27+ "MessageInput" ,
2628 "RelativeLogits" ,
2729 "ScaledL2Clip" ,
2830 "ShortConvolution" ,
@@ -373,17 +375,19 @@ def __init__(self, args: TextArgs):
373375 def __call__ (
374376 self ,
375377 hidden_states : mx .array ,
378+ key_hidden_states : mx .array ,
379+ value_hidden_states : mx .array ,
376380 * ,
377381 cache : Any | None ,
378382 conv_states : list ,
379383 conv_mask : mx .array | None ,
380384 ) -> mx .array :
381385 kv_cache = cache [0 ] if cache is not None else None
382386 key_states , conv_states [0 ] = self .k_sconv (
383- self .k_proj (hidden_states ), conv_states [0 ], conv_mask
387+ self .k_proj (key_hidden_states ), conv_states [0 ], conv_mask
384388 )
385389 value_states , conv_states [1 ] = self .v_sconv (
386- self .v_proj (hidden_states ), conv_states [1 ], conv_mask
390+ self .v_proj (value_hidden_states ), conv_states [1 ], conv_mask
387391 )
388392 return self ._attend (
389393 hidden_states ,
@@ -541,16 +545,51 @@ def __call__(
541545 self , block_states : mx .array , partial_block_states : mx .array
542546 ) -> mx .array :
543547 dims = partial_block_states .shape [- 1 ]
544- query = self .query .reshape (- 1 , dims ).astype (mx .float32 )
545- scale = dims ** - 0.5
546- if _use_kernels ():
547- return depth_attention (
548- block_states , partial_block_states , query , scale = scale
549- )
550- stacked_states = mx .concatenate ([block_states , partial_block_states [None ]])
551- stacked_states = mx .moveaxis (stacked_states , 0 , - 2 )
552- weights = mx .softmax ((stacked_states @ query .T ) * scale , axis = - 2 , precise = True )
553- return weights .swapaxes (- 1 , - 2 ) @ stacked_states
548+ return _depth_attention (
549+ block_states , partial_block_states , self .query .reshape (- 1 , dims )
550+ )
551+
552+
553+ def _depth_attention (
554+ block_states : mx .array , partial_block_states : mx .array , query : mx .array
555+ ) -> mx .array :
556+ query = query .astype (mx .float32 )
557+ scale = partial_block_states .shape [- 1 ] ** - 0.5
558+ if _use_kernels ():
559+ return depth_attention (block_states , partial_block_states , query , scale = scale )
560+ stacked_states = mx .concatenate ([block_states , partial_block_states [None ]])
561+ stacked_states = mx .moveaxis (stacked_states , 0 , - 2 )
562+ weights = mx .softmax ((stacked_states @ query .T ) * scale , axis = - 2 , precise = True )
563+ return weights .swapaxes (- 1 , - 2 ) @ stacked_states
564+
565+
566+ class MessageGate (nn .Module ):
567+ def __init__ (self , dims : int , rank : int ):
568+ super ().__init__ ()
569+ self .down_proj = nn .Linear (2 * dims , rank , bias = False )
570+ self .forget_gate_proj = nn .Linear (rank , dims , bias = False )
571+ self .message_gate_proj = nn .Linear (rank , dims , bias = False )
572+
573+ def __call__ (self , hidden_states : mx .array , message : mx .array ) -> mx .array :
574+ states = mx .concatenate (
575+ [unweighted_rms_norm (hidden_states ), unweighted_rms_norm (message )],
576+ axis = - 1 ,
577+ )
578+ states = self .down_proj (states .astype (self .down_proj .weight .dtype ))
579+ states = states .astype (mx .float32 )
580+ forget_gate = mx .tanh (self .forget_gate_proj (states ))
581+ message_gate = mx .tanh (self .message_gate_proj (states ))
582+ return (1 - forget_gate ) * hidden_states + message_gate * message
583+
584+
585+ class MessageInput (nn .Module ):
586+ def __init__ (self , dims : int , num_looped_layers : int ):
587+ super ().__init__ ()
588+ # The query, key, value and mlp queries of each looped layer.
589+ self .query = mx .zeros ((num_looped_layers , 4 , dims ), mx .float32 )
590+
591+ def __call__ (self , bank : tuple , loop_layer_idx : int ) -> mx .array :
592+ return _depth_attention (* bank , self .query [loop_layer_idx ])
554593
555594
556595class DecoderLayer (nn .Module ):
@@ -567,6 +606,20 @@ def __init__(self, args: TextArgs, layer_idx: int, *, num_queries: int):
567606 kernel_size = args .sconv_kernel_size
568607 self .post_attention_convolution = ShortConvolution (dims , kernel_size )
569608 self .post_mlp_convolution = ShortConvolution (dims , kernel_size )
609+ if layer_idx in args .loop_core :
610+ rank = args .loop_config ["gate_rank" ]
611+ self .q_message_gate = MessageGate (dims , rank )
612+ self .k_message_gate = MessageGate (dims , rank )
613+ self .v_message_gate = MessageGate (dims , rank )
614+ self .mlp_message_gate = MessageGate (dims , rank )
615+
616+ def _mix_messages (self , states : tuple , messages : mx .array ) -> tuple :
617+ q_states , k_states , v_states = states
618+ return (
619+ self .q_message_gate (q_states , messages [..., 0 , :]),
620+ self .k_message_gate (k_states , messages [..., 1 , :]),
621+ self .v_message_gate (v_states , messages [..., 2 , :]),
622+ )
570623
571624 def __call__ (
572625 self ,
@@ -575,6 +628,7 @@ def __call__(
575628 * ,
576629 cache : Any | None = None ,
577630 conv_mask : mx .array | None = None ,
631+ messages : mx .array | None = None ,
578632 ) -> tuple [mx .array , mx .array ]:
579633 dtype = hidden_states .dtype
580634 conv = cache [1 ] if cache is not None else None
@@ -586,7 +640,7 @@ def __call__(
586640 if self .attention_input is not None :
587641 attention_inputs = self .attention_input (block_states , normed_residual )
588642 hidden_states = self ._self_attention (
589- hidden_states , attention_inputs , cache , conv_states , conv_mask
643+ hidden_states , attention_inputs , cache , conv_states , conv_mask , messages
590644 )
591645 hidden_states , conv_states [2 ] = self .post_attention_convolution (
592646 hidden_states , conv_states [2 ], conv_mask
@@ -604,6 +658,8 @@ def __call__(
604658
605659 normed_states = unweighted_rms_norm (hidden_states )
606660 hidden_states = self .mlp_input (block_states , normed_states )[..., 0 , :]
661+ if messages is not None :
662+ hidden_states = self .mlp_message_gate (hidden_states , messages [..., 3 , :])
607663 hidden_states = self .mlp (
608664 self .post_attention_layernorm (hidden_states ).astype (dtype )
609665 )
@@ -627,13 +683,18 @@ def __init__(self, args: TextArgs, layer_idx: int):
627683 self .input_layernorm = nn .RMSNorm (args .hidden_size , eps = args .rms_norm_eps )
628684
629685 def _self_attention (
630- self , hidden_states , attention_inputs , cache , conv_states , conv_mask
686+ self , hidden_states , attention_inputs , cache , conv_states , conv_mask , messages
631687 ):
632688 dtype = hidden_states .dtype
633689 if attention_inputs is not None :
634690 hidden_states = attention_inputs [..., 0 , :]
691+ if messages is None :
692+ states = (self .input_layernorm (hidden_states ).astype (dtype ),) * 3
693+ else :
694+ states = self ._mix_messages ((hidden_states ,) * 3 , messages )
695+ states = [self .input_layernorm (s ).astype (dtype ) for s in states ]
635696 return self .self_attn (
636- self . input_layernorm ( hidden_states ). astype ( dtype ) ,
697+ * states ,
637698 cache = cache ,
638699 conv_states = conv_states ,
639700 conv_mask = conv_mask ,
@@ -650,7 +711,7 @@ def __init__(self, args: TextArgs, layer_idx: int):
650711 self .v_input_layernorm = nn .RMSNorm (args .hidden_size , eps = eps )
651712
652713 def _self_attention (
653- self , hidden_states , attention_inputs , cache , conv_states , conv_mask
714+ self , hidden_states , attention_inputs , cache , conv_states , conv_mask , messages
654715 ):
655716 dtype = hidden_states .dtype
656717 if attention_inputs is None :
@@ -659,6 +720,10 @@ def _self_attention(
659720 query_states , key_states , value_states = (
660721 attention_inputs [..., i , :] for i in range (3 )
661722 )
723+ if messages is not None :
724+ query_states , key_states , value_states = self ._mix_messages (
725+ (query_states , key_states , value_states ), messages
726+ )
662727 return self .self_attn (
663728 self .q_input_layernorm (query_states ).astype (dtype ),
664729 self .k_input_layernorm (key_states ).astype (dtype ),
0 commit comments