Skip to content

[FEA] Add support for mdspan - #1227

Open
MashrafeeAryan wants to merge 4 commits into
NVIDIA:mainfrom
MashrafeeAryan:maryan/mdspan-support
Open

[FEA] Add support for mdspan#1227
MashrafeeAryan wants to merge 4 commits into
NVIDIA:mainfrom
MashrafeeAryan:maryan/mdspan-support

Conversation

@MashrafeeAryan

Copy link
Copy Markdown
Contributor

What this does

Adds two features:

  1. Create a MatX tensor from an mdspan

make_tensor can now create a MatX tensor from a cuda::std::mdspan

  1. Ability to use tensor[i, j] with C++23

When the compiler supports C++23 multidimensional indexing, tensors can use:

tensor[2, 3] = value;
auto value = tensor[2, 3];

Testing

Added tests for mdspan and operator[]

Built and ran the tensor tests with:

cmake --build build \
  --target test_00_tensor_MakeTensorTests \
  --parallel 2

./build/test/test_00_tensor_MakeTensorTests \
  --gtest_filter='MakeTensorTests.*Mdspan*'

./build/test/test_00_tensor_MakeTensorTests

Testing was done using:

  • Google Colab
  • Tesla T4 GPU
  • CUDA 12.8.93
  • NVIDIA driver 580.82.07
  • GCC 11.4.0
  • CMake 3.31.10
  • C++20

The mdspan changes and the full MakeTensorTests suite passed with C++20

The new operator[] feature requires C++23 multidimensional subscript support,
which is not available with CUDA 12.8. I could not update the CUDA environment
due to administrative permission restrictions

I would really appreciate it if the C++23 tests could be run in a compatible
environment during review.

Documentation

Updated the documentation with:

  • How to create a MatX tensor from an mdspan
  • The new C++23 tensor[i, j] syntax
  • Continued support for tensor(i, j) in C++20 and C++23

Related issue

Closes #582

MashrafeeAryan and others added 3 commits August 14, 2026 00:27
Adds the new tensor[i, j] syntax using MatX's
existing indexing code. The new syntax is only
included when supported by the compiler with C++23.
It does not break the C++20 tensor(i, j) syntax.
Allow make_tensor to create a MatX tensor from a
cuda::std::mdspan without copying or owning its
data. Keep the same dimensions and strides and
add tests for different layouts, sizes, and shared
memory.
Explain how to create a MatX tensor from an mdspan
and show the supported memory layouts. Show some
examples of the new tensor[i, j] syntax available
with C++23
@copy-pr-bot

copy-pr-bot Bot commented Aug 17, 2026

Copy link
Copy Markdown

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.

@greptile-apps

greptile-apps Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds zero-copy construction of MatX tensors from supported cuda::std::mdspan layouts and C++23 multidimensional subscript syntax.

  • Preserves mdspan extents, strides, data pointer, and non-owning lifetime semantics.
  • Restricts conversion to standard layouts and default accessors.
  • Adds host/device indexing support, documentation, and focused tensor tests.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
include/matx/core/make_tensor.h Adds a constrained mdspan conversion that creates a non-owning tensor from the original pointer, extents, and strides while checking representable ranges.
include/matx/core/tensor_impl.h Adds const and mutable C++23 multidimensional operator[] overloads that delegate to existing tensor indexing.
test/00_tensor/MakeTensorTests.cu Covers supported mdspan layouts, dynamic and static extents, custom strides, non-owning storage, range rejection, and host/device multidimensional subscripting.
docs_input/api/creation/tensors/make.rst Documents zero-copy mdspan tensor creation, supported layouts, and borrowed-memory lifetime requirements.
docs_input/quickstart.rst Documents C++23 multidimensional subscript syntax while retaining C++20-compatible operator() guidance.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  M["cuda::std::mdspan"] --> V["Validate layout, accessor, extents, and strides"]
  V --> D["Create MatX shape/stride descriptor"]
  D --> T["Non-owning MatX tensor"]
  M -. "shared data_handle()" .-> T
Loading

Reviews (2): Last reviewed commit: "Only allow supported mdspan layouts" | Re-trigger Greptile

Comment thread include/matx/core/make_tensor.h Outdated
@MashrafeeAryan
MashrafeeAryan marked this pull request as draft August 17, 2026 05:36
Limit make_tensor to layout_right, layout_left,
and layout_stride to prevent incorrect results
from unsupported custom layouts
@MashrafeeAryan
MashrafeeAryan marked this pull request as ready for review August 17, 2026 07:01
@MashrafeeAryan

Copy link
Copy Markdown
Contributor Author

Hi @cliffburdick, I addressed the issue Greptile pointed out. Please let me know if I need to change anything else. Thank you for your time!

.. doxygenfunction:: make_tensor( TensorType &tensor, const index_t (&shape)[TensorType::Rank()], Allocator&& alloc)
.. doxygenfunction:: make_tensor( TensorType &tensor, ShapeType &&shape, Allocator&& alloc)

cuda::std::mdspan Support

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think we should support both cuda::std::mdspan for those on older c++ versions, then an include guard for the same ones for std::mdspan. Maybe there's a type trait you can use to simplify the code.

std::is_same_v<LayoutPolicy, cuda::std::layout_right> ||
std::is_same_v<LayoutPolicy, cuda::std::layout_left> ||
std::is_same_v<LayoutPolicy, cuda::std::layout_stride>)
auto make_tensor(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think if we want to support std::mdspan also we should add a type trait that's true for cuda::std::mdspan and std::mdspan. Have that be the only requires clause, then do separate checks inside? It could be a bit ugly but at least it keeps it in one function

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sounds like a good idea. I am working on it!

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.

[FEA] Add support for mdspan

2 participants