Principle:AI4Finance Foundation FinGPT Model Inference
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, NLP, Inference |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
The process of generating predictions from a fine-tuned language model by loading a base model with a LoRA adapter, constructing a prompt from live data, and decoding the model's output.
Description
Model inference for financial LLMs involves: (1) loading the pre-trained base model (e.g., Llama-2-7b-chat) in half precision with automatic device mapping, (2) applying a LoRA adapter via `PeftModel.from_pretrained`, (3) constructing a prompt from live market data (prices, news, fundamentals), (4) generating a response with sampling-based decoding, and (5) extracting the answer from the generated text using regex.
The inference pipeline supports streaming output via `TextStreamer`, disk offloading for low-VRAM environments (`offload_folder`), and multi-market symbol resolution (US, India NSE/BSE). The response is extracted by stripping everything before the `[/INST]` token, leaving only the model's analysis.
Usage
Use this principle when:
- Running live predictions: Generating real-time stock price movement forecasts.
- Deploying a fine-tuned model: Loading base + adapter for inference in a web app.
- Streaming responses: Using `TextStreamer` for real-time token display in a Gradio interface.
Theoretical Basis
Autoregressive generation samples tokens one at a time from the model's conditional distribution:
With `do_sample=True`, tokens are sampled stochastically rather than greedily selected, adding diversity to outputs. Generation stops at `eos_token_id` or `max_length`.
The LoRA adapter modifies the forward pass during inference:
Inference pipeline:
# Abstract: model inference pipeline
model = AutoModelForCausalLM.from_pretrained(base, dtype=fp16, device_map="auto")
model = PeftModel.from_pretrained(model, adapter_path)
model.eval()
inputs = tokenizer(prompt, return_tensors='pt').to(model.device)
output = model.generate(**inputs, max_length=4096, do_sample=True)
answer = extract_after_inst(tokenizer.decode(output[0]))
Practical Guide
- Load with `torch_dtype=torch.float16` and `device_map="auto"` for memory-efficient inference.
- Use `offload_folder="offload/"` for disk offloading when GPU memory is limited.
- Set `model.eval()` after loading the adapter.
- Use `do_sample=True` for diverse outputs; use `do_sample=False` (greedy) for deterministic results.
- Extract the answer with `re.sub(r'.*\[/INST\]\s*', , output, flags=re.DOTALL)` for Llama-2 chat models.
Related Pages
Implemented By
- Implementation:AI4Finance_Foundation_FinGPT_model_generate
- Implementation:AI4Finance_Foundation_FinGPT_AutoModelForCausalLM_from_pretrained
- Implementation:AI4Finance_Foundation_FinGPT_PeftModel_from_pretrained
- Implementation:AI4Finance_Foundation_FinGPT_parse_model_name