Skip to content
Julia Kurrr edited this page Jul 13, 2026 · 3 revisions

Local RAG Pipeline

End-to-end documentation for the experiments that were done during the architectural development, optimization, and empirical validation of the localized retrieval-augmented generation system.


Project Overview

This wiki documents the work on a retrieval-augmented generation (RAG) pipeline built over 500K Wikipedia documents, structured across 5 interconnected pipeline modules. Each section below links to the detailed analysis of an individual subsystem.

[Embeddings]    ---> [Vector Storage] ---> [Vector Index]   ---> [Search Engine]   ---> [UI Layer & LLM]
(18-Layer Gemma)     (Custom Scalar)       (Inverted File)       (Hybrid ANN +          (Streamlit UI +
(256d Embedding)     (Quantization )       (512 Centroids)       BM25 Fallback)         Local Gemma 4)

Sections

1. Representation Extraction Module (Embedding Optimization)

Full Analysis Here

  • Core Model: google/embeddinggemma-300m
  • Operational Baseline: Native bfloat16 inference with Hugging Face transformers execution architecture.
  • Structural Optimization: 18-layer token-pruned framework paired with Matryoshka Representation Learning (768d to 256d truncation).
  • Validation Protocol: Empirical grid-search benchmarking balancing hardware latency against semantic quality indices.
  • Output Artifacts: wikipedia_embeddings_256d.npy (Storage & Indexing inputs) + wikipedia_metadata_manifest.json (UI & Validation maps).

2. Vector Storage Compression Module

Full Analysis Here

  • Scalar Quantization (SQ8): Custom mathematical conversion transforming raw float32 matrices into 1-byte int8 spaces (4× reduction).
  • Low-Overhead Infrastructure: Implementation of a contiguous in-memory storage array bypassing heavy external database dependencies.
  • Output Artifacts: Compressed int8 matrix optimized for sub-linear memory addressing.

3. Vector Index Generation Module

Full Analysis Here

  • Spatial Partitioning: Scikit-learn K-Means clustering isolating 512 discrete centroids directly across the 256-dimensional Matryoshka space.
  • Structural Engineering: Construction of a production-grade Inverted File Index (IVF) mapping document tokens to localized posting lists.
  • Output Artifacts: Centroid coordinate arrays and mapped cell registers for accelerated subset querying.

4. Search Engine Routing Node

Full Analysis Here

  • Baseline Framework: High-performance exact Brute-force KNN execution using PyTorch CUDA batch matrix multiplication.
  • Optimized Framework: Sub-millisecond Approximate Nearest Neighbor (ANN) search localized within top-performing IVF cluster cells.
  • Validation Protocol: Rigorous profiling over 50 test strings capturing systemic Recall@K coefficients, latency variants, and VRAM maps.
  • Routing Architecture: Mathematical determination of a cosine similarity cutoff triggering a deterministic failover to classic BM25 keyword matching.

5. UI Layer & LLM Generation Module

Full Analysis Here

  • Integration Core: Central control script (main.py) executing cross-module function pipelines via a fast UI layer.
  • Fallback Interface: Live telemetry alerting users upon low-confidence semantic triggers and displaying immediate backup keyword search outputs.
  • Generation Framework: Structured API linkage to local Gemma 4 runtime parsing source data constraints and enforcing specific citation schemas.
  • Visual Verification: Production of responsive accordion display elements exposing raw article snippets, titles, and underlying confidence ratings.

Key Metrics

Metric Value
Embedding model google/embeddinggemma-300m (34 layers, bfloat16)
Embedding dimension 256 (Matryoshka-truncated from native 768)
Corpus size 500,000 Wikipedia documents
Full corpus encoding ~3 hours (Kaggle T4 ×2, BF16, batch=128)
Embedding latency per doc ~21 ms (batch=128 throughput)
Vector storage (SQ8 quantized) 122 MB
Vector storage (fp32 baseline) 488 MB
Model VRAM footprint ~890 MB
Mean pairwise cosine (random pairs) 0.085 (healthy, threshold < 0.5)
Query embedding latency 3–5 ms
ANN search latency (IVF-Flat, nprobe=64) 1–3 ms
BM25 fallback latency ~1 ms
Recall@10 (IVF-Flat) 0.962
LLM generation latency 0.5–3 s (gemma3:4b, 512 max tokens)
End-to-end query latency 1–4 s

Last updated: 2026-07-13