| 244 | |
| 245 | |
| 246 | class DecoderBlock(nn.Module): |
| 247 | |
| 248 | def __init__( |
| 249 | self, |
| 250 | dim, |
| 251 | num_heads, |
| 252 | mlp_ratio=4.0, |
| 253 | qkv_bias=False, |
| 254 | drop=0.0, |
| 255 | attn_drop=0.0, |
| 256 | drop_path=0.0, |
| 257 | act_layer=nn.GELU, |
| 258 | norm_layer=nn.LayerNorm, |
| 259 | norm_mem=True, |
| 260 | rope=None, |
| 261 | ): |
| 262 | super().__init__() |
| 263 | self.norm1 = norm_layer(dim) |
| 264 | self.attn = Attention( |
| 265 | dim, |
| 266 | rope=rope, |
| 267 | num_heads=num_heads, |
| 268 | qkv_bias=qkv_bias, |
| 269 | attn_drop=attn_drop, |
| 270 | proj_drop=drop, |
| 271 | ) |
| 272 | self.cross_attn = CrossAttention( |
| 273 | dim, |
| 274 | rope=rope, |
| 275 | num_heads=num_heads, |
| 276 | qkv_bias=qkv_bias, |
| 277 | attn_drop=attn_drop, |
| 278 | proj_drop=drop, |
| 279 | ) |
| 280 | self.drop_path = DropPath(drop_path) if drop_path > 0.0 else nn.Identity() |
| 281 | self.norm2 = norm_layer(dim) |
| 282 | self.norm3 = norm_layer(dim) |
| 283 | mlp_hidden_dim = int(dim * mlp_ratio) |
| 284 | self.mlp = Mlp( |
| 285 | in_features=dim, |
| 286 | hidden_features=mlp_hidden_dim, |
| 287 | act_layer=act_layer, |
| 288 | drop=drop, |
| 289 | ) |
| 290 | self.norm_y = norm_layer(dim) if norm_mem else nn.Identity() |
| 291 | |
| 292 | def forward(self, x, y, xpos, ypos): |
| 293 | x = x + self.drop_path(self.attn(self.norm1(x), xpos)) |
| 294 | y_ = self.norm_y(y) |
| 295 | x = x + self.drop_path(self.cross_attn(self.norm2(x), y_, y_, xpos, ypos)) |
| 296 | x = x + self.drop_path(self.mlp(self.norm3(x))) |
| 297 | return x, y |
| 298 | |
| 299 | |
| 300 | class CustomDecoderBlock(nn.Module): |
no outgoing calls
no test coverage detected