Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:AI4Finance Foundation FinGPT Model Inference

From Leeroopedia
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:

P(yt|y<t,x)=softmax(Wout⋅ht)

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:

h=W0x+αrBAx

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

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment