| 533 | return clip_extra_context_tokens |
| 534 | |
| 535 | class PositionalEncoding(nn.Module): |
| 536 | def __init__(self, d_model, max_len=5000): |
| 537 | super(PositionalEncoding, self).__init__() |
| 538 | |
| 539 | pe = torch.zeros(max_len, d_model) |
| 540 | position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1) |
| 541 | div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-np.log(10000.0) / d_model)) |
| 542 | pe[:, 0::2] = torch.sin(position * div_term) |
| 543 | pe[:, 1::2] = torch.cos(position * div_term) |
| 544 | pe = pe.unsqueeze(0).transpose(0, 1) |
| 545 | |
| 546 | self.register_buffer('pe', pe) |
| 547 | |
| 548 | def forward(self, x): |
| 549 | # x: [B, T, C] |
| 550 | x = x.permute(1, 0, 2) |
| 551 | x = x + self.pe[:x.size(0), :] |
| 552 | x = x.permute(1, 0, 2) |
| 553 | return x |
| 554 | |
| 555 | class WanModelTM2M(nn.Module): |
| 556 | |