test_rejection_sampling.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. """Tests for rejection sampling."""
  2. from typing import List, Tuple
  3. import pytest
  4. import torch
  5. import torch.nn.functional as F
  6. from aphrodite.modeling.layers.rejection_sampler import RejectionSampler
  7. from aphrodite.modeling.utils import set_random_seed
  8. CUDA_DEVICES = [
  9. f"cuda:{i}" for i in range(1 if torch.cuda.device_count() == 1 else 2)
  10. ]
  11. def mock_causal_accepted_tensor(
  12. k: int, last_accepted_indices: torch.Tensor) -> torch.Tensor:
  13. """Generate an "accepted" tensor which should yield causally-accepted tokens
  14. up to last accepted indices.
  15. Tokens after last_accepted_indices+1 may also be accepted, although they
  16. will not be causally accepted.
  17. """
  18. batch_size = last_accepted_indices.shape[0]
  19. accepted = (torch.arange(k).expand(batch_size, k) <=
  20. last_accepted_indices.unsqueeze(-1).broadcast_to(
  21. batch_size, k))
  22. # Sprinkle accepted values after the contiguous initial accepted values.
  23. # This replicates the behavior of rejection sampling, which may "accept"
  24. # a token that cannot be accepted because of causality.
  25. sprinkle_candidates = (
  26. torch.arange(k).expand(batch_size, k) >
  27. last_accepted_indices.unsqueeze(-1).broadcast_to(batch_size, k) + 1)
  28. sprinkle = torch.rand(batch_size, k) > 0.5
  29. accepted[sprinkle_candidates] = sprinkle[sprinkle_candidates]
  30. return accepted
  31. @pytest.mark.parametrize("seed", list(range(10)))
  32. @pytest.mark.parametrize(
  33. "which_tokens_accepted",
  34. ["all_tokens_accepted", "no_tokens_accepted", "some_tokens_accepted"])
  35. @pytest.mark.parametrize("disable_bonus_tokens", [True, False])
  36. @pytest.mark.parametrize("device", CUDA_DEVICES)
  37. @torch.inference_mode()
  38. def test_correct_output_format(which_tokens_accepted: str,
  39. disable_bonus_tokens: bool, seed: int,
  40. device: str):
  41. """Verify the output has correct format given predetermined accepted matrix.
  42. """
  43. set_random_seed(seed)
  44. torch.set_default_device(device)
  45. batch_size = 10
  46. k = 5
  47. vocab_size = 3000
  48. if which_tokens_accepted == "all_tokens_accepted":
  49. accepted = mock_causal_accepted_tensor(
  50. k, -1 + k * torch.ones((batch_size, ), dtype=torch.long))
  51. elif which_tokens_accepted == "no_tokens_accepted":
  52. accepted = mock_causal_accepted_tensor(
  53. k, -torch.ones((batch_size, ), dtype=torch.long))
  54. elif which_tokens_accepted == "some_tokens_accepted":
  55. last_accepted_indices = torch.randint(low=-1,
  56. high=k,
  57. size=(batch_size, ))
  58. accepted = mock_causal_accepted_tensor(k, last_accepted_indices)
  59. else:
  60. raise AssertionError()
  61. recovered_token_ids = torch.randint(low=0,
  62. high=vocab_size,
  63. size=(batch_size, k),
  64. dtype=torch.int64)
  65. draft_token_ids = torch.randint(low=0,
  66. high=vocab_size,
  67. size=(batch_size, k),
  68. dtype=torch.int64)
  69. bonus_token_ids = torch.randint(low=0,
  70. high=vocab_size,
  71. size=(batch_size, 1),
  72. dtype=torch.int64)
  73. rejection_sampler = RejectionSampler(
  74. disable_bonus_tokens=disable_bonus_tokens)
  75. rejection_sampler.init_gpu_tensors(device=device)
  76. output_token_ids = rejection_sampler._create_output( # pylint: disable=protected-access
  77. accepted,
  78. recovered_token_ids,
  79. draft_token_ids,
  80. bonus_token_ids,
  81. )
  82. expected_bonus_token_ids = bonus_token_ids.clone()
  83. # If bonus tokens disabled. Verify they are set to -1.
  84. if disable_bonus_tokens:
  85. expected_bonus_token_ids = expected_bonus_token_ids * 0 - 1
  86. if which_tokens_accepted == "all_tokens_accepted":
  87. # Expect all tokens to be equal to draft tokens.
  88. assert torch.equal(output_token_ids[:, :-1], draft_token_ids)
  89. # Expect all bonus tokens to be included.
  90. assert torch.equal(output_token_ids[:, -1:], expected_bonus_token_ids)
  91. elif which_tokens_accepted == "no_tokens_accepted":
  92. # Expect first token to be equal to recovered tokens.
  93. assert torch.equal(output_token_ids[:, 0], recovered_token_ids[:, 0])
  94. # Expect everything else to be -1.
  95. assert torch.equal(output_token_ids[:, 1:],
  96. torch.ones_like(output_token_ids[:, 1:]) * -1)
  97. elif which_tokens_accepted == "some_tokens_accepted":
  98. recovered_plus_bonus = torch.cat(
  99. (recovered_token_ids, expected_bonus_token_ids), dim=-1)
  100. # Assert first rejected token is a recovered token or bonus token.
  101. assert torch.equal(
  102. recovered_plus_bonus[torch.arange(0, batch_size),
  103. last_accepted_indices + 1],
  104. output_token_ids[torch.arange(0, batch_size),
  105. last_accepted_indices + 1])
  106. # Assert every subsequent token is -1.
  107. subsequent_mask = torch.arange(0, k + 1).expand(
  108. batch_size, k + 1) >= (last_accepted_indices + 2).unsqueeze(-1)
  109. assert torch.all(output_token_ids[subsequent_mask] == -1)
  110. @pytest.mark.parametrize("k", list(range(1, 6)))
  111. @pytest.mark.parametrize("vocab_size", [30_000, 50_000])
  112. @pytest.mark.parametrize("batch_size", list(range(1, 32)))
  113. @pytest.mark.parametrize("device", CUDA_DEVICES)
  114. @torch.inference_mode()
  115. def test_no_crash_with_varying_dims(k: int, vocab_size: int, batch_size: int,
  116. device: str):
  117. torch.set_default_device(device)
  118. rejection_sampler = RejectionSampler()
  119. rejection_sampler.init_gpu_tensors(device=device)
  120. draft_probs = torch.rand(batch_size, k, vocab_size, dtype=torch.float32)
  121. target_probs = torch.rand(batch_size, k, vocab_size, dtype=torch.float32)
  122. bonus_token_ids = torch.randint(low=0,
  123. high=vocab_size,
  124. size=(batch_size, 1),
  125. dtype=torch.int64)
  126. draft_token_ids = torch.randint(low=0,
  127. high=vocab_size,
  128. size=(batch_size, k),
  129. dtype=torch.int64)
  130. rejection_sampler(target_probs, bonus_token_ids, draft_probs,
  131. draft_token_ids)
  132. @pytest.mark.parametrize("frac_seeded", [0.0, 0.25, 0.5, 1.0])
  133. @pytest.mark.parametrize("k", [1, 3, 6])
  134. @pytest.mark.parametrize("vocab_size", [30_000, 50_000])
  135. @pytest.mark.parametrize("batch_size", [1, 8, 32, 128])
  136. @pytest.mark.parametrize("n_rep", [100])
  137. @pytest.mark.parametrize("device", CUDA_DEVICES)
  138. @torch.inference_mode()
  139. def test_deterministic_when_seeded(k: int, vocab_size: int, batch_size: int,
  140. frac_seeded: float, n_rep: int,
  141. device: str):
  142. torch.set_default_device(device)
  143. rejection_sampler = RejectionSampler()
  144. rejection_sampler.init_gpu_tensors(device=device)
  145. draft_probs = torch.rand(batch_size, k, vocab_size, dtype=torch.float32)
  146. target_probs = torch.rand(batch_size, k, vocab_size, dtype=torch.float32)
  147. bonus_token_ids = torch.randint(low=0,
  148. high=vocab_size,
  149. size=(batch_size, 1),
  150. dtype=torch.int64)
  151. draft_token_ids = torch.randint(low=0,
  152. high=vocab_size,
  153. size=(batch_size, k),
  154. dtype=torch.int64)
  155. seeded_mask = torch.rand(batch_size, dtype=torch.float32) <= frac_seeded
  156. results = []
  157. for _ in range(n_rep):
  158. seeded_seqs = {
  159. i: torch.Generator(device=device).manual_seed(i)
  160. for i in range(batch_size) if seeded_mask[i]
  161. }
  162. results.append(
  163. rejection_sampler(target_probs, bonus_token_ids, draft_probs,
  164. draft_token_ids, seeded_seqs))
  165. for i in range(batch_size):
  166. if seeded_mask[i]:
  167. for j in range(1, n_rep):
  168. assert torch.equal(results[j][i], results[0][i])
  169. @pytest.mark.parametrize("above_or_below_vocab_range", ["above", "below"])
  170. @pytest.mark.parametrize("which_token_ids",
  171. ["bonus_token_ids", "draft_token_ids"])
  172. @pytest.mark.parametrize("device", CUDA_DEVICES)
  173. @torch.inference_mode()
  174. def test_raises_when_vocab_oob(above_or_below_vocab_range: str,
  175. which_token_ids: str, device: str):
  176. k = 3
  177. batch_size = 5
  178. vocab_size = 30_000
  179. torch.set_default_device(device)
  180. rejection_sampler = RejectionSampler(strict_mode=True)
  181. rejection_sampler.init_gpu_tensors(device=device)
  182. draft_probs = torch.rand(batch_size, k, vocab_size, dtype=torch.float32)
  183. target_probs = torch.rand(batch_size, k, vocab_size, dtype=torch.float32)
  184. bonus_token_ids = torch.randint(low=0,
  185. high=vocab_size,
  186. size=(batch_size, 1),
  187. dtype=torch.int64)
  188. draft_token_ids = torch.randint(low=0,
  189. high=vocab_size,
  190. size=(batch_size, k),
  191. dtype=torch.int64)
  192. oob_token_ids = None
  193. if which_token_ids == "bonus_token_ids":
  194. oob_token_ids = bonus_token_ids
  195. elif which_token_ids == "draft_token_ids":
  196. oob_token_ids = draft_token_ids
  197. else:
  198. raise AssertionError()
  199. if above_or_below_vocab_range == "above":
  200. rogue_token_id = vocab_size + 1
  201. elif above_or_below_vocab_range == "below":
  202. rogue_token_id = -1
  203. else:
  204. raise AssertionError()
  205. oob_token_ids[0][0] = rogue_token_id
  206. with pytest.raises(AssertionError):
  207. rejection_sampler(target_probs, bonus_token_ids, draft_probs,
  208. draft_token_ids)
  209. @pytest.mark.parametrize("draft_and_target_probs_equal", [True, False])
  210. @pytest.mark.parametrize("seed", list(range(5)))
  211. @torch.inference_mode()
  212. def test_rejection_sampling_approximates_target_distribution(
  213. seed: int, draft_and_target_probs_equal: bool):
  214. """Verify rejection sampling approximates target distribution,
  215. despite sampling from a potentially distinct draft distribution.
  216. This is done by first creating a random target probability
  217. distribution and a random draft probability distribution. We then
  218. sample token ids from the rejection sampler using these draft
  219. and target distributions. The samples are used to estimate
  220. the output probability distribution, which we expect to approximate
  221. the target distribution.
  222. A basic distance metric is used to determine similarity between
  223. distributions.
  224. We expect that as we increase the number of samples,
  225. the distance between the observed distribution and the target
  226. distribution decreases. To measure this, we compare the distance
  227. of the observed distribution against both the target distribution
  228. and a uniform random distribution. We expect the distance between
  229. the observed distribution and the target distribution to improve
  230. much more than the distance improvement between the observed
  231. distribution and the random distribution.
  232. When draft_and_target_probs_equal=True, the draft and target
  233. probabilities are exactly equal. Rejection sampling should
  234. still work without any NaNs or exceptions.
  235. """
  236. torch.set_default_device("cpu")
  237. set_random_seed(seed)
  238. helper = _CorrectnessTestHelper(
  239. vocab_size=10,
  240. rejection_sampler=RejectionSampler(),
  241. )
  242. draft_probs, target_probs, reference_probs = helper.generate_probs_for_test(
  243. draft_and_target_probs_equal)
  244. sample_sizes = [10, 100, 1_000, 10_000, 100_000]
  245. distance_wrt_reference: List[float] = []
  246. distance_wrt_target: List[float] = []
  247. for num_samples in sample_sizes:
  248. (reference_vs_rejsample_dist,
  249. target_vs_rejsample_dist) = helper.run_and_compare_distributions(
  250. draft_probs,
  251. target_probs,
  252. reference_probs,
  253. num_samples,
  254. )
  255. distance_wrt_reference.append(reference_vs_rejsample_dist)
  256. distance_wrt_target.append(target_vs_rejsample_dist)
  257. relative_change_in_distance_wrt_target = get_ratio_first_to_last(
  258. distance_wrt_target)
  259. relative_change_in_distance_wrt_reference = get_ratio_first_to_last(
  260. distance_wrt_reference)
  261. print(f"{num_samples=} {target_vs_rejsample_dist=:.05f} "
  262. f"{reference_vs_rejsample_dist=:.05f}")
  263. print(f"{num_samples=} {relative_change_in_distance_wrt_target=:.02f} "
  264. f"{relative_change_in_distance_wrt_reference=:.02f}")
  265. relative_change_in_distance_wrt_target = get_ratio_first_to_last(
  266. distance_wrt_target)
  267. relative_change_in_distance_wrt_reference = get_ratio_first_to_last(
  268. distance_wrt_reference)
  269. expected_improvement_multiplier = 20
  270. assert (relative_change_in_distance_wrt_target >
  271. relative_change_in_distance_wrt_reference *
  272. expected_improvement_multiplier)
  273. def get_ratio_first_to_last(elements: List[float]) -> float:
  274. return elements[0] / elements[-1]
  275. class _CorrectnessTestHelper:
  276. """Class that packages together logic required for the unit-level
  277. rejection sampling correctness test.
  278. """
  279. def __init__(self, vocab_size: int, rejection_sampler: RejectionSampler):
  280. self.rejection_sampler = rejection_sampler
  281. self.vocab_size = vocab_size
  282. self.vocab_range = (0, vocab_size)
  283. self.rejection_sampler.init_gpu_tensors(device=0)
  284. # Keep test simple, use k=1
  285. self.k = 1
  286. # Bonus tokens not used, but rejection sampler requires
  287. # correct shape.
  288. self.num_bonus_tokens = 1
  289. def generate_probs_for_test(
  290. self, draft_and_target_probs_equal: bool
  291. ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
  292. draft_probs, target_probs = [
  293. F.softmax(
  294. torch.rand(self.vocab_size, dtype=torch.float32),
  295. dim=-1,
  296. ) for _ in range(2)
  297. ]
  298. num_reference_probs = 100
  299. reference_probs = F.softmax(
  300. torch.rand(num_reference_probs,
  301. self.vocab_size,
  302. dtype=torch.float32),
  303. dim=-1,
  304. )
  305. if draft_and_target_probs_equal:
  306. target_probs = draft_probs.clone()
  307. return draft_probs, target_probs, reference_probs
  308. def run_and_compare_distributions(self, draft_probs: torch.Tensor,
  309. target_probs: torch.Tensor,
  310. reference_probs: torch.Tensor,
  311. num_samples: int) -> Tuple[float, float]:
  312. # Sample using rejection sampling.
  313. rej_sample_probs = self._estimate_rejection_sampling_pdf(
  314. draft_probs, target_probs, num_samples)
  315. # Average distance from reference probs.
  316. reference_vs_rejsample_dist = torch.dist(
  317. reference_probs,
  318. rej_sample_probs).item() / reference_probs.shape[0]
  319. target_vs_rejsample_dist = torch.dist(target_probs,
  320. rej_sample_probs).item()
  321. return reference_vs_rejsample_dist, target_vs_rejsample_dist
  322. def _estimate_rejection_sampling_pdf(
  323. self,
  324. draft_probs: torch.Tensor,
  325. target_probs: torch.Tensor,
  326. num_samples: int,
  327. ) -> torch.Tensor:
  328. # Repeat draft probs num_samples times.
  329. draft_probs = draft_probs.reshape(1, self.k, self.vocab_size).repeat(
  330. num_samples, 1, 1)
  331. # Repeat target probs num_samples * k times.
  332. # Rejection sampler requires bonus token probs, but they aren't used.
  333. target_probs = target_probs.reshape(1, 1, self.vocab_size).repeat(
  334. num_samples, self.k, 1)
  335. # Randomly sample draft token ids from draft probs.
  336. draft_token_ids = torch.multinomial(draft_probs[:, 0, :],
  337. num_samples=1,
  338. replacement=True).reshape(
  339. num_samples, self.k)
  340. # Bonus tokens not used but required.
  341. bonus_token_ids = torch.zeros((1, self.num_bonus_tokens),
  342. dtype=torch.int64,
  343. device="cuda").repeat(num_samples, 1)
  344. # Get output tokens via rejection sampling.
  345. output_token_ids = self.rejection_sampler(target_probs.to("cuda"),
  346. bonus_token_ids.to("cuda"),
  347. draft_probs.to("cuda"),
  348. draft_token_ids.to("cuda"))
  349. # Remove bonus tokens
  350. output_token_ids = output_token_ids[:, :-1].flatten()
  351. # Estimate probability density function
  352. hist = torch.histogram(output_token_ids.to(dtype=torch.float,
  353. device="cpu"),
  354. bins=self.vocab_size,
  355. range=self.vocab_range,
  356. density=True)
  357. return hist.hist