94057c3d3e
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
244 lines
8.5 KiB
Plaintext
244 lines
8.5 KiB
Plaintext
/*
|
|
* Copyright (c) 2025 by SGLang team.
|
|
* Copyright (c) 2024-2025 by FlashInfer team.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
#ifndef SPECULATIVE_SAMPLING_CUH_
|
|
#define SPECULATIVE_SAMPLING_CUH_
|
|
|
|
#include <assert.h>
|
|
|
|
#include <flashinfer/sampling.cuh>
|
|
|
|
namespace flashinfer {
|
|
|
|
namespace sampling {
|
|
|
|
using namespace cub;
|
|
|
|
template <
|
|
uint32_t BLOCK_THREADS,
|
|
BlockScanAlgorithm SCAN_ALGORITHM,
|
|
BlockReduceAlgorithm REDUCE_ALGORITHM,
|
|
uint32_t VEC_SIZE,
|
|
bool DETERMINISTIC,
|
|
typename DType,
|
|
typename IdType,
|
|
typename IdType2>
|
|
__global__ void TreeSpeculativeSamplingTargetOnly(
|
|
IdType* predicts, // mutable
|
|
IdType* accept_index, // mutable
|
|
IdType* accept_token_num, // mutable
|
|
IdType2* candidates,
|
|
IdType2* retrive_index,
|
|
IdType2* retrive_next_token,
|
|
IdType2* retrive_next_sibling,
|
|
DType* uniform_samples,
|
|
DType* uniform_samples_for_final_sampling,
|
|
DType* target_probs,
|
|
DType* draft_probs,
|
|
uint32_t batch_size,
|
|
uint32_t num_speculative_tokens,
|
|
uint32_t num_draft_tokens,
|
|
uint32_t d,
|
|
DType threshold_single,
|
|
DType threshold_acc) {
|
|
const uint32_t bx = blockIdx.x, tx = threadIdx.x;
|
|
|
|
extern __shared__ __align__(alignof(SamplingTempStorage<BLOCK_THREADS, SCAN_ALGORITHM, REDUCE_ALGORITHM>))
|
|
uint8_t smem_sampling[];
|
|
auto& temp_storage =
|
|
reinterpret_cast<SamplingTempStorage<BLOCK_THREADS, SCAN_ALGORITHM, REDUCE_ALGORITHM>&>(smem_sampling);
|
|
|
|
DType prob_acc = 0.0;
|
|
uint32_t cur_prob_offset = bx * num_draft_tokens * d;
|
|
DType coin = uniform_samples[bx * num_draft_tokens];
|
|
IdType2 last_accepted_retrive_idx = retrive_index[bx * num_draft_tokens];
|
|
accept_index[bx * num_speculative_tokens] = last_accepted_retrive_idx;
|
|
uint32_t num_accepted_tokens = 0;
|
|
IdType2 cur_index = 0;
|
|
|
|
for (uint32_t j = 1; j < num_speculative_tokens; ++j) {
|
|
cur_index = retrive_next_token[bx * num_draft_tokens + cur_index];
|
|
while (cur_index != -1) {
|
|
IdType2 draft_index = retrive_index[bx * num_draft_tokens + cur_index];
|
|
IdType2 draft_token_id = candidates[bx * num_draft_tokens + cur_index];
|
|
DType target_prob_single = target_probs[cur_prob_offset + draft_token_id];
|
|
prob_acc += target_prob_single;
|
|
|
|
if (coin <= prob_acc / threshold_acc || target_prob_single >= threshold_single) {
|
|
// accept token
|
|
prob_acc = 0.;
|
|
cur_prob_offset = (bx * num_draft_tokens + cur_index) * d;
|
|
coin = uniform_samples[bx * num_draft_tokens + cur_index];
|
|
predicts[last_accepted_retrive_idx] = draft_token_id;
|
|
++num_accepted_tokens;
|
|
accept_index[bx * num_speculative_tokens + num_accepted_tokens] = draft_index;
|
|
last_accepted_retrive_idx = draft_index;
|
|
break;
|
|
} else {
|
|
// FIXME: leverage draft probs
|
|
draft_probs[cur_prob_offset + draft_token_id] = target_probs[cur_prob_offset + draft_token_id];
|
|
cur_index = retrive_next_sibling[bx * num_draft_tokens + cur_index];
|
|
}
|
|
}
|
|
if (cur_index == -1) break;
|
|
}
|
|
accept_token_num[bx] = num_accepted_tokens;
|
|
|
|
// we need a different coin for the final sampling
|
|
coin = uniform_samples_for_final_sampling[bx];
|
|
|
|
// sample from relu(target_probs - draft_probs)
|
|
DType sum_relu_q_minus_p(0);
|
|
vec_t<DType, VEC_SIZE> q_vec, p_vec;
|
|
DType relu_q_minus_p[VEC_SIZE];
|
|
for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
|
|
q_vec.fill(DType(0));
|
|
p_vec.fill(DType(0));
|
|
if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
|
|
q_vec.load(target_probs + cur_prob_offset + i * BLOCK_THREADS * VEC_SIZE + tx * VEC_SIZE);
|
|
if (num_accepted_tokens != num_speculative_tokens - 1) {
|
|
// there is no draft_probs for the bonus token
|
|
p_vec.load(draft_probs + cur_prob_offset + i * BLOCK_THREADS * VEC_SIZE + tx * VEC_SIZE);
|
|
}
|
|
}
|
|
#pragma unroll
|
|
for (uint32_t j = 0; j < VEC_SIZE; ++j) {
|
|
relu_q_minus_p[j] = max(q_vec[j] - p_vec[j], DType(0));
|
|
}
|
|
sum_relu_q_minus_p += BlockReduce<DType, BLOCK_THREADS, REDUCE_ALGORITHM>(temp_storage.block_prim.reduce)
|
|
.Sum<VEC_SIZE>(relu_q_minus_p);
|
|
__syncthreads();
|
|
}
|
|
if (tx == 0) {
|
|
temp_storage.block_aggregate.value = sum_relu_q_minus_p;
|
|
}
|
|
|
|
temp_storage.sampled_id = d;
|
|
temp_storage.last_valid_id = -1;
|
|
__syncthreads();
|
|
sum_relu_q_minus_p = temp_storage.block_aggregate.value;
|
|
DType u = coin * sum_relu_q_minus_p;
|
|
|
|
DType aggregate_relu_q_minus_p(0);
|
|
for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
|
|
q_vec.fill(DType(0));
|
|
p_vec.fill(DType(0));
|
|
if ((i * BLOCK_THREADS + tx) * VEC_SIZE < d) {
|
|
q_vec.load(target_probs + cur_prob_offset + i * BLOCK_THREADS * VEC_SIZE + tx * VEC_SIZE);
|
|
if (num_accepted_tokens != num_speculative_tokens - 1) {
|
|
// there is no draft_probs for the bonus token
|
|
p_vec.load(draft_probs + cur_prob_offset + i * BLOCK_THREADS * VEC_SIZE + tx * VEC_SIZE);
|
|
}
|
|
}
|
|
|
|
vec_t<DType, VEC_SIZE> relu_q_minus_p_vec;
|
|
#pragma unroll
|
|
for (uint32_t j = 0; j < VEC_SIZE; ++j) {
|
|
relu_q_minus_p_vec[j] = max(q_vec[j] - p_vec[j], DType(0));
|
|
}
|
|
|
|
DeviceSamplingFromProb<VEC_SIZE, BLOCK_THREADS, SCAN_ALGORITHM, REDUCE_ALGORITHM, DETERMINISTIC>(
|
|
i, d, [&](DType x) { return x > 0; }, u, relu_q_minus_p_vec, aggregate_relu_q_minus_p, &temp_storage);
|
|
if (aggregate_relu_q_minus_p > u) {
|
|
break;
|
|
}
|
|
}
|
|
__syncthreads();
|
|
// This would happen when u is very close to 1
|
|
// and the sum of probabilities is smaller than u
|
|
// In this case, we use the last valid index as the sampled id
|
|
int sampled_id = temp_storage.sampled_id;
|
|
if (sampled_id == d) {
|
|
if (temp_storage.last_valid_id == -1) {
|
|
sampled_id = d - 1;
|
|
} else {
|
|
sampled_id = temp_storage.last_valid_id;
|
|
}
|
|
}
|
|
// set the first rejected token
|
|
predicts[last_accepted_retrive_idx] = sampled_id;
|
|
// value at not used indices are undefined
|
|
}
|
|
|
|
template <typename DType, typename IdType, typename IdType2>
|
|
cudaError_t TreeSpeculativeSamplingTargetOnly(
|
|
IdType* predicts, // mutable
|
|
IdType* output_token_ids, // mutable
|
|
IdType* output_accepted_token_num, // mutable
|
|
IdType2* candidates,
|
|
IdType2* retrive_index,
|
|
IdType2* retrive_next_token,
|
|
IdType2* retrive_next_sibling,
|
|
DType* uniform_samples,
|
|
DType* uniform_samples_for_final_sampling,
|
|
DType* target_probs,
|
|
DType* draft_probs,
|
|
uint32_t batch_size,
|
|
uint32_t num_speculative_tokens,
|
|
uint32_t num_draft_tokens,
|
|
uint32_t d,
|
|
DType threshold_single = 1,
|
|
DType threshold_acc = 1,
|
|
bool deterministic = true,
|
|
cudaStream_t stream = 0) {
|
|
constexpr uint32_t BLOCK_THREADS = 1024;
|
|
const uint32_t vec_size = std::gcd(16 / sizeof(DType), d);
|
|
|
|
const uint32_t smem_size = sizeof(SamplingTempStorage<BLOCK_THREADS, SCAN_ALGO, REDUCE_ALGO>);
|
|
dim3 nblks(batch_size);
|
|
dim3 nthrs(BLOCK_THREADS);
|
|
float capped_threshold_acc = fmaxf(threshold_acc, 1e-9f);
|
|
void* args[] = {
|
|
&predicts,
|
|
&output_token_ids,
|
|
&output_accepted_token_num,
|
|
&candidates,
|
|
&retrive_index,
|
|
&retrive_next_token,
|
|
&retrive_next_sibling,
|
|
&uniform_samples,
|
|
&uniform_samples_for_final_sampling,
|
|
&target_probs,
|
|
&draft_probs,
|
|
&batch_size,
|
|
&num_speculative_tokens,
|
|
&num_draft_tokens,
|
|
&d,
|
|
&threshold_single,
|
|
&capped_threshold_acc};
|
|
DISPATCH_ALIGNED_VEC_SIZE(
|
|
vec_size, VEC_SIZE, {DISPATCH_DETERMINISTIC(deterministic, DETERMINISTIC, {
|
|
auto kernel = TreeSpeculativeSamplingTargetOnly<
|
|
BLOCK_THREADS,
|
|
SCAN_ALGO,
|
|
REDUCE_ALGO,
|
|
VEC_SIZE,
|
|
DETERMINISTIC,
|
|
DType,
|
|
IdType,
|
|
IdType2>;
|
|
FLASHINFER_CUDA_CALL(cudaFuncSetAttribute(kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, smem_size));
|
|
FLASHINFER_CUDA_CALL(cudaLaunchKernel((void*)kernel, nblks, nthrs, args, smem_size, stream));
|
|
})});
|
|
return cudaSuccess;
|
|
}
|
|
|
|
} // namespace sampling
|
|
|
|
} // namespace flashinfer
|
|
|
|
#endif // SPECULATIVE_SAMPLING_CUH_
|