Heuristic:AI4Finance Foundation FinGPT Gradient Checkpointing
| Knowledge Sources | |
|---|---|
| Domains | Optimization, Memory_Management |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
Enable gradient checkpointing and disable cache during LoRA fine-tuning to reduce VRAM usage by recomputing activations during the backward pass.
Description
All FinGPT LoRA training scripts call `model.gradient_checkpointing_enable()`, `model.enable_input_require_grads()`, and set `model.config.use_cache=False` before training. Gradient checkpointing stores only a subset of intermediate activations during the forward pass and recomputes the rest during the backward pass. This drastically reduces peak VRAM usage. `use_cache=False` is required because KV caching conflicts with gradient checkpointing during training. The MPT model architecture is excluded from gradient checkpointing.
Usage
Use this heuristic when VRAM constrained or training 7B+ parameter models on consumer GPUs. Standard practice in all FinGPT LoRA training scripts (benchmark and forecaster).
The Insight (Rule of Thumb)
- Action: Call `model.gradient_checkpointing_enable()` and set `model.config.use_cache=False`.
- Value: N/A (Boolean flags).
- Trade-off: Reduces VRAM usage by ~40-50% at the cost of ~20-30% slower training speed.
- Compatibility: Works with all Transformer models except MPT. Requires `use_cache=False` during training.
Reasoning
Deep Transformers have massive activation maps (Batch × SeqLen × HiddenDim). Storing these for backpropagation is the primary VRAM bottleneck. Gradient checkpointing stores only checkpoint boundaries and recomputes intermediate activations on-the-fly during the backward pass. This trades compute for memory, allowing significantly larger models to fit in limited VRAM. The `use_cache=False` flag is mandatory because the KV cache stores attention keys/values for generation, which conflicts with the recomputation strategy.
Code Evidence
Gradient checkpointing and cache disabling from `fingpt/FinGPT_Benchmark/train_lora.py:134-141`:
if not args.base_model == 'mpt':
model.gradient_checkpointing_enable()
model.enable_input_require_grads()
model.is_parallelizable = True
model.model_parallel = True
model.config.use_cache = (
False
)
Forecaster gradient checkpointing from `fingpt/FinGPT_Forecaster/train_lora.py:148-152`:
model.gradient_checkpointing_enable()
model.enable_input_require_grads()
model.is_parallelizable = True
model.model_parallel = True
model.model.config.use_cache = False
MPT exclusion from `fingpt/FinGPT_Benchmark/train_lora.py:62-63`:
if args.base_model != 'mpt':
tokenizer.padding_side = "left"