OptimalScale/LMFlow

An Extensible Toolkit for Finetuning and Inference of Large Foundation Models. Large Models for All.

LMFlow – A toolbox for fine‑tuning large language models

What it is – LMFlow is an open‑source Python library that helps researchers and developers adapt (fine‑tune) large language models (LLMs) such as Llama‑3, Phi‑3, Llama‑2, GPT‑2, etc. It bundles scripts, utilities, and optional integrations that make the whole pipeline – data preparation, training, inference, and deployment – faster and easier.

Key capabilities

Area What LMFlow provides
Training • Full‑parameter fine‑tuning (updates every weight)
• LoRA (low‑rank adaptation) – a lightweight, parameter‑efficient method
• LISA – a new memory‑efficient algorithm that freezes most layers and only updates a few, enabling 7 B‑scale models on a single 24 GB GPU
• QLoRA (8‑bit/4‑bit quantized LoRA) for even lower memory use
• Gradient checkpointing, FlashAttention‑2, DeepSpeed ZeRO‑3, and other tricks to squeeze out speed and reduce GPU RAM.
Inference • Standard Hugging‑Face‑style inference
• vLLM and SGLang back‑ends for high‑throughput serving
• CPU‑only 4‑bit inference via llama.cpp conversion scripts
Deployment • Gradio UI for a quick chatbot demo
• Flask integration for custom web services
Extras • Optional packages (vllm, sglang, trl, deepspeed, flash_attn, ray, multimodal, gradio, flask) that can be installed on demand.

How you use it

  1. Installpip install -e . (or add the needed extras).
  2. Prepare data – follow the docs for supported dataset formats (e.g., Alpaca).
  3. Run a training script – pick the method you want (run_finetune.sh for full, run_finetune_with_lora.sh, run_finetune_with_lisa.sh).
  4. Chat – after training, run_chatbot.sh launches a simple terminal chatbot; for a web UI, use the Gradio example.
  5. Deploy – launch a Flask or Gradio service, optionally with DeepSpeed for multi‑GPU scaling.

Hardware guidance – The README includes a handy table showing approximate GPU memory needed for different model sizes and training modes (e.g., a 7 B model needs ~120 GB for full‑precision fine‑tuning, but only ~16 GB with LoRA, ~10 GB with 8‑bit QLoRA, etc.).

Community & support – The project maintains a website, Discord, Slack, and WeChat groups, and it publishes regular updates (new model support, new algorithms like speculative decoding, conversation‑template presets, etc.).

Why it matters – Fine‑tuning LLMs is resource‑intensive; LMFlow bundles many of the latest memory‑saving tricks (LISA, LoRA, QLoRA, FlashAttention‑2, DeepSpeed) under a single, easy‑to‑install package, lowering the barrier for labs and hobbyists to adapt large models for specific tasks or domains.


Quick start snippet

# clone a stable tag
git clone -b v1.0.0 https://github.com/OptimalScale/LMFlow.git
cd LMFlow
conda create -n lmflow python=3.9 -y && conda activate lmflow
conda install mpi4py
pip install -e .

# fine‑tune GPT‑2 on Alpaca data
bash ./scripts/run_finetune.sh \
  --model_name_or_path gpt2 \
  --dataset_path data/alpaca/train_conversation \
  --output_model_path output_models/finetuned_gpt2

# chat with the result
bash ./scripts/run_chatbot.sh output_models/finetuned_gpt2

Related

  • Project
  • Project
  • Project
  • Project
  • Dispatch