Views
No views yet
LiquidAI/LFM2.5-1.2B-Instruct using a two-phase SFT + DPO pipeline with QLoRA (4-bit NF4), augmented by an XGBoost ensemble meta-learner and temperature calibration. Given technical indicator data for a stock and date, it outputs a single directional trading signal: BUY, SELL, or HOLD.val[300:]) never seen during training, calibration, or meta-learning.| Model | Accuracy | BUY predicted | SELL predicted | HOLD predicted |
|---|---|---|---|---|
| Base LFM2.5-1.2B (untouched, no fine-tuning) | 34.7% | 1500 | 0 | 0 |
| Fine-tuned LLM only (no calibration) | 33.7% | 198 | 1097 | 205 |
| Fine-tuned LLM + Temperature Calibration | 33.7% | 198 | 1097 | 205 |
| Full Ensemble (Calibrated LLM + XGBoost) | 36.1% | 469 | 798 | 233 |
Accuracy: 521/1500 = 34.7%
Distribution: BUY=1500, SELL=0, HOLD=0
precision recall f1-score support
BUY 0.35 1.00 0.52 521
SELL 0.00 0.00 0.00 477
HOLD 0.00 0.00 0.00 502
accuracy 0.35 1500
macro avg 0.12 0.33 0.17 1500
weighted avg 0.12 0.35 0.18 1500Accuracy: 541/1500 = 36.1%
Distribution: BUY=469, SELL=798, HOLD=233
precision recall f1-score support
BUY 0.37 0.33 0.35 521
SELL 0.38 0.19 0.25 477
HOLD 0.35 0.55 0.43 502
accuracy 0.36 1500
macro avg 0.37 0.36 0.34 1500
weighted avg 0.37 0.36 0.34 15001python -m venv venv
2
3# On Windows
4venv\Scripts\activate
5
6# On Mac/Linux
7source venv/bin/activate1pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
2pip install transformers accelerate bitsandbytes huggingface_hub1pip install torch torchvision torchaudio
2pip install transformers accelerate huggingface_hubNote:bitsandbytesis only needed for 4-bit GPU quantization. For CPU inference, skip it. The LFM2.5 architecture uses gated convolutional blocks instead of full attention, which makes it significantly faster on CPU than a standard transformer of the same size.
predict.py and run it with python predict.py:1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3from transformers import BitsAndBytesConfig
4
5repo_id = "ewinregirgojr/LFM2.5-Stock-Analyst-Final"
6
7# Load in 4-bit (~1 GB VRAM). Remove BitsAndBytesConfig to load in float16 (~2.4 GB VRAM)
8bnb_config = BitsAndBytesConfig(
9 load_in_4bit=True,
10 bnb_4bit_quant_type="nf4",
11 bnb_4bit_compute_dtype=torch.float16,
12)
13
14print("Loading model... (first run will download ~2.5 GB)")
15tokenizer = AutoTokenizer.from_pretrained(repo_id)
16model = AutoModelForCausalLM.from_pretrained(
17 repo_id,
18 quantization_config=bnb_config,
19 device_map="auto"
20)
21model.eval()
22print("Model ready.")
23
24def predict(ticker, price_history, rsi, volume_mult, volatility_mult):
25 stock_data = (
26 f"Analyze {ticker} and predict the stock direction.\n"
27 f"Price history: {price_history}\n"
28 f"Technical indicators:\n"
29 f"- RSI(14): {rsi}\n"
30 f"- Volume Multiplier: {volume_mult}x\n"
31 f"- Volatility Multiplier: {volatility_mult}x"
32 )
33 messages = [
34 {"role": "system", "content": "You are a financial analyst. Output ONLY ONE WORD: BUY, SELL, or HOLD."},
35 {"role": "user", "content": stock_data}
36 ]
37 formatted = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
38 inputs = tokenizer(formatted, return_tensors="pt").to("cuda")
39 with torch.inference_mode():
40 outputs = model.generate(**inputs, max_new_tokens=5)
41 signal = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True).strip()
42 return signal
43
44# Example
45signal = predict(
46 ticker="AMZN",
47 price_history="$88.37 -> $89.37 -> $89.28 -> $88.11 -> $88.90",
48 rsi=55.1,
49 volume_mult=0.69,
50 volatility_mult=0.88
51)
52print(f"Signal: {signal}")| Issue | Fix |
|---|---|
CUDA out of memory | Enable 4-bit loading with BitsAndBytesConfig as shown above, or reduce batch size |
bitsandbytes install fails on Windows | Install WSL2 and run from there, or use Google Colab |
| Model outputs a long explanation instead of BUY/SELL/HOLD | Make sure the system prompt says exactly: Output ONLY ONE WORD: BUY, SELL, or HOLD. |
trust_remote_code error | Add trust_remote_code=True to both from_pretrained calls |
LiquidAI/LFM2.5-1.2B-Instruct is loaded in 4-bit NF4 quantization (Dettmers et al., 2023) using BitsAndBytes, reducing VRAM consumption dramatically while preserving model quality. A LoRA adapter (Hu et al., 2021) with rank r=32, alpha=64 is injected into all major projection layers:target_modules: q_proj, k_proj, v_proj, out_proj, in_proj, w1, w2, w3BUY)BUY -> HOLD, SELL -> HOLD, HOLD -> BUY. Hard negatives force the model to learn fine-grained boundary cases rather than trivial opposites.(prompt + correct_label) text. This phase teaches the model the basic structure of the task.max_steps: 400per_device_train_batch_size: 16learning_rate: 2e-4lr_scheduler_type: cosinewarmup_steps: 100max_steps: 200per_device_train_batch_size: 16learning_rate: 5e-6lr_scheduler_type: cosinewarmup_steps: 40beta: 0.1val[:300], a held-out set never seen during SFT or DPO. The feature vector combines raw technical features with LLM-derived probability signals:rsi_oversold (binary, RSI < 30), rsi_overbought (binary, RSI > 70)p_buy, p_sell, p_hold - softmax probabilities from the fine-tuned LLMllm_confidence = max(p_buy, p_sell, p_hold)buy_vs_hold_margin = raw logit b - hsell_vs_hold_margin = raw logit s - hval[:300] using scipy's bounded scalar optimizer in the range [0.1, 10.0]. T > 1 softens overconfident predictions; T < 1 sharpens underconfident ones.