Skip to main content

🧠 Shard LLMs: Unleashing the Power of Large Language Models

🌟 Introduction

Welcome to the Shard LLMs project! This repository provides tools and techniques for efficiently managing and deploying Large Language Models (LLMs) through sharding. By breaking down these massive models, we can overcome resource constraints and unlock their full potential.

🚀 Why Shard LLMs?

Sharding is a game-changer for working with LLMs. Here's why:

  1. 💾 Memory Optimization: Fit billion-parameter models across multiple GPUs.
  2. ⚡ Speed Boost: Parallel processing for faster training and inference.
  3. 📈 Scalability: Effortlessly scale to larger models and datasets.
  4. 💰 Cost-Effective: Maximize hardware efficiency and reduce training costs.
  5. 🔄 Enhanced Throughput: Process more requests simultaneously.
  6. 🛡️ Fault Tolerance: Improve system resilience with distributed processing.
  7. 🔧 Flexibility: Train large models on limited hardware or scale to massive clusters.
  8. 🌐 Optimized Communication: Reduce overhead between model components.

🛠️ Getting Started

First Step: Set the environment variable:

You need to set up your Hugging Face token as an environment variable to use the Shard-Any-LLMs package with Hugging Face Hub functionality.

1. Obtain Your Hugging Face Token

  1. Go to the Hugging Face website
  2. Log in to your account
  3. Navigate to your profile settings
  4. Find and copy your API token

2. Set the Environment Variable

Depending on your operating system, use one of the following methods to set your HuggingFace token:

For macOS and Linux: Open a terminal and run:

export HF_TOKEN=your_token_here

To make this permanent, add the line to your shell configuration file (e.g., ~/.bashrc, ~/.zshrc):

echo 'export HF_TOKEN=your_token_here' >> ~/.bashrc

Then, restart your terminal or run source ~/.bashrc.

For Windows: In Command Prompt, run:

setx HF_TOKEN your_token_here

🔬 Sharding a Model

There are two ways to use Shard LLMs: via command-line interface or by running the Python script directly.

  1. Run
pip install shard-llms
shard_llms --model_name MODEL-ID --save_dir SAVE_DIRECTORY --max_shard_size SHARD_SIZE --token HF_TOKENS

Example:

shard_llms --model_name meta-llama/Meta-Llama-3.1-8B-Instruct --save_dir ~/sharded_model --max_shard_size 2GB

Since I have already set up the environment variable, I ignored the --token flag.

Method 2: Running the Python Script

Installation

Clone the repository:

# SSH
git clone git@github.com:yzm1205/Shard-Any-LLMs.git

# HTTPS
git clone https://github.com/yzm1205/Shard-Any-LLMs.git
Prerequisites

Ensure you have all the necessary dependencies:

pip install -r requirements.txt

This will install:

  • python-dotenv==1.0.1
  • transformers==4.44.2
  • torch==2.4.1

RUN

You can run the Python script directly:

python src/sharding_model.py \
  --model_name MODEL-ID \
  --save_dir SAVE_DIRECTORY \
  --max_shard_size SHARD_SIZE \
  --token YOUR_HUGGINGFACE_TOKEN

Example:
Let's shard the LLaMA-3.1-8B-Instruct model as an example:

python src/sharding_model.py \
  --model_name meta-llama/Meta-Llama-3.1-8B-Instruct \
  --save_dir ~/sharded_model \
  --max_shard_size 2GB \
  --token YOUR_HUGGINGFACE_TOKEN

Note: When using either method, ensure that you have set up your Hugging Face token as an environment variable (HF_TOKEN) as described in the "First Step" section. Otherwise, replace YOUR_HUGGINGFACE_TOKEN with your actual HuggingFace token.

🔧 Loading a Sharded Model

To use your sharded model, you can load it using HuggingFace's AutoModelForCausalLM and AutoTokenizer:

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

model_dir = "./sharded_model/Meta-Llama-3.1-8B-Instruct/"
#optional: ignore, if HF_TOKEN is set else:
hf_token = "YOUR_HUGGINGFACE_TOKEN"

model = AutoModelForCausalLM.from_pretrained(
    model_dir,
    trust_remote_code=True,
    torch_dtype=torch.float16,
    token=hf_token
)

tokenizer = AutoTokenizer.from_pretrained(
    model_dir,
    trust_remote_code=True,
    token=hf_token
)

💡 Pro Tip: You can also use other methods to load sharded models, such as the pipeline API from Transformers.

🤝 Contributing

We welcome contributions! If you have ideas for improvements or new features, feel free to open an issue or submit a pull request.

🙏 Acknowledgments

  • HuggingFace for their amazing Transformers library
  • The open-source AI community for continuous inspiration and support

Happy sharding! 🎉

Metadata

Release files for shard-llms 0.2.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shard-llms 0.2.13
File Size Uploaded Provenance Actions
shard_llms-0.2.13.tar.gz 5.0 kB Without provenance Details for shard_llms-0.2.13.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for shard-llms 0.2.13
File Interpreter ABI Platform Actions
shard_llms-0.2.13-py3-none-any.whl Python 3 none any Details for shard_llms-0.2.13-py3-none-any.whl

Total release size: 10.6 kB

Release files / shard_llms-0.2.13.tar.gz

Download URL shard_llms-0.2.13.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
c803b35cbae66b9c785ad1c8d468f8240cc15f5d061eb72fd0f295121c8226a3
BLAKE2b-256 checksum
How to use checksums
ae84c9a73f04910a35fa8dee459e70190ddd7f56abf1077dd06444bb9a81b489
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release files / shard_llms-0.2.13-py3-none-any.whl

Download URL shard_llms-0.2.13-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bff5b836e5d3ced137b026a8fe0321a5c9ad0b0e1f3ad3c2d659199fc1b683d2
BLAKE2b-256 checksum
How to use checksums
17303fabe48a92ba5abdeee0419441b8ed005135e8c6655ce1ea01c0c46e47a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Security for shard-llms 0.2.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page