Forward function. Args: x: input features with shape of (B, H, W, C)
(self, x)
| 87 | ) |
| 88 | |
| 89 | def forward(self, x): |
| 90 | """ Forward function. |
| 91 | |
| 92 | Args: |
| 93 | x: input features with shape of (B, H, W, C) |
| 94 | """ |
| 95 | B, nH, nW, C = x.shape |
| 96 | x = self.f(x) |
| 97 | x = x.permute(0, 3, 1, 2).contiguous() |
| 98 | q, ctx, gates = torch.split(x, (C, C, self.focal_level+1), 1) |
| 99 | |
| 100 | ctx_all = 0 |
| 101 | for l in range(self.focal_level): |
| 102 | ctx = self.focal_layers[l](ctx) |
| 103 | ctx_all = ctx_all + ctx*gates[:, l:l+1] |
| 104 | ctx_global = self.act(ctx.mean(2, keepdim=True).mean(3, keepdim=True)) |
| 105 | ctx_all = ctx_all + ctx_global*gates[:,self.focal_level:] |
| 106 | |
| 107 | if self.scaling_modulator: |
| 108 | ctx_all = ctx_all / (self.focal_level + 1) |
| 109 | |
| 110 | x_out = q * self.h(ctx_all) |
| 111 | x_out = x_out.permute(0, 2, 3, 1).contiguous() |
| 112 | if self.use_postln_in_modulation: |
| 113 | x_out = self.ln(x_out) |
| 114 | x_out = self.proj(x_out) |
| 115 | x_out = self.proj_drop(x_out) |
| 116 | return x_out |
| 117 | |
| 118 | class FocalModulationBlock(nn.Module): |
| 119 | """ Focal Modulation Block. |
nothing calls this directly
no outgoing calls
no test coverage detected