Skip to content

[Python] Fix mx.array DLPack dispatch#3476

Open
apstenku123 wants to merge 1 commit intoml-explore:mainfrom
apstenku123:cppmega/from-dlpack-metal-consumer
Open

[Python] Fix mx.array DLPack dispatch#3476
apstenku123 wants to merge 1 commit intoml-explore:mainfrom
apstenku123:cppmega/from-dlpack-metal-consumer

Conversation

@apstenku123
Copy link
Copy Markdown

@apstenku123 apstenku123 commented May 4, 2026

Fixes the existing mx.array(...) constructor path for DLPack producers/capsules instead of adding a new public workaround API.\n\nmx.array(obj) now detects raw DLPack PyCapsules and objects exposing dlpack before falling through to the generic accessor conversion path, so DLPack producers do not hit to_array_with_accessor / bad_cast. mlx_array remains higher precedence.\n\nThe shared consumer handles kDLCPU by copying into a fresh MLX allocation and kDLMetal by wrapping a shared MTLBuffer without copying. Unsupported devices, non-row-contiguous strides, non-zero byte offsets, invalid shapes/dtypes, and already-consumed capsules are rejected with explicit errors. Rejected capsules are left unconsumed.\n\nTests cover mx.array(...) from a DLPack producer, raw capsules, dtype override/default behavior, mlx_array precedence, used capsule rejection, and preserving rejected capsules.

Copilot AI review requested due to automatic review settings May 4, 2026 00:14

This comment was marked as low quality.

apstenku123 added a commit to DatasunriseOU/cppmega_mlx that referenced this pull request May 4, 2026
Files documenting the actual PRs we just opened upstream:

- PR #1: ml-explore/mlx#3476 — from_dlpack Metal-aware consumer (against main, clean)
- PR #2: apache/tvm#19504 — TVM_METAL_STORAGE_MODE env opt-in (against main, clean)
- PR #3: tile-ai/tilelang#2139 — mixed-dtype T.gemm via scalar fallback (stacks on PR #2130)
- PR #4: tile-ai/tilelang#2140 — FP8-input T.gemm scalar fallback routing (stacks on PR #2130)
- PR #5: tile-ai/tilelang#2141 — T.Pipelined num_stages>1 3D buffer fix (stacks on PR #2130)
- PR #6: tile-ai/tilelang#2142 — T.fp8_scaled_matmul DSL intrinsic (stacks on PR #2130)

Deferred (split into companion PRs needed): tilelang_metal_fp8 and
tilelang_metal_fp8_vector each touch both tilelang supermodule and the
TileLang/tvm vendored submodule. These need 2 PRs each — one to
tile-ai/tilelang, one to TileLang/tvm — separate filing round.

PRs #3-#6 are independent of each other; each branches directly from
jorgecurious/tilelang:metal-gemm-upstream-rebase HEAD 971c17b, so they
can be reviewed in any order. They DO depend on the upstream 4-PR Apple
Metal landing chain (#1869, #2118, #2121, #2130) merging first; if any
of those land separately, ours can be retargeted at main.
Copy link
Copy Markdown
Collaborator

@zcbenz zcbenz left a comment

Choose a reason for hiding this comment

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

mx.array(tvm_metal_tensor) falls into create_array → to_array_with_accessor and fails with bad_cast.

This would be a bug and should be fixed instead of introducing a new API that does the same thing. The constructor of mx.array is supposed to take dlpack.

@apstenku123 apstenku123 force-pushed the cppmega/from-dlpack-metal-consumer branch from 223f948 to 3c6e454 Compare May 5, 2026 21:46
@apstenku123 apstenku123 changed the title [Python] Add mx.from_dlpack(obj) Metal-aware consumer [Python] Fix mx.array DLPack dispatch May 5, 2026
Comment thread python/src/convert.cpp

const bool has_mlx_array = nb::hasattr(v, "__mlx_array__");
const bool is_dlpack =
PyCapsule_CheckExact(v.ptr()) || nb::hasattr(v, "__dlpack__");
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.

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.

3 participants