Composer Documentation
repository·main·Indexed 26 days ago
https://github.com/mosaicml/composerAn open-source deep learning training library built on PyTorch designed to simplify and accelerate large-scale distributed training for LLMs, diffusion models, and large-scale neural networks. It provides high-level abstractions for parallelism, data streaming, and training loop management, featuring a Trainer class and a functional API for algorithms such as ALiBi, AugMix, BlurPool, and ChannelsLast.
What's inside Composer
- Composer is an open-source deep learning training library built on top of PyTorch. It is designed to simplify and optimize distributed training workflows on large-scale clusters by abstracting low-level complexities such as parallelism techniques, distributed data loading, and memory optimization. It is suitable for training various model architectures, including Large Language Models (LLMs), Diffusion models, Embedding models (e.g., BERT), Transformers, and Convolutional Neural Networks (CNNs).
Overview of Composer Training Methods
mainComposer provides a variety of training methods and augmentations categorized by their application domain: Computer Vision (CV) and Natural Language Processing (NLP). These methods can be used to improve scalability, customizability, and training efficiency.
Computer Vision (CV) Methods
- AugMix: Image-preserving data augmentations.
- BlurPool: Applies blur before pooling or downsampling.
- ChannelsLast: Uses channels last memory format (NHWC).
- ColOut: Removes columns and rows from the image for augmentation and efficiency.
- CutMix: Combines pairs of examples in non-overlapping regions and mixes labels.
- CutOut: Randomly erases rectangular blocks from the image.
- GhostBatchNorm: Uses smaller number of samples to compute batchnorm.
- GyroDropout: (Note: Source text indicates this clips gradients, similar to GradientClipping).
- LabelSmoothing: Smooths labels with a uniform prior.
- MixUp: Blends pairs of examples and labels.
- ProgressiveResizing: Increases the input image size during training.
- RandAugment: Applies a series of random augmentations.
- SqueezeExcite: Replaces eligible layers with Squeeze-Excite layers.
- StochasticDepth: Replaces a specified layer with a stochastic version that randomly drops the layer or samples during training.
- Weight Standardization: Ensures convolution weights always have zero mean and unit variance.
Natural Language Processing (NLP) Methods
- Alibi: Replaces attention with AliBi.
- GatedLinearUnits: Swaps the building block from a Linear layer to a Gated Linear layer.
- SeqLengthWarmup: Progressively increases sequence length.
Cross-Domain Methods (CV & NLP)
- EMA: Maintains an exponential moving average of model weights for evaluation.
- Factorize: Factorizes GEMMs into smaller GEMMs.
- GradientClipping: Clips all gradients in the model based on a specified
clipping_type. - LayerFreezing: Progressively freezes layers during training.
- LowPrecisionGroupNorm: Forces GroupNorm to run in lower precision.
- LowPrecisionLayerNorm: Forces LayerNorm to run in lower precision.
- SAM: SAM optimizer measures sharpness of optimization space.
- SelectiveBackprop: Drops examples with small loss contributions.
- SWA: Computes running average of model weights.
Overview of Composer features
mainComposer is an open-source deep learning training library by MosaicML that optimizes PyTorch for scalability and usability. Key capabilities include:
- Scalability: Supports PyTorch FullyShardedDataParallelism (FSDP) for large models, standard Distributed Data Parallelism (DDP), and elastic sharded checkpointing (allowing you to save on one GPU count and resume on another).
- Data Streaming: Integrates with MosaicML
StreamingDatasetto stream data from cloud blob storage during training. - Customizability: Features a callback system to insert custom logic at specific training loop events (e.g.,
BATCH_END) and a collection of algorithmic speedup recipes. - Workflow Automation: Includes auto-resumption from checkpoints, CUDA OOM prevention via automatic microbatch size selection, and
Timeabstractions for specifying training duration in epochs, batches, samples, or tokens. - Integrations: Supports cloud storage (OCI, GCP, AWS S3) for checkpointing/logging and experiment tracking via Weights and Biases, MLFlow, CometML, and neptune.ai.
Composer Ecosystem Integrations
mainFor optimal performance, Composer is designed to work with the following MosaicML ecosystem components:
- Mosaic AI training (MCLI): A CLI and Python SDK for orchestrating and scaling GPU training.
- MosaicML LLM Foundry: An open-source repository for training, finetuning, and evaluating LLMs using Composer.
- MosaicML StreamingDataset: An open-source library for high-speed streaming from cloud storage.
- MosaicML Diffusion: Open-source code for training Stable Diffusion models.
Understand Composer training events
mainEvents represent specific points in the training loop where acomposer.core.Algorithmor acomposer.core.Callbackcan execute. These events allow you to hook into the lifecycle of the training process to perform custom actions, logging, or modifications to the training state.Inject custom logic with Events and State
mainThe Composer trainer uses an event-driven system to allow injecting custom logic at specific points in the training loop (e.g.,before_forward,after_forward). Algorithms and callbacks register to these events. TheStateobject stores the trainer's current state (model, optimizers, dataloader, current batch, etc.), which can be modified by algorithms during events.Save only model weights for inference
mainTo save a checkpoint containing only model weights (plus metadata and integrations) instead of the full training state, set
save_weights_only=Truein theTrainer.from composer.trainer import Trainer trainer = Trainer( ..., save_folder="checkpoints", save_weights_only=True, save_overwrite=True, ) trainer.fit()Configure FSDP Checkpointing (Save and Load)
mainControl how FSDP handles sharded checkpoints using the
state_dict_typekey infsdp_config:state_dict_type='full'(Default): Saves one large checkpoint file by gathering the model state to global rank 0. Ifload_monolith_rank0_only=Trueis set, rank 0 loads the file and scatters it to other ranks to reduce system memory usage.state_dict_type='sharded': Each rank saves an unflattened shard. Recommended for PyTorch 2.0.0 or higher.
Organizing Sharded Checkpoints: To prevent
save_folderpollution, use'sharded_ckpt_prefix_dir'to group shards into subdirectories. The default is'ep{epoch}-ba{batch}'.Loading Sharded Checkpoints:
- Set
load_pathto the directory containing the shards, not a specific file. - Ensure
state_dict_typeis set to'sharded'in your config. - Composer with PyTorch 2.0.0+ supports elastic checkpointing (resuming with a different number of ranks).
fsdp_config = { 'sharding_strategy': 'FULL_SHARD', 'state_dict_type': 'sharded', 'sharded_ckpt_prefix_dir': 'ba{batch}-shards' } trainer = Trainer( model=composer_model, max_duration='4ba', parallelism_config={'fsdp': fsdp_config}, save_folder='checkpoints', save_interval='2ba', ) # To load: trainer = Trainer( model=composer_model, parallelism_config={'fsdp': {'sharding_strategy': 'FULL_SHARD', 'state_dict_type': 'sharded'}}, load_path='./checkpoints/ba2-shards' # Path to the directory )Configure automatic checkpointing in Trainer
mainTo enable automatic checkpointing, provide a
save_folderargument when initializing theTrainer. You can customize filenames usingsave_filenameand the symlink for the latest checkpoint usingsave_latest_filename. These arguments accept Python format strings (e.g.,ep{epoch}).Available format variables can be found in the
CheckpointSaverdocumentation.from composer import Trainer trainer = Trainer( model=model, train_dataloader=train_dataloader, max_duration="2ep", save_folder="./path/to/checkpoints", save_filename="ep{epoch}", save_latest_filename="latest", save_overwrite=True, ) trainer.fit()Install Tensorboard support for Composer
mainTo enable Tensorboard logging capabilities in Composer, install the
tensorboardextra for themosaicmlpackage using pip.pip install 'mosaicml[tensorboard]'Identify DataLoader bottlenecks using traces
mainTo determine if your training is bottlenecked by data loading, compare the duration of the
dataloader/trainevent against theevent/batchevent (which encompasses the forward and backward passes):- Bottlenecked: If
dataloader/traintakes significantly more time thanevent/batch, the training is dataloader bottlenecked. Increasing the number of workers in your DataLoader is recommended. - Optimized: In an optimized setup,
dataloader/trainshould be much smaller in duration compared toevent/forwardandevent/backward.
- Bottlenecked: If
Use DistributedSampler for non-iterable datasets
mainIf you are using a
torch.utils.data.Dataset(that is NOT anIterableDataset) with atorch.utils.data.DataLoader, you must provide aDistributedSamplerto ensure different devices receive different batches. Composer will raise an error if it is missing.Use
composer.utils.dist.get_samplerto create a sampler with the correct parameters.from composer.utils import dist sampler = dist.get_sampler(dataset, shuffle=True) dataloader = DataLoader(dataset, batch_size=32, sampler=sampler)