candidate_generator.py 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. # Copyright 2023 The HuggingFace Inc. team.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import copy
  15. import weakref
  16. from typing import TYPE_CHECKING, Any, Optional, cast
  17. import numpy as np
  18. import torch
  19. import torch.nn as nn
  20. from ..pytorch_utils import prune_linear_layer
  21. from ..utils import is_sklearn_available
  22. if is_sklearn_available():
  23. from sklearn.metrics import roc_curve
  24. from .logits_process import LogitsProcessorList, MinLengthLogitsProcessor, SuppressTokensLogitsProcessor
  25. if TYPE_CHECKING:
  26. from ..modeling_utils import PreTrainedModel
  27. from ..tokenization_utils_base import PreTrainedTokenizerBase
  28. from .configuration_utils import GenerationConfig
  29. class CandidateGenerator:
  30. """Abstract base class for all candidate generators that can be applied during assisted generation."""
  31. def get_candidates(self, input_ids: torch.LongTensor) -> tuple[torch.LongTensor, torch.FloatTensor]:
  32. """
  33. Fetches the candidates to be tried for the current input.
  34. Args:
  35. input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
  36. Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
  37. Return:
  38. `torch.LongTensor` of shape `(batch_size, candidate_length)` containing the candidate sequences to be
  39. assessed by the model and, optionally, a `torch.FloatTensor` of shape `(batch_size, candidate_length,
  40. vocabulary_size)` containing the logits associated to each candidate.
  41. """
  42. raise NotImplementedError(
  43. f"{self.__class__} is an abstract class. Only classes inheriting this class can call `get_candidates`."
  44. )
  45. def update_candidate_strategy(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, num_matches: int):
  46. """
  47. Updates the candidate generation strategy based on the outcomes.
  48. Args:
  49. input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
  50. Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
  51. scores (`torch.FloatTensor` of shape `(batch_size, candidate_length, config.vocab_size)`):
  52. Prediction scores of a language modeling head. These can be logits for each vocabulary when not using
  53. beam search or log softmax for each vocabulary token when using beam search
  54. num_matches (`int`):
  55. The number of matches between the candidate sequences and the model predictions.
  56. """
  57. raise NotImplementedError(
  58. f"{self.__class__} is an abstract class. Only classes inheriting this class can call "
  59. "`update_candidate_strategy`."
  60. )
  61. class AssistedCandidateGenerator(CandidateGenerator):
  62. """
  63. `CandidateGenerator` class to be used for assisted generation and speculative decoding. This class generates
  64. candidates through the use of a smaller model. Read the following blog post for more information:
  65. https://huggingface.co/blog/assisted-generation
  66. Args:
  67. input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
  68. Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
  69. assistant_model (`PreTrainedModel`):
  70. The model to be used for generating candidates. This model should be smaller than the main model.
  71. generation_config (`~generation.GenerationConfig`, *optional*):
  72. The generation configuration to be used as base parametrization for the generation call.
  73. logits_processor (`LogitsProcessorList`):
  74. An instance of [`LogitsProcessorList`]. List of instances of class derived from [`LogitsProcessor`]
  75. used to modify the prediction scores of the language modeling head applied at each generation step.
  76. model_kwargs (`Dict`):
  77. The keyword arguments that will be passed to the main model, and are used as base inputs for the assistant
  78. model as well.
  79. inputs_tensor (`torch.Tensor`, *optional*):
  80. The model input tensor. In encoder-decoder models, this is the encoder input.
  81. """
  82. def __init__(
  83. self,
  84. input_ids: torch.LongTensor,
  85. assistant_model: "PreTrainedModel",
  86. generation_config: "GenerationConfig",
  87. model_kwargs: dict,
  88. inputs_tensor: torch.Tensor | None = None,
  89. logits_processor: Optional["LogitsProcessorList"] = None,
  90. ):
  91. # Make sure all data at the same device as assistant model
  92. device = assistant_model.device
  93. input_ids = input_ids.to(device)
  94. if inputs_tensor is not None:
  95. inputs_tensor = inputs_tensor.to(device)
  96. # Prepare the assistant and the starting number of candidate tokens
  97. self.assistant_model = assistant_model
  98. # Prepare the generation config by updating with default values if not already set by users
  99. self.assistant_generation_config = copy.deepcopy(assistant_model.generation_config)
  100. global_defaults = self.assistant_generation_config._get_default_generation_params()
  101. self.assistant_generation_config.update(**global_defaults, defaults_only=True)
  102. self.num_assistant_tokens = self.assistant_generation_config.num_assistant_tokens
  103. self.assistant_confidence_threshold = self.assistant_generation_config.assistant_confidence_threshold
  104. # Set eos in assistant same as in target model
  105. self.assistant_generation_config.eos_token_id = generation_config.eos_token_id
  106. # Prepare the kwargs for the assistant model
  107. assistant_kwargs = {}
  108. for key, value in model_kwargs.items(): # deepcopy crashes if we attempt to copy encoder outputs with grads
  109. if key not in ("encoder_outputs", "past_key_values"):
  110. assistant_kwargs[key] = (
  111. value.detach().to(device) if isinstance(value, torch.Tensor) else copy.deepcopy(value)
  112. )
  113. # Remove potential default "logits_to_keep" key
  114. if "logits_to_keep" in assistant_kwargs and not assistant_model._supports_logits_to_keep():
  115. del assistant_kwargs["logits_to_keep"]
  116. # If the assistant is an encoder-decoder model, assume the encoder is different on the assistant.
  117. if assistant_model.config.is_encoder_decoder:
  118. inputs_tensor, model_input_name, assistant_kwargs = assistant_model._prepare_model_inputs(
  119. inputs_tensor, self.assistant_generation_config.bos_token_id, assistant_kwargs
  120. )
  121. assistant_kwargs = assistant_model._prepare_encoder_decoder_kwargs_for_generation(
  122. inputs_tensor, assistant_kwargs, model_input_name, self.assistant_generation_config
  123. )
  124. elif "encoder_outputs" in model_kwargs:
  125. assistant_kwargs["encoder_outputs"] = model_kwargs["encoder_outputs"]
  126. self.assistant_kwargs = assistant_kwargs
  127. # Prepare assistant model's keys of inputs
  128. if assistant_model.config.is_encoder_decoder:
  129. # both are encoder-decoder
  130. self.input_ids_key = "decoder_input_ids"
  131. elif "encoder_outputs" in assistant_kwargs:
  132. # special case for encoder-decoder with decoder-only assistant (like DistilWhisper)
  133. self.input_ids_key = "input_ids"
  134. self.assistant_kwargs["attention_mask"] = self.assistant_kwargs.get(
  135. "decoder_attention_mask",
  136. torch.ones((input_ids.shape[0], 1), device=input_ids.device, dtype=torch.long),
  137. )
  138. else:
  139. # both are decoder-only
  140. self.input_ids_key = "input_ids"
  141. # Prepare generation-related options.
  142. self.logits_processor = logits_processor if logits_processor is not None else LogitsProcessorList()
  143. self.generation_config = copy.deepcopy(generation_config)
  144. self.generation_config.return_dict_in_generate = True
  145. self.generation_config.output_scores = True
  146. self.generation_config.assistant_confidence_threshold = self.assistant_confidence_threshold
  147. # this flag allow us set the confidence stopping criteria for assistant model generation.
  148. self.generation_config.is_assistant = True
  149. # avoid unnecessary warnings that min_length is larger than max_new_tokens
  150. # remove the `MinLengthLogitsProcessor` if exists (NOTE: no need to check for `MinNewTokensLogitsProcessor`)
  151. self.main_model_min_length = self.generation_config.min_length
  152. self.generation_config.min_length = None
  153. self.generation_config.min_new_tokens = None
  154. self.main_model_max_length = self.generation_config.max_length
  155. self.generation_config.max_length = None
  156. self.logits_processor = [
  157. processor for processor in self.logits_processor if not isinstance(processor, MinLengthLogitsProcessor)
  158. ]
  159. # We need to roll back the cache in assisted generation, only DynamicCache is supported
  160. self.generation_config.cache_implementation = "dynamic_full"
  161. if (
  162. is_sklearn_available()
  163. and self.assistant_generation_config.assistant_confidence_threshold
  164. and type(self) is AssistedCandidateGenerator
  165. ):
  166. self.probs = []
  167. self.matches = []
  168. def get_candidates(self, input_ids: torch.LongTensor) -> tuple[torch.LongTensor, torch.FloatTensor]:
  169. """
  170. Fetches the candidates to be tried for the current input.
  171. Args:
  172. input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
  173. Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
  174. Return:
  175. `torch.LongTensor` of shape `(batch_size, candidate_length)` containing the candidate sequences to be
  176. assessed by the model and a `torch.FloatTensor` of shape `(batch_size, candidate_length,
  177. vocabulary_size)` containing the logits associated to each candidate.
  178. """
  179. input_ids = input_ids.to(self.assistant_model.device)
  180. # Calculate new tokens to generate
  181. min_new_tokens, max_new_tokens = self._calculate_new_tokens(input_ids)
  182. if max_new_tokens == 0:
  183. return input_ids, None
  184. # Update past key values and masks
  185. self._update_past_and_masks(input_ids)
  186. # Generate candidates
  187. generation_args = self._prepare_generation_args(input_ids, min_new_tokens, max_new_tokens)
  188. candidate_ids, candidate_logits = self._generate_candidates(generation_args)
  189. return candidate_ids, candidate_logits
  190. def update_candidate_strategy(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, num_matches: int):
  191. """
  192. Updates the candidate generation strategy based on the outcomes.
  193. Args:
  194. input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
  195. Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
  196. scores (`torch.FloatTensor` of shape `(batch_size, candidate_length, config.vocab_size)`):
  197. Prediction scores of a language modeling head. These can be logits for each vocabulary when not using
  198. beam search or log softmax for each vocabulary token when using beam search
  199. num_matches (`int`):
  200. The number of matches between the candidate sequences and the model predictions.
  201. """
  202. # Adjust the max number of assistant tokens to use in the next iteration. This is a simple heuristic,
  203. # probably can be improved -- we want to balance the benefits of getting assistant tokens correct with the
  204. # cost of forecasting incorrect assistant tokens.
  205. if self.assistant_generation_config.num_assistant_tokens_schedule in {
  206. "heuristic",
  207. "heuristic_transient",
  208. }:
  209. # len(scores[0])-1 is the number of candidates according to the target tokenizer.
  210. if num_matches == len(scores[0]) - 1:
  211. self.num_assistant_tokens += 2
  212. else:
  213. self.num_assistant_tokens = max(1, self.num_assistant_tokens - 1)
  214. # The assistant's confidence threshold is adjusted throughout the speculative iterations to reduce the number of unnecessary draft and target forward passes. The costs are estimated based on the ROC curve, which considers the probability of the draft token and its match with the target. A cost of 25% is assigned to false positives and 75% to false negatives.
  215. # This adaptation is not compatible with UAG, as it relies on the number of matched tokens based on the draft vocabulary, which is unavailable in UAG.
  216. if (
  217. is_sklearn_available()
  218. and self.assistant_generation_config.assistant_confidence_threshold
  219. and type(self) is AssistedCandidateGenerator
  220. ):
  221. # update self.matches
  222. self.matches.extend([1] * num_matches)
  223. if len(self.probs) > len(self.matches):
  224. self.matches.append(0)
  225. # update self.probs
  226. excess_length = len(self.probs) - len(self.matches)
  227. if excess_length > 0:
  228. del self.probs[-excess_length:]
  229. if (
  230. len(self.probs) > 5 and {0, 1}.issubset(self.matches)
  231. ): # require at least 5 samples to calculate the ROC curve and at least one positive and one negative sample
  232. fpr, tpr, thresholds = roc_curve(self.matches, self.probs)
  233. fnr = 1 - tpr
  234. # Calculate the cost for each threshold
  235. costs = fpr + 3 * fnr
  236. # Find the threshold that minimizes the cost
  237. optimal_threshold_index = np.argmin(costs)
  238. best_threshold = thresholds[optimal_threshold_index]
  239. self.assistant_generation_config.assistant_confidence_threshold = best_threshold
  240. def _calculate_new_tokens(self, input_ids: torch.LongTensor) -> tuple[int, int]:
  241. """Calculate the minimum and maximum number of new tokens to generate."""
  242. new_cur_len = input_ids.shape[-1]
  243. max_new_tokens = min(int(self.num_assistant_tokens), self.main_model_max_length - new_cur_len - 1)
  244. min_new_tokens = max(min(max_new_tokens, self.main_model_min_length - new_cur_len), 0)
  245. return min_new_tokens, max_new_tokens
  246. def _update_past_and_masks(
  247. self, input_ids: torch.LongTensor, remove_from_pkv: int = 0, num_added_tokens: int = 1
  248. ) -> bool:
  249. """Update past key values and attention masks for subsequent generation rounds."""
  250. has_past_key_values = self.assistant_kwargs.get("past_key_values", None) is not None
  251. if has_past_key_values:
  252. new_cache_size = input_ids.shape[-1] - 1 - remove_from_pkv
  253. self.assistant_kwargs["past_key_values"].crop(new_cache_size - num_added_tokens)
  254. self.assistant_kwargs = _prepare_attention_mask(
  255. self.assistant_kwargs, input_ids.shape[-1], self.assistant_model.config.is_encoder_decoder
  256. )
  257. self.assistant_kwargs = _prepare_position_ids(
  258. self.assistant_kwargs, input_ids.shape[-1], self.assistant_model.config.is_encoder_decoder
  259. )
  260. self.assistant_kwargs = _prepare_token_type_ids(self.assistant_kwargs, input_ids.shape[-1])
  261. # This unsets `dynamic_full`, needed to initialize a new cache for the assistant. After the first forward
  262. # pass on each generation, we reuse the cache instead.
  263. self.generation_config.cache_implementation = None
  264. return has_past_key_values
  265. def _prepare_generation_args(self, input_ids: torch.LongTensor, min_new_tokens: int, max_new_tokens: int) -> dict:
  266. """Prepare arguments for the generation call."""
  267. return {
  268. self.input_ids_key: input_ids,
  269. "min_new_tokens": min_new_tokens,
  270. "max_new_tokens": max_new_tokens,
  271. "generation_config": self.generation_config,
  272. "logits_processor": self.logits_processor,
  273. }
  274. def _generate_candidates(self, generation_args: dict) -> tuple[torch.LongTensor, torch.FloatTensor | None]:
  275. """Generate candidate sequences using the assistant model."""
  276. assistant_output = self.assistant_model.generate(**generation_args, **self.assistant_kwargs)
  277. self.assistant_kwargs["past_key_values"] = assistant_output.past_key_values
  278. if (
  279. is_sklearn_available()
  280. and self.assistant_generation_config.assistant_confidence_threshold
  281. and type(self) is AssistedCandidateGenerator
  282. ):
  283. scores_tensor = torch.cat(assistant_output.scores, dim=0)
  284. scores_softmax = torch.softmax(scores_tensor, dim=-1)
  285. ids = assistant_output.sequences[-1, -len(assistant_output.scores) :]
  286. p = scores_softmax[range(len(ids)), ids]
  287. self.probs.extend(p.tolist())
  288. candidate_logits = torch.stack(assistant_output.scores, dim=1)
  289. candidate_ids = assistant_output.sequences
  290. return candidate_ids, candidate_logits
  291. class AssistedCandidateGeneratorDifferentTokenizers(AssistedCandidateGenerator):
  292. """
  293. `CandidateGenerator` class to be used for Universal Assisted Generation (UAD): assisted generation with different tokenizers
  294. for the assistant and main models. This class generates candidates through the use of a smaller
  295. model.
  296. The main model input tokens are re-encoded into assistant model tokens, then candidate tokens are generated in the assistant encoding, which are
  297. in turn re-encoded into main model candidate tokens. Validation then proceeds as explained above.
  298. The re-encoding steps involve decoding token ids into text and then encoding the text using a different tokenizer.
  299. Since re-encoding the tokens may result in tokenization discrepancies, UAD finds the longest common subsequence between the source and target encodings,
  300. to ensure the new tokens include the correct prompt suffix.
  301. Args:
  302. input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
  303. Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
  304. assistant_model (`PreTrainedModel`):
  305. The model to be used for generating candidates. This model should be smaller than the main model.
  306. target_tokenizer (`PreTrainedTokenizerBase`):
  307. The tokenizer used for the target model.
  308. assistant_tokenizer (`PreTrainedTokenizerBase`):
  309. The tokenizer used for the assistant model.
  310. generation_config (`~generation.GenerationConfig`, *optional*):
  311. The generation configuration to be used as base parametrization for the generation call.
  312. logits_processor (`LogitsProcessorList`):
  313. An instance of [`LogitsProcessorList`]. List of instances of class derived from [`LogitsProcessor`]
  314. used to modify the prediction scores of the language modeling head applied at each generation step.
  315. model_kwargs (`Dict`):
  316. The keyword arguments that will be passed to the main model, and are used as base inputs for the assistant
  317. model as well.
  318. inputs_tensor (`torch.Tensor`, *optional*):
  319. The model input tensor. In encoder-decoder models, this is the encoder input.
  320. """
  321. def __init__(
  322. self,
  323. input_ids: torch.LongTensor,
  324. assistant_model: "PreTrainedModel",
  325. target_tokenizer: "PreTrainedTokenizerBase",
  326. assistant_tokenizer: "PreTrainedTokenizerBase",
  327. generation_config: "GenerationConfig",
  328. model_kwargs: dict,
  329. inputs_tensor: torch.Tensor | None = None,
  330. logits_processor: Optional["LogitsProcessorList"] = None,
  331. ):
  332. super().__init__(input_ids, assistant_model, generation_config, model_kwargs, inputs_tensor, logits_processor)
  333. self.target_tokenizer = target_tokenizer
  334. self.assistant_tokenizer = assistant_tokenizer
  335. self.prev_target_ids_len: int | None = None
  336. self.prev_assistant_ids: torch.LongTensor | None = None
  337. self.target_lookbehind = self.assistant_generation_config.target_lookbehind
  338. self.assistant_lookbehind = self.assistant_generation_config.assistant_lookbehind
  339. @staticmethod
  340. def _get_longest_diag_dict(input_matrix, nonzero_idx):
  341. """
  342. Calculates the length of the longest diagonal sequence in a given matrix.
  343. Args:
  344. input_matrix (torch.Tensor): The input matrix.
  345. nonzero_idx (torch.Tensor): The indices of the non-zero elements in the matrix.
  346. Returns:
  347. dict: A dictionary where the keys are the indices of the non-zero elements and the values are the lengths of the longest diagonal sequences starting from those indices.
  348. """
  349. visited = set()
  350. diags = {}
  351. for idx in nonzero_idx:
  352. start_idx = torch.clone(idx)
  353. tuple_start_idx = tuple(start_idx.tolist())
  354. if tuple_start_idx in visited:
  355. continue
  356. visited.add(tuple_start_idx)
  357. cur_diag_len = 1
  358. start_idx += 1
  359. while start_idx[0] < input_matrix.shape[0] and start_idx[1] < input_matrix.shape[1]:
  360. tuple_start_idx = tuple(start_idx.tolist())
  361. visited.add(tuple_start_idx)
  362. if input_matrix[start_idx[0], start_idx[1]] == 1:
  363. cur_diag_len += 1
  364. start_idx += 1
  365. else:
  366. break
  367. diags[idx] = cur_diag_len
  368. return diags
  369. @staticmethod
  370. def _get_longest_diag_index(input_matrix):
  371. """
  372. Returns the start index and length of the longest diagonal in the given input.
  373. Args:
  374. input_matrix (numpy.ndarray): The input matrix.
  375. Returns:
  376. tuple: A tuple containing the start index and length of the longest diagonal.
  377. """
  378. diags = AssistedCandidateGeneratorDifferentTokenizers._get_longest_diag_dict(
  379. input_matrix, input_matrix.nonzero()
  380. )
  381. diags_values = list(diags.values())
  382. diags_keys = list(diags.keys())
  383. best_diag = np.argmax(diags_values)
  384. diag_start_index = diags_keys[best_diag]
  385. diag_start_length = diags_values[best_diag]
  386. return diag_start_index, diag_start_length
  387. @staticmethod
  388. def _get_tokens_diag(prompt, prompt_plus_new_tokens):
  389. """
  390. Input:
  391. prompt: 2D array of shape (batch_size, prompt_length), represents the original prompt tokens
  392. prompt_plus_new_tokens: 2D array of shape (batch_size, prompt_length), represents the suffix of the original prompt, with additional new tokens.
  393. Output:
  394. discrepancy_length: int, represents the number of tokens that need to be replaced from prompt
  395. new_tokens_only: 2D array of shape (batch_size, new_token_length), represents the new tokens that are not in prompt
  396. discrepancy_only: 2D array of shape (batch_size, discrepancy_length), represents the new tokens that are in prompt but not in prompt_plus_new_tokens
  397. """
  398. compare_mat = prompt == prompt_plus_new_tokens.T
  399. if not torch.is_tensor(compare_mat):
  400. compare_mat = torch.tensor(compare_mat)
  401. compare_mat_int = compare_mat.to(int)
  402. if not compare_mat_int.any().item():
  403. # empty intersection between prompt and prompt_plus_new_tokens
  404. return None, None, None
  405. longest_location, longest_diag_length = AssistedCandidateGeneratorDifferentTokenizers._get_longest_diag_index(
  406. compare_mat_int
  407. )
  408. new_token_start_index = longest_location[0] + longest_diag_length
  409. discrepancy_with_old = longest_location[1] + longest_diag_length
  410. discrepancy_length = (prompt.shape[1] - discrepancy_with_old).item()
  411. new_tokens_only = prompt_plus_new_tokens[:, new_token_start_index + discrepancy_length :]
  412. discrepancy_only = prompt_plus_new_tokens[
  413. :, new_token_start_index : new_token_start_index + discrepancy_length
  414. ]
  415. return discrepancy_length, new_tokens_only, discrepancy_only
  416. def convert_source_tokens_to_target_tokens(
  417. self,
  418. input_ids,
  419. source_tokenizer,
  420. destination_tokenizer,
  421. ):
  422. """
  423. Convert token IDs from one tokenizer to another.
  424. Args:
  425. input_ids: The input token IDs.
  426. source_tokenizer: The source tokenizer.
  427. destination_tokenizer: The destination tokenizer.
  428. Returns:
  429. The converted token IDs.
  430. """
  431. text = source_tokenizer.decode(input_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)
  432. dest_ids = destination_tokenizer(text, add_special_tokens=True, return_tensors="pt")["input_ids"]
  433. return dest_ids.to(input_ids.device)
  434. def get_candidates(self, input_ids: torch.LongTensor) -> tuple[torch.LongTensor, torch.FloatTensor]:
  435. """
  436. Fetches the candidates to be tried for the current input.
  437. Args:
  438. input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
  439. Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
  440. Return:
  441. `torch.LongTensor` of shape `(batch_size, candidate_length)` containing the candidate sequences to be
  442. assessed by the model and a `torch.FloatTensor` of shape `(batch_size, candidate_length,
  443. vocabulary_size)` containing the logits associated to each candidate.
  444. """
  445. max_new_tokens = int(self.num_assistant_tokens)
  446. if max_new_tokens == 0:
  447. return input_ids, None
  448. input_ids = input_ids.to(self.assistant_model.device)
  449. remove_from_pkv = 0
  450. assistant_input_ids, remove_from_pkv = self._prepare_assistant_input_ids(input_ids)
  451. self.prev_assistant_ids = assistant_input_ids
  452. min_new_tokens = max(min(max_new_tokens, self.main_model_min_length - assistant_input_ids.shape[-1]), 0)
  453. self._update_past_and_masks(assistant_input_ids, remove_from_pkv)
  454. generation_args = self._prepare_generation_args(assistant_input_ids, min_new_tokens, max_new_tokens)
  455. self.assistant_kwargs.pop("attention_mask", None)
  456. assistant_output = self.assistant_model.generate(**generation_args, **self.assistant_kwargs)
  457. new_target_ids = self._process_assistant_outputs(input_ids, assistant_output.sequences)
  458. # Update state
  459. self.prev_target_ids_len = input_ids.shape[1]
  460. self.assistant_kwargs["past_key_values"] = assistant_output.past_key_values
  461. self.prev_assistant_ids = assistant_output.sequences
  462. if self.prev_target_ids_len >= new_target_ids.shape[1]:
  463. return input_ids, None
  464. return new_target_ids, None
  465. def _prepare_assistant_input_ids(self, input_ids: torch.LongTensor) -> tuple[torch.LongTensor, int]:
  466. """Converts target input IDs to assistant input IDs, handling discrepancies."""
  467. convert_kwargs = {
  468. "source_tokenizer": self.target_tokenizer,
  469. "destination_tokenizer": self.assistant_tokenizer,
  470. }
  471. remove_from_pkv = 0
  472. if self.prev_assistant_ids is not None and self.prev_target_ids_len > self.target_lookbehind:
  473. # input_ids contains all target prompt input ids and some new target input ids
  474. start_index_in_target_window = self.prev_target_ids_len - self.target_lookbehind
  475. new_assistant_ids = self.convert_source_tokens_to_target_tokens(
  476. input_ids[:, start_index_in_target_window:], **convert_kwargs
  477. )
  478. prompt_use_length = new_assistant_ids.shape[1]
  479. prompt_use = self.prev_assistant_ids[:, -prompt_use_length:]
  480. discrepancy_length, new_tokens_only, discrepancy_only = self._get_tokens_diag(
  481. prompt_use, new_assistant_ids
  482. )
  483. assistant_input_ids = self.prev_assistant_ids
  484. if new_tokens_only is not None:
  485. if discrepancy_length > 0 and discrepancy_only.shape[1] > 0:
  486. if discrepancy_length == discrepancy_only.shape[1]:
  487. assistant_input_ids[:, -discrepancy_length:] = discrepancy_only
  488. elif discrepancy_length > discrepancy_only.shape[1]:
  489. discrepancy_length_diff = discrepancy_length - discrepancy_only.shape[1]
  490. assistant_input_ids = assistant_input_ids[:, :-discrepancy_length_diff]
  491. assistant_input_ids[:, -discrepancy_only.shape[1] :] = discrepancy_only
  492. remove_from_pkv = discrepancy_length
  493. if new_tokens_only.shape[1] > 0:
  494. assistant_input_ids = torch.cat([assistant_input_ids, new_tokens_only], dim=-1)
  495. else:
  496. # edge case: in case of no intersection between prompt and new_assistant_ids
  497. assistant_input_ids = torch.cat([assistant_input_ids, new_assistant_ids], dim=-1)
  498. else:
  499. assistant_input_ids = self.convert_source_tokens_to_target_tokens(input_ids, **convert_kwargs)
  500. self.prev_target_ids_len = input_ids.shape[1]
  501. return assistant_input_ids, remove_from_pkv
  502. def _process_assistant_outputs(
  503. self, input_ids: torch.LongTensor, assistant_sequences: torch.LongTensor
  504. ) -> torch.LongTensor:
  505. """Processes assistant outputs to obtain target input IDs."""
  506. prev_assistant_ids = cast(torch.LongTensor, self.prev_assistant_ids)
  507. num_prev_assistant = prev_assistant_ids.shape[1]
  508. start_assistant_look_index = num_prev_assistant - self.assistant_lookbehind
  509. new_target_ids_from_window = self.convert_source_tokens_to_target_tokens(
  510. assistant_sequences[:, start_assistant_look_index:],
  511. source_tokenizer=self.assistant_tokenizer,
  512. destination_tokenizer=self.target_tokenizer,
  513. )
  514. target_prompt_use_length = new_target_ids_from_window.shape[1]
  515. target_prompt_use = input_ids[:, -target_prompt_use_length:]
  516. _, target_new_tokens_only, _ = self._get_tokens_diag(target_prompt_use, new_target_ids_from_window)
  517. new_target_ids = input_ids
  518. if target_new_tokens_only is not None:
  519. if target_new_tokens_only.shape[1] > 0:
  520. new_target_ids = torch.cat([new_target_ids, target_new_tokens_only], dim=-1)
  521. else:
  522. # edge case: in case of no intersection between prompt and new_target_ids
  523. new_target_ids = torch.cat([new_target_ids, new_target_ids_from_window], dim=-1)
  524. if self.main_model_max_length is not None:
  525. new_target_ids = new_target_ids[:, : self.main_model_max_length]
  526. return new_target_ids
  527. class _PruneReindexingLMHead(nn.Module):
  528. """
  529. A class to prune and reindex the language model head.
  530. This class prunes the language model head to only include the specified token IDs and reindexes the logits
  531. to map back to the original vocabulary.
  532. Args:
  533. original_lm_head (nn.Module): The original language model head.
  534. token_ids (list[int]): The list of token IDs to keep.
  535. """
  536. def __init__(self, original_lm_head, assistant_overlap_token_ids):
  537. super().__init__()
  538. self.pruned_lm_head = prune_linear_layer(original_lm_head, assistant_overlap_token_ids).to(
  539. original_lm_head.weight.dtype
  540. )
  541. def forward(self, hidden_states):
  542. pruned_logits = self.pruned_lm_head(hidden_states)
  543. return pruned_logits
  544. class _MapInputEmbedding(nn.Module):
  545. def __init__(self, original_embedding: nn.Embedding, assistant_overlap_token_ids):
  546. """
  547. Wraps an existing embedding layer and remaps token IDs before lookup.
  548. Args:
  549. original_embedding (nn.Embedding): Pre-trained or existing embedding layer.
  550. assistant_overlap_token_ids (dict): Mapping from original token IDs to new token IDs.
  551. Example: {old_id: new_id}
  552. """
  553. super().__init__()
  554. self.original_embedding = original_embedding
  555. self.weight = original_embedding.weight
  556. self.assistant_overlap_token_ids = assistant_overlap_token_ids
  557. self.map = False
  558. def forward(self, input_ids: torch.LongTensor) -> torch.FloatTensor:
  559. """
  560. Args:
  561. input_ids (torch.LongTensor): Tensor of token IDs (batch_size, seq_len).
  562. Returns:
  563. torch.FloatTensor: Corresponding input embeddings.
  564. """
  565. if self.map:
  566. # Get the last item from input_ids
  567. my_input_ids = self.assistant_overlap_token_ids[input_ids[0, -1]].unsqueeze(0).unsqueeze(0)
  568. else:
  569. self.map = True
  570. my_input_ids = input_ids
  571. return self.original_embedding(my_input_ids)
  572. class AssistantToTargetTranslator:
  573. """
  574. Translates token ids and logits between assistant and target model vocabularies. This class is used to handle
  575. vocabulary mismatches when using different tokenizers for the assistant and target models in speculative decoding,
  576. as introduced in the paper "Lossless Speculative Decoding Algorithms for Heterogeneous Vocabularies"
  577. (https://huggingface.co/papers/2502.05202).
  578. It maintains mappings between the two vocabularies and handles token/logit conversion.
  579. Args:
  580. target_tokenizer (`PreTrainedTokenizerBase`):
  581. The tokenizer used by the target (main) model.
  582. assistant_tokenizer (`PreTrainedTokenizerBase`):
  583. The tokenizer used by the assistant model.
  584. target_vocab_size (`int`):
  585. The size of the target model's vocabulary. If not provided, will be inferred from the target tokenizer.
  586. assistant_model (Optional[PreTrainedModel], optional): The assistant model to be used. Defaults to None for backward compatibility.
  587. assistant_prune_lm_head (bool): Whether to prune the assistant model's language model
  588. head to match the target vocabulary. This is only applicable if `assistant_model` is provided.
  589. Defaults to False for backward compatibility.
  590. """
  591. FILTER_VALUE: float = -float("Inf") # The value used to filter out unmapped tokens in the logits.
  592. SUPPRESS_TOKEN_ID: int = -1 # The ID used to mark suppressed tokens in the mapping.
  593. def __init__(
  594. self,
  595. target_tokenizer: "PreTrainedTokenizerBase",
  596. assistant_tokenizer: "PreTrainedTokenizerBase",
  597. target_vocab_size: int, # required since target_vocab_size can be different from the length of target_tokenizer.get_vocab()
  598. assistant_model: Optional["PreTrainedModel"] = None,
  599. assistant_prune_lm_head: bool = False,
  600. ):
  601. self._target_tokenizer: PreTrainedTokenizerBase = target_tokenizer
  602. self._assistant_tokenizer: PreTrainedTokenizerBase = assistant_tokenizer
  603. self._assistant_model_device = assistant_model.device if assistant_model is not None else "cpu"
  604. self.target_vocab_size: int = target_vocab_size
  605. self._assistant_to_target_input_ids, self.target_to_assistant_input_ids = (
  606. self._get_assistant_to_target_input_ids()
  607. )
  608. self._suppress_input_ids: list[int] = self._get_suppress_input_ids()
  609. self.logits_processors: LogitsProcessorList | None = None
  610. self.assistant_prune_lm_head = assistant_prune_lm_head and assistant_model is not None
  611. if len(self._suppress_input_ids) > 0:
  612. # the assistant vocab is not a subset of the target vocab
  613. if self.assistant_prune_lm_head and assistant_model is not None:
  614. self.assistant_overlap_token_ids = torch.tensor(
  615. list(self.target_to_assistant_input_ids.values()),
  616. dtype=torch.long,
  617. device=self._assistant_model_device,
  618. )
  619. original_lm_head = assistant_model.get_output_embeddings()
  620. pruned_lm_head = _PruneReindexingLMHead(original_lm_head, self.assistant_overlap_token_ids)
  621. del original_lm_head
  622. assistant_model.set_output_embeddings(pruned_lm_head)
  623. original_input_embeddings = assistant_model.get_input_embeddings()
  624. map_input_embeddings = _MapInputEmbedding(original_input_embeddings, self.assistant_overlap_token_ids)
  625. del original_input_embeddings
  626. assistant_model.set_input_embeddings(map_input_embeddings)
  627. self.map_input_embeddings = map_input_embeddings
  628. else:
  629. self.logits_processors = LogitsProcessorList(
  630. [SuppressTokensLogitsProcessor(self._get_suppress_input_ids(), self._assistant_model_device)]
  631. )
  632. def unmap_input_ids(self):
  633. """
  634. Disables the mapping of input ids despite the assistant pruning for the language model head being enabled.
  635. This method is required for the first forward pass of `_MapInputEmbedding` where input ids are already in the assistant vocabulary space. By disabling the mapping, it ensures that the input ids are processed correctly without remapping.
  636. """
  637. if self.assistant_prune_lm_head:
  638. self.map_input_embeddings.map = False
  639. def _get_assistant_to_target_input_ids(self):
  640. target_vocab = self._target_tokenizer.get_vocab()
  641. assistant_vocab = self._assistant_tokenizer.get_vocab()
  642. space_str = " "
  643. target_space_ids = self._target_tokenizer(space_str, add_special_tokens=False)["input_ids"]
  644. if len(target_space_ids) > 0:
  645. target_space_sign = self._target_tokenizer.convert_ids_to_tokens(target_space_ids)[0][0]
  646. assistant_space_ids = self._assistant_tokenizer(space_str, add_special_tokens=False)["input_ids"]
  647. if len(assistant_space_ids) > 0:
  648. assistant_space_sign = self._assistant_tokenizer.convert_ids_to_tokens(assistant_space_ids)[0][0]
  649. if target_space_sign != assistant_space_sign:
  650. # If the assistant tokenizer has a different space sign than the target tokenizer,
  651. # we need to replace the assistant space sign with the target space sign in the assistant_vocab.
  652. assistant_vocab = {
  653. (
  654. tok.replace(assistant_space_sign, target_space_sign, 1)
  655. if tok.startswith(assistant_space_sign)
  656. else tok
  657. ): idx
  658. for tok, idx in assistant_vocab.items()
  659. }
  660. max_assistant_index = max(assistant_vocab.values())
  661. assistant_to_target_input_ids = torch.full((max_assistant_index + 1,), self.SUPPRESS_TOKEN_ID, dtype=int)
  662. target_to_assistant_input_ids: dict[int, int] = {}
  663. for tok, assistant_id in assistant_vocab.items():
  664. target_id = target_vocab.get(tok)
  665. if target_id is not None:
  666. assistant_to_target_input_ids[assistant_id] = target_id
  667. target_to_assistant_input_ids[target_id] = assistant_id
  668. return assistant_to_target_input_ids.to(self._assistant_model_device), target_to_assistant_input_ids
  669. def _get_suppress_input_ids(self) -> list[int]:
  670. """
  671. Get the input ids that are in the assistant vocab but not in the target vocab.
  672. """
  673. return torch.where(self._assistant_to_target_input_ids == self.SUPPRESS_TOKEN_ID)[0]
  674. def get_target_ids(
  675. self, assistant_input_ids, target_input_ids, assistant_candidate_ids: torch.LongTensor
  676. ) -> torch.LongTensor:
  677. """
  678. Return the target candidate ids that correspond to the assistant candidate ids.
  679. Note that we have already the target ids for the prompt and we only need to find the target ids for the new tokens.
  680. Moreover, assistant ids of the original prompt does not necessarily appear in _assistant_to_target_input_ids.
  681. """
  682. num_new_tokens = len(assistant_candidate_ids[0]) - assistant_input_ids.shape[1]
  683. if num_new_tokens == 0:
  684. return target_input_ids
  685. else:
  686. # Get last `num_new_tokens` candidate IDs
  687. last_candidate_ids = assistant_candidate_ids[0, -num_new_tokens:]
  688. if self.assistant_prune_lm_head:
  689. # Map assistant IDs -> target input IDs
  690. last_candidate_ids = self.assistant_overlap_token_ids[last_candidate_ids]
  691. transformed_slice = self._assistant_to_target_input_ids[last_candidate_ids]
  692. return torch.cat((target_input_ids, transformed_slice.unsqueeze(0)), dim=1)
  693. def get_target_logits(self, assistant_logits: torch.FloatTensor) -> torch.FloatTensor:
  694. """
  695. Return the target logits that correspond to the assistant logits.
  696. """
  697. target_shape: tuple[int, ...] = (*assistant_logits.shape[:-1], self.target_vocab_size)
  698. target_logits: torch.FloatTensor = torch.full(
  699. target_shape, self.FILTER_VALUE, device=self._assistant_model_device
  700. )
  701. # Mask for valid indices
  702. assistant_indices_mask = self._assistant_to_target_input_ids != self.SUPPRESS_TOKEN_ID
  703. # Exclude invalid indices
  704. target_logits_supported_indices = self._assistant_to_target_input_ids[assistant_indices_mask]
  705. if self.assistant_prune_lm_head:
  706. target_logits[..., target_logits_supported_indices] = assistant_logits
  707. else:
  708. valid_assistant_logits = assistant_logits[..., : self._assistant_to_target_input_ids.shape[0]]
  709. target_logits[..., target_logits_supported_indices] = valid_assistant_logits[..., assistant_indices_mask]
  710. return target_logits
  711. class AssistantVocabTranslatorCache:
  712. """
  713. Cache for `AssistantToTargetTranslator` instances. The instances are computed at
  714. pre-processing time, and this cache allows us to avoid recomputing them.
  715. """
  716. _cache = weakref.WeakKeyDictionary()
  717. @classmethod
  718. def get_translator(
  719. cls,
  720. target_tokenizer: "PreTrainedTokenizerBase",
  721. assistant_tokenizer: "PreTrainedTokenizerBase",
  722. target_vocab_size: int,
  723. assistant_model: Optional["PreTrainedModel"] = None,
  724. assistant_prune_lm_head: bool = False,
  725. ) -> AssistantToTargetTranslator:
  726. assistant_dict = cls._cache.get(target_tokenizer)
  727. if assistant_dict is None:
  728. assistant_dict = weakref.WeakKeyDictionary()
  729. cls._cache[target_tokenizer] = assistant_dict
  730. mapping = assistant_dict.get(assistant_tokenizer)
  731. if mapping is None:
  732. mapping = AssistantToTargetTranslator(
  733. target_tokenizer,
  734. assistant_tokenizer,
  735. target_vocab_size,
  736. assistant_model,
  737. assistant_prune_lm_head,
  738. )
  739. assistant_dict[assistant_tokenizer] = mapping
  740. return mapping
  741. @classmethod
  742. def cleanup(cls):
  743. """
  744. Clean up dead references in the cache.
  745. This removes entries where either the target_tokenizer or assistant_tokenizer
  746. has been garbage collected.
  747. """
  748. # Remove entries from the outer cache where the target_tokenizer is no longer alive
  749. dead_keys = [key for key in cls._cache if key is None]
  750. for key in dead_keys:
  751. del cls._cache[key]
  752. # For each assistant_dict, remove entries where assistant_tokenizer is no longer alive
  753. for assistant_dict in cls._cache.values():
  754. dead_keys = [key for key in assistant_dict if key is None]
  755. for key in dead_keys:
  756. del assistant_dict[key]
  757. class UniversalSpeculativeDecodingGenerator(AssistedCandidateGeneratorDifferentTokenizers):
  758. """
  759. `CandidateGenerator` class to be used for Universal Speculative Decoding (USD): speculative decoding with different tokenizers
  760. for the assistant and main models. This class generates candidates through the use of a smaller model.
  761. """
  762. def __init__(
  763. self,
  764. input_ids: torch.LongTensor,
  765. assistant_model: "PreTrainedModel",
  766. target_tokenizer: "PreTrainedTokenizerBase",
  767. assistant_tokenizer: "PreTrainedTokenizerBase",
  768. generation_config: "GenerationConfig",
  769. model_kwargs: dict,
  770. atm_translator: AssistantToTargetTranslator,
  771. inputs_tensor: torch.Tensor | None = None,
  772. logits_processor: Optional["LogitsProcessorList"] = None,
  773. ):
  774. # Initialize translator before parent class
  775. self._atm_translator = atm_translator
  776. super().__init__(
  777. input_ids,
  778. assistant_model,
  779. target_tokenizer,
  780. assistant_tokenizer,
  781. generation_config,
  782. model_kwargs,
  783. inputs_tensor,
  784. logits_processor,
  785. )
  786. # Track sequence lengths and previous assistant IDs
  787. self._target_seq_len_with_candidates: int = 0
  788. self._prev_assistant_ids: torch.LongTensor | None = None
  789. def get_candidates(self, input_ids: torch.LongTensor) -> tuple[torch.LongTensor, torch.FloatTensor]:
  790. """
  791. Simplified version of get_candidates that uses the translator cache for token conversion.
  792. """
  793. target_input_ids = input_ids.to(self.assistant_model.device)
  794. assistant_input_ids, num_added_tokens = self._prepare_assistant_input_ids(target_input_ids)
  795. min_new_tokens, max_new_tokens = self._calculate_new_tokens(target_input_ids)
  796. if max_new_tokens == 0:
  797. return input_ids, None
  798. self._update_past_and_masks(assistant_input_ids, num_added_tokens=num_added_tokens)
  799. generation_args = self._prepare_generation_args(assistant_input_ids, min_new_tokens, max_new_tokens)
  800. # Ensure scores are returned
  801. generation_args["generation_config"].output_scores = True
  802. generation_args["generation_config"].return_dict_in_generate = True
  803. # Generate and process outputs using translator
  804. if self._atm_translator.logits_processors is not None:
  805. generation_args["logits_processor"] = self._atm_translator.logits_processors
  806. self._prev_assistant_ids, assistant_candidate_logits = self._generate_candidates(generation_args)
  807. # Use translator to convert tokens and logits
  808. target_candidate_ids = self._atm_translator.get_target_ids(
  809. assistant_input_ids, target_input_ids, self._prev_assistant_ids
  810. )
  811. self._target_seq_len_with_candidates = target_candidate_ids.shape[-1]
  812. target_candidate_logits = self._atm_translator.get_target_logits(assistant_candidate_logits)
  813. return target_candidate_ids, target_candidate_logits
  814. def _update_past_and_masks(self, assistant_input_ids: torch.LongTensor, num_added_tokens: int = 1) -> bool:
  815. if self._prev_assistant_ids is None:
  816. # Prepare attention mask for the first generation.
  817. # For subsequent generations, the attention mask is updated in super()_update_past_and_masks.
  818. self.assistant_kwargs = _prepare_attention_mask(
  819. self.assistant_kwargs, assistant_input_ids.shape[-1], self.assistant_model.config.is_encoder_decoder
  820. )
  821. self.assistant_kwargs = _prepare_position_ids(
  822. self.assistant_kwargs, assistant_input_ids.shape[-1], self.assistant_model.config.is_encoder_decoder
  823. )
  824. return super()._update_past_and_masks(assistant_input_ids, num_added_tokens=num_added_tokens)
  825. def _prepare_assistant_input_ids(self, target_input_ids: torch.LongTensor) -> torch.LongTensor:
  826. """
  827. Simplified token conversion that only processes new tokens.
  828. """
  829. # Calculate new tokens since last call
  830. target_seq_len = target_input_ids.shape[-1]
  831. if self._target_seq_len_with_candidates == 0:
  832. new_token_count = target_seq_len
  833. else:
  834. new_token_count = 1
  835. target_new_ids = target_input_ids[:, -new_token_count:]
  836. # Convert the new tokens
  837. assistant_new_ids = None
  838. if self._target_seq_len_with_candidates > 0:
  839. # we have only one new token and we can directly convert it
  840. assistant_new_ids = self._atm_translator.target_to_assistant_input_ids.get(target_new_ids[0].item())
  841. if assistant_new_ids is None:
  842. target_new_text = self.target_tokenizer.decode(
  843. target_new_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
  844. )
  845. assistant_new_ids = self.assistant_tokenizer(
  846. target_new_text, add_special_tokens=False, return_tensors="pt"
  847. )["input_ids"].to(self.assistant_model.device)
  848. else:
  849. assistant_new_ids = torch.tensor([[assistant_new_ids]], device=self.assistant_model.device)
  850. # Update or initialize assistant IDs
  851. if self._prev_assistant_ids is None:
  852. assistant_input_ids = assistant_new_ids
  853. else:
  854. tokens_to_remove = self._target_seq_len_with_candidates + 1 - target_seq_len
  855. # If the number of new tokens is greater than zero, truncate the previous assistant IDs
  856. if tokens_to_remove > 0:
  857. self._prev_assistant_ids = self._prev_assistant_ids[:, :-tokens_to_remove]
  858. assistant_input_ids = torch.cat([self._prev_assistant_ids, assistant_new_ids], dim=-1)
  859. assistant_input_ids = assistant_input_ids.to(dtype=torch.long)
  860. self._atm_translator.unmap_input_ids()
  861. return assistant_input_ids, len(assistant_new_ids[0])
  862. class PromptLookupCandidateGenerator(CandidateGenerator):
  863. """
  864. `CandidateGenerator` class to be used for prompt lookup generation. This class generates candidates by looking up
  865. likely continuations in the provided prompt (input_ids) itself.
  866. Read the following blog post for more information: https://github.com/apoorvumang/prompt-lookup-decoding
  867. Args:
  868. eos_token_id (`torch.Tensor`, *optional*):
  869. The token id of the end of sequence token.
  870. num_output_tokens (`int`, *optional*, defaults to 10):
  871. The number of tokens to be output as candidate tokens.
  872. max_matching_ngram_size (`int`, *optional*, defaults to 2):
  873. The maximum ngram size to be considered for matching in the prompt
  874. max_length (`int`, *optional*, defaults to 20):
  875. The number of total maximum tokens that can be generated. For decoder-only models that includes the
  876. prompt length. Defaults to 20, which is the max length used as default in generation config.
  877. logits_processor (`LogitsProcessorList`, *optional*):
  878. An instance of [`LogitsProcessorList`]. List of instances of class derived from [`LogitsProcessor`]
  879. used to modify the prediction scores of the language modeling head applied at each generation step. In
  880. prompt lookup assisted generation, they are not used to manipulate probabilities, but rather to find
  881. forbidden tokens (p = -inf) and block them from being valid candidates.
  882. vocab_size (`int`, *optional*):
  883. The size of the vocabulary. Required if `logits_processor` is provided.
  884. """
  885. def __init__(
  886. self,
  887. eos_token_id: torch.Tensor | None = None,
  888. num_output_tokens: int = 10,
  889. max_matching_ngram_size: int = 2,
  890. max_length: int = 20,
  891. logits_processor: Optional["LogitsProcessorList"] = None,
  892. vocab_size: int | None = None,
  893. ):
  894. self.num_output_tokens = num_output_tokens
  895. self.max_matching_ngram_size = max_matching_ngram_size
  896. self.max_length = max_length
  897. self.eos_token_id = eos_token_id
  898. self.logits_processor = logits_processor
  899. self.vocab_size = vocab_size
  900. if self.max_matching_ngram_size <= 0 or self.num_output_tokens <= 0:
  901. raise ValueError("Invalid max_matching_ngram_size or num_output_tokens")
  902. def get_candidates(self, input_ids: torch.LongTensor) -> tuple[torch.LongTensor, torch.FloatTensor]:
  903. """
  904. Fetches the candidates to be tried for the current input.
  905. Args:
  906. input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
  907. Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
  908. Return:
  909. `torch.LongTensor` of shape `(num_candidates, candidate_length)`: The candidate sequences to be tried.
  910. """
  911. bsz, input_length = input_ids.shape
  912. # Don't generate more than `max_length - 1` candidates since the target model generates one extra token.
  913. if self.max_length == input_length + 1:
  914. return input_ids, None
  915. chosen_ids = None
  916. match_found = False
  917. for ngram_size in range(min(self.max_matching_ngram_size, input_length - 1), 0, -1):
  918. # Create sliding windows of size ngram_size
  919. windows = input_ids.unfold(dimension=1, size=ngram_size, step=1)
  920. # Convert ngram to a tensor for comparison
  921. ngram_tensor = input_ids[0, -ngram_size:]
  922. # Find where the windows match the ngram
  923. matches = (windows == ngram_tensor).all(dim=2)
  924. # Get the indices of matches
  925. match_indices = matches.nonzero(as_tuple=True)[1]
  926. # Iterate through match indices to find a valid continuation
  927. # TODO (joao): this finds the first valid candidates (left to right), but perhaps we should find the
  928. # longest valid candidates?
  929. for idx in match_indices:
  930. start_idx = idx + ngram_size
  931. end_idx = start_idx + self.num_output_tokens
  932. end_idx = min(end_idx, input_length, self.max_length)
  933. if start_idx < end_idx:
  934. chosen_ids = input_ids[0, start_idx:end_idx]
  935. # Check if the each new candidate token is forbidden according to the logits processor. If all
  936. # tokens are allowed, we keep `chosen_ids` as is.
  937. # 1. create random logits.
  938. # 2. apply the logits processor to get output logits for the next token, using the arbitrary
  939. # logits as input.
  940. # 3. compare the output logits with the next candidate token. If they are -inf, then the next
  941. # candidate token is forbidden and we don't want to generate it.
  942. if self.logits_processor is not None:
  943. sequence_with_candidate = input_ids
  944. fake_input_logits = torch.ones(
  945. (bsz, self.vocab_size), device=input_ids.device, dtype=torch.float32
  946. )
  947. for candidate_idx, new_candidate_token in enumerate(chosen_ids):
  948. fake_output_logits = self.logits_processor(sequence_with_candidate, fake_input_logits)
  949. fake_candidate_logits = fake_output_logits[0, new_candidate_token]
  950. # next candidate token is forbidden -> crop chosen_ids accordingly
  951. if fake_candidate_logits in (-float("Inf"), torch.finfo(fake_candidate_logits.dtype).min):
  952. chosen_ids = chosen_ids[:candidate_idx]
  953. break
  954. else:
  955. sequence_with_candidate = torch.cat(
  956. (input_ids, chosen_ids[: candidate_idx + 1].unsqueeze(0)), dim=1
  957. )
  958. # no valid candidate tokens -> look for a different match
  959. if chosen_ids.shape[0] == 0:
  960. continue
  961. match_found = True
  962. # remove remaining candidate ids if an "eos" token is found, otherwise the target model may
  963. # accept eos and the rest as valid, thus not stopping generation after "eos"
  964. # NOTE: below code is written based on the fact that assisted decoding supports only bs=1
  965. mask = torch.isin(chosen_ids, self.eos_token_id)
  966. match_indices_eos = torch.nonzero(mask)
  967. if match_indices_eos.numel() > 0:
  968. first_eos_index = match_indices_eos[0].item()
  969. chosen_ids = chosen_ids[:first_eos_index]
  970. break
  971. if match_found:
  972. break
  973. # In case we didn't find a match return the input sequence unchanged, reverts back to autoregressive decoding
  974. if not match_found or chosen_ids is None or len(chosen_ids) == 0:
  975. return input_ids, None
  976. # Now need extend input_ids with chosen_ids
  977. chosen_ids = chosen_ids.unsqueeze(0)
  978. candidate_input_ids = torch.cat((input_ids, chosen_ids), dim=1)
  979. # assisted_generation expects logits as well, but we don't have those here, so returning None
  980. return candidate_input_ids, None
  981. def update_candidate_strategy(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, num_matches: int):
  982. """
  983. Updates the candidate generation strategy based on the outcomes.
  984. Args:
  985. input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
  986. Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
  987. scores (`torch.FloatTensor` of shape `(batch_size, candidate_length, config.vocab_size)`):
  988. Prediction scores of a language modeling head. These can be logits for each vocabulary when not using
  989. beam search or log softmax for each vocabulary token when using beam search
  990. num_matches (`int`):
  991. The number of matches between the candidate sequences and the model predictions.
  992. """
  993. # Currently does nothing
  994. return
  995. class EarlyExitCandidateGenerator(AssistedCandidateGenerator):
  996. """
  997. `CandidateGenerator` class to be used for assisted generation and speculative decoding. This class generates
  998. candidates through the use of **the model itself**, exiting early. Can only be used with models that support early
  999. exit, e.g., `facebook/layerskip-llama3.2-1B`.
  1000. Args:
  1001. input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
  1002. Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)
  1003. assistant_model (`PreTrainedModel`):
  1004. The original model. This model must support early exit (i.e. is trained to compute logits in earlier
  1005. layers).
  1006. generation_config (`~generation.GenerationConfig`, *optional*):
  1007. The generation configuration to be used as base parametrization for the generation call.
  1008. logits_processor (`LogitsProcessorList`):
  1009. An instance of [`LogitsProcessorList`]. List of instances of class derived from [`LogitsProcessor`]
  1010. used to modify the prediction scores of the language modeling head applied at each generation step.
  1011. model_kwargs (`Dict`):
  1012. The keyword arguments that will be passed to the main model, and are used as base inputs for the assistant
  1013. model as well.
  1014. inputs_tensor (`torch.Tensor`, *optional*):
  1015. The model input tensor. In encoder-decoder models, this is the encoder input.
  1016. """
  1017. def __init__(
  1018. self,
  1019. input_ids: torch.LongTensor,
  1020. assistant_model: "PreTrainedModel",
  1021. generation_config: "GenerationConfig",
  1022. model_kwargs: dict,
  1023. inputs_tensor: torch.Tensor | None = None,
  1024. logits_processor: Optional["LogitsProcessorList"] = None,
  1025. ):
  1026. super().__init__(
  1027. input_ids=input_ids,
  1028. assistant_model=assistant_model,
  1029. generation_config=generation_config,
  1030. model_kwargs=model_kwargs,
  1031. inputs_tensor=inputs_tensor,
  1032. logits_processor=logits_processor,
  1033. )
  1034. # We have to move early exit out of the generation config, otherwise the assistant will also call `generate`
  1035. # with early exit
  1036. self.assistant_early_exit = self.generation_config.assistant_early_exit
  1037. self.generation_config.assistant_early_exit = None
  1038. def get_candidates(self, input_ids: torch.LongTensor) -> tuple[torch.LongTensor, torch.FloatTensor]:
  1039. # Temporarily sets the number of hidden layers to the early exit value
  1040. base_model = getattr(self.assistant_model, self.assistant_model.base_model_prefix)
  1041. original_num_hidden_layers = base_model.config.num_hidden_layers
  1042. base_model.config.num_hidden_layers = self.assistant_early_exit
  1043. candidate_ids, candidate_logits = super().get_candidates(input_ids)
  1044. base_model.config.num_hidden_layers = original_num_hidden_layers
  1045. return candidate_ids, candidate_logits
  1046. def _prepare_attention_mask(model_kwargs: dict[str, Any], new_length: int, is_encoder_decoder: bool) -> dict[str, Any]:
  1047. """Expands or crops the model's mask for decoding purposes, to the defined length"""
  1048. mask_key = "decoder_attention_mask" if is_encoder_decoder else "attention_mask"
  1049. if mask_key not in model_kwargs:
  1050. return model_kwargs
  1051. mask = model_kwargs[mask_key]
  1052. mask_length_diff = new_length - mask.shape[1]
  1053. if mask_length_diff < 0:
  1054. model_kwargs[mask_key] = mask[:, :mask_length_diff]
  1055. elif mask_length_diff > 0:
  1056. model_kwargs[mask_key] = torch.cat([mask, mask.new_ones((mask.shape[0], mask_length_diff))], dim=-1)
  1057. # Handle cross attention models
  1058. if "cross_attention_mask" in model_kwargs:
  1059. # Mllama case
  1060. cross_mask = model_kwargs["cross_attention_mask"]
  1061. if mask_length_diff < 0:
  1062. model_kwargs["cross_attention_mask"] = cross_mask[:, :mask_length_diff]
  1063. elif mask_length_diff > 0:
  1064. new_mask = cross_mask[:, -1:, :, :].repeat(1, mask_length_diff, 1, 1)
  1065. model_kwargs["cross_attention_mask"] = torch.cat([cross_mask, new_mask], dim=1)
  1066. elif "image_attention_mask" in model_kwargs:
  1067. # IDEFICS case
  1068. cross_mask = model_kwargs["image_attention_mask"]
  1069. if mask_length_diff < 0:
  1070. model_kwargs["image_attention_mask"] = cross_mask[:, :mask_length_diff]
  1071. elif mask_length_diff > 0:
  1072. new_mask = cross_mask[:, -1:, :].repeat(1, mask_length_diff, 1)
  1073. model_kwargs["image_attention_mask"] = torch.cat([cross_mask, new_mask], dim=1)
  1074. return model_kwargs
  1075. def _prepare_position_ids(model_kwargs: dict[str, Any], new_length: int, is_encoder_decoder: bool) -> dict[str, Any]:
  1076. """Expands or crops the model's position ids for decoding purposes, to the defined length"""
  1077. position_key = "decoder_position_ids" if is_encoder_decoder else "position_ids"
  1078. if model_kwargs.get(position_key) is None:
  1079. return model_kwargs
  1080. positions = model_kwargs[position_key]
  1081. position_length_diff = new_length - positions.shape[-1]
  1082. if position_length_diff < 0:
  1083. model_kwargs[position_key] = positions[:, :position_length_diff]
  1084. elif position_length_diff > 0:
  1085. # Works for 2D and 3D position tensors
  1086. required_dim = [1] * (positions.dim() - 1) + [-1]
  1087. next_position_ids = (
  1088. torch.arange(position_length_diff, dtype=positions.dtype, device=positions.device).view(*required_dim)
  1089. + positions[..., -1:]
  1090. + 1
  1091. )
  1092. next_position_ids = torch.cat([positions, next_position_ids], dim=-1)
  1093. model_kwargs[position_key] = next_position_ids
  1094. return model_kwargs
  1095. def _prepare_token_type_ids(model_kwargs: dict[str, Any], new_length: int) -> dict[str, Any]:
  1096. """Expands or crops the model's token_type_ids for decoding purposes, to the defined length"""
  1097. if model_kwargs.get("token_type_ids") is None:
  1098. return model_kwargs
  1099. # Multimodal models call this arg `mm_token_type_ids`
  1100. token_type_ids = model_kwargs["token_type_ids"]
  1101. final_token_type = token_type_ids[:, -1].unsqueeze(-1)
  1102. type_length_diff = new_length - token_type_ids.shape[1]
  1103. if type_length_diff < 0:
  1104. model_kwargs["token_type_ids"] = token_type_ids[:, :type_length_diff]
  1105. elif type_length_diff > 0:
  1106. token_type_copies = final_token_type.repeat(1, type_length_diff)
  1107. model_kwargs["token_type_ids"] = torch.cat([model_kwargs["token_type_ids"], token_type_copies], dim=-1)
  1108. return model_kwargs