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

Revamp MLX C API #38

Open
wants to merge 60 commits into
base: main
Choose a base branch
from
Open

Revamp MLX C API #38

wants to merge 60 commits into from

Conversation

andresy
Copy link
Member

@andresy andresy commented Oct 9, 2024

This introduces the new API of MLX C

  • Less overheads on objects (no vtable, no refcount)
  • Return values are handled via (pointer) arguments
  • Better error management: C++ exceptions are now mostly captured, status is returned as an int value for most ops
  • Feels more like C: a number of C++ object wrapper disappeared (tuples, variants...)


.. code-block:: c

mlx_stream stream = mlx_gpu_stream();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this need to be destroyed? Maybe not since it isn't new?

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, I think it needs to be freed -- the implementation of this is:

extern "C" mlx_stream mlx_gpu_stream() {
  try {
    return mlx_stream_new_(

/**
* Get stream description.
*/
mlx_string mlx_stream_tostring(mlx_device stream);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
mlx_string mlx_stream_tostring(mlx_device stream);
mlx_string mlx_stream_tostring(mlx_stream stream);

davidkoski added a commit to davidkoski/mlx-swift that referenced this pull request Oct 11, 2024
int mlx_fast_metal_kernel_set_input_names(
mlx_fast_metal_kernel cls,
int num,
...);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Per offline discussion swift can't call variadic C functions

const mlx_array a,
const mlx_stream s) {
try {
std::tie(mlx_array_get_(*res_0), mlx_array_get_(*res_1)) =
Copy link
Collaborator

Choose a reason for hiding this comment

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

I get:

MLX error: expected a non-empty mlx_array at mlx-swift2/Source/Cmlx/include/mlx/c/linalg.cpp:161

from calling like this:

public func qr(_ array: MLXArray, stream: StreamOrDevice = .default) -> (MLXArray, MLXArray) {
    var r0 = mlx_array_new()
    var r1 = mlx_array_new()

    mlx_linalg_qr(&r0, &r1, array.ctx, stream.ctx)

    return (MLXArray(r0), MLXArray(r1))
}

The mlx_array_get() doesn't seem like it is meant to be used on out params.

mlx_fast_metal_kernel cls,
const mlx_vector_array inputs,
const mlx_stream stream,
mlx_vector_array* outputs);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Per offline discussion this function doesn't match the pattern of other functions -- the out parameter is last instead of first.

const mlx_array key,
const mlx_stream s) {
try {
std::tie(mlx_array_get_(*res_0), mlx_array_get_(*res_1)) =
Copy link
Collaborator

Choose a reason for hiding this comment

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

Another case of:

MLX error: expected a non-empty mlx_array at mlx-swift2/Source/Cmlx/include/mlx/c/random.cpp:281

I think mlx_array_get() is not meant to be used on out parameters.

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