Add runnable distributed examples - #4219
Merged
Merged
Conversation
erwinzhang7
force-pushed
the
distributed-examples
branch
from
August 13, 2026 02:02
d196aad to
4b8f94e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2930. The docs added in #2973 explain data and tensor parallelism, but
examples/pythonstill has nothing you can launch, so this adds two scripts that run as they are:
Both work on any number of ranks including one, and both end on a number you can check rather
than on output you have to take on trust.
distributed_data_parallel.pytrains a small MLP with SGD. The dataset is drawn from fixedkeys so every rank starts from the same data, then each rank keeps its own slice and drops the
rest. Averaging the gradients of equal slices gives the gradient of the whole dataset, so the
run is equivalent to single process training and the final loss does not move with the number of
ranks:
The sixth decimal moves because the additions happen in a different order, which the docstring
says.
distributed_tensor_parallel.pyshards an MLP across ranks,AllToShardedLinearfor the upprojection and
ShardedToAllLinearfor the down projection, one all reduce per block. It runsthe full model first and prints the largest difference against the sharded one:
0.000e+00at 1and 2 ranks,
2.086e-07at 4, which is float32 reassociation from summing four partial results.Both scripts call
mx.evalon every rank before the rank 0 print. Leaving that to.item()inside the rank check means only rank 0 forces the collective and the others exit while it
waits, which surfaces as
[ring] connection to a peer was lostand does not point anywhere nearthe cause. There is a comment saying so in both files, since it is an easy thing to reproduce by
accident.
There is no fully sharded example because
RingGroup::sum_scatterthrows, sonn.fully_shardcannot run on the ring backend and
mlx.launchuses ring locally.Verified
macOS 26.6, M5 Max, ring backend via
mlx.launchat 1, 2 and 4 ranks.