Skip to content

Commit 64ac9ab

Browse files
authored
CUDA : Fix CUB's argsort when nrows % block_size == 0 CCCL < 3.1 (#21181)
* CUDA: Fix CUB's argsort when nrows % block_size == 0 CCCL < 3.1 We wrongly calculated offset_grid as `ceildiv(nrows, block_size)`, while it must be `ceildiv(nrows + 1, block_size)`. As a consequence, we had uninitialized values in `offset_iterator[nrows]` for the case when `nrows % block_size == 0`. Fixes #21162 * Reduce nrows in test case to 256, don't need 768
1 parent cad2d38 commit 64ac9ab

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

ggml/src/ggml-cuda/argsort.cu

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ void argsort_f32_i32_cuda_cub(ggml_cuda_pool & pool,
4747
#ifdef STRIDED_ITERATOR_AVAILABLE
4848
auto offset_iterator = cuda::make_strided_iterator(cuda::make_counting_iterator(0), ncols);
4949
#else
50-
ggml_cuda_pool_alloc<int> offsets_alloc(pool, nrows + 1);
50+
// offset_iterator needs to populate nrows + 1 elements, so we also have to ceildiv nrows + 1 by block_size
51+
const int nrows_offset = nrows + 1;
52+
ggml_cuda_pool_alloc<int> offsets_alloc(pool, nrows_offset);
5153
int * offset_iterator = offsets_alloc.get();
52-
const dim3 offset_grid((nrows + block_size - 1) / block_size);
54+
const dim3 offset_grid((nrows_offset + block_size - 1) / block_size);
5355
init_offsets<<<offset_grid, block_size, 0, stream>>>(offset_iterator, ncols, nrows);
5456
#endif
5557
CUDA_CHECK(cudaMemcpyAsync(temp_keys, x, ncols * nrows * sizeof(float), cudaMemcpyDeviceToDevice, stream));

tests/test-backend-ops.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8424,6 +8424,7 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
84248424
test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {1023, 2, 1, 3}, order));
84258425
test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {1024, 2, 1, 3}, order));
84268426
test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {1025, 2, 1, 3}, order));
8427+
test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {1025, 256, 1, 1}, order)); // test ceildiv in CUDA's CUB's DeviceSegmentedSort
84278428
test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {2047, 2, 1, 3}, order));
84288429
test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {2048, 2, 1, 3}, order));
84298430
test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {2049, 2, 1, 3}, order));

0 commit comments

Comments
 (0)