Patchifies and embeds the input tensor(s). Args: x (List[torch.Tensor] | torch.Tensor): The input tensor(s) to be patchified and embedded. Returns: Tuple[torch.Tensor, torch.Tensor, List[Tuple[int, int]], torch.Tensor]: A tuple containing the patchi
(self, x, freqs_cis)
| 301 | ) |
| 302 | |
| 303 | def forward(self, x, freqs_cis): |
| 304 | """ |
| 305 | Patchifies and embeds the input tensor(s). |
| 306 | |
| 307 | Args: |
| 308 | x (List[torch.Tensor] | torch.Tensor): The input tensor(s) to be patchified and embedded. |
| 309 | |
| 310 | Returns: |
| 311 | Tuple[torch.Tensor, torch.Tensor, List[Tuple[int, int]], torch.Tensor]: A tuple containing the patchified |
| 312 | and embedded tensor(s), the mask indicating the valid patches, the original image size(s), and the |
| 313 | frequency tensor(s). |
| 314 | """ |
| 315 | freqs_cis = freqs_cis.to(x[0].device) |
| 316 | patch_height = patch_width = self.patch_size |
| 317 | batch_size, channel, height, width = x.size() |
| 318 | height_tokens, width_tokens = height // patch_height, width // patch_width |
| 319 | |
| 320 | x = x.view(batch_size, channel, height_tokens, patch_height, width_tokens, patch_width).permute( |
| 321 | 0, 2, 4, 1, 3, 5 |
| 322 | ) |
| 323 | x = x.flatten(3) |
| 324 | x = self.proj(x) |
| 325 | x = x.flatten(1, 2) |
| 326 | |
| 327 | mask = torch.ones(x.shape[0], x.shape[1], dtype=torch.int32, device=x.device) |
| 328 | |
| 329 | return ( |
| 330 | x, |
| 331 | mask, |
| 332 | [(height, width)] * batch_size, |
| 333 | freqs_cis[:height_tokens, :width_tokens].flatten(0, 1).unsqueeze(0), |
| 334 | ) |
| 335 | |
| 336 | |
| 337 | class CogVideoXPatchEmbed(nn.Module): |