Skip to content

[cuda.compute]: add support for bfloat16 - #10933

Merged
NaderAlAwar merged 5 commits into
NVIDIA:mainfrom
NaderAlAwar:cuda-compute-bfloat16
Aug 24, 2026
Merged

[cuda.compute]: add support for bfloat16#10933
NaderAlAwar merged 5 commits into
NVIDIA:mainfrom
NaderAlAwar:cuda-compute-bfloat16

Conversation

@NaderAlAwar

Copy link
Copy Markdown
Contributor

Description

closes #10906

This PR allows using bfloat16 in cuda.compute, which corresponds to _nv_bfloat16 in CUDA C++. One wrinkle is that numpy does not support bfloat16, so we have to rely on the ml_dtypes package. I decided to not add this to the list of required dependencies, rather users who want to use bfloat16 must install ml_dtypes themselves, which is how CuPy does it.

I verified that this is compatible with CuPy arrays and PyTorch tensors.

Checklist

  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@NaderAlAwar
NaderAlAwar requested review from a team as code owners August 21, 2026 15:15
@NaderAlAwar
NaderAlAwar requested a review from elstehle August 21, 2026 15:15
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Aug 21, 2026
@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Review in CCCL Aug 21, 2026
@coderabbitai

coderabbitai Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

  • New Features
    • Added CUDA bfloat16 support across C++ and Python compute APIs.
    • Added public TypeEnum.BFLOAT16 and bfloat16 type descriptor.
    • Enabled bfloat16 reductions, scans, sorting, transforms, serialization, iterators, and related operations.
    • Improved interoperability with NumPy and PyTorch bfloat16 data.
  • Bug Fixes
    • Improved bfloat16 dtype detection and data conversion.
  • Tests
    • Added comprehensive bfloat16 coverage across supported compute operations and platforms.
  • Limitations
    • Python callable operators and bfloat16 histogram binning are not supported.

Walkthrough

Changes

Bfloat16 support is added to CCCL C++ and Python compute APIs. Type conversion, JIT generation, interop, reductions, scans, sorting, serialization, histogram handling, and CI tests now handle __nv_bfloat16 when supported.

Bfloat16 support

Layer / File(s) Summary
C++ bfloat16 type integration
c/parallel..., c/parallel.v2/...
The public type enums, JIT code generation, type conversion, serialization, histogram checks, and test helpers recognize CCCL_BFLOAT16.
Python bfloat16 API and interop
python/cuda_cccl/cuda/compute/...
The Python bindings expose BFLOAT16 and bfloat16. Optional ml_dtypes support, PyTorch dtype detection, byte conversion, C++ mapping, and callable validation are included.
Bfloat16 validation and CI
python/cuda_cccl/tests/compute/test_bfloat16.py, c/parallel/test/test_reduce.cpp, ci/...
Tests cover reductions, scans, sorting, transforms, serialization, and unsupported callable operators. Linux and Windows CI install ml_dtypes and run the dedicated tests.

Assessment against linked issues

