sampling.cuh 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. /*
  2. * Copyright (c) 2024 by PygmalionAI team.
  3. * Copyright (c) 2024 by FlashInfer team.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #ifndef APHRODITE_SAMPLING_CUH_
  18. #define APHRODITE_SAMPLING_CUH_
  19. #include <cub/block/block_adjacent_difference.cuh>
  20. #include <cub/block/block_reduce.cuh>
  21. #include <cub/block/block_scan.cuh>
  22. #include <numeric>
  23. #include "math.cuh"
  24. #include "utils.cuh"
  25. #include "vec_dtypes.cuh"
  26. namespace aphrodite {
  27. namespace sampling {
  28. using namespace cub;
  29. #define DISPATCH_DETERMINISTIC(deterministic, DETERMINISTIC, ...) \
  30. if (deterministic) { \
  31. constexpr bool DETERMINISTIC = true; \
  32. __VA_ARGS__ \
  33. } else { \
  34. constexpr bool DETERMINISTIC = false; \
  35. __VA_ARGS__ \
  36. }
  37. constexpr BlockScanAlgorithm SCAN_ALGO = BLOCK_SCAN_WARP_SCANS;
  38. constexpr BlockReduceAlgorithm REDUCE_ALGO = BLOCK_REDUCE_WARP_REDUCTIONS;
  39. #if (__CUDACC_VER_MAJOR__ * 10000 + __CUDACC_VER_MINOR__ * 100 >= 120100)
  40. #define APHRODITE_CUB_SUBTRACTLEFT_DEFINED
  41. #endif
  42. template <typename T>
  43. struct Pair {
  44. T value;
  45. int count;
  46. __device__ Pair operator+(const Pair& other) const {
  47. return {value + other.value, count + other.count};
  48. }
  49. __device__ Pair& operator+=(const Pair& other) {
  50. value += other.value;
  51. count += other.count;
  52. return *this;
  53. }
  54. };
  55. struct BoolDiffOp {
  56. __device__ __forceinline__ bool operator()(const bool& lhs,
  57. const bool& rhs) const {
  58. return lhs != rhs;
  59. }
  60. };
  61. template <typename T, uint32_t BLOCK_THREADS, BlockScanAlgorithm SCAN_ALGORITHM,
  62. BlockReduceAlgorithm REDUCE_ALGORITHM>
  63. struct SamplingTempStorage {
  64. union {
  65. T deterministic_scan[BLOCK_THREADS / 32];
  66. typename BlockScan<T, BLOCK_THREADS, SCAN_ALGORITHM>::TempStorage scan;
  67. typename BlockReduce<T, BLOCK_THREADS, REDUCE_ALGORITHM>::TempStorage
  68. reduce;
  69. typename BlockReduce<Pair<T>, BLOCK_THREADS, REDUCE_ALGORITHM>::TempStorage
  70. reduce_pair;
  71. typename BlockAdjacentDifference<bool, BLOCK_THREADS>::TempStorage adj_diff;
  72. } block_prim;
  73. struct {
  74. int32_t sampled_id;
  75. union {
  76. T value;
  77. Pair<T> pair;
  78. T max_p;
  79. } block_aggregate;
  80. } data;
  81. };
  82. /*!
  83. * \brief Deterministic inclusive scan implementation, use Belloch scan
  84. * algorithm. \note This implementation is slower than the cub::BlockScan, but
  85. * it is deterministic.
  86. */
  87. template <uint32_t VEC_SIZE, uint32_t BLOCK_THREADS,
  88. BlockScanAlgorithm SCAN_ALGORITHM,
  89. BlockReduceAlgorithm REDUCE_ALGORITHM, typename T>
  90. __device__ __forceinline__ void DeterministicInclusiveSum(
  91. const T* in_data, T* out_data,
  92. SamplingTempStorage<T, BLOCK_THREADS, SCAN_ALGORITHM, REDUCE_ALGORITHM>*
  93. temp_storage) {
  94. T* smem_prefix_sum = temp_storage->block_prim.deterministic_scan;
  95. T thread_data[VEC_SIZE];
  96. T thread_sum = 0;
  97. #pragma unroll
  98. for (uint32_t i = 0; i < VEC_SIZE; ++i) {
  99. thread_sum += in_data[i];
  100. thread_data[i] = thread_sum;
  101. }
  102. T thread_exclusive_prefix_sum = thread_sum;
  103. #pragma unroll
  104. for (uint32_t offset = 1; offset < 32; offset *= 2) {
  105. T tmp = __shfl_up_sync(0xffffffff, thread_exclusive_prefix_sum, offset);
  106. if ((threadIdx.x + 1) % (offset * 2) == 0) {
  107. thread_exclusive_prefix_sum += tmp;
  108. }
  109. }
  110. T warp_sum = __shfl_sync(0xffffffff, thread_exclusive_prefix_sum,
  111. threadIdx.x | 0xffffffff);
  112. if (threadIdx.x % 32 == 31) {
  113. thread_exclusive_prefix_sum = 0;
  114. }
  115. #pragma unroll
  116. for (uint32_t offset = 16; offset >= 1; offset /= 2) {
  117. T tmp = __shfl_xor_sync(0xffffffff, thread_exclusive_prefix_sum, offset);
  118. if ((threadIdx.x + 1) % (offset * 2) == 0) {
  119. thread_exclusive_prefix_sum = tmp + thread_exclusive_prefix_sum;
  120. }
  121. if ((threadIdx.x + 1) % (offset * 2) == offset) {
  122. thread_exclusive_prefix_sum = tmp;
  123. }
  124. }
  125. smem_prefix_sum[threadIdx.x / 32] = warp_sum;
  126. __syncthreads();
  127. if (threadIdx.x < 32) {
  128. T warp_exclusive_prefix_sum =
  129. (threadIdx.x < BLOCK_THREADS / 32) ? smem_prefix_sum[threadIdx.x] : 0;
  130. #pragma unroll
  131. for (uint32_t offset = 1; offset < 32; offset *= 2) {
  132. T tmp = __shfl_up_sync(0xffffffff, warp_exclusive_prefix_sum, offset);
  133. if ((threadIdx.x + 1) % (offset * 2) == 0) {
  134. warp_exclusive_prefix_sum += tmp;
  135. }
  136. }
  137. if (threadIdx.x % 32 == 31) {
  138. warp_exclusive_prefix_sum = 0;
  139. }
  140. #pragma unroll
  141. for (uint32_t offset = 16; offset >= 1; offset /= 2) {
  142. T tmp = __shfl_xor_sync(0xffffffff, warp_exclusive_prefix_sum, offset);
  143. if ((threadIdx.x + 1) % (offset * 2) == 0) {
  144. warp_exclusive_prefix_sum = tmp + warp_exclusive_prefix_sum;
  145. }
  146. if ((threadIdx.x + 1) % (offset * 2) == offset) {
  147. warp_exclusive_prefix_sum = tmp;
  148. }
  149. }
  150. if (threadIdx.x < BLOCK_THREADS / 32) {
  151. smem_prefix_sum[threadIdx.x] = warp_exclusive_prefix_sum;
  152. }
  153. }
  154. __syncthreads();
  155. #pragma unroll
  156. for (uint32_t i = 0; i < VEC_SIZE; ++i) {
  157. out_data[i] = smem_prefix_sum[threadIdx.x / 32] +
  158. thread_exclusive_prefix_sum + thread_data[i];
  159. }
  160. }
  161. template <uint32_t VEC_SIZE, uint32_t BLOCK_THREADS,
  162. BlockScanAlgorithm SCAN_ALGORITHM,
  163. BlockReduceAlgorithm REDUCE_ALGORITHM, bool DETERMINISTIC, typename T>
  164. __device__ __forceinline__ void DeviceSamplingFromProb(
  165. uint32_t i, uint32_t d, T threshold, T u, vec_t<T, VEC_SIZE> prob_vec,
  166. T& aggregate,
  167. SamplingTempStorage<T, BLOCK_THREADS, SCAN_ALGORITHM, REDUCE_ALGORITHM>*
  168. temp_storage) {
  169. const uint32_t tx = threadIdx.x;
  170. T prob_greater_than_threshold[VEC_SIZE];
  171. T inclusive_cdf[VEC_SIZE];
  172. bool greater_than_u[VEC_SIZE], valid[VEC_SIZE];
  173. #pragma unroll
  174. for (uint32_t j = 0; j < VEC_SIZE; ++j) {
  175. prob_greater_than_threshold[j] =
  176. (prob_vec[j] > threshold) ? prob_vec[j] : T(0);
  177. valid[j] =
  178. prob_vec[j] > threshold && (i * BLOCK_THREADS + tx) * VEC_SIZE < d;
  179. }
  180. T aggregate_local = BlockReduce<T, BLOCK_THREADS, REDUCE_ALGORITHM>(
  181. temp_storage->block_prim.reduce)
  182. .Sum<VEC_SIZE>(prob_greater_than_threshold);
  183. if (tx == 0) {
  184. temp_storage->data.block_aggregate.value = aggregate_local;
  185. }
  186. __syncthreads();
  187. aggregate_local = temp_storage->data.block_aggregate.value;
  188. if (aggregate + aggregate_local > u) {
  189. if constexpr (DETERMINISTIC) {
  190. DeterministicInclusiveSum<VEC_SIZE, BLOCK_THREADS, SCAN_ALGORITHM,
  191. REDUCE_ALGORITHM, T>(
  192. prob_greater_than_threshold, inclusive_cdf, temp_storage);
  193. } else {
  194. BlockScan<T, BLOCK_THREADS, SCAN_ALGORITHM>(temp_storage->block_prim.scan)
  195. .InclusiveSum<VEC_SIZE>(prob_greater_than_threshold, inclusive_cdf);
  196. __syncthreads();
  197. }
  198. #pragma unroll
  199. for (uint32_t j = 0; j < VEC_SIZE; ++j) {
  200. greater_than_u[j] = inclusive_cdf[j] + aggregate > u;
  201. }
  202. bool greater_than_u_diff[VEC_SIZE];
  203. #ifdef APHRODITE_CUB_SUBTRACTLEFT_DEFINED
  204. BlockAdjacentDifference<bool, BLOCK_THREADS>(
  205. temp_storage->block_prim.adj_diff)
  206. .SubtractLeft<VEC_SIZE>(greater_than_u, greater_than_u_diff,
  207. BoolDiffOp());
  208. #else
  209. BlockAdjacentDifference<bool, BLOCK_THREADS>(
  210. temp_storage->block_prim.adj_diff)
  211. .FlagHeads<VEC_SIZE>(greater_than_u_diff, greater_than_u, BoolDiffOp(),
  212. 0);
  213. #endif
  214. __syncthreads();
  215. #pragma unroll
  216. for (uint32_t j = 0; j < VEC_SIZE; ++j) {
  217. if (greater_than_u_diff[j] && valid[j]) {
  218. if constexpr (DETERMINISTIC) {
  219. temp_storage->data.sampled_id =
  220. (i * BLOCK_THREADS + tx) * VEC_SIZE + j;
  221. } else {
  222. // cub's block scan result might not be monotonic, so we need to find
  223. // the first element
  224. atomicMin(&(temp_storage->data.sampled_id),
  225. (i * BLOCK_THREADS + tx) * VEC_SIZE + j);
  226. }
  227. }
  228. }
  229. __syncthreads();
  230. }
  231. aggregate += aggregate_local;
  232. }
  233. template <uint32_t BLOCK_THREADS, BlockScanAlgorithm SCAN_ALGORITHM,
  234. BlockReduceAlgorithm REDUCE_ALGORITHM, uint32_t VEC_SIZE,
  235. bool DETERMINISTIC, typename DType, typename IdType>
  236. __global__ void SamplingFromProbKernel(DType* probs, DType* uniform_samples,
  237. IdType* output, IdType* row_indices,
  238. uint32_t d) {
  239. const uint32_t bx = blockIdx.x, tx = threadIdx.x;
  240. const uint32_t row_idx = row_indices == nullptr ? bx : row_indices[bx];
  241. extern __shared__ __align__(
  242. alignof(SamplingTempStorage<DType, BLOCK_THREADS, SCAN_ALGORITHM,
  243. REDUCE_ALGORITHM>)) uint8_t smem_sampling[];
  244. auto& temp_storage =
  245. reinterpret_cast<SamplingTempStorage<DType, BLOCK_THREADS, SCAN_ALGORITHM,
  246. REDUCE_ALGORITHM>&>(smem_sampling);
  247. temp_storage.data.sampled_id = d - 1;
  248. __syncthreads();
  249. vec_t<DType, VEC_SIZE> probs_vec;
  250. DType aggregate(0);
  251. float u = uniform_samples[bx];
  252. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  253. probs_vec.fill(DType(0));
  254. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  255. probs_vec.load(probs + row_idx * d + i * BLOCK_THREADS * VEC_SIZE +
  256. tx * VEC_SIZE);
  257. }
  258. DeviceSamplingFromProb<VEC_SIZE, BLOCK_THREADS, SCAN_ALGORITHM,
  259. REDUCE_ALGORITHM, DETERMINISTIC, DType>(
  260. i, d, DType(0), u, probs_vec, aggregate, &temp_storage);
  261. if (float(aggregate) > u) {
  262. break;
  263. }
  264. }
  265. output[bx] = temp_storage.data.sampled_id;
  266. }
  267. template <uint32_t BLOCK_THREADS, BlockScanAlgorithm SCAN_ALGORITHM,
  268. BlockReduceAlgorithm REDUCE_ALGORITHM, uint32_t VEC_SIZE,
  269. bool DETERMINISTIC, typename DType, typename IdType>
  270. __global__ void TopKSamplingFromProbKernel(DType* probs, DType* uniform_samples,
  271. IdType* output, bool* success,
  272. IdType* top_k_arr,
  273. uint32_t top_k_val, uint32_t d,
  274. uint32_t max_top_k_rounds) {
  275. const uint32_t batch_size = gridDim.x;
  276. const uint32_t bx = blockIdx.x, tx = threadIdx.x;
  277. uint32_t k = top_k_arr == nullptr ? top_k_val : top_k_arr[bx];
  278. extern __shared__ __align__(
  279. alignof(SamplingTempStorage<DType, BLOCK_THREADS, SCAN_ALGORITHM,
  280. REDUCE_ALGORITHM>)) uint8_t smem_sampling[];
  281. auto& temp_storage =
  282. reinterpret_cast<SamplingTempStorage<DType, BLOCK_THREADS, SCAN_ALGORITHM,
  283. REDUCE_ALGORITHM>&>(smem_sampling);
  284. vec_t<DType, VEC_SIZE> probs_vec;
  285. DType aggregate;
  286. DType q = DType(1);
  287. DType pivot = DType(0);
  288. IdType sampled_id;
  289. for (uint32_t round = 0; round < max_top_k_rounds; ++round) {
  290. temp_storage.data.sampled_id = d - 1;
  291. __syncthreads();
  292. DType u = uniform_samples[round * batch_size + bx] * q;
  293. aggregate = DType(0);
  294. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  295. probs_vec.fill(DType(0));
  296. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  297. probs_vec.load(probs + bx * d + (i * BLOCK_THREADS + tx) * VEC_SIZE);
  298. }
  299. DeviceSamplingFromProb<VEC_SIZE, BLOCK_THREADS, SCAN_ALGORITHM,
  300. REDUCE_ALGORITHM, DETERMINISTIC, DType>(
  301. i, d, pivot, u, probs_vec, aggregate, &temp_storage);
  302. if (aggregate > u) {
  303. break;
  304. }
  305. }
  306. __syncthreads();
  307. sampled_id = temp_storage.data.sampled_id;
  308. pivot = max(pivot, probs[bx * d + sampled_id]);
  309. Pair<DType> aggregate_gt_pivot{DType(0), 0};
  310. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  311. probs_vec.fill(DType(0));
  312. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  313. probs_vec.load(probs + bx * d + (i * BLOCK_THREADS + tx) * VEC_SIZE);
  314. }
  315. Pair<DType> probs_gt_pivot[VEC_SIZE];
  316. #pragma unroll
  317. for (uint32_t j = 0; j < VEC_SIZE; ++j) {
  318. probs_gt_pivot[j] = {(probs_vec[j] > pivot) ? probs_vec[j] : DType(0),
  319. (probs_vec[j] > pivot &&
  320. (i * BLOCK_THREADS + tx) * VEC_SIZE + j < d)};
  321. }
  322. aggregate_gt_pivot +=
  323. BlockReduce<Pair<DType>, BLOCK_THREADS, REDUCE_ALGORITHM>(
  324. temp_storage.block_prim.reduce_pair)
  325. .Sum<VEC_SIZE>(probs_gt_pivot);
  326. if (tx == 0) {
  327. temp_storage.data.block_aggregate.pair = aggregate_gt_pivot;
  328. }
  329. __syncthreads();
  330. }
  331. q = temp_storage.data.block_aggregate.pair.value;
  332. if (temp_storage.data.block_aggregate.pair.count < k) {
  333. break;
  334. }
  335. }
  336. __syncthreads();
  337. if (tx == 0) {
  338. output[bx] = sampled_id;
  339. if (temp_storage.data.block_aggregate.pair.count >= k) {
  340. // failed to sample within MAX_TOP_P_ROUNDS
  341. if (success != nullptr) {
  342. success[bx] = false;
  343. }
  344. } else {
  345. if (success != nullptr) {
  346. success[bx] = true;
  347. }
  348. }
  349. }
  350. }
  351. template <uint32_t BLOCK_THREADS, BlockScanAlgorithm SCAN_ALGORITHM,
  352. BlockReduceAlgorithm REDUCE_ALGORITHM, uint32_t VEC_SIZE,
  353. bool DETERMINISTIC, typename DType, typename IdType>
  354. __global__ void TopPSamplingFromProbKernel(DType* probs, DType* uniform_samples,
  355. IdType* output, bool* success,
  356. IdType* row_indices,
  357. float* top_p_arr, float top_p_val,
  358. uint32_t d,
  359. uint32_t max_top_p_rounds) {
  360. const uint32_t batch_size = gridDim.x;
  361. const uint32_t bx = blockIdx.x, tx = threadIdx.x;
  362. float top_p = (top_p_arr == nullptr) ? top_p_val : top_p_arr[bx];
  363. const uint32_t row_idx = row_indices == nullptr ? bx : row_indices[bx];
  364. extern __shared__ __align__(
  365. alignof(SamplingTempStorage<DType, BLOCK_THREADS, SCAN_ALGORITHM,
  366. REDUCE_ALGORITHM>)) uint8_t smem_sampling[];
  367. auto& temp_storage =
  368. reinterpret_cast<SamplingTempStorage<DType, BLOCK_THREADS, SCAN_ALGORITHM,
  369. REDUCE_ALGORITHM>&>(smem_sampling);
  370. vec_t<DType, VEC_SIZE> probs_vec;
  371. DType aggregate;
  372. DType q = DType(1);
  373. DType pivot = DType(0);
  374. IdType sampled_id;
  375. for (uint32_t round = 0; round < max_top_p_rounds; ++round) {
  376. temp_storage.data.sampled_id = d - 1;
  377. __syncthreads();
  378. DType u = uniform_samples[round * batch_size + bx] * q;
  379. aggregate = DType(0);
  380. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  381. probs_vec.fill(DType(0));
  382. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  383. probs_vec.load(probs + row_idx * d +
  384. (i * BLOCK_THREADS + tx) * VEC_SIZE);
  385. }
  386. DeviceSamplingFromProb<VEC_SIZE, BLOCK_THREADS, SCAN_ALGORITHM,
  387. REDUCE_ALGORITHM, DETERMINISTIC, DType>(
  388. i, d, pivot, u, probs_vec, aggregate, &temp_storage);
  389. if (aggregate > u) {
  390. break;
  391. }
  392. }
  393. __syncthreads();
  394. sampled_id = temp_storage.data.sampled_id;
  395. pivot = max(pivot, probs[row_idx * d + sampled_id]);
  396. DType aggregate_gt_pivot = DType(0);
  397. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  398. probs_vec.fill(DType(0));
  399. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  400. probs_vec.load(probs + row_idx * d +
  401. (i * BLOCK_THREADS + tx) * VEC_SIZE);
  402. }
  403. DType probs_gt_pivot[VEC_SIZE];
  404. #pragma unroll
  405. for (uint32_t j = 0; j < VEC_SIZE; ++j) {
  406. probs_gt_pivot[j] = (probs_vec[j] > pivot) ? probs_vec[j] : DType(0);
  407. }
  408. aggregate_gt_pivot +=
  409. BlockReduce<DType, BLOCK_THREADS>(temp_storage.block_prim.reduce)
  410. .Sum<VEC_SIZE>(probs_gt_pivot);
  411. if (tx == 0) {
  412. temp_storage.data.block_aggregate.value = aggregate_gt_pivot;
  413. }
  414. __syncthreads();
  415. }
  416. q = temp_storage.data.block_aggregate.value;
  417. if (float(q) < top_p) {
  418. break;
  419. }
  420. }
  421. __syncthreads();
  422. if (tx == 0) {
  423. output[bx] = sampled_id;
  424. if (float(q) >= top_p) {
  425. // failed to sample within MAX_TOP_P_ROUNDS
  426. if (success != nullptr) {
  427. success[bx] = false;
  428. }
  429. } else {
  430. if (success != nullptr) {
  431. success[bx] = true;
  432. }
  433. }
  434. }
  435. }
  436. template <uint32_t BLOCK_THREADS, BlockScanAlgorithm SCAN_ALGORITHM,
  437. BlockReduceAlgorithm REDUCE_ALGORITHM, uint32_t VEC_SIZE,
  438. bool DETERMINISTIC, typename DType, typename IdType>
  439. __global__ void MinPSamplingFromProbKernel(DType* probs, DType* uniform_samples,
  440. DType* min_p_arr, IdType* output,
  441. bool* success, float min_p_val,
  442. uint32_t d,
  443. uint32_t max_min_p_rounds) {
  444. const uint32_t batch_size = gridDim.x;
  445. const uint32_t bx = blockIdx.x, tx = threadIdx.x;
  446. DType p = (min_p_arr == nullptr) ? min_p_val : min_p_arr[bx];
  447. extern __shared__ __align__(
  448. alignof(SamplingTempStorage<DType, BLOCK_THREADS, SCAN_ALGORITHM,
  449. REDUCE_ALGORITHM>)) uint8_t smem_sampling[];
  450. auto& temp_storage =
  451. reinterpret_cast<SamplingTempStorage<DType, BLOCK_THREADS, SCAN_ALGORITHM,
  452. REDUCE_ALGORITHM>&>(smem_sampling);
  453. vec_t<DType, VEC_SIZE> probs_vec;
  454. DType aggregate;
  455. DType q = DType(1);
  456. DType pivot = DType(0);
  457. DType max_p = 0;
  458. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  459. probs_vec.fill(DType(0));
  460. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  461. probs_vec.load(probs + bx * d + (i * BLOCK_THREADS + tx) * VEC_SIZE);
  462. }
  463. DType probs_[VEC_SIZE];
  464. #pragma unroll
  465. for (uint32_t j = 0; j < VEC_SIZE; ++j) {
  466. probs_[j] = probs_vec[j];
  467. }
  468. max_p = max(
  469. max_p, BlockReduce<DType, BLOCK_THREADS>(temp_storage.block_prim.reduce)
  470. .Reduce<VEC_SIZE>(probs_, cub::Max()));
  471. __syncthreads();
  472. }
  473. if (tx == 0) {
  474. temp_storage.data.block_aggregate.max_p = max_p;
  475. }
  476. __syncthreads();
  477. DType scaled_p = temp_storage.data.block_aggregate.max_p * p;
  478. IdType sampled_id;
  479. for (uint32_t round = 0; round < max_min_p_rounds; ++round) {
  480. temp_storage.data.sampled_id = d - 1;
  481. __syncthreads();
  482. DType u = uniform_samples[round * batch_size + bx] * q;
  483. aggregate = DType(0);
  484. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  485. probs_vec.fill(DType(0));
  486. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  487. probs_vec.load(probs + bx * d + (i * BLOCK_THREADS + tx) * VEC_SIZE);
  488. }
  489. DeviceSamplingFromProb<VEC_SIZE, BLOCK_THREADS, SCAN_ALGORITHM,
  490. REDUCE_ALGORITHM, DETERMINISTIC, DType>(
  491. i, d, pivot, u, probs_vec, aggregate, &temp_storage);
  492. if (aggregate > u) {
  493. break;
  494. }
  495. }
  496. __syncthreads();
  497. sampled_id = temp_storage.data.sampled_id;
  498. pivot = max(pivot, probs[bx * d + sampled_id]);
  499. if (pivot >= scaled_p) {
  500. break;
  501. }
  502. DType aggregate_gt_pivot = DType(0);
  503. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  504. probs_vec.fill(DType(0));
  505. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  506. probs_vec.load(probs + bx * d + (i * BLOCK_THREADS + tx) * VEC_SIZE);
  507. }
  508. DType probs_gt_pivot[VEC_SIZE];
  509. #pragma unroll
  510. for (uint32_t j = 0; j < VEC_SIZE; ++j) {
  511. probs_gt_pivot[j] = (probs_vec[j] > pivot) ? probs_vec[j] : DType(0);
  512. }
  513. aggregate_gt_pivot +=
  514. BlockReduce<DType, BLOCK_THREADS>(temp_storage.block_prim.reduce)
  515. .Sum<VEC_SIZE>(probs_gt_pivot);
  516. if (tx == 0) {
  517. temp_storage.data.block_aggregate.value = aggregate_gt_pivot;
  518. }
  519. __syncthreads();
  520. }
  521. q = temp_storage.data.block_aggregate.value;
  522. }
  523. __syncthreads();
  524. if (tx == 0) {
  525. output[bx] = sampled_id;
  526. if (pivot < scaled_p) {
  527. // failed to sample within MAX_ROUNDS
  528. if (success != nullptr) {
  529. success[bx] = false;
  530. }
  531. } else {
  532. if (success != nullptr) {
  533. success[bx] = true;
  534. }
  535. }
  536. }
  537. }
  538. template <uint32_t BLOCK_THREADS, BlockScanAlgorithm SCAN_ALGORITHM,
  539. BlockReduceAlgorithm REDUCE_ALGORITHM, uint32_t VEC_SIZE,
  540. bool DETERMINISTIC, typename DType, typename IdType>
  541. __global__ void TopKTopPSamplingFromProbKernel(
  542. DType* probs, DType* uniform_samples, IdType* top_k_arr, DType* top_p_arr,
  543. IdType* output, bool* success, IdType top_k_val, DType top_p_val,
  544. uint32_t d, uint32_t max_rounds) {
  545. const uint32_t batch_size = gridDim.x;
  546. const uint32_t bx = blockIdx.x, tx = threadIdx.x;
  547. IdType k = top_k_arr == nullptr ? top_k_val : top_k_arr[bx];
  548. DType p = top_p_arr == nullptr ? top_p_val : top_p_arr[bx];
  549. extern __shared__ __align__(
  550. alignof(SamplingTempStorage<DType, BLOCK_THREADS, SCAN_ALGORITHM,
  551. REDUCE_ALGORITHM>)) uint8_t smem_sampling[];
  552. auto& temp_storage =
  553. reinterpret_cast<SamplingTempStorage<DType, BLOCK_THREADS, SCAN_ALGORITHM,
  554. REDUCE_ALGORITHM>&>(smem_sampling);
  555. vec_t<DType, VEC_SIZE> probs_vec;
  556. DType aggregate;
  557. DType q = DType(1);
  558. DType pivot = DType(0);
  559. IdType sampled_id;
  560. for (uint32_t round = 0; round < max_rounds; ++round) {
  561. temp_storage.data.sampled_id = d - 1;
  562. __syncthreads();
  563. DType u = uniform_samples[round * batch_size + bx] * q;
  564. aggregate = DType(0);
  565. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  566. probs_vec.fill(DType(0));
  567. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  568. probs_vec.load(probs + bx * d + (i * BLOCK_THREADS + tx) * VEC_SIZE);
  569. }
  570. DeviceSamplingFromProb<VEC_SIZE, BLOCK_THREADS, SCAN_ALGORITHM,
  571. REDUCE_ALGORITHM, DETERMINISTIC, DType>(
  572. i, d, pivot, u, probs_vec, aggregate, &temp_storage);
  573. if (aggregate > u) {
  574. break;
  575. }
  576. }
  577. __syncthreads();
  578. sampled_id = temp_storage.data.sampled_id;
  579. pivot = max(pivot, probs[bx * d + sampled_id]);
  580. Pair<DType> aggregate_gt_pivot{DType(0), 0};
  581. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  582. probs_vec.fill(DType(0));
  583. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  584. probs_vec.load(probs + bx * d + (i * BLOCK_THREADS + tx) * VEC_SIZE);
  585. }
  586. Pair<DType> probs_gt_pivot[VEC_SIZE];
  587. #pragma unroll
  588. for (uint32_t j = 0; j < VEC_SIZE; ++j) {
  589. probs_gt_pivot[j] = {(probs_vec[j] > pivot) ? probs_vec[j] : DType(0),
  590. (probs_vec[j] > pivot &&
  591. (i * BLOCK_THREADS + tx) * VEC_SIZE + j < d)};
  592. }
  593. aggregate_gt_pivot +=
  594. BlockReduce<Pair<DType>, BLOCK_THREADS, REDUCE_ALGORITHM>(
  595. temp_storage.block_prim.reduce_pair)
  596. .Sum<VEC_SIZE>(probs_gt_pivot);
  597. if (tx == 0) {
  598. temp_storage.data.block_aggregate.pair = aggregate_gt_pivot;
  599. }
  600. __syncthreads();
  601. }
  602. q = temp_storage.data.block_aggregate.pair.value;
  603. if (temp_storage.data.block_aggregate.pair.count < k && float(q) < p) {
  604. break;
  605. }
  606. }
  607. __syncthreads();
  608. if (tx == 0) {
  609. output[bx] = sampled_id;
  610. if (temp_storage.data.block_aggregate.pair.count >= k || float(q) >= p) {
  611. // failed to sample within MAX_TOP_P_ROUNDS
  612. if (success != nullptr) {
  613. success[bx] = false;
  614. }
  615. } else {
  616. if (success != nullptr) {
  617. success[bx] = true;
  618. }
  619. }
  620. }
  621. }
  622. template <typename T, typename IdType>
  623. cudaError_t SamplingFromProb(T* probs, T* uniform_samples, IdType* output,
  624. uint32_t batch_size, uint32_t d,
  625. bool deterministic, cudaStream_t stream = 0) {
  626. constexpr uint32_t BLOCK_THREADS = 1024;
  627. const uint32_t vec_size = std::gcd(16 / sizeof(T), d);
  628. dim3 nblks(batch_size);
  629. dim3 nthrs(BLOCK_THREADS);
  630. IdType* row_indices_placeholder = nullptr;
  631. void* args[] = {&probs, &uniform_samples, &output, &row_indices_placeholder,
  632. &d};
  633. const uint32_t smem_size =
  634. sizeof(SamplingTempStorage<T, BLOCK_THREADS, SCAN_ALGO, REDUCE_ALGO>);
  635. DISPATCH_ALIGNED_VEC_SIZE(
  636. vec_size, VEC_SIZE,
  637. {DISPATCH_DETERMINISTIC(deterministic, DETERMINISTIC, {
  638. auto kernel =
  639. SamplingFromProbKernel<BLOCK_THREADS, SCAN_ALGO, REDUCE_ALGO,
  640. VEC_SIZE, DETERMINISTIC, T, IdType>;
  641. APHRODITE_CUDA_CALL(cudaLaunchKernel((void*)kernel, nblks, nthrs, args,
  642. smem_size, stream));
  643. })});
  644. return cudaSuccess;
  645. }
  646. template <typename T, typename IdType>
  647. cudaError_t ParallelSamplingFromProb(T* probs, T* uniform_samples,
  648. IdType* output, IdType* row_indices,
  649. uint32_t batch_size, uint32_t d,
  650. bool deterministic,
  651. cudaStream_t stream = 0) {
  652. constexpr uint32_t BLOCK_THREADS = 1024;
  653. const uint32_t vec_size = std::gcd(16 / sizeof(T), d);
  654. dim3 nblks(batch_size);
  655. dim3 nthrs(BLOCK_THREADS);
  656. void* args[] = {&probs, &uniform_samples, &output, &row_indices, &d};
  657. const uint32_t smem_size =
  658. sizeof(SamplingTempStorage<T, BLOCK_THREADS, SCAN_ALGO, REDUCE_ALGO>);
  659. DISPATCH_ALIGNED_VEC_SIZE(
  660. vec_size, VEC_SIZE,
  661. {DISPATCH_DETERMINISTIC(deterministic, DETERMINISTIC, {
  662. auto kernel =
  663. SamplingFromProbKernel<BLOCK_THREADS, SCAN_ALGO, REDUCE_ALGO,
  664. VEC_SIZE, DETERMINISTIC, T, IdType>;
  665. APHRODITE_CUDA_CALL(cudaLaunchKernel((void*)kernel, nblks, nthrs, args,
  666. smem_size, stream));
  667. })});
  668. return cudaSuccess;
  669. }
  670. template <typename T, typename IdType>
  671. cudaError_t TopKSamplingFromProb(T* probs, T* uniform_samples, IdType* output,
  672. bool* success, T* top_k_arr,
  673. uint32_t batch_size, uint32_t top_k_val,
  674. uint32_t d, uint32_t max_top_k_rounds,
  675. bool deterministic, cudaStream_t stream = 0) {
  676. constexpr uint32_t BLOCK_THREADS = 1024;
  677. const uint32_t vec_size = std::gcd(16 / sizeof(T), d);
  678. const uint32_t smem_size =
  679. sizeof(SamplingTempStorage<T, BLOCK_THREADS, SCAN_ALGO, REDUCE_ALGO>);
  680. dim3 nblks(batch_size);
  681. dim3 nthrs(BLOCK_THREADS);
  682. void* args[] = {&probs, &uniform_samples, &output, &success,
  683. &top_k_arr, &top_k_val, &d, &max_top_k_rounds};
  684. DISPATCH_ALIGNED_VEC_SIZE(
  685. vec_size, VEC_SIZE,
  686. {DISPATCH_DETERMINISTIC(deterministic, DETERMINISTIC, {
  687. auto kernel =
  688. TopKSamplingFromProbKernel<BLOCK_THREADS, SCAN_ALGO, REDUCE_ALGO,
  689. VEC_SIZE, DETERMINISTIC, T, IdType>;
  690. APHRODITE_CUDA_CALL(cudaFuncSetAttribute(
  691. kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, smem_size));
  692. APHRODITE_CUDA_CALL(cudaLaunchKernel((void*)kernel, nblks, nthrs, args,
  693. smem_size, stream));
  694. })});
  695. return cudaSuccess;
  696. }
  697. template <typename T, typename IdType>
  698. cudaError_t TopPSamplingFromProb(T* probs, T* uniform_samples, IdType* output,
  699. bool* success, T* top_p_arr,
  700. uint32_t batch_size, T top_p_val, uint32_t d,
  701. uint32_t max_top_p_rounds, bool deterministic,
  702. cudaStream_t stream = 0) {
  703. constexpr uint32_t BLOCK_THREADS = 1024;
  704. const uint32_t vec_size = std::gcd(16 / sizeof(T), d);
  705. const uint32_t smem_size =
  706. sizeof(SamplingTempStorage<T, BLOCK_THREADS, SCAN_ALGO, REDUCE_ALGO>);
  707. dim3 nblks(batch_size);
  708. dim3 nthrs(BLOCK_THREADS);
  709. IdType* row_indices_placeholder = nullptr;
  710. void* args[] = {&probs,
  711. &uniform_samples,
  712. &output,
  713. &success,
  714. &row_indices_placeholder,
  715. &top_p_arr,
  716. &top_p_val,
  717. &d,
  718. &max_top_p_rounds};
  719. DISPATCH_ALIGNED_VEC_SIZE(
  720. vec_size, VEC_SIZE,
  721. {DISPATCH_DETERMINISTIC(deterministic, DETERMINISTIC, {
  722. auto kernel =
  723. TopPSamplingFromProbKernel<BLOCK_THREADS, SCAN_ALGO, REDUCE_ALGO,
  724. VEC_SIZE, DETERMINISTIC, T, IdType>;
  725. APHRODITE_CUDA_CALL(cudaFuncSetAttribute(
  726. kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, smem_size));
  727. APHRODITE_CUDA_CALL(cudaLaunchKernel((void*)kernel, nblks, nthrs, args,
  728. smem_size, stream));
  729. })});
  730. return cudaSuccess;
  731. }
  732. template <typename T, typename IdType>
  733. cudaError_t MinPSamplingFromProb(T* probs, T* uniform_samples, T* min_p_arr,
  734. IdType* output, bool* success,
  735. uint32_t batch_size, float min_p_val,
  736. uint32_t d, uint32_t max_rounds,
  737. bool deterministic, cudaStream_t stream = 0) {
  738. constexpr uint32_t BLOCK_THREADS = 1024;
  739. const uint32_t vec_size = std::gcd(16 / sizeof(T), d);
  740. const uint32_t smem_size =
  741. sizeof(SamplingTempStorage<T, BLOCK_THREADS, SCAN_ALGO, REDUCE_ALGO>);
  742. dim3 nblks(batch_size);
  743. dim3 nthrs(BLOCK_THREADS);
  744. void* args[] = {&probs, &uniform_samples, &min_p_arr, &output,
  745. &success, &min_p_val, &d, &max_rounds};
  746. DISPATCH_ALIGNED_VEC_SIZE(
  747. vec_size, VEC_SIZE,
  748. {DISPATCH_DETERMINISTIC(deterministic, DETERMINISTIC, {
  749. auto kernel =
  750. MinPSamplingFromProbKernel<BLOCK_THREADS, SCAN_ALGO, REDUCE_ALGO,
  751. VEC_SIZE, DETERMINISTIC, T, IdType>;
  752. APHRODITE_CUDA_CALL(cudaFuncSetAttribute(
  753. kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, smem_size));
  754. APHRODITE_CUDA_CALL(cudaLaunchKernel((void*)kernel, nblks, nthrs, args,
  755. smem_size, stream));
  756. })});
  757. return cudaSuccess;
  758. }
  759. template <typename T, typename IdType>
  760. cudaError_t TopKTopPSamplingFromProb(T* probs, T* uniform_samples,
  761. IdType* top_k_arr, T* top_p_arr,
  762. IdType* output, bool* success,
  763. uint32_t batch_size, IdType top_k_val,
  764. T top_p_val, uint32_t d,
  765. uint32_t max_rounds, bool deterministic,
  766. cudaStream_t stream = 0) {
  767. constexpr uint32_t BLOCK_THREADS = 1024;
  768. const uint32_t vec_size = std::gcd(16 / sizeof(T), d);
  769. const uint32_t smem_size =
  770. sizeof(SamplingTempStorage<T, BLOCK_THREADS, SCAN_ALGO, REDUCE_ALGO>);
  771. dim3 nblks(batch_size);
  772. dim3 nthrs(BLOCK_THREADS);
  773. void* args[] = {&probs, &uniform_samples, &top_k_arr, &top_p_arr,
  774. &output, &success, &top_k_val, &top_p_val,
  775. &d, &max_rounds};
  776. DISPATCH_ALIGNED_VEC_SIZE(
  777. vec_size, VEC_SIZE,
  778. {DISPATCH_DETERMINISTIC(deterministic, DETERMINISTIC, {
  779. auto kernel = TopKTopPSamplingFromProbKernel<BLOCK_THREADS, SCAN_ALGO,
  780. REDUCE_ALGO, VEC_SIZE,
  781. DETERMINISTIC, T, IdType>;
  782. APHRODITE_CUDA_CALL(cudaFuncSetAttribute(
  783. kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, smem_size));
  784. APHRODITE_CUDA_CALL(cudaLaunchKernel((void*)kernel, nblks, nthrs, args,
  785. smem_size, stream));
  786. })});
  787. return cudaSuccess;
  788. }
  789. template <typename T, uint32_t BLOCK_THREADS,
  790. BlockReduceAlgorithm REDUCE_ALGORITHM>
  791. struct RenormTempStorage {
  792. union {
  793. typename BlockReduce<T, BLOCK_THREADS, REDUCE_ALGORITHM>::TempStorage
  794. reduce;
  795. typename BlockReduce<int, BLOCK_THREADS, REDUCE_ALGORITHM>::TempStorage
  796. reduce_int;
  797. typename BlockReduce<Pair<T>, BLOCK_THREADS, REDUCE_ALGORITHM>::TempStorage
  798. reduce_pair;
  799. } block_prim;
  800. struct {
  801. T max_val;
  802. T min_val;
  803. union {
  804. T value;
  805. int count;
  806. Pair<T> pair;
  807. } block_aggregate;
  808. } data;
  809. };
  810. template <uint32_t BLOCK_THREADS, BlockReduceAlgorithm REDUCE_ALGORITHM,
  811. uint32_t VEC_SIZE, typename DType>
  812. __global__ void TopPRenormProbKernel(DType* probs, DType* renormed_prob,
  813. DType* top_p_arr, float top_p_val,
  814. uint32_t d) {
  815. const uint32_t bx = blockIdx.x, tx = threadIdx.x;
  816. const uint32_t row_idx = bx;
  817. float p = top_p_arr == nullptr ? top_p_val : top_p_arr[bx];
  818. extern __shared__ __align__(
  819. alignof(RenormTempStorage<DType, BLOCK_THREADS, REDUCE_ALGO>))
  820. uint8_t smem_renorm[];
  821. auto& temp_storage =
  822. reinterpret_cast<RenormTempStorage<DType, BLOCK_THREADS, REDUCE_ALGO>&>(
  823. smem_renorm);
  824. temp_storage.data.max_val = DType(0);
  825. vec_t<DType, VEC_SIZE> probs_vec;
  826. DType probs_greater_than_pivot[VEC_SIZE]; // pivot initialized to 0
  827. DType threadlocal_max_val = DType(0);
  828. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  829. probs_vec.fill(DType(0));
  830. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  831. probs_vec.load(probs + row_idx * d + i * BLOCK_THREADS * VEC_SIZE +
  832. tx * VEC_SIZE);
  833. }
  834. #pragma unroll
  835. for (uint32_t j = 0; j < VEC_SIZE; ++j) {
  836. probs_greater_than_pivot[j] = probs_vec[j];
  837. }
  838. threadlocal_max_val =
  839. max(threadlocal_max_val,
  840. BlockReduce<DType, BLOCK_THREADS, REDUCE_ALGORITHM>(
  841. temp_storage.block_prim.reduce)
  842. .Reduce<VEC_SIZE>(probs_greater_than_pivot, cub::Max()));
  843. __syncthreads();
  844. }
  845. if (tx == 0) {
  846. temp_storage.data.max_val = threadlocal_max_val;
  847. }
  848. __syncthreads();
  849. threadlocal_max_val = temp_storage.data.max_val;
  850. float low = 0, high = threadlocal_max_val;
  851. DType min_gt_low, max_le_high;
  852. DType sum_low(1);
  853. // f(x) = sum(probs[probs > x]), f(x) is non-increasing
  854. // min_gt_low = min{p \in probs | p > low}, max_le_high = max{p \in probs | p
  855. // <= high} loop invariant:
  856. // - f(low) >= p, f(high) < p
  857. // - f(low) > f(min_gt_low) >= f(max_le_high) == f(high)
  858. // stopping condition
  859. // - f(low) >= p, f(min_gt_low) == f(max_le_high) == f(high) < p
  860. do {
  861. DType threadlocal_sum(0);
  862. float mid = (low + high) / 2;
  863. min_gt_low = high;
  864. max_le_high = low;
  865. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  866. probs_vec.fill(DType(0));
  867. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  868. probs_vec.load(probs + row_idx * d + i * BLOCK_THREADS * VEC_SIZE +
  869. tx * VEC_SIZE);
  870. }
  871. #pragma unroll
  872. for (uint32_t j = 0; j < VEC_SIZE; ++j) {
  873. probs_greater_than_pivot[j] =
  874. (probs_vec[j] > mid) ? probs_vec[j] : DType(0);
  875. if (probs_vec[j] > low && (i * BLOCK_THREADS + tx) * VEC_SIZE + j < d) {
  876. min_gt_low = min(min_gt_low, probs_vec[j]);
  877. }
  878. if (probs_vec[j] <= high &&
  879. (i * BLOCK_THREADS + tx) * VEC_SIZE + j < d) {
  880. max_le_high = max(max_le_high, probs_vec[j]);
  881. }
  882. }
  883. threadlocal_sum += BlockReduce<DType, BLOCK_THREADS, REDUCE_ALGORITHM>(
  884. temp_storage.block_prim.reduce)
  885. .Sum<VEC_SIZE>(probs_greater_than_pivot);
  886. __syncthreads();
  887. }
  888. min_gt_low = BlockReduce<DType, BLOCK_THREADS, REDUCE_ALGORITHM>(
  889. temp_storage.block_prim.reduce)
  890. .Reduce(min_gt_low, cub::Min());
  891. __syncthreads();
  892. max_le_high = BlockReduce<DType, BLOCK_THREADS, REDUCE_ALGORITHM>(
  893. temp_storage.block_prim.reduce)
  894. .Reduce(max_le_high, cub::Max());
  895. if (tx == 0) {
  896. temp_storage.data.block_aggregate.value = threadlocal_sum;
  897. temp_storage.data.min_val = min_gt_low;
  898. temp_storage.data.max_val = max_le_high;
  899. }
  900. __syncthreads();
  901. threadlocal_sum = temp_storage.data.block_aggregate.value;
  902. min_gt_low = temp_storage.data.min_val;
  903. max_le_high = temp_storage.data.max_val;
  904. if (threadlocal_sum >= p) {
  905. low = mid;
  906. sum_low = float(threadlocal_sum);
  907. } else {
  908. high = min(mid, max_le_high);
  909. }
  910. } while (min_gt_low != max_le_high);
  911. DType normalizer = math::ptx_rcp(max(sum_low, 1e-8));
  912. // normalize
  913. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  914. probs_vec.fill(DType(0));
  915. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  916. probs_vec.load(probs + row_idx * d + i * BLOCK_THREADS * VEC_SIZE +
  917. tx * VEC_SIZE);
  918. }
  919. #pragma unroll
  920. for (uint32_t j = 0; j < VEC_SIZE; ++j) {
  921. probs_vec[j] =
  922. (probs_vec[j] > low) ? probs_vec[j] * normalizer : DType(0);
  923. }
  924. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  925. probs_vec.store(renormed_prob + row_idx * d +
  926. i * BLOCK_THREADS * VEC_SIZE + tx * VEC_SIZE);
  927. }
  928. }
  929. }
  930. template <uint32_t BLOCK_THREADS, BlockReduceAlgorithm REDUCE_ALGORITHM,
  931. uint32_t VEC_SIZE, typename DType, typename IdType>
  932. __global__ void TopKMaskLogitsKernel(DType* logits, DType* masked_logits,
  933. IdType* top_k_arr, uint32_t top_k_val,
  934. uint32_t d) {
  935. const uint32_t bx = blockIdx.x, tx = threadIdx.x;
  936. const uint32_t row_idx = bx;
  937. uint32_t k = top_k_arr == nullptr ? top_k_val : top_k_arr[bx];
  938. float pivot = -std::numeric_limits<float>::infinity();
  939. vec_t<DType, VEC_SIZE> logits_vec;
  940. if (k < d) {
  941. extern __shared__ __align__(
  942. alignof(RenormTempStorage<DType, BLOCK_THREADS, REDUCE_ALGO>))
  943. uint8_t smem_renorm[];
  944. auto& temp_storage =
  945. reinterpret_cast<RenormTempStorage<DType, BLOCK_THREADS, REDUCE_ALGO>&>(
  946. smem_renorm);
  947. DType logits_greater_than_pivot[VEC_SIZE]; // pivot initialized to 0
  948. DType threadlocal_max_val = DType(-std::numeric_limits<float>::infinity()),
  949. threadlocal_min_val = DType(std::numeric_limits<float>::infinity());
  950. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  951. logits_vec.fill(DType(0));
  952. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  953. logits_vec.load(logits + row_idx * d + i * BLOCK_THREADS * VEC_SIZE +
  954. tx * VEC_SIZE);
  955. }
  956. #pragma unroll
  957. for (uint32_t j = 0; j < VEC_SIZE; ++j) {
  958. logits_greater_than_pivot[j] = logits_vec[j];
  959. }
  960. threadlocal_max_val =
  961. max(threadlocal_max_val,
  962. BlockReduce<DType, BLOCK_THREADS, REDUCE_ALGORITHM>(
  963. temp_storage.block_prim.reduce)
  964. .Reduce<VEC_SIZE>(logits_greater_than_pivot, cub::Max()));
  965. __syncthreads();
  966. threadlocal_min_val =
  967. min(threadlocal_min_val,
  968. BlockReduce<DType, BLOCK_THREADS, REDUCE_ALGORITHM>(
  969. temp_storage.block_prim.reduce)
  970. .Reduce<VEC_SIZE>(logits_greater_than_pivot, cub::Min()));
  971. __syncthreads();
  972. }
  973. if (tx == 0) {
  974. temp_storage.data.max_val = threadlocal_max_val;
  975. temp_storage.data.min_val = threadlocal_min_val;
  976. }
  977. __syncthreads();
  978. threadlocal_max_val = temp_storage.data.max_val;
  979. threadlocal_min_val = temp_storage.data.min_val;
  980. float low = threadlocal_min_val - 1, high = threadlocal_max_val;
  981. DType min_gt_low, max_le_high;
  982. // f(x) = len(nonzero(probs > x)), f(x) is non-increasing
  983. // min_gt_low = min{p \in probs | p > low}, max_le_high = max{p \in probs |
  984. // p <= high} loop invariant:
  985. // - f(low) >= k, f(high) < k
  986. // - f(low) > f(min_gt_low) >= f(max_le_high) == f(high)
  987. // stopping condition: min_gt_low == max_le_high
  988. // - f(low) >= k, f(min_gt_low) == f(max_le_high) == f(high) < k
  989. do {
  990. int threadlocal_count_sum = 0;
  991. int probs_greater_than_pivot_count[VEC_SIZE]; // pivot initialized to 0
  992. float mid = (low + high) / 2;
  993. min_gt_low = high;
  994. max_le_high = low;
  995. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  996. logits_vec.fill(DType(0));
  997. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  998. logits_vec.load(logits + row_idx * d + i * BLOCK_THREADS * VEC_SIZE +
  999. tx * VEC_SIZE);
  1000. }
  1001. #pragma unroll
  1002. for (uint32_t j = 0; j < VEC_SIZE; ++j) {
  1003. probs_greater_than_pivot_count[j] =
  1004. logits_vec[j] > mid &&
  1005. (i * BLOCK_THREADS + tx) * VEC_SIZE + j < d;
  1006. if (logits_vec[j] > low &&
  1007. (i * BLOCK_THREADS + tx) * VEC_SIZE + j < d) {
  1008. min_gt_low = min(min_gt_low, logits_vec[j]);
  1009. }
  1010. if (logits_vec[j] <= high &&
  1011. (i * BLOCK_THREADS + tx) * VEC_SIZE + j < d) {
  1012. max_le_high = max(max_le_high, logits_vec[j]);
  1013. }
  1014. }
  1015. threadlocal_count_sum +=
  1016. BlockReduce<int, BLOCK_THREADS, REDUCE_ALGORITHM>(
  1017. temp_storage.block_prim.reduce_int)
  1018. .Sum<VEC_SIZE>(probs_greater_than_pivot_count);
  1019. __syncthreads();
  1020. }
  1021. min_gt_low = BlockReduce<DType, BLOCK_THREADS, REDUCE_ALGORITHM>(
  1022. temp_storage.block_prim.reduce)
  1023. .Reduce(min_gt_low, cub::Min());
  1024. __syncthreads();
  1025. max_le_high = BlockReduce<DType, BLOCK_THREADS, REDUCE_ALGORITHM>(
  1026. temp_storage.block_prim.reduce)
  1027. .Reduce(max_le_high, cub::Max());
  1028. if (tx == 0) {
  1029. temp_storage.data.block_aggregate.count = threadlocal_count_sum;
  1030. temp_storage.data.min_val = min_gt_low;
  1031. temp_storage.data.max_val = max_le_high;
  1032. }
  1033. __syncthreads();
  1034. threadlocal_count_sum = temp_storage.data.block_aggregate.count;
  1035. min_gt_low = temp_storage.data.min_val;
  1036. max_le_high = temp_storage.data.max_val;
  1037. if (threadlocal_count_sum >= k) {
  1038. low = mid;
  1039. } else {
  1040. high = min(mid, max_le_high);
  1041. }
  1042. } while (min_gt_low != max_le_high);
  1043. pivot = low;
  1044. }
  1045. // masking
  1046. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  1047. logits_vec.fill(DType(0));
  1048. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  1049. logits_vec.load(logits + row_idx * d + i * BLOCK_THREADS * VEC_SIZE +
  1050. tx * VEC_SIZE);
  1051. }
  1052. #pragma unroll
  1053. for (uint32_t j = 0; j < VEC_SIZE; ++j) {
  1054. logits_vec[j] = (logits_vec[j] > pivot)
  1055. ? logits_vec[j]
  1056. : DType(-std::numeric_limits<float>::infinity());
  1057. }
  1058. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  1059. logits_vec.store(masked_logits + row_idx * d +
  1060. i * BLOCK_THREADS * VEC_SIZE + tx * VEC_SIZE);
  1061. }
  1062. }
  1063. }
  1064. template <uint32_t BLOCK_THREADS, BlockReduceAlgorithm REDUCE_ALGORITHM,
  1065. uint32_t VEC_SIZE, typename DType, typename IdType>
  1066. __global__ void TopKRenormProbKernel(DType* probs, DType* renormed_prob,
  1067. IdType* top_k_arr, uint32_t top_k_val,
  1068. uint32_t d) {
  1069. const uint32_t bx = blockIdx.x, tx = threadIdx.x;
  1070. const uint32_t row_idx = bx;
  1071. uint32_t k = top_k_arr == nullptr ? top_k_val : top_k_arr[bx];
  1072. float pivot = -std::numeric_limits<float>::infinity(), normalizer = 1;
  1073. vec_t<DType, VEC_SIZE> probs_vec;
  1074. if (k < d) {
  1075. extern __shared__ __align__(
  1076. alignof(RenormTempStorage<DType, BLOCK_THREADS, REDUCE_ALGO>))
  1077. uint8_t smem_renorm[];
  1078. auto& temp_storage =
  1079. reinterpret_cast<RenormTempStorage<DType, BLOCK_THREADS, REDUCE_ALGO>&>(
  1080. smem_renorm);
  1081. temp_storage.data.max_val = DType(0);
  1082. DType probs_greater_than_pivot[VEC_SIZE]; // pivot initialized to 0
  1083. DType threadlocal_max_val = DType(0);
  1084. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  1085. probs_vec.fill(DType(0));
  1086. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  1087. probs_vec.load(probs + row_idx * d + i * BLOCK_THREADS * VEC_SIZE +
  1088. tx * VEC_SIZE);
  1089. }
  1090. #pragma unroll
  1091. for (uint32_t j = 0; j < VEC_SIZE; ++j) {
  1092. probs_greater_than_pivot[j] = probs_vec[j];
  1093. }
  1094. threadlocal_max_val =
  1095. max(threadlocal_max_val,
  1096. BlockReduce<DType, BLOCK_THREADS, REDUCE_ALGORITHM>(
  1097. temp_storage.block_prim.reduce)
  1098. .Reduce<VEC_SIZE>(probs_greater_than_pivot, cub::Max()));
  1099. __syncthreads();
  1100. }
  1101. if (tx == 0) {
  1102. temp_storage.data.max_val = threadlocal_max_val;
  1103. }
  1104. __syncthreads();
  1105. threadlocal_max_val = temp_storage.data.max_val;
  1106. float low = 0, high = threadlocal_max_val;
  1107. DType min_gt_low, max_le_high;
  1108. DType sum_low(1);
  1109. // f(x) = len(nonzero(probs > x)), f(x) is non-increasing
  1110. // min_gt_low = min{p \in probs | p > low}, max_le_high = max{p \in probs |
  1111. // p <= high} loop invariant:
  1112. // - f(low) >= k, f(high) < k
  1113. // - f(low) > f(min_gt_low) >= f(max_le_high) == f(high)
  1114. // stopping condition: min_gt_low == max_le_high
  1115. // - f(low) >= k, f(min_gt_low) == f(max_le_high) == f(high) < k
  1116. do {
  1117. Pair<DType> threadlocal_sum{DType(0), 0};
  1118. Pair<DType>
  1119. probs_greater_than_pivot_pair[VEC_SIZE]; // pivot initialized to 0
  1120. float mid = (low + high) / 2;
  1121. min_gt_low = high;
  1122. max_le_high = low;
  1123. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  1124. probs_vec.fill(DType(0));
  1125. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  1126. probs_vec.load(probs + row_idx * d + i * BLOCK_THREADS * VEC_SIZE +
  1127. tx * VEC_SIZE);
  1128. }
  1129. #pragma unroll
  1130. for (uint32_t j = 0; j < VEC_SIZE; ++j) {
  1131. probs_greater_than_pivot_pair[j] = {
  1132. (probs_vec[j] > mid) ? probs_vec[j] : DType(0),
  1133. (probs_vec[j] > mid &&
  1134. (i * BLOCK_THREADS + tx) * VEC_SIZE + j < d)};
  1135. if (probs_vec[j] > low &&
  1136. (i * BLOCK_THREADS + tx) * VEC_SIZE + j < d) {
  1137. min_gt_low = min(min_gt_low, probs_vec[j]);
  1138. }
  1139. if (probs_vec[j] <= high &&
  1140. (i * BLOCK_THREADS + tx) * VEC_SIZE + j < d) {
  1141. max_le_high = max(max_le_high, probs_vec[j]);
  1142. }
  1143. }
  1144. threadlocal_sum +=
  1145. BlockReduce<Pair<DType>, BLOCK_THREADS, REDUCE_ALGORITHM>(
  1146. temp_storage.block_prim.reduce_pair)
  1147. .Sum<VEC_SIZE>(probs_greater_than_pivot_pair);
  1148. __syncthreads();
  1149. }
  1150. min_gt_low = BlockReduce<DType, BLOCK_THREADS, REDUCE_ALGORITHM>(
  1151. temp_storage.block_prim.reduce)
  1152. .Reduce(min_gt_low, cub::Min());
  1153. __syncthreads();
  1154. max_le_high = BlockReduce<DType, BLOCK_THREADS, REDUCE_ALGORITHM>(
  1155. temp_storage.block_prim.reduce)
  1156. .Reduce(max_le_high, cub::Max());
  1157. if (tx == 0) {
  1158. temp_storage.data.block_aggregate.pair = threadlocal_sum;
  1159. temp_storage.data.min_val = min_gt_low;
  1160. temp_storage.data.max_val = max_le_high;
  1161. }
  1162. __syncthreads();
  1163. threadlocal_sum = temp_storage.data.block_aggregate.pair;
  1164. min_gt_low = temp_storage.data.min_val;
  1165. max_le_high = temp_storage.data.max_val;
  1166. if (threadlocal_sum.count >= k) {
  1167. low = mid;
  1168. sum_low = float(threadlocal_sum.value);
  1169. } else {
  1170. high = min(mid, max_le_high);
  1171. }
  1172. } while (min_gt_low != max_le_high);
  1173. normalizer = math::ptx_rcp(max(sum_low, 1e-8));
  1174. pivot = low;
  1175. }
  1176. // normalize
  1177. for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
  1178. probs_vec.fill(DType(0));
  1179. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  1180. probs_vec.load(probs + row_idx * d + i * BLOCK_THREADS * VEC_SIZE +
  1181. tx * VEC_SIZE);
  1182. }
  1183. #pragma unroll
  1184. for (uint32_t j = 0; j < VEC_SIZE; ++j) {
  1185. probs_vec[j] =
  1186. (probs_vec[j] > pivot) ? probs_vec[j] * normalizer : DType(0);
  1187. }
  1188. if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
  1189. probs_vec.store(renormed_prob + row_idx * d +
  1190. i * BLOCK_THREADS * VEC_SIZE + tx * VEC_SIZE);
  1191. }
  1192. }
  1193. }
  1194. template <typename DType>
  1195. cudaError_t TopPRenormProb(DType* probs, DType* renormed_prob, DType* top_p_arr,
  1196. uint32_t batch_size, float top_p_val, uint32_t d,
  1197. cudaStream_t stream = 0) {
  1198. const uint32_t BLOCK_THREADS = 1024;
  1199. const uint32_t vec_size = std::gcd(16 / sizeof(DType), d);
  1200. const uint32_t smem_size =
  1201. sizeof(RenormTempStorage<DType, BLOCK_THREADS, REDUCE_ALGO>);
  1202. dim3 nblks(batch_size);
  1203. dim3 nthrs(BLOCK_THREADS);
  1204. void* args[] = {&probs, &renormed_prob, &top_p_arr, &top_p_val, &d};
  1205. DISPATCH_ALIGNED_VEC_SIZE(vec_size, VEC_SIZE, {
  1206. auto kernel =
  1207. TopPRenormProbKernel<BLOCK_THREADS, REDUCE_ALGO, VEC_SIZE, DType>;
  1208. APHRODITE_CUDA_CALL(cudaFuncSetAttribute(
  1209. kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, smem_size));
  1210. APHRODITE_CUDA_CALL(
  1211. cudaLaunchKernel((void*)kernel, nblks, nthrs, args, smem_size, stream));
  1212. });
  1213. return cudaSuccess;
  1214. }
  1215. template <typename DType, typename IdType>
  1216. cudaError_t TopKRenormProb(DType* probs, DType* renormed_prob,
  1217. IdType* top_k_arr, uint32_t batch_size,
  1218. uint32_t top_k_val, uint32_t d,
  1219. cudaStream_t stream = 0) {
  1220. const uint32_t BLOCK_THREADS = 1024;
  1221. const uint32_t vec_size = std::gcd(16 / sizeof(DType), d);
  1222. const uint32_t smem_size =
  1223. sizeof(RenormTempStorage<DType, BLOCK_THREADS, REDUCE_ALGO>);
  1224. dim3 nblks(batch_size);
  1225. dim3 nthrs(BLOCK_THREADS);
  1226. void* args[] = {&probs, &renormed_prob, &top_k_arr, &top_k_val, &d};
  1227. DISPATCH_ALIGNED_VEC_SIZE(vec_size, VEC_SIZE, {
  1228. auto kernel = TopKRenormProbKernel<BLOCK_THREADS, REDUCE_ALGO, VEC_SIZE,
  1229. DType, IdType>;
  1230. APHRODITE_CUDA_CALL(cudaFuncSetAttribute(
  1231. kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, smem_size));
  1232. APHRODITE_CUDA_CALL(
  1233. cudaLaunchKernel((void*)kernel, nblks, nthrs, args, smem_size, stream));
  1234. });
  1235. return cudaSuccess;
  1236. }
  1237. template <typename DType, typename IdType>
  1238. cudaError_t TopKMaskLogits(DType* logits, DType* masked_logits,
  1239. IdType* top_k_arr, uint32_t batch_size,
  1240. uint32_t top_k_val, uint32_t d,
  1241. cudaStream_t stream = 0) {
  1242. const uint32_t BLOCK_THREADS = 1024;
  1243. const uint32_t vec_size = std::gcd(16 / sizeof(DType), d);
  1244. const uint32_t smem_size =
  1245. sizeof(RenormTempStorage<DType, BLOCK_THREADS, REDUCE_ALGO>);
  1246. dim3 nblks(batch_size);
  1247. dim3 nthrs(BLOCK_THREADS);
  1248. void* args[] = {&logits, &masked_logits, &top_k_arr, &top_k_val, &d};
  1249. DISPATCH_ALIGNED_VEC_SIZE(vec_size, VEC_SIZE, {
  1250. auto kernel = TopKMaskLogitsKernel<BLOCK_THREADS, REDUCE_ALGO, VEC_SIZE,
  1251. DType, IdType>;
  1252. APHRODITE_CUDA_CALL(cudaFuncSetAttribute(
  1253. kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, smem_size));
  1254. APHRODITE_CUDA_CALL(
  1255. cudaLaunchKernel((void*)kernel, nblks, nthrs, args, smem_size, stream));
  1256. });
  1257. return cudaSuccess;
  1258. }
  1259. template <typename T, typename IdType>
  1260. cudaError_t ParallelTopPSamplingFromProb(
  1261. T* probs, T* uniform_samples, IdType* output, bool* success,
  1262. IdType* row_indices, T* top_p_arr, uint32_t batch_size, uint32_t d,
  1263. uint32_t max_top_p_rounds, bool deterministic, cudaStream_t stream = 0) {
  1264. constexpr uint32_t BLOCK_THREADS = 1024;
  1265. const uint32_t vec_size = std::gcd(16 / sizeof(T), d);
  1266. const uint32_t smem_size =
  1267. sizeof(SamplingTempStorage<T, BLOCK_THREADS, SCAN_ALGO, REDUCE_ALGO>);
  1268. dim3 nblks(batch_size);
  1269. dim3 nthrs(BLOCK_THREADS);
  1270. T top_p_placeholder = 0;
  1271. void* args[] = {
  1272. &probs, &uniform_samples, &output, &success, &row_indices,
  1273. &top_p_arr, &top_p_placeholder, &d, &max_top_p_rounds};
  1274. DISPATCH_ALIGNED_VEC_SIZE(
  1275. vec_size, VEC_SIZE,
  1276. {DISPATCH_DETERMINISTIC(deterministic, DETERMINISTIC, {
  1277. auto kernel =
  1278. TopPSamplingFromProbKernel<BLOCK_THREADS, SCAN_ALGO, REDUCE_ALGO,
  1279. VEC_SIZE, DETERMINISTIC, T, IdType>;
  1280. APHRODITE_CUDA_CALL(cudaFuncSetAttribute(
  1281. kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, smem_size));
  1282. APHRODITE_CUDA_CALL(cudaLaunchKernel((void*)kernel, nblks, nthrs, args,
  1283. smem_size, stream));
  1284. })});
  1285. return cudaSuccess;
  1286. }
  1287. } // namespace sampling
  1288. } // namespace aphrodite
  1289. #endif // APHRODITE_SAMPLING_CUH_