Implementation:AI4Finance Foundation FinGPT AutoModelForCausalLM from pretrained
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, Model_Loading |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
HuggingFace Transformers' model loading API used across FinGPT workflows for loading pre-trained causal language models, with repo-specific quantization and memory configuration.
Description
The AutoModelForCausalLM.from_pretrained function is the primary model loading entry point used in all FinGPT workflows. It is used in three contexts:
- Training (Forecaster/Benchmark): Load full-precision base model with `trust_remote_code=True` for LoRA injection.
- Inference (Forecaster app): Load Llama-2-7b-chat in fp16 with `device_map="auto"` and disk offloading.
- Evaluation (Benchmark): Load with 3-tier quantization fallback (8-bit → 4-bit NF4 → full precision with low CPU memory).
The function is typically paired with `parse_model_name` to resolve short model names (e.g., "llama2") to HuggingFace model paths.
Usage
Import this class when you need to load a pre-trained causal LM:
- Training: Load base model for LoRA fine-tuning (full precision, trust_remote_code).
- Inference: Load with fp16 and device_map="auto" for memory-efficient inference.
- Evaluation: Load with 8-bit or 4-bit quantization for low-VRAM evaluation.
Code Reference
Source Location
- Repository: FinGPT
- File: fingpt/FinGPT_Forecaster/train_lora.py (L87-91), fingpt/FinGPT_Benchmark/train_lora.py (L48-53), fingpt/FinGPT_Benchmark/benchmarks/benchmarks.py (L42-75), fingpt/FinGPT_Forecaster/app.py (L32-39)
Signature
# Training context (Forecaster/Benchmark)
model = AutoModelForCausalLM.from_pretrained(
model_name: str,
trust_remote_code: bool = True,
) -> AutoModelForCausalLM
# Inference context (app.py)
model = AutoModelForCausalLM.from_pretrained(
'meta-llama/Llama-2-7b-chat-hf',
token: str,
trust_remote_code: bool = True,
device_map: str = "auto",
torch_dtype: torch.float16,
offload_folder: str = "offload/",
) -> AutoModelForCausalLM
# Evaluation context (benchmarks.py) - 3-tier fallback
# Tier 1: 8-bit
model = AutoModelForCausalLM.from_pretrained(
model_name, trust_remote_code=True,
load_in_8bit=True, device_map="auto",
offload_folder="./offload",
)
# Tier 2: 4-bit via BitsAndBytesConfig
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
)
model = AutoModelForCausalLM.from_pretrained(
model_name, trust_remote_code=True,
quantization_config=bnb_config,
device_map="auto", offload_folder="./offload",
)
# Tier 3: Full precision with low memory
model = AutoModelForCausalLM.from_pretrained(
model_name, trust_remote_code=True,
device_map="auto", offload_folder="./offload",
low_cpu_mem_usage=True,
)
Import
from transformers import AutoModelForCausalLM
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model_name | str | Yes | HuggingFace model ID or local path (via parse_model_name) |
| trust_remote_code | bool | No | Allow custom model code (default True in FinGPT) |
| device_map | str | No | Device placement strategy ("auto" for inference/eval) |
| torch_dtype | torch.dtype | No | Model precision (fp16 for inference) |
| load_in_8bit | bool | No | 8-bit quantization (evaluation only) |
| quantization_config | BitsAndBytesConfig | No | 4-bit quantization config (evaluation only) |
| offload_folder | str | No | Disk offload directory for low VRAM |
| low_cpu_mem_usage | bool | No | Reduce CPU memory during loading |
Outputs
| Name | Type | Description |
|---|---|---|
| return value | AutoModelForCausalLM | Loaded model (optionally quantized) |
Usage Examples
Training: Load Base Model for LoRA
from transformers import AutoModelForCausalLM
from utils import parse_model_name
model_name = parse_model_name("llama2", from_remote=True)
model = AutoModelForCausalLM.from_pretrained(
model_name, trust_remote_code=True
)
# Configure for training
model.gradient_checkpointing_enable()
model.enable_input_require_grads()
model.config.use_cache = False
Inference: Load with fp16 and Device Map
import torch
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(
'meta-llama/Llama-2-7b-chat-hf',
token=hf_token,
trust_remote_code=True,
device_map="auto",
torch_dtype=torch.float16,
offload_folder="offload/",
)
Evaluation: 4-bit Quantized Loading
import torch
from transformers import AutoModelForCausalLM, BitsAndBytesConfig
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
)
model = AutoModelForCausalLM.from_pretrained(
model_name, trust_remote_code=True,
quantization_config=bnb_config,
device_map="auto", offload_folder="./offload",
)
model.model_parallel = True
Related Pages
Implements Principle
- Principle:AI4Finance_Foundation_FinGPT_LoRA_Fine_Tuning
- Principle:AI4Finance_Foundation_FinGPT_Quantized_Model_Loading
- Principle:AI4Finance_Foundation_FinGPT_Model_Inference
Uses Heuristic
- Heuristic:AI4Finance_Foundation_FinGPT_Quantization_Fallback_Chain
- Heuristic:AI4Finance_Foundation_FinGPT_Model_Specific_Tokenization