Add positional encoding. Args: x (torch.Tensor): Input. Its shape is (batch, time, ...) offset (int, torch.tensor): position offset Returns: torch.Tensor: Encoded tensor. Its shape is (batch, time, ...) torch.Tensor: for compatibility
(self,
x: torch.Tensor,
offset: Union[int, torch.Tensor] = 0)
| 57 | self.pe = self.pe.unsqueeze(0) |
| 58 | |
| 59 | def forward(self, |
| 60 | x: torch.Tensor, |
| 61 | offset: Union[int, torch.Tensor] = 0) \ |
| 62 | -> Tuple[torch.Tensor, torch.Tensor]: |
| 63 | """Add positional encoding. |
| 64 | |
| 65 | Args: |
| 66 | x (torch.Tensor): Input. Its shape is (batch, time, ...) |
| 67 | offset (int, torch.tensor): position offset |
| 68 | |
| 69 | Returns: |
| 70 | torch.Tensor: Encoded tensor. Its shape is (batch, time, ...) |
| 71 | torch.Tensor: for compatibility to RelPositionalEncoding |
| 72 | """ |
| 73 | |
| 74 | self.pe = self.pe.to(x.device) |
| 75 | pos_emb = self.position_encoding(offset, x.size(1), False) |
| 76 | x = x * self.xscale + pos_emb |
| 77 | return self.dropout(x), self.dropout(pos_emb) |
| 78 | |
| 79 | def position_encoding(self, |
| 80 | offset: Union[int, torch.Tensor], |
nothing calls this directly
no test coverage detected