Skip to content

cuda.core: map ml_dtypes bfloat16 to kDLBfloat in DLPack export - #3359

Open
Sainath-Reddy7 wants to merge 1 commit into
NVIDIA:mainfrom
Sainath-Reddy7:gh-3337-bf16-dlpack-export
Open

Sainath-Reddy7 wants to merge 1 commit into
NVIDIA:mainfrom
Sainath-Reddy7:gh-3337-bf16-dlpack-export

Conversation

@Sainath-Reddy7

Copy link
Copy Markdown

Description

closes #3337

_smv_dtype_numpy_to_dlpack picked the DLPack type code from numpy.dtype.kind, but ml_dtypes.bfloat16 has kind "V", so a bf16 view fell through to the unsupported-dtype BufferError and could not be exported — even though the import side already accepts kDLBfloat (mapping it back to bfloat16 when ml_dtypes is installed) and _tensor_map.pyx maps bfloat16 the same way. A bf16 tensor from PyTorch/CuPy/JAX can be viewed but not re-exported.

This adds an explicit bfloat16 branch ahead of the kind chain that emits kDLBfloat with 16 bits, guarded by the module-level ml_dtypes availability check already used on the import side.

The new test round-trips through StridedMemoryView.from_dlpack itself, so it cannot skip on stacks where NumPy's own DLPack support for extended dtypes is unavailable — the hole that let the existing test_dlpack_export_roundtrip_dtypes[bfloat16] miss this via its NumPy-consumer probe.

Evidence from the released cuda-core 1.2.2 wheel (host memory; numpy 2.3.5, ml_dtypes 0.6.0), matching the issue's reproducer:

>>> view.__dlpack__()
BufferError: Unsupported dtype for DLPack export: bfloat16

with float16/uint16 views of the same buffer exporting fine. Additionally: cython -3 on cuda/core/_memoryview.pyx produces identical diagnostics on base and on this branch (the two existing cuStreamWaitEvent assign notes are pre-existing; none added), and the new branch's dtype equality check matches bfloat16 while not matching bool/uint16/float16/int8. Full build and suite validation runs in this repository's CI.

Checklist

  • I have read the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

AI assistance disclosure: the change and test were prepared with an AI coding agent (GLM-5.3) under my direction; I reviewed each line, verified the reproducer and evidence above, and stand behind the change. The new test carries @pytest.mark.agent_authored(model="glm-5.3") per the provenance rules in AGENTS.md.

@copy-pr-bot

copy-pr-bot Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the cuda.core Everything related to the cuda.core module label Nov 4, 2026
@coderabbitai

coderabbitai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Explain Change →

Summary

Summary by CodeRabbit

  • Bug Fixes
    • DLPack exports now correctly recognize bfloat16, improving compatibility when sharing these arrays with other tools. Importing and exporting bfloat16 arrays preserves their dtype, shape, and data pointer. Handling of other data types remains unchanged.

DLPack export now maps ml_dtypes.bfloat16 to kDLBfloat. A round-trip test checks the imported dtype, shape, and data pointer.

Changes: bfloat16 DLPack export

Layer / File(s) Summary
Map and round-trip bfloat16
cuda_core/cuda/core/_memoryview.pyx, cuda_core/tests/test_utils_dlpack.py
_smv_dtype_numpy_to_dlpack maps ml_dtypes.bfloat16 to kDLBfloat when ml_dtypes is available. The test checks the imported dtype, shape, and data pointer. It skips when ml_dtypes is unavailable.

Assessment against linked issues

Objective Addressed Explanation
Enable StridedMemoryView bfloat16 DLPack export and same-memory re-import [#3337] ✅

Suggested reviewers: rluo8

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Low · up to 3d46b

The change is low risk, but the test should fail loudly when its declared ml-dtypes dependency is unavailable.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Tip

🎯 Customize your risk assessments

Guide how CodeRabbit assesses merge risk and security architecture risk. Highlight sensitive changes and explain your security boundaries using Merge Risk Instructions and Security Risk Instructions in your review settings. Security architecture risk assessments require the Advanced plan or above.

✅ Automatic approvals, with your rules

Let CodeRabbit approve pull requests once required threads are resolved, pre-merge checks pass, and your risk limits are met. Enable Automatic approvals in review settings, with overrides for teams or file paths.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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
Commits

Reviewing files that changed from the base of the PR and between 4010017 and eb3fdb0.

Files selected for processing: 2
  • cuda_core/cuda/core/_memoryview.pyx
  • cuda_core/tests/test_utils_dlpack.py

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

if ml_dtypes is None:
pytest.skip("ml_dtypes is not installed")
src = np.zeros((2, 3), dtype=np.uint16)
view = StridedMemoryView.from_any_interface(src, stream_ptr=-1).view(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Major · Stability & Availability · Quick win

important: Establish a CUDA context for this test.

.view() queries Buffer.device_id, which calls cuPointerGetAttributes in cuda/core/_memory/_buffer.pyx. Without init_cuda, the test can depend on a context left by another test. (raw.githubusercontent.com)

Request init_cuda. As per coding guidelines: “Request init_cuda for any test that calls into the driver.”

Source: Coding guidelines

_smv_dtype_numpy_to_dlpack dispatched on numpy dtype kind, but
ml_dtypes.bfloat16 has kind "V", so bf16 views fell through to the
unsupported-dtype BufferError and could not be re-exported, even though
the DLPack import path already accepts kDLBfloat and _tensor_map.pyx
maps bfloat16. Detect bfloat16 up front and export it as kDLBfloat
(16 bits).

The new round-trip test goes through StridedMemoryView.from_dlpack so
it cannot skip on stacks where NumPy's own DLPack support for extended
dtypes is unavailable, unlike the existing parametrized round-trip
test's NumPy-consumer probe.

Signed-off-by: Sainath-Reddy7 <180950590+Sainath-Reddy7@users.noreply.github.com>
@Sainath-Reddy7
Sainath-Reddy7 force-pushed the gh-3337-bf16-dlpack-export branch from eb3fdb0 to 3d46b39 Compare November 4, 2026 08:25
@Sainath-Reddy7

Copy link
Copy Markdown
Author

Addressed: test_dlpack_export_bfloat16_roundtrip now requests init_cuda, since .view() reaches the driver via cuPointerGetAttributes — thanks for catching that this also matches the other driver-touching tests in this file.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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
Commits

Reviewing files that changed from the base of the PR and between eb3fdb0 and 3d46b39.

Files selected for processing: 1
  • cuda_core/tests/test_utils_dlpack.py

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

Comment on lines +82 to +83
if ml_dtypes is None:
pytest.skip("ml_dtypes is not installed")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor · Functional Correctness · Quick win

important: Import ml_dtypes directly.

ml-dtypes is declared in both CUDA test dependency groups. (raw.githubusercontent.com) This skip hides a broken test environment and can leave the new export path untested. Remove the skip and use a direct import.

As per coding guidelines: “If the target is listed, import it directly.”

Source: Coding guidelines

This branch has not been deployed

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

Labels

cuda.core Everything related to the cuda.core module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StridedMemoryView.__dlpack__ raises BufferError for bfloat16 views

1 participant