MCPcopy Create free account
hub / github.com/OpenSparseLLMs/Linear-MoE / forward

Method forward

linear_moe/model/mixtral/model.py:159–222  ·  view source on GitHub ↗

Forward function of the GPT Model This function passes the input tensors through the embedding layer, and then the decoeder and finally into the post processing layer (optional). It either returns the Loss values if labels are given or the final hidden units

(
        self,
        input_ids: Tensor,
        position_ids: Tensor,
        attention_mask: Tensor,
        decoder_input: Tensor = None,
        labels: Tensor = None,
        inference_params: InferenceParams = None,
        packed_seq_params: PackedSeqParams = None,
        extra_block_kwargs: dict = None,
    )

Source from the content-addressed store, hash-verified

157 self.decoder.set_input_tensor(input_tensor[0])
158
159 def forward(
160 self,
161 input_ids: Tensor,
162 position_ids: Tensor,
163 attention_mask: Tensor,
164 decoder_input: Tensor = None,
165 labels: Tensor = None,
166 inference_params: InferenceParams = None,
167 packed_seq_params: PackedSeqParams = None,
168 extra_block_kwargs: dict = None,
169 ) -> Tensor:
170 """Forward function of the GPT Model This function passes the input tensors
171 through the embedding layer, and then the decoeder and finally into the post
172 processing layer (optional).
173
174 It either returns the Loss values if labels are given or the final hidden units
175 """
176 # If decoder_input is provided (not None), then input_ids and position_ids are ignored.
177 # Otherwise, apply embedding layer on input_ids and position_ids to get decoder_input.
178
179 # Decoder embedding.
180 if decoder_input is not None:
181 pass
182 elif self.pre_process:
183 decoder_input = self.embedding(input_ids=input_ids, position_ids=position_ids)
184 else:
185 # intermediate stage of pipeline
186 # decoder will get hidden_states from encoder.input_tensor
187 decoder_input = None
188
189 # Rotary positional embeddings (embedding is None for PP intermediate devices)
190 rotary_pos_emb = None
191 if self.position_embedding_type == 'rope':
192 rotary_seq_len = self.rotary_pos_emb.get_rotary_seq_len(
193 inference_params, self.decoder, decoder_input, self.config
194 )
195 rotary_pos_emb = self.rotary_pos_emb(rotary_seq_len)
196
197 # Run decoder.
198 hidden_states = self.decoder(
199 hidden_states=decoder_input,
200 attention_mask=attention_mask,
201 inference_params=inference_params,
202 rotary_pos_emb=rotary_pos_emb,
203 packed_seq_params=packed_seq_params,
204 **(extra_block_kwargs or {}),
205 )
206
207 if not self.post_process:
208 return hidden_states
209
210 # logits and loss
211 output_weight = None
212 if self.share_embeddings_and_output_weights:
213 output_weight = self.shared_embedding_or_output_weight()
214 logits, _ = self.output_layer(hidden_states, weight=output_weight)
215
216 if labels is None:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected