MCPcopy Create free account
hub / github.com/MiniMax-AI/VTP / _forward_list

Method _forward_list

vtp/models/layers/block.py:235–298  ·  view source on GitHub ↗
(self, x_list: List[Tensor], rope_list=None, drop_ratio: Optional[float] = None)

Source from the content-addressed store, hash-verified

233 return x_ffn
234
235 def _forward_list(self, x_list: List[Tensor], rope_list=None, drop_ratio: Optional[float] = None) -> List[Tensor]:
236 b_list = [x.shape[0] for x in x_list]
237 effective_drop_ratio = drop_ratio if drop_ratio is not None else self.sample_drop_ratio
238 if self.training and effective_drop_ratio > 0.0:
239 branges_scales_1 = [get_branges_scales(x, effective_drop_ratio) for x in x_list]
240 indices_1_list = [br for br, _ in branges_scales_1]
241 residual_scale_factors = [sc for _, sc in branges_scales_1]
242 x_subset_1_list = [x[indices_1] for x, indices_1 in zip(x_list, indices_1_list)]
243
244 if rope_list is not None:
245 rope_subset_list = [
246 self._maybe_index_rope(rope, indices_1) for rope, indices_1 in zip(rope_list, indices_1_list)
247 ]
248 else:
249 rope_subset_list = rope_list
250
251 flattened, shapes, num_tokens = cat_keep_shapes(x_subset_1_list)
252 norm1 = uncat_with_shapes(self.norm1(flattened), shapes, num_tokens)
253 residual_1_list = self.attn.forward_list(norm1, rope_list=rope_subset_list)
254
255 x_attn_list = [
256 torch.index_add(
257 x,
258 dim=0,
259 source=self.ls1(residual_1),
260 index=indices_1,
261 alpha=residual_scale_factor,
262 )
263 for x, residual_1, indices_1, residual_scale_factor in zip(
264 x_list, residual_1_list, indices_1_list, residual_scale_factors
265 )
266 ]
267
268 branges_scales_2 = [get_branges_scales(x_attn, effective_drop_ratio) for x_attn in x_attn_list]
269 indices_2_list = [br for br, _ in branges_scales_2]
270 residual_scale_factors_2 = [sc for _, sc in branges_scales_2]
271 x_subset_2_list = [x[indices_2] for x, indices_2 in zip(x_attn_list, indices_2_list)]
272 flattened, shapes, num_tokens = cat_keep_shapes(x_subset_2_list)
273 norm2_flat = self.norm2(flattened)
274 norm2_list = uncat_with_shapes(norm2_flat, shapes, num_tokens)
275
276 residual_2_list = self.mlp.forward_list(norm2_list)
277
278 x_ffn = [
279 torch.index_add(
280 x_attn,
281 dim=0,
282 source=self.ls2(residual_2),
283 index=indices_2,
284 alpha=residual_scale_factor,
285 )
286 for x_attn, residual_2, indices_2, residual_scale_factor in zip(
287 x_attn_list, residual_2_list, indices_2_list, residual_scale_factors_2
288 )
289 ]
290 else:
291 x_out = []
292 for x, rope in zip(x_list, rope_list):

Callers 1

forwardMethod · 0.95

Calls 5

_maybe_index_ropeMethod · 0.95
get_branges_scalesFunction · 0.85
cat_keep_shapesFunction · 0.85
uncat_with_shapesFunction · 0.85
forward_listMethod · 0.45

Tested by

no test coverage detected