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 Quantized Model Loading

From Leeroopedia
Knowledge Sources
Domains Deep_Learning, Model_Optimization
Last Updated 2026-10-18 00:00 GMT

Overview

A memory optimization technique that loads pre-trained models in reduced precision (8-bit or 4-bit) to enable inference on GPUs with limited VRAM, using progressive fallback strategies.

Description

Quantized model loading reduces the memory footprint of large language models by representing weights in lower precision formats. In FinGPT's benchmark evaluation pipeline, models are loaded with a 3-tier fallback strategy: first attempting 8-bit quantization, then 4-bit quantization with NF4 (Normal Float 4-bit) format, and finally falling back to full precision with `low_cpu_mem_usage=True`.

4-bit quantization with NF4 uses the `BitsAndBytesConfig` with double quantization and fp16 compute dtype. This reduces model memory by ~4× (vs fp16) while maintaining near-equivalent generation quality, enabling evaluation of 7B-13B models on consumer GPUs.

Usage

Use this principle when:

  • Evaluating models on limited VRAM: 8-bit or 4-bit quantization fits large models on single GPUs.
  • Inference-only deployment: Quantization is used for evaluation, not training (training uses LoRA on full-precision base).
  • Memory-constrained environments: Disk offloading (`offload_folder`) provides additional memory relief.

This principle is specific to the Benchmark Evaluation workflow.

Theoretical Basis

Quantization maps floating-point weights to lower-precision representations:

  • 8-bit: Uses int8 with absmax or ZSQ (Zero-Symmetric Quantization) scaling.
  • 4-bit NF4: Normal Float 4-bit, optimized for the normal distribution of pre-trained weights.

Wquant=dequant(quant(Wfp16))

Double quantization quantizes the quantization constants themselves, saving additional memory. The compute dtype remains fp16, so computations are performed in fp16 after dequantization.

3-tier fallback strategy:

# Abstract: progressive quantization fallback
try:
    model = load(model_name, load_in_8bit=True)
except:
    try:
        bnb_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4")
        model = load(model_name, quantization_config=bnb_config)
    except:
        model = load(model_name, low_cpu_mem_usage=True)

Practical Guide

  • Start with 8-bit quantization for best quality-memory tradeoff.
  • Use 4-bit NF4 with `bnb_4bit_use_double_quant=True` for maximum memory savings.
  • Set `device_map="auto"` to let the framework manage GPU placement.
  • Create an `offload_folder` for CPU disk offloading when GPU memory is insufficient.
  • Set `model.model_parallel = True` after loading for multi-layer parallelism.

Related Pages

Implemented By

Uses Heuristic

Page Connections

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