Claude Code for DeepSpeed: Distributed LLM Training — Claude Skills 360 Blog
Blog / AI / Claude Code for DeepSpeed: Distributed LLM Training
AI

Claude Code for DeepSpeed: Distributed LLM Training

Published: September 21, 2027
Read time: 5 min read
By: Claude Skills 360

DeepSpeed trains large models across multiple GPUs with ZeRO memory optimization. pip install deepspeed. Native: model_engine, optimizer, _, _ = deepspeed.initialize(model=model, optimizer=optimizer, config=ds_config). model_engine(inputs).backward(loss). model_engine.step(). ds_config dict or path to ds_config.json. ZeRO-2: {"zero_optimization": {"stage": 2, "allgather_partitions": true, "allgather_bucket_size": 2e8, "overlap_comm": true, "reduce_scatter": true}}. ZeRO-3: {"stage": 3, "offload_optimizer": {"device": "cpu"}, "offload_param": {"device": "cpu"}} — partitions optimizer states, gradients, AND parameters across GPUs. With HuggingFace Trainer: TrainingArguments(deepspeed="ds_config.json", bf16=True, per_device_train_batch_size=4) — Trainer handles deepspeed.initialize internally. Accelerate: from accelerate import Accelerator, plugin = DeepSpeedPlugin(zero_stage=2, gradient_accumulation_steps=4), accelerator = Accelerator(deepspeed_plugin=plugin, mixed_precision="bf16"), model, optimizer, dataloader = accelerator.prepare(model, optimizer, dataloader), accelerator.backward(loss). Mixed precision: {"fp16": {"enabled": true, "loss_scale": 0, "loss_scale_window": 1000}} or {"bf16": {"enabled": true}}. Gradient checkpointing: model.gradient_checkpointing_enable() before deepspeed.initialize. Launch: deepspeed --num_gpus 8 train.py --deepspeed ds_config.json or deepspeed --hostfile hostfile --num_nodes 2 --num_gpus 8 train.py. Pipeline parallelism: from deepspeed.pipe import PipelineModule, layers = [LayerSpec(EmbedLayer), LayerSpec(TransformerLayer, ...), LayerSpec(HeadLayer)], model = PipelineModule(layers=layers, num_stages=4). engine.train_batch() handles micro-batching across pipeline stages. Checkpoint: engine.save_checkpoint(save_dir, tag), engine.load_checkpoint(load_dir, tag). Claude Code generates DeepSpeed configs, ZeRO training loops, HuggingFace Trainer integration, Accelerate pipelines, and multi-node launch scripts.

CLAUDE.md for DeepSpeed

## DeepSpeed Stack
- Version: deepspeed >= 0.14, transformers >= 4.40, accelerate >= 0.30
- Native: deepspeed.initialize(model, optimizer, config=ds_config) → engine, optimizer, _, _
- HF Trainer: TrainingArguments(deepspeed="ds_config.json", bf16=True) — no manual init needed
- Accelerate: DeepSpeedPlugin(zero_stage=2) → Accelerator(deepspeed_plugin) → accelerator.prepare()
- ZeRO stages: 0=none, 1=optimizer states, 2=+gradients, 3=+parameters
- Offload: zero_optimization.offload_optimizer.device = "cpu" | "nvme"
- Launch: deepspeed --num_gpus N train.py --deepspeed ds_config.json

ZeRO Training Script

# train/deepspeed_train.py — DeepSpeed ZeRO distributed training
from __future__ import annotations
import argparse
import json
import os
from pathlib import Path

import torch
import torch.nn as nn
from torch.utils.data import DataLoader, Dataset
from transformers import (
    AutoModelForCausalLM,
    AutoTokenizer,
    get_cosine_schedule_with_warmup,
)

import deepspeed


# ── ZeRO config builders ──────────────────────────────────────────────────────

def zero2_config(
    batch_size: int = 16,
    micro_batch: int = 2,
    bf16: bool = True,
) -> dict:
    """ZeRO-2: partition optimizer states + gradients across GPUs."""
    return {
        "train_batch_size": batch_size,
        "train_micro_batch_size_per_gpu": micro_batch,
        "gradient_accumulation_steps": batch_size // micro_batch,
        "bf16": {"enabled": bf16},
        "fp16": {"enabled": not bf16},
        "zero_optimization": {
            "stage": 2,
            "allgather_partitions": True,
            "allgather_bucket_size": 200_000_000,
            "overlap_comm": True,
            "reduce_scatter": True,
            "reduce_bucket_size": 200_000_000,
            "contiguous_gradients": True,
        },
        "gradient_clipping": 1.0,
        "steps_per_print": 50,
        "wall_clock_breakdown": False,
    }


