Principle:AI4Finance Foundation FinGPT DeepSpeed Supervised Finetuning
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, Distributed_Computing, NLP |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
A distributed training framework that enables memory-efficient supervised fine-tuning of large language models through ZeRO optimization stages, custom LoRA injection, and mixed-precision training.
Description
DeepSpeed-Chat SFT is a specialized training paradigm used in FinGPT's RAG instruction-tuning pipeline. Unlike the HuggingFace Trainer approach used in the Benchmark pipeline, this method uses DeepSpeed's native engine initialization, custom model creation (`create_hf_model`), and a custom LoRA implementation (`convert_linear_layer_to_lora`) that injects low-rank adapters directly into `nn.Linear` modules rather than using the PEFT library.
The pipeline supports ZeRO stages 0-3, DeepSpeedCPUAdam/FusedAdam optimizers, and gradient checkpointing. The custom LoRA (`LinearLayer_LoRA`) replaces target linear layers with adapted versions that add `lora_dropout(x) @ lora_right_weight @ lora_left_weight * lora_scaling` to the standard linear transformation during training.
Usage
Use this principle when:
- Training with DeepSpeed native engine: When you need ZeRO Stage 3 model parallelism beyond what HuggingFace Trainer provides.
- Custom LoRA injection: When you need LoRA applied to specific module name patterns rather than PEFT's target_modules.
- RAG pipeline instruction tuning: The FinGPT RAG subproject uses this approach for its SFT step.
Theoretical Basis
DeepSpeed-Chat SFT combines three key techniques:
- ZeRO Optimization: Partitions optimizer states, gradients, and parameters across data-parallel processes to reduce per-GPU memory.
- Custom LoRA: Injects trainable low-rank matrices into linear layers matching a name pattern, replacing the standard `nn.Linear` with `LinearLayer_LoRA`.
The custom LoRA forward pass:
Pseudo-code:
# Abstract: DeepSpeed SFT pipeline
model = create_hf_model(LlamaForCausalLM, model_path, tokenizer, ds_config)
model = convert_linear_layer_to_lora(model, "q_proj", lora_dim=8)
engine, optimizer, _, _ = deepspeed.initialize(model=model, ...)
for batch in train_dataloader:
loss = engine(batch)
engine.backward(loss)
engine.step()
Practical Guide
- Set `--lora_dim 0` to disable LoRA (full fine-tuning).
- Use `--only_optimize_lora` to freeze all non-LoRA parameters (memory savings).
- Enable gradient checkpointing for large models (incompatible with `--only_optimize_lora`).
- Use `save_hf_format` / `save_zero_three_model` for saving ZeRO Stage 3 models.
- The `data_split` argument (default "6,2,2") controls train/validation/test proportions.