Add padding to sequences. Arguments: hidden_states: (total_nnz, ...), where total_nnz = number of tokens in selected in attention_mask. indices: (total_nnz) batch: int batch_size seqlen: int max sequence length Returns: hidden_states: (batch, seqlen,
(hidden_states: torch.Tensor, indices: torch.Tensor, batch: int, seqlen: int)
| 126 | |
| 127 | |
| 128 | def pad_input(hidden_states: torch.Tensor, indices: torch.Tensor, batch: int, seqlen: int) -> torch.Tensor: |
| 129 | """Add padding to sequences. |
| 130 | |
| 131 | Arguments: |
| 132 | hidden_states: (total_nnz, ...), where total_nnz = number of tokens in selected in attention_mask. |
| 133 | indices: (total_nnz) |
| 134 | batch: int batch_size |
| 135 | seqlen: int max sequence length |
| 136 | |
| 137 | Returns: |
| 138 | hidden_states: (batch, seqlen, ...) |
| 139 | """ |
| 140 | output = index_put_first_axis(hidden_states, indices, batch * seqlen) |
| 141 | return rearrange(output, "(b s) ... -> b s ...", b=batch) # type: ignore |
no outgoing calls