def zero3_offload_config(
    batch_size: int = 16,
    micro_batch: int = 1,
    offload_device: str = "cpu",  # "cpu" or "nvme"
) -> dict:
    """ZeRO-3 + CPU offload: partition everything including model params."""
    return {
        "train_batch_size": batch_size,
        "train_micro_batch_size_per_gpu": micro_batch,
        "gradient_accumulation_steps": batch_size // micro_batch,
        "bf16": {"enabled": True},
        "zero_optimization": {
            "stage": 3,
            "overlap_comm": True,
            "contiguous_gradients": True,
            "sub_group_size": 1e9,
            "reduce_bucket_size": "auto",
            "stage3_prefetch_bucket_size": "auto",
            "stage3_param_persistence_threshold": "auto",
            "stage3_max_live_parameters": 1e9,
            "stage3_max_reuse_distance": 1e9,
            "stage3_gather_16bit_weights_on_model_save": True,
            "offload_optimizer": {
                "device": offload_device,
                "pin_memory": True,
            },
            "offload_param": {
                "device": offload_device,
                "pin_memory": True,
            },
        },
        "gradient_clipping": 1.0,
    }


# ── Training loop ─────────────────────────────────────────────────────────────

class TextDataset(Dataset):
    """Simple tokenized text dataset."""

    def __init__(self, texts: list[str], tokenizer, max_length: int = 512):
        self.encodings = tokenizer(
            texts,
            truncation=True,
            max_length=max_length,
            padding="max_length",
            return_tensors="pt",
        )

    def __len__(self) -> int:
        return len(self.encodings["input_ids"])

    def __getitem__(self, idx: int) -> dict[str, torch.Tensor]:
        return {k: v[idx] for k, v in self.encodings.items()}


def get_dataloader(tokenizer, texts: list[str], batch_size: int = 4) -> DataLoader:
    dataset = TextDataset(texts, tokenizer)
    return DataLoader(dataset, batch_size=batch_size, shuffle=True, pin_memory=True)


def train_with_deepspeed(
    model_id:   str  = "meta-llama/Llama-3.2-3B-Instruct",
    output_dir: str  = "outputs/ds-finetune",
    epochs:     int  = 3,
    zero_stage: int  = 2,
    use_offload: bool = False,
) -> None:
    """Full DeepSpeed ZeRO training loop."""
    # Load model + tokenizer
    model = AutoModelForCausalLM.from_pretrained(
        model_id,
        torch_dtype=torch.bfloat16,
        use_cache=False,
    )
    model.gradient_checkpointing_enable()

    tokenizer = AutoTokenizer.from_pretrained(model_id)
    if tokenizer.pad_token is None:
        tokenizer.pad_token = tokenizer.eos_token

    # Build ZeRO config
    MICRO_BATCH = 2
    GLOBAL_BATCH = 16
    if zero_stage == 3 or use_offload:
        ds_config = zero3_offload_config(
            batch_size=GLOBAL_BATCH,
            micro_batch=MICRO_BATCH,
            offload_device="cpu" if use_offload else "none",
        )
    else:
        ds_config = zero2_config(batch_size=GLOBAL_BATCH, micro_batch=MICRO_BATCH)

    # Optimizer (AdamW — DeepSpeed will partition states with ZeRO)
    optimizer = torch.optim.AdamW(model.parameters(), lr=2e-5, weight_decay=0.01)

    # Scheduler
    total_steps = 1000  # Adjust to dataset size * epochs / grad_accum
    scheduler   = get_cosine_schedule_with_warmup(
        optimizer, num_warmup_steps=int(0.05 * total_steps), num_training_steps=total_steps
    )

    # Initialize DeepSpeed engine
    ds_config["scheduler"] = {
        "type": "WarmupDecayLR",
        "params": {
            "warmup_min_lr": 0,
            "warmup_max_lr": 2e-5,
            "warmup_num_steps": int(0.05 * total_steps),
            "total_num_steps": total_steps,
        },
    }

    engine, optimizer, _, scheduler = deepspeed.initialize(
        model=model,
        optimizer=optimizer,
        lr_scheduler=scheduler,
        config=ds_config,
    )

    # Dummy training data (replace with real dataset)
    sample_texts = ["The quick brown fox jumps over the lazy dog."] * 100
    dataloader   = get_dataloader(tokenizer, sample_texts, batch_size=MICRO_BATCH)

    # Training loop
    engine.train()
    global_step = 0

    for epoch in range(epochs):
        for batch in dataloader:
            input_ids      = batch["input_ids"].to(engine.local_rank)
            attention_mask = batch["attention_mask"].to(engine.local_rank)

            # Labels = input_ids shifted left (causal LM)
            labels = input_ids.clone()
            labels[labels == tokenizer.pad_token_id] = -100

            outputs = engine(
                input_ids=input_ids,
                attention_mask=attention_mask,
                labels=labels,
            )
            loss = outputs.loss

            engine.backward(loss)
            engine.step()

            global_step += 1
            if global_step % 50 == 0 and engine.local_rank == 0:
                print(f"Epoch {epoch+1} | Step {global_step} | loss={loss.item():.4f}")

    # Save checkpoint
    Path(output_dir).mkdir(parents=True, exist_ok=True)
    engine.save_checkpoint(output_dir, tag=f"epoch-{epochs}")
    if engine.local_rank == 0:
        tokenizer.save_pretrained(output_dir)
        print(f"Checkpoint saved: {output_dir}")