Objective Addressed Explanation
Add bfloat16 support to cuda.compute [#10906]

Suggested reviewers: oleksandr-pavlyk, elstehle

Merge Risk: 🟡 Moderate · up to b979d

At the current head, bfloat16 support has a known failing histogram test pending PR #10954, along with unresolved compile and host-layout risks in supported configurations; the test marker also will not fail on unexpected passes. Merge should wait for these issues to be fixed or explicitly accepted.


Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (1)
c/parallel/test/test_reduce.cpp (1)

525-525: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

suggestion: Qualify operation_t::operator cccl_op_t() as const, then declare op const. A const operation_t cannot use the current conversion in reduce(...).

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 91e893a9-45c6-44ef-b092-36ca3460efd6

📥 Commits

Reviewing files that changed from the base of the PR and between d10a88a and 86adf70.

📒 Files selected for processing (23)
  • c/parallel.v2/include/cccl/c/types.h
  • c/parallel.v2/src/hostjit/codegen/cub_call.cpp
  • c/parallel.v2/src/hostjit/codegen/types.cpp
  • c/parallel/include/cccl/c/types.h
  • c/parallel/src/histogram.cu
  • c/parallel/src/jit_templates/template_pre.h.in
  • c/parallel/src/kernels/operators.cpp
  • c/parallel/src/util/serialization.h
  • c/parallel/src/util/types.h
  • c/parallel/test/test_reduce.cpp
  • c/parallel/test/test_util.h
  • ci/test_cuda_compute_python.sh
  • ci/windows/test_cuda_compute_python.ps1
  • python/cuda_cccl/cuda/compute/_bindings.pyi
  • python/cuda_cccl/cuda/compute/_bindings_impl.pyx
  • python/cuda_cccl/cuda/compute/_cccl_interop.py
  • python/cuda_cccl/cuda/compute/_cpp_compile.py
  • python/cuda_cccl/cuda/compute/_jit.py
  • python/cuda_cccl/cuda/compute/_utils/protocols.py
  • python/cuda_cccl/cuda/compute/iterators/_common.py
  • python/cuda_cccl/cuda/compute/types.py
  • python/cuda_cccl/pyproject.toml
  • python/cuda_cccl/tests/compute/test_bfloat16.py

Included review availability: Your plan provides up to 12 included reviews per hour; 11 remain after this review.

Comment thread c/parallel/src/histogram.cu
Comment thread python/cuda_cccl/cuda/compute/types.py
Comment thread python/cuda_cccl/tests/compute/test_bfloat16.py Outdated
return _register_struct_with_numba(struct_class)

# Numba has no bfloat16 support; fail with a clear message rather than a
# cryptic numba error deep in the compilation pipeline.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #10941 to track.

@github-actions

This comment has been minimized.

@NaderAlAwar

Copy link
Copy Markdown
Contributor Author

CI won't pass until the histogram bfloat16 bug fix is merged #10954

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: e7d1550d-b362-4d7b-b80d-12cdaaa9ce82

📥 Commits

Reviewing files that changed from the base of the PR and between 86adf70 and 50a5dd2.

📒 Files selected for processing (4)
  • c/parallel/src/histogram.cu
  • c/parallel/test/test_util.h
  • python/cuda_cccl/cuda/compute/types.py
  • python/cuda_cccl/tests/compute/test_bfloat16.py

Included review availability: Your plan provides up to 12 included reviews per hour; 8 remain after this review.

Comment on lines +489 to +509
def test_histogram_even():
num_samples = 1000
num_bins = 8
h_samples = random_bfloat16(num_samples, low=0.0, high=8.0, seed=29)
d_samples = DeviceArray.from_numpy(h_samples)
d_histogram = DeviceArray.from_numpy(np.zeros(num_bins, dtype=np.uint32))

cuda.compute.histogram_even(
d_samples=d_samples,
d_histogram=d_histogram,
num_output_levels=num_bins + 1,
lower_level=np.array([0.0], dtype=BFLOAT16)[0],
upper_level=np.array([8.0], dtype=BFLOAT16)[0],
num_samples=num_samples,
)

# histogram_even uses half-open [lower, upper) bins; samples that rounded
# up to exactly 8.0 in bfloat16 are out of range.
samples = h_samples.astype(np.float64)
expected = np.histogram(samples[samples < 8.0], bins=num_bins, range=(0, 8))[0]
np.testing.assert_array_equal(d_histogram.copy_to_host(), expected)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift

important: Include the histogram bfloat16 fix before this test runs in required CI. The PR objective states that this test cannot pass until PR #10954 merges. Rebase or stack this change on that fix, then run the affected test. As per coding guidelines, “Build and run relevant tests before submitting contributions to ensure existing functionality is not broken.”

Source: Coding guidelines

@github-actions

This comment has been minimized.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 8c59129e-e356-47bf-96f9-fc30e2e6e584

📥 Commits

Reviewing files that changed from the base of the PR and between 50a5dd2 and b979df5.

📒 Files selected for processing (2)
  • python/cuda_cccl/cuda/compute/algorithms/_histogram.py
  • python/cuda_cccl/tests/compute/test_bfloat16.py

Included review availability: Your plan provides up to 12 included reviews per hour; 10 remain after this review.

Comment on lines +489 to +493
@pytest.mark.xfail(
raises=NotImplementedError,
reason="CUB's HistogramEven bins bfloat16 samples incorrectly, so "
"make_histogram_even rejects bfloat16; "
"see https://github.com/NVIDIA/cccl/issues/10940",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

while IFS= read -r file; do
  rg -n -C 3 'xfail_strict|strict\s*=' "$file" || true
done < <(fd -a -t f '^(pyproject\.toml|pytest\.ini|setup\.cfg|tox\.ini)$' .)

rg -n -C 8 '`@pytest`\.mark\.xfail|histogram' \
  python/cuda_cccl/tests/compute/test_bfloat16.py

Repository: NVIDIA/cccl

Length of output: 201


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '%s\n' '--- pytest configuration files ---'
find . -maxdepth 3 -type f \( -name 'pyproject.toml' -o -name 'pytest.ini' -o -name 'setup.cfg' -o -name 'tox.ini' \) -print

printf '%s\n' '--- strict-xfail settings ---'
rg -n -C 4 'xfail_strict|strict[[:space:]]*=' . --glob 'pyproject.toml' --glob 'pytest.ini' --glob 'setup.cfg' --glob 'tox.ini' || true

printf '%s\n' '--- test marker and body ---'
sed -n '470,525p' python/cuda_cccl/tests/compute/test_bfloat16.py

printf '%s\n' '--- test guidance and required project docs ---'
for file in .agent/skills/cccl-test/SKILL.md CONTRIBUTING.md docs/infrastructure/ci/references/ci_overview.rst; do
  if [ -f "$file" ]; then
    printf '%s\n' "--- $file ---"
    sed -n '1,220p' "$file"
  else
    printf '%s\n' "MISSING: $file"
  fi
done

Repository: NVIDIA/cccl

Length of output: 23523


important: Add strict=True to this pytest.mark.xfail marker. The repository has no global strict-xfail setting. Without strict mode, an XPASS will not fail CI.

@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 1h 42m: Pass: 100%/531 | Total: 5d 19h | Max: 1h 17m | Hits: 100%/624069

See results here.

@NaderAlAwar
NaderAlAwar merged commit 8f7e8bf into NVIDIA:main Aug 24, 2026
559 checks passed
@github-project-automation github-project-automation Bot moved this from In Review to Done in CCCL Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

[cuda.compute]: add support for bfloat16

3 participants