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

Exposed array flags #50

Merged
merged 2 commits into from
Jan 24, 2025
Merged

Exposed array flags #50

merged 2 commits into from
Jan 24, 2025

Conversation

stemann
Copy link
Contributor

@stemann stemann commented Dec 19, 2024

Array flags could be convenient to be able to check - in particular when using the C interface from a system which typically uses column-major ordering such as Julia.

@awni
Copy link
Member

awni commented Dec 23, 2024

Array flags in MLX are kind of like strides. They are meaningless unless the array is evaluated. That's one reason we don't expose strides and flags in e.g. Python (because it's pretty easy to use them incorrectly).

What do you plan to use them for? Usually it's an error to use the flags directly.. but it would be helpful to understand if you have a reasonable use case in which case we can consider exposing them.

We could for example expose them but raise / throw if you try to access flags on an unevaluated array.

@stemann
Copy link
Contributor Author

stemann commented Jan 7, 2025

The use case here was to check/ensure that the array was properly represented/stored - because it's likewise pretty easy to get confused by row-major and column-major representations :-)

E.g. I'm currently reversing the dimension order, returned by mlx_array_ndim, when reporting the size of a (row-major order) MLX array when reporting the size to Julia (which usually expects a column-major ordering):

function Base.size(array::MLXArray)
    return Tuple(
        Int.(
            reverse(
                unsafe_wrap(
                    Vector{Cint},
                    Wrapper.mlx_array_shape(array.mlx_array),
                    Wrapper.mlx_array_ndim(array.mlx_array),
                ),
            )
        ),
    )
end

https://github.com/stemann/MLX.jl/blob/master/src/array.jl#L65-L77

Exposing the flags would also allow replication of the C++ tests that checks the flags on the Julia-side.

It would be fine to expose the flags and throw an exception for non-available arrays.

@andresy andresy force-pushed the feature/array_flags branch from 5ba8806 to 6d17545 Compare January 24, 2025 23:05
@andresy andresy merged commit c53d104 into ml-explore:main Jan 24, 2025
@stemann stemann deleted the feature/array_flags branch January 26, 2025 09:11
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