Score new token batch. Args: ys (torch.Tensor): torch.int64 prefix tokens (n_batch, ylen). states (List[Any]): Scorer states for prefix tokens. xs (torch.Tensor): The encoder feature that generates ys (n_batch, xlen, n_feat). Retu
(
self, ys: torch.Tensor, states: List[Any], xs: torch.Tensor
)
| 136 | raise NotImplementedError |
| 137 | |
| 138 | def batch_score( |
| 139 | self, ys: torch.Tensor, states: List[Any], xs: torch.Tensor |
| 140 | ) -> Tuple[torch.Tensor, List[Any]]: |
| 141 | """Score new token batch. |
| 142 | |
| 143 | Args: |
| 144 | ys (torch.Tensor): torch.int64 prefix tokens (n_batch, ylen). |
| 145 | states (List[Any]): Scorer states for prefix tokens. |
| 146 | xs (torch.Tensor): |
| 147 | The encoder feature that generates ys (n_batch, xlen, n_feat). |
| 148 | |
| 149 | Returns: |
| 150 | tuple[torch.Tensor, List[Any]]: Tuple of |
| 151 | batchfied scores for next token with shape of `(n_batch, n_vocab)` |
| 152 | and next state list for ys. |
| 153 | |
| 154 | """ |
| 155 | # merge states |
| 156 | n_batch = len(ys) |
| 157 | ys = self.embed(ys[:, -1:]) |
| 158 | |
| 159 | # workaround for remaining beam width of 1 |
| 160 | if type(states[0]) is list: |
| 161 | states = states[0] |
| 162 | |
| 163 | assert ys.size(1) == 1, ys.shape |
| 164 | ys = ys.squeeze(1) |
| 165 | |
| 166 | ys, states = self.decoder.step(ys, state=states, memory=xs) |
| 167 | logp = self.output(ys).log_softmax(dim=-1) |
| 168 | |
| 169 | states_list = [ |
| 170 | [state[b].unsqueeze(0) if state is not None else None for state in states] |
| 171 | for b in range(n_batch) |
| 172 | ] |
| 173 | |
| 174 | return logp, states_list |
no test coverage detected