Skip to main content

Shard Large Language Models

Project description

🧠 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

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

Installation

  1. 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
    
  2. Navigate to the project directory:

    cd Shard-Any-LLMs
    

First Step: Set the environment variable:

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

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

To shard a HuggingFace model, use the following command:

python 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 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

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/"
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! 🎉

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

shard_llms-0.2.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shard_llms-0.2.1-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file shard_llms-0.2.1.tar.gz.

File metadata

  • Download URL: shard_llms-0.2.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for shard_llms-0.2.1.tar.gz
Algorithm Hash digest
SHA256 0732af600ba657c80273d251983b71c5c746ae0df9270349f31a512d19e25e20
MD5 a23ad7f418fba8f9062fde8bec71a56e
BLAKE2b-256 1f2fc5dc3f175b08d46d26477c0bac43866106b92ef3c2998548d0878df3139b

See more details on using hashes here.

File details

Details for the file shard_llms-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: shard_llms-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for shard_llms-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6e51917e8722aba60d9be009de4eec2d95cbc1b3464ff894d529af5007f25528
MD5 b052b789c74a20ce5dd3c3c7c613b1bd
BLAKE2b-256 b83640bef4db4e56243a6afe0d2fa7153fd70d1cdb62f7a18e3c41e261baa404

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page