Skip to content

Distributed: accept a null stream, and bind clear_backends() and init with an all-gather factory #134

Open

Description

@ronaldmannak

This is a follow up to PR 482 on MLX-Swift to expose missing distributed functions. Happy to open PRs for the parts that can't be auto-generated, but I'm not sure which ones those are.

1. Accept a null stream in the distributed ops

Python's stream=None lets the group pick its communication stream: the GPU for NCCL, the CPU otherwise. mlx-c can't express that, and communication_stream() is internal, so a binding has to guess. Proposal: map a null stream to StreamOrDevice{} in all eight mlx_distributed_* ops.

int mlx_distributed_all_sum(
    mlx_array* res,
    const mlx_array x,
    const mlx_distributed_group group /* may be null */,
    const mlx_stream s /* may be null */);
s.ctx ? StreamOrDevice{mlx_stream_get_(s)} : StreamOrDevice{}

Doing it in the StreamOrDevice rule for every op would work too.

2. Bind clear_backends()

int mlx_distributed_clear_backends(void);

A non-strict init caches its size-one fallback, so a later strict init of that backend returns it instead of retrying. Python also calls this at exit, which matters once 3 holds a binding's callback.

3. Bind init with an all-gather factory

For mx.distributed.init(backend="jaccl", all_gather_factory=...), flattened into one callback with payload and dtor as in mlx_closure_new_func_payload:

int mlx_distributed_init_with_all_gather(
    mlx_distributed_group* res,
    bool strict,
    const char* bk,
    int (*all_gather)(
        int rank, int size, const char* src, char* dst, size_t n_bytes, void* payload),
    void* payload,
    void (*dtor)(void*));

A non-zero return fails the init, and dtor runs exactly once, including on failure.

4. Status-returning group_rank and group_size (minor)

Both return 0 on error, which is also a valid rank. Either in place, as #110 did for init:

int mlx_distributed_group_rank(int* res, mlx_distributed_group group);
int mlx_distributed_group_size(int* res, mlx_distributed_group group);

or as new mlx_distributed_group_get_rank and _get_size, if you'd rather not break them.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions