Skip to main content
Multimodal VLM Case Study 4-Bit QLoRA · FastAPI · React

Vision Q/A: Fine-Tuning Qwen3-VL-4B with LoRA & FastAPI

Full-stack Visual Question Answering system powered by Qwen/Qwen3-VL-4B-Instruct fine-tuned on TextVQA using 4-bit NF4 LoRA, served behind an async FastAPI GPU semaphore lock and a React/TypeScript UI.

Qwen3-VL-4B-Instruct PyTorch LoRA (PEFT v0.18.1) 4-Bit NF4 TextVQA Dataset FastAPI asyncio.Semaphore React & TypeScript
Base VLM
4.45B Params

Qwen3-VL-4B-Instruct

4,449,612,288 total parameters

LoRA Adapter
0.265% Trained

11,796,480 Trainable Params

Rank r=16 · alpha=32 · NF4

VQA Dataset
34,600+ Rows

lmms-lab/textvqa

+ 443.5K COCO baseline pairs

Inference Guard
Semaphore(1)

FastAPI + React (Vite)

Non-blocking executor pool

Model Evolution: From CNN+LSTM to 4-Bit QLoRA VLM

gm-project.ipynb Baseline Exploration

Generation 01: ResNet50 + LSTM & BLIP

Aligned 443,547 COCO train2014 triplets (filtered to 26,587 top-1,000 answer samples) and trained a two-branch Keras classifier (ResNet50 vision encoder + 5,000-word LSTM text branch), alongside a zero-shot Salesforce/blip-vqa-base prototype.

Limitation: Fixed 1,000-class vocabulary struggled with scene OCR text.
gm-image-qa.ipynb Production Architecture

Generation 02: Qwen3-VL-4B + 4-Bit LoRA

Replaced fixed classification with open-ended instruction tuning of Qwen/Qwen3-VL-4B-Instruct on lmms-lab/textvqa using 4-bit NormalFloat (nf4) quantization and Low-Rank Adaptation (LoRA).

Consensus answer target + <|image_pad|> vision token alignment

BitsAndBytesConfig

4-Bit NF4 Quantization

  • load_in_4bit True
  • bnb_4bit_quant_type "nf4"
  • bnb_4bit_compute_dtype torch.float16
  • bnb_4bit_use_double_quant True

model/adapter_config.json

LoRA Adapter Hyperparameters

  • peft_type / task_type LORA / CAUSAL_LM
  • r / lora_alpha / dropout 16 / 32 / 0.05
  • target_modules q, v, k, o_proj
  • batch / grad_accum / lr 2 / 4 / 2e-4 (fp16)

Full-Stack Serving: React → FastAPI → Qwen3-VL

STAGE 01 /frontend

Frontend UI

React + TypeScript (Vite)

ImageUpload.tsx and ChatInterface.tsx send multipart/form-data (image + question) to the REST backend.

STAGE 02 /backend/app

API Gateway

FastAPI REST Router

Validates image/* MIME payloads on POST /api/v1/vqa/, converts bytes to RGB PIL.Image, and exposes GET /health.

STAGE 03 inference.py

Concurrency Lock

asyncio.Semaphore(1)

Serializes GPU/CPU tensor generation via loop.run_in_executor so concurrent HTTP requests never trigger CUDA OOM.

STAGE 04 /model

VLM Inference

Qwen3-VL-4B + LoRA Adapter

Merges base 4-bit NF4 weights with local PeftModel (/model) and generates grounded answers (max_new_tokens=50).

Engineering Trade-Offs & Notes

1. Semaphore(1) Serialization vs. vLLM Continuous Batching

Protecting single-GPU VRAM stability vs. high-throughput parallel serving

Serializing inference with asyncio.Semaphore(1) guarantees predictable memory usage on a single GPU or CPU host, but queues simultaneous users sequentially. For high-concurrency production traffic, the LoRA adapter should be merged and served via vLLM continuous batching.

2. CPU Default Fallback vs. CUDA Auto Device Mapping

Safe local boot (device_map="cpu") vs. GPU notebook execution (device_map="auto")

In backend/app/services/inference.py, device_map="cpu" is configured as a safe local default when an NVIDIA GPU is absent, whereas gm-image-qa.ipynb trains and runs with device_map="auto" on CUDA.

3. Qualitative Widget Evaluation vs. Formal TextVQA Holdout Table

Single-epoch instruction tuning verified interactively in notebook & web UI

The training notebook completed a single-epoch LoRA fine-tuning run on lmms-lab/textvqa (per_device_train_batch_size=2, gradient_accumulation_steps=4, lr=2e-4, fp16=True); a formal held-out VQA accuracy table is not yet committed to model/README.md.