Skip to content

feat(mtsrust): ArcArray for data [dlpk]#1052

Merged
Luthaf merged 4 commits intometatensor:dlpackfrom
HaoZeke:toArcArray
Feb 16, 2026
Merged

feat(mtsrust): ArcArray for data [dlpk]#1052
Luthaf merged 4 commits intometatensor:dlpackfrom
HaoZeke:toArcArray

Conversation

@HaoZeke
Copy link
Member

@HaoZeke HaoZeke commented Feb 6, 2026

Closes #1043. Needs an upstream release of dlpk, i.e. metatensor/dlpk#20

Also of course, the tmp commit needs to be removed.

cc @Luthaf would you prefer handling the whole of #1043 here? i.e. generalizing types instead of being stuck at f64?

Precursor to #1025 and therefore #1045.

Contributor (creator of pull-request) checklist

  • Tests updated (for new features and bugfixes)?
  • Documentation updated (for new features)?
  • Issue referenced (for PRs that solve an issue)?

Reviewer checklist

  • CHANGELOG updated with public API or any other important changes?

@HaoZeke HaoZeke requested a review from Luthaf February 6, 2026 09:02
@HaoZeke HaoZeke marked this pull request as draft February 6, 2026 09:06
@HaoZeke HaoZeke force-pushed the toArcArray branch 2 times, most recently from 9c2e23b to 2d39278 Compare February 6, 2026 09:29
@HaoZeke HaoZeke marked this pull request as ready for review February 6, 2026 09:29
@HaoZeke HaoZeke force-pushed the toArcArray branch 2 times, most recently from aa9ea9e to 33991bb Compare February 7, 2026 11:42
@HaoZeke HaoZeke requested a review from Luthaf February 7, 2026 11:42
@HaoZeke HaoZeke force-pushed the toArcArray branch 3 times, most recently from 7d80761 to d1ca567 Compare February 12, 2026 12:57
chore(mtsrust): apply arcarray consistently
............ cuz into_shared() may copy, into() may not, and
ArcArray::() is even more explicit
Copy link
Member

@Luthaf Luthaf left a comment

Choose a reason for hiding this comment

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

Looks good, just some small questions and suggestions!

Comment on lines +306 to +307
if TypeId::of::<T>() != TypeId::of::<f64>() {
panic!("Array::data() is only supported for f64 arrays");
Copy link
Member

Choose a reason for hiding this comment

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

this is fine, but we'll need to remove this function before 0.2 anyway, everyone should go through the dlpack version

Copy link
Member Author

Choose a reason for hiding this comment

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

yup I was thinking of removing it in #1045

let mut array = std::mem::take(self);
array = array.to_shape(shape).expect("invalid shape").to_owned();
std::mem::swap(self, &mut array);
*self = self.to_shape(shape).expect("invalid shape").to_shared();
Copy link
Member

Choose a reason for hiding this comment

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

I guess this would invalidate any pointers held through the as_dlpack function?

Copy link
Member Author

@HaoZeke HaoZeke Feb 16, 2026

Choose a reason for hiding this comment

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

Yes, with the current reshape implementation (to_shape().to_shared()), it would since to_shape() returns a CowArray view, and .to_shared() on a view calls to_owned().into_shared(), which allocates a new buffer. The ArcArray and any outstanding DLPack tensor would then point to different data, despite flags=0 (shared).

The pointer doesn't dangle (Arc keeps the old buffer alive in the DLPack ManagerContext), but the producer and consumer silently diverge.

};
let max_version = DLPackVersion { major: 1, minor: 1 };
let device = DLDevice::cpu();
let max_version = DLPackVersion { major: 1, minor: 9 };
Copy link
Member

Choose a reason for hiding this comment

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

small comment here about why this is minor: 9?

Copy link
Member Author

Choose a reason for hiding this comment

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

it needs to be some arbitrarily large thing, so probably better to take it from the library and increment it :D

Copy link
Member Author

Choose a reason for hiding this comment

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

it needs to be some arbitrarily large thing, so probably better to take it from the library and increment it :D but I'm inclined to push it to #1045

Copy link
Member

Choose a reason for hiding this comment

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

Sure, I'm just asking to have a comment explaining this so next readers are not confused

Copy link
Member Author

Choose a reason for hiding this comment

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

ah I fixed it in the next PR to take current + 1

@Luthaf Luthaf merged commit c654ca5 into metatensor:dlpack Feb 16, 2026
80 checks passed
@HaoZeke HaoZeke deleted the toArcArray branch February 16, 2026 17:12
HaoZeke added a commit that referenced this pull request Feb 16, 2026
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.

2 participants