| 85 | return self.final_op(rb_res, x_size) |
| 86 | |
| 87 | class BasicLayer(nn.Module): |
| 88 | def __init__(self, embed_dim, depth, num_heads, |
| 89 | init_value: float, heads_range: float, |
| 90 | ffn_dim=96, drop_path=0., norm_layer=nn.LayerNorm, |
| 91 | use_checkpoint=False, |
| 92 | layerscale=False, layer_init_values=1e-5): |
| 93 | |
| 94 | super().__init__() |
| 95 | self.embed_dim = embed_dim |
| 96 | self.depth = depth |
| 97 | self.use_checkpoint = use_checkpoint |
| 98 | |
| 99 | self.Relpos = DynRelPos2d(embed_dim, num_heads, init_value, heads_range) |
| 100 | |
| 101 | # build blocks |
| 102 | self.blocks = nn.ModuleList([ |
| 103 | ApertureAttentionBlock(embed_dim=embed_dim, num_heads=num_heads, ffn_dim=ffn_dim, |
| 104 | layerscale=layerscale, norm_layer=norm_layer, layer_init_values=layer_init_values, |
| 105 | drop_path=drop_path[i] if isinstance(drop_path, list) else drop_path) |
| 106 | for i in range(depth)]) |
| 107 | |
| 108 | def forward(self, x, att_range_factor=None): |
| 109 | b, h, w, d = x.size() |
| 110 | |
| 111 | |
| 112 | rel_pos = self.Relpos((h, w), range_factor=att_range_factor) |
| 113 | |
| 114 | for blk in self.blocks: |
| 115 | if self.use_checkpoint: |
| 116 | tmp_blk = partial(blk, attention_rel_pos=rel_pos) |
| 117 | x = checkpoint.checkpoint(tmp_blk, x) |
| 118 | else: |
| 119 | x = blk(x, attention_rel_pos=rel_pos) |
| 120 | |
| 121 | return x |
| 122 | |
| 123 | class ApertureAttentionBlock(nn.Module): |
| 124 | |