sampling_params.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. """Sampling parameters for text generation."""
  2. import copy
  3. import os
  4. from enum import IntEnum
  5. from functools import cached_property
  6. from typing import Any, Callable, Dict, List, Optional, Union
  7. import torch
  8. from loguru import logger
  9. from pydantic import Field
  10. from typing_extensions import Annotated
  11. _SAMPLING_EPS = 1e-5
  12. APHRODITE_NO_DEPRECATION_WARNING = bool(
  13. int(os.environ.get("APHRODITE_NO_DEPRECATION_WARNING", "0")))
  14. class SamplingType(IntEnum):
  15. GREEDY = 0
  16. RANDOM = 1
  17. RANDOM_SEED = 2
  18. BEAM = 3
  19. LogitsProcessorFunc = Union[Callable[[List[int], torch.Tensor], torch.Tensor],
  20. Callable[[List[int], List[int], torch.Tensor],
  21. torch.Tensor]]
  22. """LogitsProcessor is a function that takes a list
  23. of previously generated tokens, the logits tensor
  24. for the next token and, optionally, prompt tokens as a
  25. first argument, and returns a modified tensor of logits
  26. to sample from."""
  27. class SamplingParams:
  28. """Sampling parameters for text generation.
  29. Overall, we follow the sampling parameters from the OpenAI text completion
  30. API (https://platform.openai.com/docs/api-reference/completions/create).
  31. In addition, we support multiple additional samplers which are not supported
  32. by OpenAI.
  33. Args:
  34. n: Number of output sequences to return for the given prompt.
  35. best_of: Number of output sequences that are generated from the prompt.
  36. From these `best_of` sequences, the top `n` sequences are returned.
  37. `best_of` must be greater than or equal to `n`. This is treated as
  38. the beam width when `use_beam_search` is True. By default, `best_of`
  39. is set to `n`.
  40. presence_penalty: Float that penalizes new tokens based on whether they
  41. appear in the generated text so far. Values > 0 encourage the model
  42. to use new tokens, while values < 0 encourage the model to repeat
  43. tokens.
  44. frequency_penalty: Float that penalizes new tokens based on their
  45. frequency in the generated text so far. Values > 0 encourage the
  46. model to use new tokens, while values < 0 encourage the model to
  47. repeat tokens.
  48. repetition_penalty: Float that penalizes new tokens based on their
  49. frequency in the generated text so far.
  50. freq_pen is applied additively while
  51. rep_pen is applied multiplicatively.
  52. Must be in [1, inf). Set to 1 to disable the effect.
  53. temperature: Float that controls the randomness of the sampling. Lower
  54. values make the model more deterministic, while higher values make
  55. the model more random. Zero means greedy sampling.
  56. top_p: Float that controls the cumulative probability of the top tokens
  57. to consider. Must be in (0, 1]. Set to 1 to consider all tokens.
  58. top_k: Integer that controls the number of top tokens to consider. Set
  59. to -1 to consider all tokens.
  60. top_a: Float that controls the cutoff for Top-A sampling.
  61. Exact cutoff is top_a*max_prob**2. Must be in [0,inf], 0 to disable.
  62. min_p: Float that controls the cutoff for min-p sampling.
  63. Exact cutoff is min_p*max_prob. Must be in [0,1], 0 to disable.
  64. tfs: Float that controls the cumulative approximate curvature of the
  65. distribution to retain for Tail Free Sampling.
  66. Must be in (0, 1]. Set to 1 to disable
  67. eta_cutoff: Float that controls the cutoff threshold for Eta sampling
  68. (a form of entropy adaptive truncation sampling)
  69. threshold is computed as min(eta, sqrt(eta)*entropy(probs)).
  70. Specified in units of 1e-4. Set to 0 to disable
  71. epsilon_cutoff: Float that controls the cutoff threshold for
  72. Epsilon sampling (simple probability threshold truncation).
  73. Specified in units of 1e-4. Set to 0 to disable.
  74. typical_p: Float that controls the cumulative probability of tokens
  75. closest in surprise to the expected surprise to consider.
  76. Must be in (0, 1]. Set to 1 to disable.
  77. mirostat_mode: Can either be 0 (disabled) or 2 (Mirostat v2).
  78. mirostat_tau: Target "surprisal" that mirostat works towards.
  79. Range [0, inf).
  80. mirostat_eta: Rate at which mirostat updates its internal surprisal
  81. value. Range [0, inf).
  82. dynatemp_min: Minimum temperature for dynatemp sampling.
  83. Range [0, inf).
  84. dynatemp_max: Maximum temperature for dynatemp sampling.
  85. Range [0, inf).
  86. dynatemp_exponent: Exponent for dynatemp sampling. Range [0, inf).
  87. smoothing_factor: Smoothing factor for Quadratic Sampling.
  88. smoothing_curve: Smoothing curve for Quadratic (Cubic) Sampling.
  89. seed: Random seed to use for the generation.
  90. use_beam_search: Whether to use beam search instead of sampling.
  91. length_penalty: Float that penalizes sequences based on their length.
  92. Used in beam search.
  93. early_stopping: Controls the stopping condition for beam search. It
  94. accepts the following values: `True`, where the generation stops as
  95. soon as there are `best_of` complete candidates; `False`, where an
  96. heuristic is applied and the generation stops when is it very
  97. unlikely to find better candidates; `"never"`, where the beam search
  98. procedure only stops when there cannot be better candidates
  99. (canonical beam search algorithm).
  100. stop: List of strings that stop the generation when they are generated.
  101. The returned output will not contain the stop strings.
  102. stop_token_ids: List of tokens that stop the generation when they are
  103. generated. The returned output will contain the stop tokens unless
  104. the stop tokens are special tokens.
  105. include_stop_str_in_output: Whether to include the stop strings in
  106. output text. Defaults to False.
  107. ignore_eos: Whether to ignore the EOS token and continue generating
  108. tokens after the EOS token is generated.
  109. max_tokens: Maximum number of tokens to generate per output sequence.
  110. min_tokens: Minimum number of tokens to generate per output sequence
  111. before EOS or stop tokens are generated.
  112. logprobs: Number of log probabilities to return per output token.
  113. When set to None, no probability is returned. If set to a non-None
  114. value, the result includes the log probabilities of the specified
  115. number of most likely tokens, as well as the chosen tokens.
  116. Note that the implementation follows the OpenAI API: The API will
  117. always return the log probability of the sampled token, so there
  118. may be up to `logprobs+1` elements in the response.
  119. prompt_logprobs: Number of log probabilities to return per prompt token.
  120. detokenize: Whether to detokenize the output. Defaults to True.
  121. custom_token_bans: List of token IDs to ban from generating
  122. skip_special_tokens: Whether to skip special tokens in the output.
  123. defaults to true.
  124. spaces_between_special_tokens: Whether to add spaces between special
  125. tokens in the output. Defaults to True.
  126. logits_processors: List of functions that modify logits based on
  127. previously generated tokens, and optionally prompt tokens as
  128. a first argument.
  129. truncate_prompt_tokens: If set to an integer k, will use only the last
  130. k tokens from the prompt (i.e. left-truncation). Defaults to None
  131. (i.e. no truncation).
  132. """
  133. def __init__(
  134. self,
  135. n: int = 1,
  136. best_of: Optional[int] = None,
  137. presence_penalty: float = 0.0,
  138. frequency_penalty: float = 0.0,
  139. repetition_penalty: float = 1.0,
  140. temperature: float = 1.0,
  141. top_p: float = 1.0,
  142. top_k: int = -1,
  143. top_a: float = 0.0,
  144. min_p: float = 0.0,
  145. tfs: float = 1.0,
  146. eta_cutoff: float = 0.0,
  147. epsilon_cutoff: float = 0.0,
  148. typical_p: float = 1.0,
  149. smoothing_factor: float = 0.0,
  150. smoothing_curve: float = 1.0,
  151. seed: Optional[int] = None,
  152. use_beam_search: bool = False,
  153. length_penalty: float = 1.0,
  154. early_stopping: Union[bool, str] = False,
  155. stop: Union[None, str, List[str]] = None,
  156. stop_token_ids: Optional[List[int]] = None,
  157. include_stop_str_in_output: bool = False,
  158. ignore_eos: bool = False,
  159. max_tokens: Optional[int] = 16,
  160. min_tokens: int = 0,
  161. logprobs: Optional[int] = None,
  162. prompt_logprobs: Optional[int] = None,
  163. detokenize: bool = True,
  164. custom_token_bans: Optional[List[int]] = None,
  165. skip_special_tokens: bool = True,
  166. spaces_between_special_tokens: bool = True,
  167. logits_processors: Optional[List[LogitsProcessorFunc]] = None,
  168. truncate_prompt_tokens: Optional[Annotated[int, Field(ge=1)]] = None,
  169. ) -> None:
  170. self.n = n
  171. self.best_of = best_of if best_of is not None else n
  172. self.presence_penalty = presence_penalty
  173. self.frequency_penalty = frequency_penalty
  174. self.repetition_penalty = repetition_penalty
  175. self.temperature = temperature
  176. self.top_p = top_p
  177. self.top_k = top_k
  178. self.top_a = top_a
  179. self.min_p = min_p
  180. self.tfs = tfs
  181. self.eta_cutoff = eta_cutoff
  182. self.epsilon_cutoff = epsilon_cutoff
  183. self.typical_p = typical_p
  184. self.smoothing_factor = smoothing_factor
  185. self.smoothing_curve = smoothing_curve
  186. if seed == -1:
  187. self.seed = None
  188. else:
  189. self.seed = seed
  190. self.use_beam_search = use_beam_search
  191. self.length_penalty = length_penalty
  192. self.early_stopping = early_stopping
  193. if stop is None:
  194. self.stop = []
  195. elif isinstance(stop, str):
  196. self.stop = [stop]
  197. else:
  198. self.stop = list(stop)
  199. self.stop_token_ids = stop_token_ids or []
  200. self.ignore_eos = ignore_eos
  201. self.max_tokens = max_tokens
  202. self.min_tokens = min_tokens
  203. self.logprobs = 1 if logprobs is True else logprobs
  204. self.prompt_logprobs = 1 if prompt_logprobs is True else prompt_logprobs
  205. # NOTE: This parameter is only exposed at the engine level for now.
  206. # It is not exposed in the OpenAI API server, as the OpenAI API does
  207. # not support returning only a list of token IDs.
  208. self.detokenize = detokenize
  209. self.custom_token_bans = custom_token_bans or []
  210. self.skip_special_tokens = skip_special_tokens
  211. self.spaces_between_special_tokens = spaces_between_special_tokens
  212. self.logits_processors = logits_processors or []
  213. self.include_stop_str_in_output = include_stop_str_in_output
  214. self.truncate_prompt_tokens = truncate_prompt_tokens
  215. # Number of characters to hold back for stop string evaluation
  216. # until sequence is finished.
  217. if self.stop and not include_stop_str_in_output:
  218. self.output_text_buffer_length = max(len(s) for s in self.stop) - 1
  219. else:
  220. self.output_text_buffer_length = 0
  221. self.default_values = {
  222. "n": 1,
  223. "best_of": 1,
  224. "presence_penalty": 0.0,
  225. "frequency_penalty": 0.0,
  226. "repetition_penalty": 1.0,
  227. "temperature": 1.0,
  228. "top_p": 1.0,
  229. "top_k": -1,
  230. "top_a": 0.0,
  231. "min_p": 0.0,
  232. "tfs": 1.0,
  233. "eta_cutoff": 0.0,
  234. "epsilon_cutoff": 0.0,
  235. "typical_p": 1.0,
  236. "smoothing_factor": 0.0,
  237. "smoothing_curve": 1.0,
  238. "seed": None,
  239. "use_beam_search": False,
  240. "length_penalty": 1.0,
  241. "early_stopping": False,
  242. "stop": [],
  243. "stop_token_ids": [],
  244. "ignore_eos": False,
  245. "max_tokens": 16,
  246. "min_tokens": 0,
  247. "logprobs": None,
  248. "prompt_logprobs": None,
  249. "detokenize": True,
  250. "custom_token_bans": [],
  251. "skip_special_tokens": True,
  252. "spaces_between_special_tokens": True,
  253. "include_stop_str_in_output": False,
  254. "truncate_prompt_tokens": None,
  255. }
  256. # Number of characters to hold back for stop string evaluation
  257. # until sequence is finished.
  258. if self.stop and not include_stop_str_in_output:
  259. self.output_text_buffer_length = max(len(s) for s in self.stop) - 1
  260. else:
  261. self.output_text_buffer_length = 0
  262. self._verify_args()
  263. if self.use_beam_search:
  264. if not APHRODITE_NO_DEPRECATION_WARNING:
  265. logger.warning(
  266. "[IMPORTANT] We plan to discontinue the support for beam "
  267. "search in the next major release. Set "
  268. "APHRODITE_NO_DEPRECATION_WARNING=1 to "
  269. "suppress this warning.")
  270. self._verify_beam_search()
  271. else:
  272. self._verify_non_beam_search()
  273. if self.temperature < _SAMPLING_EPS:
  274. # Zero temperature means greedy sampling.
  275. self.top_p = 1.0
  276. self.top_k = -1
  277. self.min_p = 0.0
  278. self.top_a = 0.0
  279. self._verify_greedy_sampling()
  280. # eos_token_id is added to this by the engine
  281. self.all_stop_token_ids = set(self.stop_token_ids)
  282. def _verify_args(self) -> None:
  283. if self.n < 1:
  284. raise ValueError(f"n must be at least 1, got {self.n}.")
  285. if self.best_of < self.n:
  286. raise ValueError(f"best_of must be greater than or equal to n, "
  287. f"got n={self.n} and best_of={self.best_of}.")
  288. if not -2.0 <= self.presence_penalty <= 2.0:
  289. raise ValueError("presence_penalty must be in [-2, 2], got "
  290. f"{self.presence_penalty}.")
  291. if not -2.0 <= self.frequency_penalty <= 2.0:
  292. raise ValueError("frequency_penalty must be in [-2, 2], got "
  293. f"{self.frequency_penalty}.")
  294. if self.repetition_penalty < 1.0:
  295. raise ValueError("repetition_penalty must be in [1, inf), got "
  296. f"{self.repetition_penalty}.")
  297. if self.temperature < 0.0:
  298. raise ValueError(
  299. f"temperature must be non-negative, got {self.temperature}.")
  300. if not 0.0 < self.top_p <= 1.0:
  301. raise ValueError(f"top_p must be in (0, 1], got {self.top_p}.")
  302. if self.top_k < -1 or self.top_k == 0:
  303. raise ValueError(f"top_k must be -1 (disable), or at least 1, "
  304. f"got {self.top_k}.")
  305. if self.top_a < 0:
  306. raise ValueError(f"top_a must be non negative, got {self.top_a}.")
  307. if not 0.0 <= self.min_p <= 1.0:
  308. raise ValueError(f"min_p must be in [0, 1], got {self.min_p}.")
  309. if not 0.0 < self.tfs <= 1.0:
  310. raise ValueError(f"tfs must be in (0, 1], got {self.tfs}.")
  311. if self.epsilon_cutoff < 0.0 or self.epsilon_cutoff > 1000.0:
  312. raise ValueError("epsilon_cutoff must be in [0, 1000], got "
  313. f"{self.epsilon_cutoff}.")
  314. # pylint: disable=unneeded-not
  315. if not self.eta_cutoff >= 0:
  316. raise ValueError(
  317. f"eta_cutoff must be non negative, got {self.eta_cutoff}.")
  318. if not 0.0 <= self.typical_p <= 1.0:
  319. raise ValueError(
  320. f"typical_p must be in (0, 1], got {self.typical_p}.")
  321. if self.max_tokens is not None and self.max_tokens < 1:
  322. raise ValueError(
  323. f"max_tokens must be at least 1, got {self.max_tokens}.")
  324. if self.min_tokens < 0:
  325. raise ValueError(f"min_tokens must be greater than or equal to 0, "
  326. f"got {self.min_tokens}.")
  327. if self.max_tokens is not None and self.min_tokens > self.max_tokens:
  328. raise ValueError(
  329. f"min_tokens must be less than or equal to "
  330. f"max_tokens={self.max_tokens}, got {self.min_tokens}.")
  331. if self.logprobs is not None and self.logprobs < 0:
  332. raise ValueError(
  333. f"logprobs must be non-negative, got {self.logprobs}.")
  334. if self.prompt_logprobs is not None and self.prompt_logprobs < 0:
  335. raise ValueError("prompt_logprobs must be non-negative, got "
  336. f"{self.prompt_logprobs}.")
  337. if (self.truncate_prompt_tokens is not None
  338. and self.truncate_prompt_tokens < 1):
  339. raise ValueError(f"truncate_prompt_tokens must be >= 1, "
  340. f"got {self.truncate_prompt_tokens}")
  341. if any(not stop_str for stop_str in self.stop):
  342. raise ValueError("stop cannot contain an empty string.")
  343. if self.stop and not self.detokenize:
  344. raise ValueError(
  345. "stop strings are only supported when detokenize is True. "
  346. "Set detokenize=True to use stop.")
  347. def _verify_beam_search(self) -> None:
  348. if self.best_of == 1:
  349. raise ValueError("best_of must be greater than 1 when using beam "
  350. f"search. Got {self.best_of}.")
  351. if self.temperature > _SAMPLING_EPS:
  352. raise ValueError("temperature must be 0 when using beam search.")
  353. if self.top_p < 1.0 - _SAMPLING_EPS:
  354. raise ValueError("top_p must be 1 when using beam search.")
  355. if self.top_k != -1:
  356. raise ValueError("top_k must be -1 when using beam search.")
  357. if self.early_stopping not in [True, False, "never"]:
  358. raise ValueError(
  359. f"early_stopping must be True, False, or 'never', "
  360. f"got {self.early_stopping}.")
  361. def _verify_non_beam_search(self) -> None:
  362. if self.early_stopping is not False:
  363. raise ValueError("early_stopping is not effective and must be "
  364. "False when not using beam search.")
  365. if (self.length_penalty < 1.0 - _SAMPLING_EPS
  366. or self.length_penalty > 1.0 + _SAMPLING_EPS):
  367. raise ValueError(
  368. "length_penalty is not effective and must be the "
  369. "default value of 1.0 when not using beam search.")
  370. def _verify_greedy_sampling(self) -> None:
  371. if self.best_of > 1:
  372. raise ValueError("best_of must be 1 when using greedy sampling."
  373. f"Got {self.best_of}.")
  374. if self.top_p < 1.0 - _SAMPLING_EPS:
  375. raise ValueError("top_p must be 1 when using greedy sampling.")
  376. if self.top_k != -1:
  377. raise ValueError("top_k must be -1 when using greedy sampling.")
  378. def update_from_generation_config(
  379. self,
  380. generation_config: Dict[str, Any],
  381. model_eos_token_id: Optional[int] = None) -> None:
  382. """Update if there are non-default values from generation_config"""
  383. if model_eos_token_id is not None:
  384. # Add the eos token id into the sampling_params to support
  385. # min_tokens processing.
  386. self.all_stop_token_ids.add(model_eos_token_id)
  387. # Update eos_token_id for generation
  388. if (eos_ids := generation_config.get("eos_token_id")) is not None:
  389. # it can be either int or list of int
  390. eos_ids = {eos_ids} if isinstance(eos_ids, int) else set(eos_ids)
  391. if model_eos_token_id is not None:
  392. # We don't need to include the primary eos_token_id in
  393. # stop_token_ids since it's handled separately for stopping
  394. # purposes.
  395. eos_ids.discard(model_eos_token_id)
  396. if eos_ids:
  397. self.all_stop_token_ids.update(eos_ids)
  398. if not self.ignore_eos:
  399. eos_ids.update(self.stop_token_ids)
  400. self.stop_token_ids = list(eos_ids)
  401. @cached_property
  402. def sampling_type(self) -> SamplingType:
  403. if self.use_beam_search:
  404. return SamplingType.BEAM
  405. if self.temperature < _SAMPLING_EPS:
  406. return SamplingType.GREEDY
  407. if self.seed is not None:
  408. return SamplingType.RANDOM_SEED
  409. return SamplingType.RANDOM
  410. def clone(self) -> "SamplingParams":
  411. """Deep copy excluding LogitsProcessor objects.
  412. LogitsProcessor objects are excluded because they may contain an
  413. arbitrary, nontrivial amount of data.
  414. """
  415. logit_processor_refs = None if self.logits_processors is None else {
  416. id(lp): lp
  417. for lp in self.logits_processors
  418. }
  419. return copy.deepcopy(self, memo=logit_processor_refs)
  420. def __repr__(self) -> str:
  421. repr_str = "SamplingParams("
  422. for param, default_value in self.default_values.items():
  423. current_value = getattr(self, param)
  424. if current_value != default_value:
  425. repr_str += f"{param}={current_value}, "
  426. repr_str = repr_str.rstrip(', ') + ")"
  427. return repr_str