deepseek-ai / deepseek-ai/DeepSelect

Sorted Top-K index offsets overflow before int64 conversion

Open
#1 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Cuda
Stars
343
Forks
21
Avg merge
10m
Merged PRs (30d)
2

Description

With `sorted=True` and int64 output indices, the epilogue adds `output_idx_offset` in signed 32-bit arithmetic before converting to the output type. Selecting index 1 with offset `2147483647` therefore overflows instead of producing the representable int64 result `2147483648`.

The short-row path also computes `idx_oob_fill_value - output_idx_offset` in signed 32-bit arithmetic to encode padding. The default fill `2147483647` and offset `-1` overflow that subtraction, although padding is documented to remain unchanged by the offset.

Both expressions are in `csrc/cuda_kernels/common_parts.cuh` at revision `8e70df7`. Standalone host copies of the expressions reproduce signed-overflow diagnostics with UBSan; the CUDA kernels have not been run for these cases.

Proposed CUDA reproduction on supported hardware:

```python
import torch
import deep_select

x = torch.zeros((1, 8192), dtype=torch.float32, device="cuda")
x[0, 1] = 1
offset = torch.tensor([2147483647], dtype=torch.int32, device="cuda")
_, indices = deep_select.topk(x, 1, sorted=True, output_idx_offset=offset)
assert indices.item() == 2147483648
```

Valid indices should be widened before addition, and short-row padding should be emitted without offset arithmetic. Tests should cover both selection and short-row paths with extreme signed offsets and fill values.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in csrc/cuda_kernels/common_parts.cuh at the two expressions used by the sorted output and short-row paths, then run the proposed CUDA reproduction on supported hardware. Add coverage for selection and padding with extreme signed offsets and fill values, verifying valid indices widen correctly and padding remains unchanged by the offset.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
75/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.