- context_len allows for a rolling window context, allowing each prediction window to potentially condition on some context :param token_list: list List of tokens to be PREDICTED :param max_seq_len: int max_seq_len of model (or max_seq_len we want to use) :par
(token_list, prefix_token, max_seq_len, context_len)
| 120 | |
| 121 | |
| 122 | def get_rolling_token_windows(token_list, prefix_token, max_seq_len, context_len): |
| 123 | """ |
| 124 | - context_len allows for a rolling window context, allowing each prediction window to potentially |
| 125 | condition on some context |
| 126 | |
| 127 | :param token_list: list |
| 128 | List of tokens to be PREDICTED |
| 129 | :param max_seq_len: int |
| 130 | max_seq_len of model (or max_seq_len we want to use) |
| 131 | :param context_len: int |
| 132 | Amount of desired token context for prediction. Needs to be at least 1. |
| 133 | :param prefix_token: token |
| 134 | Dummy token like <eos> so the first token has something to condition on |
| 135 | :return: generator |
| 136 | Generator of tuples |
| 137 | (input_tokens, pred_tokens) |
| 138 | Note: Score only the last len(pred_tokens) logits of the LM |
| 139 | """ |
| 140 | assert 1 <= context_len <= max_seq_len |
| 141 | if not token_list: |
| 142 | return |
| 143 | # +1 offset, going from input->preds |
| 144 | pred_len = max_seq_len - context_len + 1 |
| 145 | predicted = 0 |
| 146 | |
| 147 | # Special handling for first window: predict all tokens |
| 148 | first_seq_len = min(max_seq_len, len(token_list)) |
| 149 | yield ([prefix_token] + token_list[: first_seq_len - 1], token_list[:first_seq_len]) |
| 150 | predicted += first_seq_len |
| 151 | |
| 152 | while predicted < len(token_list): |
| 153 | window_pred_len = min(len(token_list) - predicted, pred_len) |
| 154 | window_end = predicted + window_pred_len |
| 155 | |
| 156 | yield ( |
| 157 | token_list[window_end - max_seq_len - 1 : window_end - 1], |
| 158 | token_list[window_end - window_pred_len : window_end], |
| 159 | ) |
| 160 | predicted += window_pred_len |
| 161 | |
| 162 | |
| 163 | def make_disjoint_window(pair): |
no outgoing calls
no test coverage detected