Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: Ensure Loop Termination by Enforcing IEEE-754 Compliance in Sampling Kernels #774

Merged
merged 1 commit into from
Feb 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions include/flashinfer/sampling.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ __global__ void TopPRenormProbKernel(DType* probs, DType* renormed_prob, DType*
__syncthreads();
threadlocal_max_val = temp_storage.max_val;

float low = 0, high = threadlocal_max_val;
double low = 0, high = threadlocal_max_val;
DType min_gt_low, max_le_high;
DType sum_low(1);
// f(x) = sum(probs[probs > x]), f(x) is non-increasing
Expand All @@ -839,7 +839,7 @@ __global__ void TopPRenormProbKernel(DType* probs, DType* renormed_prob, DType*
// - f(low) >= p, f(min_gt_low) == f(max_le_high) == f(high) < p
do {
DType threadlocal_sum(0);
float mid = (low + high) / 2;
double mid = (low + high) / 2;
min_gt_low = high;
max_le_high = low;
for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
Expand Down Expand Up @@ -949,7 +949,7 @@ __global__ void TopKMaskLogitsKernel(DType* logits, DType* masked_logits, IdType
threadlocal_max_val = temp_storage.max_val;
threadlocal_min_val = temp_storage.min_val;

float low = threadlocal_min_val - 1, high = threadlocal_max_val;
double low = threadlocal_min_val - 1, high = threadlocal_max_val;
DType min_gt_low, max_le_high;
// f(x) = len(nonzero(probs > x)), f(x) is non-increasing
// min_gt_low = min{p \in probs | p > low}, max_le_high = max{p \in probs | p <= high}
Expand All @@ -961,7 +961,7 @@ __global__ void TopKMaskLogitsKernel(DType* logits, DType* masked_logits, IdType
do {
int threadlocal_count_sum = 0;
int probs_greater_than_pivot_count[VEC_SIZE]; // pivot initialized to 0
float mid = (low + high) / 2;
double mid = (low + high) / 2;
min_gt_low = high;
max_le_high = low;
for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
Expand Down Expand Up @@ -1067,7 +1067,7 @@ __global__ void TopKRenormProbKernel(DType* probs, DType* renormed_prob, IdType*
__syncthreads();
threadlocal_max_val = temp_storage.max_val;

float low = 0, high = threadlocal_max_val;
double low = 0, high = threadlocal_max_val;
DType min_gt_low, max_le_high;
DType sum_low(1);
// f(x) = len(nonzero(probs > x)), f(x) is non-increasing
Expand All @@ -1080,7 +1080,7 @@ __global__ void TopKRenormProbKernel(DType* probs, DType* renormed_prob, IdType*
do {
Pair<DType> threadlocal_sum{DType(0), 0};
Pair<DType> probs_greater_than_pivot_pair[VEC_SIZE]; // pivot initialized to 0
float mid = (low + high) / 2;
double mid = (low + high) / 2;
min_gt_low = high;
max_le_high = low;
for (uint32_t i = 0; i < ceil_div(d, BLOCK_THREADS * VEC_SIZE); ++i) {
Expand Down