Skip to content

Add DeviceType to Array2D type annotation - #23

Merged
horizon-blue merged 4 commits into
masterfrom
xiaoyan/array2d-with-device-type
Nov 25, 2025
Merged

Add DeviceType to Array2D type annotation#23
horizon-blue merged 4 commits into
masterfrom
xiaoyan/array2d-with-device-type

Conversation

@horizon-blue

@horizon-blue horizon-blue commented Nov 25, 2025

Copy link
Copy Markdown
Contributor

Recreating #21 because I accidentally closes it and GitHub doesn't let me reopen the PR..


(Closes MET-14)

Summary of Changes

Similar to raw pointers, our current Array2D type does not record where the underlying data comes from, as the memory accessing pattern are roughly the same on CPU & GPU. However, mentally tacking the location of the memory can be error-prone. In addition, many other Python array/tensor libraries store the device type explicitly, and we won't be able easily convert to them without knowing where our memory is.

As such, I'm adding a DeviceType template parameter to our Array2D to keep track of the location of the memory. With this change, we are finally able to take CPU/GPU buffers from Python side and return them correctly without running into segfault.

Test Plans

You can find examples of creating Array2D from CPU/GPU memory buffers with numpy and JAX ih the included test_utils.py.

As always, to run all the tests:

pixi run test

@linear

linear Bot commented Nov 25, 2025

Copy link
Copy Markdown
MET-14 Copy-free data movement to/from Python

and ideally, we should have something that works for both device & host.

One idea to achieve this is to use DLPack which has been widely adopted by ML & scientific computing libraries.

@horizon-blue
horizon-blue marked this pull request as ready for review November 25, 2025 20:43
@horizon-blue

Copy link
Copy Markdown
Contributor Author

Update: rename DeviceType::CPU/DeviceType::GPU to MemoryLocation::HOST/MemoryLocation::DEVICE based on @mugamma's suggestion.

@horizon-blue

Copy link
Copy Markdown
Contributor Author

I'm going to merge this PR now because the original one has been approved

@horizon-blue
horizon-blue merged commit 2b87ea5 into master Nov 25, 2025
1 check passed
@horizon-blue
horizon-blue deleted the xiaoyan/array2d-with-device-type branch November 25, 2025 21:14
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