(self)
| 255 | self.initialize_weights() |
| 256 | |
| 257 | def initialize_weights(self): |
| 258 | # Initialize transformer layers: |
| 259 | def _basic_init(module): |
| 260 | if isinstance(module, nn.Linear): |
| 261 | torch.nn.init.xavier_uniform_(module.weight) |
| 262 | if module.bias is not None: |
| 263 | nn.init.constant_(module.bias, 0) |
| 264 | self.apply(_basic_init) |
| 265 | |
| 266 | # Initialize (and freeze) pos_embed by sin-cos embedding: |
| 267 | pos_embed = get_2d_sincos_pos_embed(self.pos_embed.shape[-1], int(self.x_embedder.num_patches ** 0.5)) |
| 268 | self.pos_embed.data.copy_(torch.from_numpy(pos_embed).float().unsqueeze(0)) |
| 269 | |
| 270 | temp_embed = get_1d_sincos_temp_embed(self.temp_embed.shape[-1], self.temp_embed.shape[-2]) |
| 271 | self.temp_embed.data.copy_(torch.from_numpy(temp_embed).float().unsqueeze(0)) |
| 272 | |
| 273 | # Initialize patch_embed like nn.Linear (instead of nn.Conv2d): |
| 274 | w = self.x_embedder.proj.weight.data |
| 275 | nn.init.xavier_uniform_(w.view([w.shape[0], -1])) |
| 276 | nn.init.constant_(self.x_embedder.proj.bias, 0) |
| 277 | |
| 278 | if self.extras == 2: |
| 279 | # Initialize label embedding table: |
| 280 | nn.init.normal_(self.y_embedder.embedding_table.weight, std=0.02) |
| 281 | |
| 282 | # Initialize timestep embedding MLP: |
| 283 | nn.init.normal_(self.t_embedder.mlp[0].weight, std=0.02) |
| 284 | nn.init.normal_(self.t_embedder.mlp[2].weight, std=0.02) |
| 285 | |
| 286 | # Zero-out adaLN modulation layers in Latte blocks: |
| 287 | for block in self.blocks: |
| 288 | nn.init.constant_(block.adaLN_modulation[-1].weight, 0) |
| 289 | nn.init.constant_(block.adaLN_modulation[-1].bias, 0) |
| 290 | |
| 291 | # Zero-out output layers: |
| 292 | nn.init.constant_(self.final_layer.adaLN_modulation[-1].weight, 0) |
| 293 | nn.init.constant_(self.final_layer.adaLN_modulation[-1].bias, 0) |
| 294 | nn.init.constant_(self.final_layer.linear.weight, 0) |
| 295 | nn.init.constant_(self.final_layer.linear.bias, 0) |
| 296 | |
| 297 | def unpatchify(self, x): |
| 298 | """ |
no test coverage detected