# ── HuggingFace Trainer + DeepSpeed ──────────────────────────────────────────

def save_ds_config(config: dict, path: str = "ds_config.json") -> str:
    """Save DeepSpeed config to JSON file for TrainingArguments."""
    with open(path, "w") as f:
        json.dump(config, f, indent=2)
    print(f"DeepSpeed config saved: {path}")
    return path


def train_with_hf_trainer(
    model_id:     str = "meta-llama/Llama-3.2-3B-Instruct",
    output_dir:   str = "outputs/hf-ds-finetune",
    zero_stage:   int = 2,
) -> None:
    """HuggingFace Trainer with DeepSpeed — simplest integration path."""
    from datasets import load_dataset
    from transformers import Trainer, TrainingArguments, DataCollatorForLanguageModeling

    # Save config to disk (Trainer expects a file path)
    ds_config = zero2_config() if zero_stage == 2 else zero3_offload_config()
    # Remove batch size — Trainer manages this
    ds_config.pop("train_batch_size", None)
    ds_config.pop("train_micro_batch_size_per_gpu", None)
    ds_config.pop("gradient_accumulation_steps", None)
    config_path = save_ds_config(ds_config, "ds_config.json")

    model     = AutoModelForCausalLM.from_pretrained(model_id, use_cache=False)
    tokenizer = AutoTokenizer.from_pretrained(model_id)
    if tokenizer.pad_token is None:
        tokenizer.pad_token = tokenizer.eos_token

    # Load and tokenize dataset
    raw      = load_dataset("iamtarun/python_code_instructions_18k_alpaca", split="train[:2000]")
    tokenized = raw.map(
        lambda ex: tokenizer(
            ex["output"], truncation=True, max_length=512, padding="max_length"
        ),
        batched=True,
        remove_columns=raw.column_names,
    )

    training_args = TrainingArguments(
        output_dir=output_dir,
        deepspeed=config_path,          # ← DeepSpeed integration point
        num_train_epochs=3,
        per_device_train_batch_size=2,
        gradient_accumulation_steps=4,
        gradient_checkpointing=True,
        bf16=True,
        learning_rate=2e-5,
        lr_scheduler_type="cosine",
        warmup_ratio=0.05,
        logging_steps=25,
        save_steps=200,
        save_total_limit=2,
        report_to=["tensorboard"],
        dataloader_pin_memory=False,
    )

    trainer = Trainer(
        model=model,
        args=training_args,
        train_dataset=tokenized,
        data_collator=DataCollatorForLanguageModeling(tokenizer, mlm=False),
    )

    print(f"Training with DeepSpeed ZeRO-{zero_stage}...")
    trainer.train()
    trainer.save_model(output_dir)
    tokenizer.save_pretrained(output_dir)
    print(f"Model saved: {output_dir}")


# ── Accelerate + DeepSpeed ────────────────────────────────────────────────────

