Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusion about calling asArray on MLXArray returned from indexing #83

Open
minghuaw opened this issue May 4, 2024 · 2 comments · May be fixed by #96
Open

Confusion about calling asArray on MLXArray returned from indexing #83

minghuaw opened this issue May 4, 2024 · 2 comments · May be fixed by #96
Assignees
Labels
bug Something isn't working

Comments

@minghuaw
Copy link

minghuaw commented May 4, 2024

This may just be my confusion but as shown in the example below, I was expecting that calling asArray on the MLXArray returned from indexing would give [1, 2, 4, 5] but ended up getting [1,2,3,4]. This seems to be inconsistent with the python api where calling tolist() would give [[1, 2], [4, 5]].

The documentation doesn't seem to mention this behavior either

Return the contents as a single contiguous 1d Swift.Array.

let a = MLXArray(0..<9, [3,3])

let s = a[0..<2, 1..<3]
XCTAssertEqual(s.ndim, 2)
XCTAssertEqual(s.shape, [2,2])
assertEqual(s, MLXArray([1, 2, 4, 5], [2, 2]))

let s_arr = s.asArray(Int32.self)
XCTAssertEqual(s_arr, [1, 2, 3, 4])
@minghuaw minghuaw changed the title asArray on MLXArray returned from indexing Confusion about calling asArray on MLXArray returned from indexing May 4, 2024
@davidkoski
Copy link
Collaborator

I will take a look -- I suspect the readout (asArray) is expecting contiguous memory but not getting it. asData may have the same issue.

@dcvz
Copy link

dcvz commented May 5, 2024

I will take a look -- I suspect the readout (asArray) is expecting contiguous memory but not getting it. asData may have the same issue.

tolist() in python checks the stride: https://github.com/ml-explore/mlx/blob/21623156a32910b9db7c913f91612dcde0282caf/python/src/array.cpp#L34

@davidkoski davidkoski added the bug Something isn't working label May 8, 2024
@davidkoski davidkoski self-assigned this May 8, 2024
davidkoski added a commit to davidkoski/mlx-swift that referenced this issue May 29, 2024
- fixes ml-explore#83
- mlx::core::array can have non-contiguous backing
- handle those cases and simplify the readout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants