Skip to content

feat(nvidia): add top-k top-p sampling provider - #922

Draft
voltjia wants to merge 1 commit into
masterfrom
feat/nvidia-top-k-top-p-sampling
Draft

feat(nvidia): add top-k top-p sampling provider#922
voltjia wants to merge 1 commit into
masterfrom
feat/nvidia-top-k-top-p-sampling

Conversation

@voltjia

@voltjia voltjia commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Add NVIDIA implementation 0 for the canonical TopKTopPSamplingFromLogits operator under src/native/cuda/nvidia/ops/top_k_top_p_sampling_from_logits/.
  • Support contiguous float16, bfloat16, float32, and float64 logits, host-side per-request filters, optional host row indices, and int32 or int64 output as required by the public API.
  • Implement distinct top_k_first and joint filtering semantics with counter-based deterministic seed and offset handling.
  • Add focused Python and C++ coverage for filtering, dtype boundaries, row selection, caller-provided workspace, large offsets, output distribution, and concurrent CUDA streams.

Motivation

The canonical TopKTopPSamplingFromLogits API has no native NVIDIA implementation. This change makes the aligned API usable on NVIDIA without relying on workload-specific sampling operators, while preserving deterministic seed and offset behavior and supporting both public filter orders.

No linked issue.

Type of Change

  • feat - new feature / new operator / new platform
  • fix - bug fix
  • perf - performance improvement (no behavioral change)
  • refactor - code restructuring without behavior change
  • test - adding or fixing tests only
  • docs - documentation only
  • build / ci - build system or CI configuration
  • chore - tooling, formatting, or other non-code changes
  • Breaking change

Platforms Affected

  • CPU (WITH_CPU)
  • NVIDIA (WITH_NVIDIA)
  • Iluvatar (WITH_ILUVATAR)
  • MetaX (WITH_METAX)
  • Cambricon (WITH_CAMBRICON)
  • Moore (WITH_MOORE)
  • Ascend (WITH_ASCEND)
  • PyTorch C++ bindings (WITH_TORCH)
  • Build system / CMake / CI
  • Python bindings / user-facing API

Smoke Test Result

Hardware: NVIDIA A100-SXM4-80GB

Image: accelerator-dev/nvidia:latest

python3 -m pip install . --target /workspace/python-smoke --upgrade --force-reinstall --no-build-isolation --no-deps -C build-dir=/workspace/build-smoke -C cmake.define.INFINI_RT_ROOT=/workspace/infinirt-prefix -C cmake.define.WITH_CPU=OFF -C cmake.define.WITH_NVIDIA=ON -C cmake.define.INFINI_OPS_SMOKE_BUILD=ON -C cmake.define.GENERATE_PYTHON_BINDINGS=ON -C cmake.define.CMAKE_CUDA_ARCHITECTURES=80
Wheel SHA256: 4aa3c761d36a16ddfae7cba1a82bc21a00f2f8f6c80037d3e4fdf89aeb0835ce

python3 -m pytest tests -m smoke -q --devices nvidia
82 passed, 25 skipped, 7803 deselected in 4.15s

Test Results on Supported Platforms

Platform Affected Build / Smoke Result Full Result / Notes
NVIDIA Yes Smoke build and tests passed Focused provider: 17 passed; C++ caller workspace: 1 passed; full suite not run
Iluvatar No N/A - not affected N/A
MetaX No N/A - not affected N/A
Cambricon No N/A - not affected N/A
Moore No N/A - not affected N/A
Ascend No N/A - not affected N/A
Focused NVIDIA validation
python3 -m pytest -q tests/test_top_k_top_p_sampling_from_logits.py --devices nvidia
17 passed in 2.72s

python3 -m pytest -q tests/test_cpp_api.py::test_cpp_top_k_top_p_sampling_uses_caller_workspace --devices nvidia
1 passed in 2.09s

Targeted wheel SHA256: 8a8fbea8318632c50063bd62bd1912eb0b6ca38439b3882b72e5a8b4a96dbfd1

Formatting checks: clang-format 21.1.8 dry-run passed; Ruff 0.15.22 format and lint checks passed.

Benchmark / Performance Impact

N/A. There is no previous native NVIDIA provider to use as a baseline, and this Draft PR validates correctness rather than throughput.

Notes for Reviewers

  • joint uses random * min(top_p * full_probability_mass, top_k_cumulative_probability) and is intentionally distinct from top_k_first.
  • The default workspace cache is bounded to two stream-aware slots per cached operator. Additional streams wait for a completed slot before reuse; caller-provided workspace remains preferred.
  • Random values are generated in O(1) from seed and offset + row using a counter-based generator.
  • The provider currently supports deterministic=true and check_nan=false.
  • top_k, top_p, and optional indices must be contiguous host tensors.
  • Rows are processed independently with CUB radix sort and inclusive scan. Performance tuning is outside this correctness-focused change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant