| 88 | |
| 89 | |
| 90 | class PatchEmbed(Module): |
| 91 | |
| 92 | def __init__(self, |
| 93 | img_size: int, |
| 94 | patch_size: int, |
| 95 | input_c: int, |
| 96 | output_c: int, |
| 97 | bias: bool = True, |
| 98 | dtype: trt.DataType = None): |
| 99 | super().__init__() |
| 100 | self.img_size = img_size |
| 101 | self.patch_size = patch_size |
| 102 | self.num_patches = (img_size // patch_size)**2 |
| 103 | self.proj = Conv2d(input_c, |
| 104 | output_c, |
| 105 | kernel_size=(patch_size, patch_size), |
| 106 | stride=(patch_size, patch_size), |
| 107 | bias=bias, |
| 108 | dtype=dtype) |
| 109 | |
| 110 | def forward(self, x): |
| 111 | assert x.shape[2] == self.img_size |
| 112 | assert x.shape[3] == self.img_size |
| 113 | x = self.proj(x) |
| 114 | x = x.flatten(2).transpose(1, 2) # NCHW -> NLC |
| 115 | return x |
| 116 | |
| 117 | |
| 118 | class DiTBlock(Module): |