For getting encoding in a streaming fashion Attention!!!!! we apply dropout only once at the whole utterance level in a none streaming way, but will call this function several times with increasing input size in a streaming scenario, so the dropout will be a
(self,
offset: Union[int, torch.Tensor],
size: int,
apply_dropout: bool = True)
| 78 | return self.dropout(x), self.dropout(pos_emb) |
| 79 | |
| 80 | def position_encoding(self, |
| 81 | offset: Union[int, torch.Tensor], |
| 82 | size: int, |
| 83 | apply_dropout: bool = True) -> torch.Tensor: |
| 84 | """ For getting encoding in a streaming fashion |
| 85 | |
| 86 | Attention!!!!! |
| 87 | we apply dropout only once at the whole utterance level in a none |
| 88 | streaming way, but will call this function several times with |
| 89 | increasing input size in a streaming scenario, so the dropout will |
| 90 | be applied several times. |
| 91 | |
| 92 | Args: |
| 93 | offset (int or torch.tensor): start offset |
| 94 | size (int): required size of position encoding |
| 95 | |
| 96 | Returns: |
| 97 | torch.Tensor: Corresponding encoding |
| 98 | """ |
| 99 | # How to subscript a Union type: |
| 100 | # https://github.com/pytorch/pytorch/issues/69434 |
| 101 | if isinstance(offset, int): |
| 102 | assert offset + size <= self.max_len |
| 103 | pos_emb = self.pe[:, offset:offset + size] |
| 104 | elif isinstance(offset, torch.Tensor) and offset.dim() == 0: # scalar |
| 105 | assert offset + size <= self.max_len |
| 106 | pos_emb = self.pe[:, offset:offset + size] |
| 107 | else: # for batched streaming decoding on GPU |
| 108 | assert torch.max(offset) + size <= self.max_len |
| 109 | index = offset.unsqueeze(1) + \ |
| 110 | torch.arange(0, size).to(offset.device) # B X T |
| 111 | flag = index > 0 |
| 112 | # remove negative offset |
| 113 | index = index * flag |
| 114 | pos_emb = F.embedding(index, self.pe[0]) # B X T X d_model |
| 115 | |
| 116 | if apply_dropout: |
| 117 | pos_emb = self.dropout(pos_emb) |
| 118 | return pos_emb |
| 119 | |
| 120 | |
| 121 | class RelPositionalEncoding(PositionalEncoding): |