3737 infer_initial_time_step ,
3838 sample_decode ,
3939)
40- from axlearn .common .embedding import BaseEmbedding , TransformerTextEmbeddings
40+ from axlearn .common .embedding import TransformerTextEmbeddings
4141from axlearn .common .layers import Dropout , LayerNorm , set_dropout_rate_recursively
4242from axlearn .common .logit_modifiers import LogitsToLogitsFn
4343from axlearn .common .module import (
@@ -446,7 +446,7 @@ class Config(BaseLayer.Config):
446446 # explicitly.
447447 dropout_rate : float = 0.0
448448 # Vector from input ids table.
449- emb : BaseEmbedding .Config = TransformerTextEmbeddings .default_config ()
449+ emb : TransformerTextEmbeddings .Config = TransformerTextEmbeddings .default_config ()
450450 # Transformer model trunk.
451451 transformer : BaseStackedTransformerLayer .Config = StackedTransformerLayer .default_config ()
452452 # Layer norm applied to transformer output.
@@ -519,25 +519,25 @@ def _forward_for_mode(
519519 emb_batch = {** input_batch }
520520 emb_batch ["inputs" ] = emb_batch ["input_ids" ]
521521
522+ x = self .emb (input_batch = emb_batch )
523+
522524 if mode == ForwardMode .FORWARD :
523- x = self .emb (input_batch = emb_batch )
524- x = self .transformer (
525- x ,
526- self_attention_logit_biases = self_attention_logit_biases ,
527- target_segment_ids = input_segment_ids ,
528- target_positions = positions ,
529- cross_attention_data = cross_attention_data ,
530- cross_attention_logit_biases = cross_attention_logit_biases ,
525+ transformer_state , x = (
526+ None ,
527+ self .transformer (
528+ x ,
529+ self_attention_logit_biases = self_attention_logit_biases ,
530+ target_segment_ids = input_segment_ids ,
531+ target_positions = positions ,
532+ cross_attention_data = cross_attention_data ,
533+ cross_attention_logit_biases = cross_attention_logit_biases ,
534+ ),
531535 )
532- cached_states = None
533536 elif mode == ForwardMode .INIT_STATES :
534537 assert cached_states is not None
535538 if input_segment_ids is not None :
536539 raise ValueError ("input_segment_ids is not supported in INIT_STATES." )
537- cached_states ["emb" ], x = self .emb .extend_step (
538- cached_states = cached_states ["emb" ], input_batch = emb_batch
539- )
540- cached_states ["transformer_state" ], x = self .transformer .init_states (
540+ transformer_state , x = self .transformer .init_states (
541541 time_step = cached_states ["transformer_state" ],
542542 data = x ,
543543 self_attention_logit_biases = self_attention_logit_biases ,
@@ -548,10 +548,7 @@ def _forward_for_mode(
548548 assert cached_states is not None
549549 if input_segment_ids is not None :
550550 raise ValueError ("input_segment_ids is not supported in EXTEND_STEP." )
551- cached_states ["emb" ], x = self .emb .extend_step (
552- cached_states = cached_states ["emb" ], input_batch = emb_batch
553- )
554- cached_states ["transformer_state" ], x = self .transformer .extend_step (
551+ transformer_state , x = self .transformer .extend_step (
555552 cached_states = cached_states ["transformer_state" ],
556553 data = x ,
557554 self_attention_logit_biases = self_attention_logit_biases ,
@@ -591,7 +588,7 @@ def _forward_for_mode(
591588 logits = self ._output_logits_modifier (logits )
592589 logits = with_sharding_constraint (logits , PartitionSpec (* self .config .logits_partition_spec ))
593590 # TODO(markblee): Rename to just "transformer". "transformer_state" is a bit redundant.
594- return cached_states , dict (logits = logits , hidden_states = x )
591+ return dict ( transformer_state = transformer_state ) , dict (logits = logits , hidden_states = x )
595592
596593 def forward (
597594 self ,
@@ -650,14 +647,12 @@ def init_states(
650647 ) -> NestedTensor :
651648 """See `BaseDecoder.init_states` for details."""
652649 cfg : Decoder .Config = self .config
653- emb = self .emb .init_states (batch_size = batch_size , dtype = dtype )
654- transformer_state , _ = self .transformer .init_states (
650+ init_state , _ = self .transformer .init_states (
655651 time_step = None ,
656652 data = TensorSpec ([batch_size , max_sequence_length , cfg .dim ], dtype = dtype ),
657653 )
658654 return dict (
659- emb = emb ,
660- transformer_state = transformer_state ,
655+ transformer_state = init_state ,
661656 input_ids = jnp .full (
662657 (batch_size , max_sequence_length ), cfg .pad_token_id , dtype = jnp .int32
663658 ),
@@ -682,14 +677,13 @@ def prefill_states(
682677 See `BaseDecoder.prefill_states` for details.
683678 """
684679 validate_contains_paths (input_batch , paths = ["input_ids" ])
685- input_ids : Tensor = input_batch ["input_ids" ]
680+ input_ids = input_batch ["input_ids" ]
686681 input_segment_ids = input_batch .get ("input_segment_ids" , None )
687682 positions = input_batch .get ("positions" , None )
688683
689- emb = self .emb .init_states (batch_size = input_ids .shape [0 ], dtype = self .dtype ())
690684 states , outputs = self ._forward_for_mode (
691685 mode = ForwardMode .INIT_STATES ,
692- cached_states = dict (emb = emb , transformer_state = time_step ),
686+ cached_states = dict (transformer_state = time_step ),
693687 input_batch = input_batch ,
694688 # TODO(markblee): Consider supporting packed inputs for more efficient prefilling.
695689 self_attention_logit_biases = self .compute_attention_logit_biases (
@@ -754,13 +748,14 @@ def extend_step(
754748 cached_states = cached_states ,
755749 ** kwargs ,
756750 )
757- updated_states . update (
751+ updated_states = dict (
758752 input_ids = updated_inputs ,
759753 # There are some non-greedy DFS/BFS and sliding attention algorithms that
760754 # recursively search through potentials.
761755 # They backtrace to some anchor time step after exploring for t steps.
762756 # This requires tracking time_step separately from the attention time_step.
763757 time_step = cached_states ["time_step" ] + 1 ,
758+ ** updated_states ,
764759 )
765760 return updated_states , outputs
766761
0 commit comments