def train_with_accelerate(
    model_id:   str = "meta-llama/Llama-3.2-3B-Instruct",
    output_dir: str = "outputs/accel-ds-finetune",
    zero_stage: int = 2,
) -> None:
    """Accelerate-based training loop with DeepSpeed plugin."""
    from accelerate import Accelerator
    from accelerate.utils import DeepSpeedPlugin

    plugin = DeepSpeedPlugin(
        zero_stage=zero_stage,
        gradient_accumulation_steps=4,
        gradient_clipping=1.0,
        offload_optimizer_device="cpu" if zero_stage == 3 else "none",
        offload_param_device="cpu"     if zero_stage == 3 else "none",
    )
    accelerator = Accelerator(
        mixed_precision="bf16",
        deepspeed_plugin=plugin,
    )

    model     = AutoModelForCausalLM.from_pretrained(model_id, use_cache=False)
    tokenizer = AutoTokenizer.from_pretrained(model_id)
    if tokenizer.pad_token is None:
        tokenizer.pad_token = tokenizer.eos_token

    model.gradient_checkpointing_enable()
    optimizer = torch.optim.AdamW(model.parameters(), lr=2e-5)

    sample_texts = ["Accelerate + DeepSpeed training example."] * 50
    dataloader   = get_dataloader(tokenizer, sample_texts, batch_size=2)

    model, optimizer, dataloader = accelerator.prepare(model, optimizer, dataloader)

    scheduler = get_cosine_schedule_with_warmup(
        optimizer,
        num_warmup_steps=50,
        num_training_steps=len(dataloader) * 3,
    )

    for epoch in range(3):
        model.train()
        for batch in dataloader:
            input_ids = batch["input_ids"]
            labels    = input_ids.clone()
            labels[labels == tokenizer.pad_token_id] = -100

            outputs = model(input_ids=input_ids, labels=labels)
            loss    = outputs.loss

            accelerator.backward(loss)
            optimizer.step()
            scheduler.step()
            optimizer.zero_grad()

    accelerator.wait_for_everyone()
    unwrapped = accelerator.unwrap_model(model)
    if accelerator.is_main_process:
        Path(output_dir).mkdir(parents=True, exist_ok=True)
        unwrapped.save_pretrained(output_dir, safe_serialization=True)
        tokenizer.save_pretrained(output_dir)
        print(f"Accelerate+DeepSpeed model saved: {output_dir}")


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--mode",       choices=["native", "trainer", "accelerate"], default="trainer")
    parser.add_argument("--zero-stage", type=int, choices=[0, 1, 2, 3], default=2)
    parser.add_argument("--offload",    action="store_true")
    args = parser.parse_args()

    if args.mode == "native":
        train_with_deepspeed(zero_stage=args.zero_stage, use_offload=args.offload)
    elif args.mode == "trainer":
        train_with_hf_trainer(zero_stage=args.zero_stage)
    else:
        train_with_accelerate(zero_stage=args.zero_stage)

Launch commands:

# Single node, 8 GPUs — ZeRO-2
deepspeed --num_gpus 8 train/deepspeed_train.py --mode trainer --zero-stage 2

# Multi-node (hostfile lists node IPs and GPU counts)
# hostfile: worker1 slots=8
#           worker2 slots=8
deepspeed --hostfile hostfile --num_nodes 2 --num_gpus 8 train/deepspeed_train.py --mode native --zero-stage 3 --offload

# With Accelerate launcher (reads accelerate config or plugin)
accelerate launch --multi_gpu --num_processes 8 train/deepspeed_train.py --mode accelerate

For the FSDP (Fully Sharded Data Parallel) alternative when staying within PyTorch native ecosystem and needing tight integration with torch.compile and the latest PyTorch performance features without an external C++ extension — PyTorch FSDP provides similar parameter sharding to ZeRO-3 but is built into PyTorch while DeepSpeed’s ZeRO-3 with CPU/NVMe offload is the only option for training models larger than total GPU memory and has battle-tested support for 70B+ parameter LLMs on commodity hardware. For the Megatron-LM alternative when training truly massive models (175B+) that require tensor parallelism and pipeline parallelism tightly fused with the transformer architecture — Megatron-LM requires rewriting the model to its specific layer implementations while DeepSpeed integrates with any HuggingFace model through a config file, making it the practical choice for most fine-tuning workflows. The Claude Skills 360 bundle includes DeepSpeed skill sets covering ZeRO config generation, HuggingFace Trainer integration, Accelerate plugin setup, and multi-node launch scripts. Start with the free tier to try distributed LLM training generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free