Skip to content

add tvm-ffi metal - #506

Open
ved1beta wants to merge 1 commit into
huggingface:mainfrom
ved1beta:tvf-ffi-metal
Open

add tvm-ffi metal #506
ved1beta wants to merge 1 commit into
huggingface:mainfrom
ved1beta:tvf-ffi-metal

Conversation

@ved1beta

@ved1beta ved1beta commented Apr 26, 2026

Copy link
Copy Markdown

fixes #352

Summary

  • Adds noarch variants for the tvm-ffi framework (tvm-ffi-metal, tvm-ffi-cuda, tvm-ffi-cpu, tvm-ffi-rocm, tvm-ffi-xpu, tvm-ffi-universal)
  • Introduces TvmFfiNoarch mirroring TorchNoarch;
  • Extends parse_variant and BUILD_VARIANT_REGEX
  • Updates _sort_variants so within noarch, Torch is preferred over tvm-ffi

tests

Torch beats tvm-ffi when both are present
noarch fallback when no arch variant matches
arch variant beats noarch on the actual hardware

@danieldk

danieldk commented Apr 26, 2026

Copy link
Copy Markdown
Member

We already have a branch for tvm-ffi and there is also support for kernel-builder in it. However Metal support will require upstream tvm-ffi changes, because there is currently no way for tvm-ffi to pass MTLBuffers (only pointers to the raw data), which is needed for other Metal APIs (as well as no way to get other compute context).

For this reason we are postponing tvm-ffi Metal support until these issue have been fixed upstream.

@ved1beta

Copy link
Copy Markdown
Author

ahh thanks for the response , will try to put a pr upstream 🫡

pytorchmergebot pushed a commit to pytorch/pytorch that referenced this pull request Aug 11, 2026
## Summary

`toDLPackNonOwning` writes `src.data_ptr()` into `DLTensor.data` and sets
`byte_offset = 0`.
For MPS tensors this is wrong: PyTorch's MPS allocator
encodes `id<MTLBuffer>` directly into `c10::DataPtr.data`
in `aten/src/ATen/mps/MPSAllocator.mm:768-770`,
so for any sliced/viewed
MPS tensor `data_ptr()` returns `id<MTLBuffer> + storage_offset*elemsize` —
pointer arithmetic on an Objective-C object pointer, which is not a valid
buffer handle.
The owning path already special-cases this
correctly. The non-owning path was missing the same logic.

##fix
mirror the owning path's MPS handling: write the storage base into
DLTensor.data and the element offset into byte_offset

##Test
Adds test_dlpack_exchange_api_mps_sliced in test/test_dlpack.py,
which exercises the C exchange API on a sliced MPS tensor and asserts

related upstreams
apache/tvm-ffi#578
huggingface/kernels#506
thanks

Pull Request resolved: #182924
Approved by: https://github.com/Skylion007

Co-authored-by: Aaron Gokaslan <aaronGokaslan@gmail.com>
ankitbhrdwj pushed a commit to ankitbhrdwj/pytorch that referenced this pull request Aug 12, 2026
…2924)

## Summary

`toDLPackNonOwning` writes `src.data_ptr()` into `DLTensor.data` and sets
`byte_offset = 0`.
For MPS tensors this is wrong: PyTorch's MPS allocator
encodes `id<MTLBuffer>` directly into `c10::DataPtr.data`
in `aten/src/ATen/mps/MPSAllocator.mm:768-770`,
so for any sliced/viewed
MPS tensor `data_ptr()` returns `id<MTLBuffer> + storage_offset*elemsize` —
pointer arithmetic on an Objective-C object pointer, which is not a valid
buffer handle.
The owning path already special-cases this
correctly. The non-owning path was missing the same logic.

##fix
mirror the owning path's MPS handling: write the storage base into
DLTensor.data and the element offset into byte_offset

##Test
Adds test_dlpack_exchange_api_mps_sliced in test/test_dlpack.py,
which exercises the C exchange API on a sliced MPS tensor and asserts

related upstreams
apache/tvm-ffi#578
huggingface/kernels#506
thanks

Pull Request resolved: pytorch#182924
Approved by: https://github.com/Skylion007

Co-authored-by: Aaron Gokaslan <aaronGokaslan@gmail.com>
gavinwang269 pushed a commit to gavinwang269/pytorch that referenced this pull request Aug 13, 2026
…2924)

## Summary

`toDLPackNonOwning` writes `src.data_ptr()` into `DLTensor.data` and sets
`byte_offset = 0`.
For MPS tensors this is wrong: PyTorch's MPS allocator
encodes `id<MTLBuffer>` directly into `c10::DataPtr.data`
in `aten/src/ATen/mps/MPSAllocator.mm:768-770`,
so for any sliced/viewed
MPS tensor `data_ptr()` returns `id<MTLBuffer> + storage_offset*elemsize` —
pointer arithmetic on an Objective-C object pointer, which is not a valid
buffer handle.
The owning path already special-cases this
correctly. The non-owning path was missing the same logic.

##fix
mirror the owning path's MPS handling: write the storage base into
DLTensor.data and the element offset into byte_offset

##Test
Adds test_dlpack_exchange_api_mps_sliced in test/test_dlpack.py,
which exercises the C exchange API on a sliced MPS tensor and asserts

related upstreams
apache/tvm-ffi#578
huggingface/kernels#506
thanks

Pull Request resolved: pytorch#182924
Approved by: https://github.com/Skylion007

Co-authored-by: Aaron Gokaslan <aaronGokaslan@gmail.com>
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.

tvm-ffi Metal support

2 participants