[BUG] Cluster metrics fail above 46,340 distinct labels: `int32` overflow in the contingency matrix size and index
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- cpp
- Domain
- data, machine-learning
Research direction
Read cpp/include/raft/stats/detail/mutual_info_score.cuh around the matrix-size calculation and cpp/include/raft/stats/detail/contingencyMatrix.cuh around the element index; the issue also identifies a related size calculation in adjusted_rand_index.cuh. Start by checking the integer types used in those calculations and the existing statistics tests. Done means the reproductions no longer overflow or write out of bounds, with a clear error if the dense matrix cannot be supported.
Written by the indexing model from the issue text.
Description
Describe the bug
Two int overflows limit the dense contingency matrix to 46,340 labels ($46{,}341^2 > 2^{31} - 1$):
-
raft::stats::mutual_info_scorecomputes the size of the matrix asnumUniqueClasses * numUniqueClassesinint. With 46,341 labels the product wraps to a negative value, and the allocation fails withRequested size overflows device_uvector storage.homogeneity_score,completeness_scoreandv_measureare built onmutual_info_scoreand fail with it. -
raft::stats::contingency_matrixcomputes the element index(gt - outIdxOffset) * outMatWidth + pd - outIdxOffsetin the label type. Withintlabels it overflows for the largest labels, and the kernel writes out of bounds. The same call succeeds withint64_tlabels.
Steps/Code to reproduce bug
repro.cu (needs about 8 GiB of GPU memory):
#include <raft/core/device_mdarray.hpp>
#include <raft/core/resource/cuda_stream.hpp>
#include <raft/core/resources.hpp>
#include <raft/stats/contingency_matrix.cuh>
#include <raft/stats/mutual_info_score.cuh>
#include <raft/util/cudart_utils.hpp>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <exception>
#include <numeric>
#include <string>
#include <vector>
std::string first_line(const std::exception& e)
{
std::string message = e.what();
return message.substr(0, message.find('\n'));
}
int main()
{
raft::resources handle;
auto stream = raft::resource::get_cuda_stream(handle);
for (int n_labels : {46340, 46341}) {
// every sample has its own label
std::vector<int> host_labels(n_labels);
std::iota(host_labels.begin(), host_labels.end(), 0);
auto labels = raft::make_device_vector<int, int64_t>(handle, n_labels);
raft::update_device(labels.data_handle(), host_labels.data(), n_labels, stream);
auto view = raft::make_const_mdspan(labels.view());
try {
double mi = raft::stats::mutual_info_score(handle, view, view, 0, n_labels - 1);
std::printf("n_labels=%d: mutual_info_score = %.6f (expected %.6f)\n",
n_labels, mi, std::log(n_labels));
} catch (const std::exception& e) {
std::printf("n_labels=%d: mutual_info_score failed: %s\n", n_labels, first_line(e).c_str());
}
try {
auto matrix = raft::make_device_matrix<int, int64_t>(handle, n_labels, n_labels);
raft::stats::contingency_matrix(handle, view, view, matrix.view());
int last = 0;
raft::update_host(&last, matrix.data_handle() + matrix.size() - 1, 1, stream);
raft::resource::sync_stream(handle);
std::printf("n_labels=%d: contingency_matrix: last diagonal entry = %d (expected 1)\n",
n_labels, last);
} catch (const std::exception& e) {
std::printf("n_labels=%d: contingency_matrix failed: %s\n", n_labels, first_line(e).c_str());
}
}
return 0;
}
Built in a conda environment with the nightly libraft-headers:
nvcc -std=c++20 -arch=native --extended-lambda --expt-relaxed-constexpr \
-I"$CONDA_PREFIX/include" repro.cu -o repro \
-L"$CONDA_PREFIX/lib" -lrmm -lrapids_logger -Xlinker=-rpath,"$CONDA_PREFIX/lib"
./repro
Output:
n_labels=46340: mutual_info_score = 10.743761 (expected 10.743761)
n_labels=46340: contingency_matrix: last diagonal entry = 1 (expected 1)
n_labels=46341: mutual_info_score failed: RMM failure at: .../include/rmm/device_uvector.hpp:628: Requested size overflows device_uvector storage.
n_labels=46341: contingency_matrix failed: CUDA error encountered at: file=.../include/raft/util/cudart_utils.hpp line=138 function=void raft::copy(Type*, const Type*, size_t, cuda::__4::stream_ref) [with Type = int; size_t = long unsigned int]: call='cudaMemcpyAsync(dst, src, len * sizeof(Type), cudaMemcpyDefault, stream.get())', Reason=cudaErrorIllegalAddress:an illegal memory access was encountered
Expected behavior
The mutual information of a labeling with itself is its entropy, here $\ln(46{,}341) ≈ 10.743782$, and the last diagonal entry of its contingency matrix is 1. If label counts this large are not meant to be supported, a clear error would be preferable to a wrapped allocation size or an out-of-bounds write.
Environment details (please complete the following information):
- Environment location: Docker (RAPIDS devcontainer,
cuda13.4-conda) - Method of RAFT install: conda (
libraft-headers27.02.00a17 nightly, commit b74966fb;librmm27.02.00a9). The output is the same with the headers of a source checkout at that commit. - CUDA 13.4 (
nvcc13.4.92, GCC 14.4), Ubuntu 24.04, NVIDIA RTX PRO 4000 Blackwell (24 GB), driver 595.99.02
Additional context
- I found this through cuML, whose
mutual_info_score,homogeneity_score,completeness_scoreandv_measure_scoreall work at 46,340 labels and fail at 46,341. There the first overflow was reported asstd::bad_alloc: out_of_memory: CUDA error (failed to allocate 18446744065119635556 bytes), which is the wrapped size multiplied by 4 bytes and converted tosize_t. adjusted_rand_index<int, unsigned long long, int64_t>returns 1.0 for 46,340 labels with two samples each, and fails withcudaErrorIllegalAddressfor 46,341. Withmath_t = intit fails withcudaErrorInvalidValueinmapThenReduceImplinstead;nUniqClasses * nUniqClassesis evaluated inmath_tthere.- This is separate from #3220, which concerns the product of two marginal counts inside the kernel and affects large clusters; this one concerns the number of labels. The fix proposed in #3221 does not affect the lines above.
- Removing the overflows would raise the limit but not remove it, since the contingency matrix is dense: its size grows with the square of the number of labels (40 GB for 100,000 labels).
Drafted with Claude Code (Fable 5.2), reproduced manually.
- Dominant language
- Cuda
- Stars
- 1.1k
- Forks
- 260
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 21
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from NVIDIA/raft
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
[BUG] Half-precision gemm fails with CUBLAS_STATUS_NOT_SUPPORTED when A or B is not 16-byte alignedOpenbug
Difficulty 4/5 3-5 days Newbie friendliness 55/100
Maintainers usually reply within 1 day
-
[BUG] A column with one infinity sums to NaN in `raft::stats::sum`Possibly taken @galleonli claimed this 4 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 45/100
Maintainers usually reply within 1 day
-
[FEA] Return centers generated by raft::random::make_blobsPossibly taken @NIne-WIngEd claimed this 36 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 53/100
Maintainers usually reply within 1 day
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 58/100
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
emeryberger/CSrankings#15742 ·
Maintainers usually reply within 3 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Unstructured-IO/unstructured#4876 ·
Maintainers usually reply within 6 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
fairagro/m4.2_sql_to_arc#388 ·
Maintainers usually reply within 1 day
-
Add: FAP TV Gay FeedOpencheck:failed feeds:add
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
iptv-org/database#40182 · 1 comment ·
Maintainers usually reply within 1 day