# LayerCheck **LayerCheck** is a research-driven framework for **efficient and fault-tolerant checkpointing in Large Language Model (LLM) training**. Instead of periodically saving the entire model and optimizer states, LayerCheck introduces **layer-wise selective checkpointing**, which selectively saves only the layers that undergo significant updates. This reduces storage costs, improves I/O efficiency, and accelerates recovery while preserving training effectiveness. --- ## Key Features - **Selective Layer Checkpointing** Dynamically identifies and stores layers with significant updates, minimizing redundant storage. - **Low-Contention Incremental I/O** Writes far less data per checkpoint, so the blocking I/O window shrinks. - **Compatibility with Distributed Training** Works with **DeepSpeed ZeRO-3**, and HuggingFace `Trainer` integration. - **Quick Recovery** With the LLMTailor tool inside of the LayerCheck, layer-wise partial checkpoints can be merged into one checkpoint quickly. - **Optimizer State Handling** Tracks layer-wise optimizer states (e.g., Adam momentum/variance) and reconstructs them efficiently during recovery. --- ## LLMTailor Integration **LayerCheck** includes a companion tool, **LLMTailor**, designed for **layer-wise slicing and merging of LLMs and their optimizer states**. LLMTailor enables: - Cutting and reassembling LLM checkpoints at the **layer granularity**. - Handling auxiliary layers (e.g., embeddings, `lm_head`, norms) consistently across different checkpoints. - Supporting **multi-GPU / ZeRO sharded optimizer states** during merging. - YAML-based configuration for flexible merge/cut planning. This makes LLMTailor useful not only for LayerCheck’s partial checkpoint recovery but also for **model surgery tasks** such as merging experts, splitting layers, or building Frankenstein checkpoints. --- ## Getting Started ### Installation LayerCheck targets **Python 3.11** and **CUDA 12.8** (torch 2.9). It is built to install with [`uv`](https://github.com/astral-sh/uv), which resolves torch from the CUDA index and the bundled `LLMTailor` submodule in one step. ```bash git clone https://github.com/DIR-LAB/LayerCheck.git cd LayerCheck git submodule update --init # fetch the LLMTailor submodule uv pip install -e . # installs layercheck + llmtailor + all deps ``` Plain-pip users can install from the pinned list instead (see the notes inside the file): ```bash pip install -r requirements.txt pip install -e ./LLMTailor --no-deps pip install -e . --no-deps ``` > **DeepSpeed and the optimizer.** `transformers/integrations/deepspeed.py` clears > `trainer.optimizer` so DeepSpeed can re-init it, which would drop `AdamX` and with it the > per-layer monitoring. `LayerTrainer.create_optimizer` hands the original `AdamX` back, so a > stock transformers install works as is. Section 1 of `Experiment.ipynb` prints the state of > that line. Driving `AdamX` through a plain HF `Trainer` needs the line commented out > manually: > ```python > trainer.optimizer = None # important for when deepspeed_init is used as re-init > ``` ## Usage ### End-to-end notebook (recommended) Open **`experiments/Experiment.ipynb`** and run it top to bottom. It follows the paper's structure: train a model with selective checkpointing alongside a DeepSpeed baseline, show the layer-wise update imbalance that motivates the design as Fig 2, measure the resulting staleness as Fig 4, recover through an LLMTailor merge and resume while tracking the loss difference as Fig 5, report checkpoint efficiency as Table I and Table II, benchmark the recovered model with `lm-eval` as Table III, and compare training and recovery cost against the baseline as Fig 6 and Fig 7. Every number the notebook prints is measured on the machine you run it on. The paper also compares against CheckFreq, GEMINI and LowDiff; those are separate systems and their code lives in the LowDiff repository. ### Command line Training and recovery are driven by `experiments/run.sh`: ```bash cd experiments # train from scratch with selective checkpointing ./run.sh orig --model llama-3.2-1b --dataset wikitext --threshold 100 --max-steps 200 # recover: merge partial checkpoints (LLMTailor), then resume ./run.sh merge --model llama-3.2-1b --dataset wikitext --threshold 100 \ --merge-step 100 --resume /path/to/merged/model ``` Models and datasets live in `configs/models.yaml` and `configs/datasets.yaml`; shared hyperparameters in `configs/defaults.yaml`. External locations (datasets, model cache, output roots) are overridable via the `LAYERCHECK_*` environment variables — see the top of `run.sh`. The selective-checkpoint optimizer `AdamX` is configured per model, e.g.: ```python optimizer = AdamX( param_groups, lr=1e-5, layer_num=16, # from configs/models.yaml K=100.0, threshold_end=1e-5, layers_to_checkpoint=[], update_log_path="logs/updates.jsonl", thr_log_path="logs/thr.jsonl", flag_log_path="logs/flags.jsonl", only_rank0=True, ) ``` ### Merging checkpoints with LLMTailor `LLMTailor` reconstructs a complete model from the partial checkpoints (driven automatically in the notebook's recovery section): ```bash bash LLMTailor/examples/start_merge.sh \ --streamcheck_json /logs/checkpoint_flags.jsonl \ --streamcheck_path /checkpoints \ --config_yml out.yaml --output_path merged/ \ --failure_step 100 --total_layers 16 ``` --- ## Note All checkpoint writes, LayerCheck's and the baseline's alike, run synchronously on the training thread, so the reported `t_ckpt` includes the disk write. Overlapping I/O with compute is orthogonal to layer-wise selection and composes with it; we leave that to future work.