| 1206 | |
| 1207 | |
| 1208 | class PromptLookupDecoding(DraftProvider): |
| 1209 | def __init__(self, max_ngram_size: int = 2, num_pred_tokens: int = 10) -> None: |
| 1210 | self._max_ngram_size = max_ngram_size |
| 1211 | self._num_pred_tokens = num_pred_tokens |
| 1212 | |
| 1213 | def draft( |
| 1214 | self, |
| 1215 | input_ids: np.ndarray, |
| 1216 | /, |
| 1217 | *, |
| 1218 | seq_id: int, |
| 1219 | max_tokens: Optional[int], |
| 1220 | ) -> np.ndarray: |
| 1221 | input_length = input_ids.shape[0] |
| 1222 | if input_length < 2: |
| 1223 | return np.array([], dtype=np.intc) |
| 1224 | num_pred_tokens = self._num_pred_tokens |
| 1225 | if max_tokens is not None: |
| 1226 | num_pred_tokens = min(num_pred_tokens, max_tokens) |
| 1227 | if num_pred_tokens <= 0: |
| 1228 | return np.array([], dtype=np.intc) |
| 1229 | max_ngram_size = min(self._max_ngram_size, input_length - 1) |
| 1230 | for ngram_size in range(max_ngram_size, 0, -1): |
| 1231 | windows = np.lib.stride_tricks.sliding_window_view(input_ids, (ngram_size,)) |
| 1232 | ngram = input_ids[-ngram_size:] |
| 1233 | matches = np.all(windows == ngram, axis=1) |
| 1234 | match_indices = np.nonzero(matches)[0] |
| 1235 | for index in match_indices: |
| 1236 | start = index + ngram_size |
| 1237 | if start >= input_length: |
| 1238 | continue |
| 1239 | end = min(start + num_pred_tokens, input_length) |
| 1240 | if start < end: |
| 1241 | return input_ids[start:end].astype(np.intc, copy=False) |
| 1242 | return np.array([], dtype=np.intc) |
| 1243 | |
| 1244 | |
| 1245 | class MTPDraftProvider(DraftProvider): |
no outgoing calls
no test coverage detected
searching dependent graphs…