MCPcopy Create free account
hub / github.com/GeWu-Lab/AnyTouch2 / forward_nested

Method forward_nested

model/layers/block.py:209–247  ·  view source on GitHub ↗

x_list contains a list of tensors to nest together and run

(self, x_list: List[Tensor])

Source from the content-addressed store, hash-verified

207
208class NestedTensorBlock(Block):
209 def forward_nested(self, x_list: List[Tensor]) -> List[Tensor]:
210 """
211 x_list contains a list of tensors to nest together and run
212 """
213 assert isinstance(self.attn, MemEffAttention)
214
215 if self.training and self.sample_drop_ratio > 0.0:
216
217 def attn_residual_func(x: Tensor, attn_bias=None) -> Tensor:
218 return self.attn(self.norm1(x), attn_bias=attn_bias)
219
220 def ffn_residual_func(x: Tensor, attn_bias=None) -> Tensor:
221 return self.mlp(self.norm2(x))
222
223 x_list = drop_add_residual_stochastic_depth_list(
224 x_list,
225 residual_func=attn_residual_func,
226 sample_drop_ratio=self.sample_drop_ratio,
227 scaling_vector=self.ls1.gamma if isinstance(self.ls1, LayerScale) else None,
228 )
229 x_list = drop_add_residual_stochastic_depth_list(
230 x_list,
231 residual_func=ffn_residual_func,
232 sample_drop_ratio=self.sample_drop_ratio,
233 scaling_vector=self.ls2.gamma if isinstance(self.ls1, LayerScale) else None,
234 )
235 return x_list
236 else:
237
238 def attn_residual_func(x: Tensor, attn_bias=None) -> Tensor:
239 return self.ls1(self.attn(self.norm1(x), attn_bias=attn_bias))
240
241 def ffn_residual_func(x: Tensor, attn_bias=None) -> Tensor:
242 return self.ls2(self.mlp(self.norm2(x)))
243
244 attn_bias, x = get_attn_bias_and_cat(x_list)
245 x = x + attn_residual_func(x, attn_bias=attn_bias)
246 x = x + ffn_residual_func(x)
247 return attn_bias.split(x)
248
249 def forward(self, x_or_x_list):
250 if isinstance(x_or_x_list, Tensor):

Callers 1

forwardMethod · 0.95

Tested by

no test coverage detected