| 303 | self.initialize_parameters() |
| 304 | |
| 305 | def initialize_parameters(self): |
| 306 | nn.init.normal_(self.token_embedding.weight, std=0.02) |
| 307 | nn.init.normal_(self.positional_embedding, std=0.01) |
| 308 | |
| 309 | if isinstance(self.visual, ModifiedResNet): |
| 310 | if self.visual.attnpool is not None: |
| 311 | std = self.visual.attnpool.c_proj.in_features ** -0.5 |
| 312 | nn.init.normal_(self.visual.attnpool.q_proj.weight, std=std) |
| 313 | nn.init.normal_(self.visual.attnpool.k_proj.weight, std=std) |
| 314 | nn.init.normal_(self.visual.attnpool.v_proj.weight, std=std) |
| 315 | nn.init.normal_(self.visual.attnpool.c_proj.weight, std=std) |
| 316 | |
| 317 | for resnet_block in [self.visual.layer1, self.visual.layer2, self.visual.layer3, self.visual.layer4]: |
| 318 | for name, param in resnet_block.named_parameters(): |
| 319 | if name.endswith("bn3.weight"): |
| 320 | nn.init.zeros_(param) |
| 321 | |
| 322 | proj_std = (self.transformer.width ** -0.5) * ((2 * self.transformer.layers) ** -0.5) |
| 323 | attn_std = self.transformer.width ** -0.5 |
| 324 | fc_std = (2 * self.transformer.width) ** -0.5 |
| 325 | for block in self.transformer.resblocks: |
| 326 | nn.init.normal_(block.attn.in_proj_weight, std=attn_std) |
| 327 | nn.init.normal_(block.attn.out_proj.weight, std=proj_std) |
| 328 | nn.init.normal_(block.mlp.c_fc.weight, std=fc_std) |
| 329 | nn.init.normal_(block.mlp.c_proj.weight, std=proj_std) |
| 330 | |
| 331 | if self.text_projection is not None: |
| 332 | nn.init.normal_(self.text_projection, std=self.transformer.width ** -0.5) |
| 333 | |
| 334 | def build_attention_mask(self): |
| 335 | # lazily create causal attention mask, with full attention between the vision tokens |