ids: [B, L] of torch.LongTensor.
(self, ids)
| 120 | self.norm = nn.LayerNorm(dim, eps=eps) |
| 121 | |
| 122 | def forward(self, ids): |
| 123 | """ |
| 124 | ids: [B, L] of torch.LongTensor. |
| 125 | """ |
| 126 | b, s = ids.shape |
| 127 | mask = ids.ne(self.pad_id).long() |
| 128 | |
| 129 | # embeddings |
| 130 | x = self.token_embedding(ids) + \ |
| 131 | self.type_embedding(torch.zeros_like(ids)) + \ |
| 132 | self.pos_embedding(self.pad_id + torch.cumsum(mask, dim=1) * mask) |
| 133 | if self.post_norm: |
| 134 | x = self.norm(x) |
| 135 | x = self.dropout(x) |
| 136 | |
| 137 | # blocks |
| 138 | mask = torch.where( |
| 139 | mask.view(b, 1, 1, s).gt(0), 0.0, |
| 140 | torch.finfo(x.dtype).min) |
| 141 | for block in self.blocks: |
| 142 | x = block(x, mask) |
| 143 | |
| 144 | # output |
| 145 | if not self.post_norm: |
| 146 | x = self.norm(x) |
| 147 | return x |
| 148 | |
| 149 | |
| 150 | def xlm_roberta_large(pretrained=False, |