(self, B: int, H: int, W: int)
| 114 | self.eps = 1e-6 |
| 115 | |
| 116 | def forward(self, B: int, H: int, W: int): |
| 117 | device = self.token_projection.weight.device |
| 118 | y_embed = torch.arange(1, H+1, dtype=torch.float32, device=device).unsqueeze(1).repeat(1, 1, W) |
| 119 | x_embed = torch.arange(1, W+1, dtype=torch.float32, device=device).repeat(1, H, 1) |
| 120 | y_embed = y_embed / (y_embed[:, -1:, :] + self.eps) * self.scale |
| 121 | x_embed = x_embed / (x_embed[:, :, -1:] + self.eps) * self.scale |
| 122 | dim_t = torch.arange(self.hidden_dim, dtype=torch.float32, device=device) |
| 123 | dim_t = self.temperature ** (2 * torch.div(dim_t, 2, rounding_mode='floor') / self.hidden_dim) |
| 124 | pos_x = x_embed[:, :, :, None] / dim_t |
| 125 | pos_y = y_embed[:, :, :, None] / dim_t |
| 126 | pos_x = torch.stack([pos_x[:, :, :, 0::2].sin(), pos_x[:, :, :, 1::2].cos()], dim=4).flatten(3) |
| 127 | pos_y = torch.stack([pos_y[:, :, :, 0::2].sin(), pos_y[:, :, :, 1::2].cos()], dim=4).flatten(3) |
| 128 | pos = torch.cat((pos_y, pos_x), dim=3).permute(0, 3, 1, 2) |
| 129 | pos = self.token_projection(pos) |
| 130 | return pos.repeat(B, 1, 1, 1) # (B, C, H, W) |
| 131 | |
| 132 | |
| 133 | def conv3x3(in_planes, out_planes, stride=1): |
nothing calls this directly
no outgoing calls
no test coverage detected