Implementation:AI4Finance Foundation FinGPT create hf model
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, Model_Loading |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
Concrete tool for creating HuggingFace models with DeepSpeed ZeRO configuration provided by the FinGPT RAG training utilities.
Description
The create_hf_model function loads a HuggingFace model with DeepSpeed configuration. It reads the model config, sets dropout to 0.0, applies `HfDeepSpeedConfig` for ZeRO Stage 3 if configured, loads the model from pre-trained weights, sets pad/eos token IDs, and resizes token embeddings to a multiple of 8 for optimal GPU performance.
This function is used in the DeepSpeed-Chat SFT pipeline as an alternative to the standard `AutoModelForCausalLM.from_pretrained`, providing DeepSpeed-specific optimizations.
Usage
Import this function when you need to create a model with DeepSpeed integration:
- DeepSpeed SFT: Model creation in the RAG instruction-tuning pipeline.
- ZeRO Stage 3: Models requiring parameter partitioning across GPUs.
Code Reference
Source Location
- Repository: FinGPT
- File: fingpt/FinGPT_RAG/instruct-FinGPT/training/utils/model/model_utils.py
- Lines: L18-46
Signature
def create_hf_model(
model_class,
model_name_or_path: str,
tokenizer,
ds_config: Optional[dict] = None,
rlhf_training: bool = False,
) -> PreTrainedModel
Import
from utils.model.model_utils import create_hf_model
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model_class | type | Yes | Model class (LlamaForCausalLM or AutoModelForCausalLM) |
| model_name_or_path | str | Yes | HuggingFace model ID or local path |
| tokenizer | AutoTokenizer | Yes | Tokenizer for pad/eos token config |
| ds_config | dict | No | DeepSpeed config for ZeRO Stage 3 |
| rlhf_training | bool | No | If True, create from config (not from_pretrained) |
Outputs
| Name | Type | Description |
|---|---|---|
| return value | PreTrainedModel | Model with DeepSpeed config, resized embeddings |
Usage Examples
Create Model for DeepSpeed SFT
from utils.model.model_utils import create_hf_model
from transformers import LlamaForCausalLM, LlamaTokenizer
tokenizer = LlamaTokenizer.from_pretrained(
"meta-llama/Llama-2-7b-hf", add_eos_token=True, fast_tokenizer=True
)
model = create_hf_model(
LlamaForCausalLM,
"meta-llama/Llama-2-7b-hf",
tokenizer,
ds_config=ds_config,
)