A package for MLX model sharding and distributed inference
Project description
MLX Sharding
This project demonstrates how to implement pipeline parallelism for large language models using MLX. It includes tools for sharding a model, serving shards across multiple machines, and generating text using the distributed model. Additionally, it features an OpenAI API-compatible server for easier integration and usage.
Demo Video
To see the distributed inference in action, check out our demo video:
Sharding DeepSeek-Coder-V2-Lite-Instruct Demo
Quick Start
Installation
Install the package using pip:
pip install mlx-sharding
Running the Servers
-
For the shard node:
mlx-sharding-server --model mlx-community/DeepSeek-Coder-V2-Lite-Instruct-4bit-mlx --start-layer 14 --end-layer 27
-
For the primary node:
mlx-sharding-api --model mlx-community/DeepSeek-Coder-V2-Lite-Instruct-4bit-mlx --start-layer 0 --end-layer 14 --llm-shard-addresses <your shard node address>
Replace
<your shard node address>
with the actual address of your shard node (e.g.,localhost:50051
).
Educational Purpose
This repository is designed for educational purposes to illustrate how pipeline parallelism can be implemented in MLX. It provides a basic framework for:
- Sharding a large language model
- Distributing model shards across multiple machines
- Implementing a simple pipeline for text generation
- Serving the model through an OpenAI API-compatible interface
While not optimized for production use, this demo serves as a starting point for understanding and experimenting with pipeline parallelism in machine learning workflows.
Setup and Usage
1. Model Preparation
You have two main options for preparing and using the model:
Option A: Pre-Sharding the Model
If you prefer to pre-shard the model, use sharding_weight.py
:
python sharding_weight.py --model "mlx-community/DeepSeek-Coder-V2-Lite-Instruct-4bit-mlx" --output_dir shard_0 --start_layer 0 --end_layer 14 --total_layers 27
python sharding_weight.py --model "mlx-community/DeepSeek-Coder-V2-Lite-Instruct-4bit-mlx" --output_dir shard_1 --start_layer 14 --end_layer 27 --total_layers 27
# Repeat for additional shards as needed
Option B: Dynamic Sharding
You can let the system dynamically load and shard the weights when starting the server. This option doesn't require pre-sharding.
2. Distribute Shards (If Using Option A)
If you've pre-sharded the model, copy the shard directories to their respective machines. Skip this step for Option B.
3. Start the Servers
Start server instances based on your chosen approach:
For Pre-Sharded Model (Option A)
On each machine with a shard, start a server instance. For example:
python -m shard.main --model mzbac/DeepSeek-Coder-V2-Lite-Instruct-4bit-mlx-shard-1
For Dynamic Sharding (Option B)
Start the server with specific layer ranges:
python -m shard.main --model "mlx-community/DeepSeek-Coder-V2-Lite-Instruct-4bit-mlx" --start-layer 0 --end-layer 14
Note the IP address and port printed by each server.
4. Generate Text
Using the generate script
For a dynamically sharded setup:
python generate.py --model "mlx-community/DeepSeek-Coder-V2-Lite-Instruct-4bit-mlx" --start_layer 0 --end_layer 14 --server_address <remote_ip1>:<port1>,<remote_ip2>:<port2> --prompt "Your prompt here" --max_tokens 512
For a pre-sharded setup:
python generate.py --model mzbac/DeepSeek-Coder-V2-Lite-Instruct-4bit-mlx-shard-0 --server_address <remote_ip1>:<port1>,<remote_ip2>:<port2> --prompt "Your prompt here" --max_tokens 512
Using the OpenAI API-compatible server
-
Start the server:
For dynamic sharding:
python -m shard.openai_api --model "mlx-community/DeepSeek-Coder-V2-Lite-Instruct-4bit-mlx" --llm-shard-addresses localhost:50051,<remote_ip1>:<port1>,<remote_ip2>:<port2> --start-layer 0 --end-layer 14
For pre-sharded model:
python -m shard.openai_api --model mzbac/DeepSeek-Coder-V2-Lite-Instruct-4bit-mlx-shard-0 --llm-shard-addresses localhost:50051,<remote_ip1>:<port1>,<remote_ip2>:<port2>
-
Use the API endpoints:
/v1/completions
: Text completion endpoint/v1/chat/completions
: Chat completion endpoint
Example usage:
curl localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"messages": [{"role": "user", "content": "Say this is a test!"}],
"temperature": 0.7
}'
5. Web User Interface
This project now includes a web-based user interface for easy interaction with the model. To use the UI:
-
Ensure the OpenAI API-compatible server is running (as described in step 4).
-
Navigate to
http://localhost:8080
(or the appropriate host and port if you've configured it differently) in your web browser. -
Use the interface to input prompts, adjust parameters, and view the model's responses.
The UI provides a user-friendly way to interact with the model, making it easier to experiment with different inputs and settings without needing to use command-line tools or write code.
Limitations and Considerations
-
Network Dependency: The performance of this pipeline parallelism implementation is heavily dependent on network speed and latency between machines.
-
Error Handling: The current implementation has basic error handling. In a production environment, you'd want to implement more robust error handling and recovery mechanisms.
-
Security: This demo uses insecure gRPC channels. For any real-world application, implement proper security measures.
-
Shard Configuration: Ensure that when using multiple shards, the layer ranges are set correctly to cover the entire model without overlap.
Extending the System
To extend the system for more shards:
- If pre-sharding, create additional shards using
sharding_weight.py
. - Set up more server instances, one for each new shard.
- In
generate.py
or when using the OpenAI API server, include all shard addresses. - Adjust the layer ranges accordingly when using dynamic sharding.
Requirements
- Python 3.x
- MLX library
- gRPC and related dependencies
- NumPy
- Transformers library
- Sufficient RAM on each machine to load and process its model shard
Acknowledgments
- MLX team for providing the framework
- Exo(https://github.com/exo-explore/exo) that I heavily inspired from for their implementation of pipeline parallelism
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mlx_sharding-0.1.3.tar.gz
.
File metadata
- Download URL: mlx_sharding-0.1.3.tar.gz
- Upload date:
- Size: 21.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | faa204e3e91de9bbe76bf08915b99cf1ed81dece71bd989c83e2f0579194453f |
|
MD5 | ae88d977a682df57c44b66fc172d7afc |
|
BLAKE2b-256 | 7ff150a80873b24e8d03cf00e647f0f9542add6a3769ffda2f651ec0ea885657 |
File details
Details for the file mlx_sharding-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: mlx_sharding-0.1.3-py3-none-any.whl
- Upload date:
- Size: 26.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1183e8a1c399e8cd81e988bea8ec1a09255f8a73ea0c66cda381bc5800a89b3e |
|
MD5 | 715b7b376e5c388dba00e039a70f17ff |
|
BLAKE2b-256 | a6cef48234957c230a0acb00417f3ad835b9fef9d2c54d34c30c6af287534570 |