๐Ÿ›ก๏ธ DPDP SSense: Legal SLM Subsystem (ml/)

License Python PyTorch Transformers Unsloth Hardware

Industrial-grade Data Engineering, Synthetic GAN Forge, Supervised Fine-Tuning (SFT), Reference-Free Preference Optimization (SimPO), SOTA Hybrid RAG, and Adversarial Certification Harness for the Indian Digital Personal Data Protection (DPDP) Act 2023 & Rules 2025.


๐Ÿ“‘ Table of Contents

  1. Executive Summary & Architectural Paradigm
  2. Directory & Subsystem Structure
  3. Hardware & Provisioning Pipeline
  4. Stage 1: SOTA Data Engineering & Synthesis
  5. Stage 2: The Training Loop & Alignment Architecture
  6. Stage 3: Functional, Adversarial & SOTA Certification
  7. Stage 4: Unified Hugging Face Mono-Repo Deployment
  8. Quickstart & Inference Guide
  9. Operational Runbook (Step-by-Step DGX Execution)

1. Executive Summary & Architectural Paradigm

General-purpose foundation models frequently hallucinate legal sections, invent arbitrary penalties, stretch statutory provisions, or bleed conversational preambles into structured JSON APIs.

To eliminate these vulnerabilities, Ssense decouples legal analysis into two domain-specialized Small Language Models (SLMs) fine-tuned on top of Qwen2.5-7B-Instruct in native BF16 precision, backed by a deterministic Hybrid RAG retriever:

                                 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                                 โ”‚                DPDP Act 2023 & Rules 2025              โ”‚
                                 โ”‚             Raw Legal Knowledge & Case Law             โ”‚
                                 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                                             โ”‚
                                                             โ–ผ
                                 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                                 โ”‚       STAGE 1: DATA ENGINEERING & SYNTHESIS            โ”‚
                                 โ”‚  โ€ข Hybrid BM25 + BGE Embeddings + Cross-Encoder        โ”‚
                                 โ”‚  โ€ข Teacher 72B GAN Forge (Qwen2-72B-Instruct-FP8)      โ”‚
                                 โ”‚  โ€ข Deterministic Rust Decision Tree Compilation        โ”‚
                                 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                                             โ”‚ (SFT + SimPO Datasets)
                                                             โ–ผ
                                 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                                 โ”‚      STAGE 2: UNSLOTH TRAINING & SIMPO ALIGNMENT       โ”‚
                                 โ”‚  Base Model: Qwen2.5-7B-Instruct (Native BF16)         โ”‚
                                 โ”‚  Architecture: rsLoRA + FP32 AdamW + Fused Triton      โ”‚
                                 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                                       โ”‚                   โ”‚
                            โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                            โ–ผ                                                                         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚             FORENSIC LEGAL AUDITOR SLM                 โ”‚         โ”‚             CONVERSATIONAL CHATBOT SLM                 โ”‚
โ”‚                 (audit-model-final)                    โ”‚         โ”‚                 (chatbot-model-final)                  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค         โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ โ€ข Task: Comprehensive Privacy Policy Forensic Auditing โ”‚         โ”‚ โ€ข Task: Citizen Dialogue & Data Rights Guidance        โ”‚
โ”‚ โ€ข Context Window: 8,192 tokens                         โ”‚         โ”‚ โ€ข Context Window: 4,096 tokens                         โ”‚
โ”‚ โ€ข Output: Strict dpdp_schema.json validated JSON       โ”‚         โ”‚ โ€ข Output: High-fluidity Markdown Natural Language      โ”‚
โ”‚ โ€ข LoRA: rsLoRA r=128, ฮฑ=32, target: all-linear         โ”‚         โ”‚ โ€ข LoRA: rsLoRA r=64, ฮฑ=16, target: all-linear          โ”‚
โ”‚ โ€ข Alignment: SimPO ฮฒ=2.0 (Zero-hallucination mandate)  โ”‚         โ”‚ โ€ข Alignment: SimPO ฮฒ=1.0 (Conversational RAFT)         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                            โ”‚                                                                  โ”‚
                            โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                                       โ–ผ   โ–ผ
                                 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                                 โ”‚   STAGE 3: FUNCTIONAL & ADVERSARIAL CERTIFICATION      โ”‚
                                 โ”‚  โ€ข 18 Threshold Gates across 9 Eval Sub-Suites         โ”‚
                                 โ”‚  โ€ข Wilson 95% CI Lower Bound Statistical Gating        โ”‚
                                 โ”‚  โ€ข MinHash 7-Gram Contamination Firewall               โ”‚
                                 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                                             โ”‚
                                                             โ–ผ
                                 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                                 โ”‚   STAGE 4: UNIFIED HUGGING FACE LFS DEPLOYMENT         โ”‚
                                 โ”‚  โ€ข Push to PRiyanshu0-1/DPDP-SSense Mono-Repo          โ”‚
                                 โ”‚  โ€ข vLLM Production Server / Edge GGUF Delivery         โ”‚
                                 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

2. Directory & Subsystem Structure

The ml/ repository is self-contained and structured to manage data synthesis, model training, evaluation, and Git LFS deployment:

ml/
โ”œโ”€โ”€ requirements.txt             # Pinned dependencies (PyTorch 2.11, Transformers 5.5, TRL 0.24, Unsloth)
โ”œโ”€โ”€ README.md                    # Hugging Face Model Card & Technical Specification
โ”œโ”€โ”€ .gitattributes               # Git LFS tracking rules (safetensors, gguf, pkl, pdf)
โ”œโ”€โ”€ .gitignore                   # Firewall for optimizer states, raw dumps, and caches
โ”‚
โ”œโ”€โ”€ data-forge/                  # STAGE 1: Data Engineering, Vector DB & Synthetic Forge
โ”‚   โ”œโ”€โ”€ DPDP_Act_2023.pdf        # Official Gazette of India: DPDP Act 2023
โ”‚   โ”œโ”€โ”€ DPDP_Rules_2025.pdf      # Official Draft: DPDP Rules 2025
โ”‚   โ”œโ”€โ”€ dpdp_act_and_rules_2025.txt # Curated statutory reference corpus
โ”‚   โ”œโ”€โ”€ build_vector_db.py       # SOTA Hybrid Search Engine Builder (BM25 + Dense + Reranker)
โ”‚   โ”œโ”€โ”€ gan_forge.py             # 72B Teacher Synthetic Generator & Loophole Injection
โ”‚   โ”œโ”€โ”€ build_dpdp_tree.py       # Mathematical Rust Decision Tree Generator
โ”‚   โ”œโ”€โ”€ prepare_unsloth_data.py  # ChatML formatting, Response-Only Masking, & Leaky-Split Isolation
โ”‚   โ”œโ”€โ”€ dpdp_hybrid_index.pkl    # Serialized hybrid search index artifact
โ”‚   โ””โ”€โ”€ training-pairs/          # Raw and formatted SFT / SimPO training data
โ”‚
โ”œโ”€โ”€ slm-training/                # STAGE 2: High-Throughput SFT & SimPO Training Loops
โ”‚   โ”œโ”€โ”€ train_audit.py           # Industrial SFT + SimPO pipeline for Forensic Auditor (8k context)
โ”‚   โ”œโ”€โ”€ train_chatbot.py         # Industrial SFT + SimPO pipeline for Conversational Chatbot (4k context)
โ”‚   โ””โ”€โ”€ data/                    # Ingestion-ready JSONL datasets for Unsloth
โ”‚
โ”œโ”€โ”€ evals/                       # STAGE 3: Industrial Functional & Adversarial Certification Harness
โ”‚   โ”œโ”€โ”€ backend_loader.py        # Polymorphic Model Loader (Unsloth / vLLM / llama.cpp / 72B Judge)
โ”‚   โ”œโ”€โ”€ stats.py                 # Wilson Score 95% Confidence Interval & Lexical Fluidity Metrics
โ”‚   โ”œโ”€โ”€ metrics.py               # Shared Deduplicated Evaluators & Parametric Citation Registry
โ”‚   โ”œโ”€โ”€ path_resolver.py         # Centralized immutable path resolution
โ”‚   โ”œโ”€โ”€ run_grammar_evals.py     # Pillar 1 & 5: Schema Compliance, Delimiter Integrity, Latency
โ”‚   โ”œโ”€โ”€ run_accuracy_evals.py    # Pillar 2-4: Severity-Weighted F1, Trust MAE, Zero-Hallucination
โ”‚   โ”œโ”€โ”€ evaluate_rag.py          # Pillar 1 RAG: BM25 + Dense + Reranker Recall@3 & NDCG@3
โ”‚   โ”œโ”€โ”€ run_chatbot_evals.py     # Chatbot Statutory Accuracy, Schema Bleed, & MTLD Fluidity
โ”‚   โ”œโ”€โ”€ evaluate_chatbot.py      # SOTA Chatbot 5-Axis Certification (SCP, Context Faithfulness, JCR)
โ”‚   โ”œโ”€โ”€ run_hallucination_benchmark.py # Red-Team Statutory Traps & Silent Refusal (N=50)
โ”‚   โ”œโ”€โ”€ run_security_evals.py    # Adversarial Security: 2D NIAH (20k), Prompt Injection, Sycophancy
โ”‚   โ”œโ”€โ”€ benchmark_latency.py     # Concurrency (1, 4, 8, 16) & 32k-Token Memory Stress Benchmark
โ”‚   โ”œโ”€โ”€ compare_sota_models.py   # Baseline vs SLM Win-Condition Comparator
โ”‚   โ”œโ”€โ”€ verify.py                # Master Automated Verification Orchestrator & Markdown Generator
โ”‚   โ”œโ”€โ”€ verify.sh                # Full-Precision BF16 Verification Runner
โ”‚   โ”œโ”€โ”€ verify_edge.sh           # Quantized GGUF Q4_K_M Edge Verification Runner
โ”‚   โ”œโ”€โ”€ holdout_policies/        # Statistically balanced holdout evaluation datasets
โ”‚   โ”œโ”€โ”€ benchmarks/              # Expanded adversarial & security benchmark vectors
โ”‚   โ””โ”€โ”€ reports/                 # JSON and Markdown certification scorecards
โ”‚
โ”œโ”€โ”€ scripts/                     # Operational Lifecycle & Automation Scripts
โ”‚   โ”œโ”€โ”€ install.sh               # DGX Spark GB10 Hardware Provisioning & Source Compilation
โ”‚   โ”œโ”€โ”€ 01_prepare_data.sh       # Stage 1: Vector DB, GAN Forge & Dataset Formatting
โ”‚   โ”œโ”€โ”€ 02_train_models.sh       # Stage 2: VRAM Airlock & Unsloth Training Loops
โ”‚   โ”œโ”€โ”€ 03_evaluate_models.sh    # Stage 3: Master Certification Pipeline Runner
โ”‚   โ”œโ”€โ”€ evaluate.sh              # Direct shortcut alias for Stage 3 evaluation
โ”‚   โ””โ”€โ”€ check_data_leakage.py    # MinHash 7-gram Jaccard Contamination Firewall
โ”‚
โ””โ”€โ”€ models/                      # Checkpoints, LoRA Adapters, Merged Safetensors & GGUF Artifacts

3. Hardware & Provisioning Pipeline

Engineered natively for NVIDIA DGX Spark (Grace-Blackwell GB10, aarch64, CUDA 13.0, compute capability sm_121 targeting sm_120 forward compatibility).

3.1 Strict Version Lock Matrix (ml/requirements.txt)

Because Unsloth enforces strict compatibility bounds, dependencies are pinned:

  • PyTorch: torch==2.11.0 (cu130)
  • Transformers: transformers==5.5.0 (Hard cap for Unsloth)
  • TRL: trl==0.24.0 (Stable CPOConfig with native SimPO loss)
  • Accelerate: accelerate==1.10.0
  • PEFT: peft==0.18.1 (Patched for TorchAO quantization dispatch)
  • TorchAO: torchao>=0.13.0
  • Unsloth: unsloth==2026.8.15, unsloth_zoo==2026.8.10

3.2 Deterministic Compilation (ml/scripts/install.sh)

Standard pip install fails on aarch64 CUDA 13.0 environments. ml/scripts/install.sh provisions the system in 5 phases:

  1. Phase 1: Build Essentials & Base Packages: Builds ninja, cmake, and foundation tools.
  2. Phase 2: Xformers Source Build: Compiles v0.0.30 with MAX_JOBS=8 without build isolation.
  3. Phase 3: Flash-Attention Source Build: Compiles v2.6.3 with MAX_JOBS=4 to avoid host RAM exhaustion.
  4. Phase 4: Llama-CPP-Python: Builds CUDA GGML kernels (-DGGML_CUDA=on -DCMAKE_CUDA_ARCHITECTURES=120).
  5. Phase 5: Unsloth & BitsAndBytes (--no-deps): Ingests pre-built aarch64 wheels with strict --no-deps to preserve compiled PyTorch bindings.

4. Stage 1: SOTA Data Engineering & Synthesis

4.1 Legal-Aware Hybrid RAG Engine (build_vector_db.py)

Legal statutory retrieval requires exact keyword matching for specific statutory clauses (e.g., "Section 8(1)(a)", "Rule 13(4)") and dense semantic understanding for conceptual queries (e.g., "consent mechanics for minor data processing").

User Legal Query
      โ”‚
      โ”œโ”€โ”€โ–บ BM25 Legal Tokenizer (Statutory Entity Preservation) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
      โ”‚                                                                 โ”œโ”€โ–บ RRF Score Fusion โ”€โ–บ BGE-Reranker-v2-m3 โ”€โ–บ Top-3 Grounding Chunks
      โ””โ”€โ”€โ–บ Dense Semantic Embedding (BAAI/bge-small-en-v1.5 + L2 Norm) โ”€โ”˜
  1. Custom Statutory Tokenizer: Collapses multi-word statutory terms ("data fiduciary" -> "data_fiduciary", "Section 8(1)" -> "section_8_1") to prevent Inverse Document Frequency (IDF) dilution in BM25.
  2. Dense Vector Embeddings: Encodes 800-character chunks with 80-character sliding overlaps using BAAI/bge-small-en-v1.5. Unpolluted chunk vectors with L2 unit-norm normalization prevent dense space drift.
  3. Cross-Encoder Re-Ranking: RRF candidate pools (Top 50 Lexical + Top 50 Dense) are fused and re-ranked using BAAI/bge-reranker-v2-m3 across the top 30 candidates, achieving $\ge 98%$ Recall@3 and $\ge 0.95$ NDCG@3.
  4. Serialized Persistence: The complete hybrid index is serialized into ml/data-forge/dpdp_hybrid_index.pkl, eliminating database concurrency deadlocks during distributed training.

4.2 Teacher-Student Synthetic GAN Forge (gan_forge.py)

Generates 10,000+ synthetic corporate privacy policies with labeled ground-truth violations across 8 statutory categories:

  • Teacher Model: Qwen2-72B-Instruct-FP8 running on vLLM.
  • Chosen ($y_w$): Perfect forensic audits strictly adhering to dpdp_schema.json, citing exact statutory sections, with 0% hallucination and verbatim quotes.
  • Rejected ($y_l$): Plausible but legally flawed audits exhibiting subtle legal hallucinations, non-existent sections, or loose quote attribution.

4.3 Deterministic Statutory Decision Tree (build_dpdp_tree.py)

Extracts the DPDP Act 2023 & Rules 2025 text into an Abstract Syntax Tree (AST). The decision tree compiles into dpdp_act_tree.json and is utilized by the upstream Rust core engine (libs/rust-utils) for sub-millisecond heuristic screening.

4.4 Unsloth Dataset Formatting & Leaky-Split Firewall (prepare_unsloth_data.py)

  • ChatML Normalization: Formats conversations strictly using native tokens (<|im_start|>system...).
  • Leaky-Split Protection: Groups train/eval splits by source_document_id rather than individual rows, guaranteeing that the evaluation split tests true generalization.

5. Stage 2: The Training Loop & Alignment Architecture

5.1 VRAM Airlock & Hardware Isolation

Before initializing the PyTorch context, ml/scripts/02_train_models.sh executes the VRAM Airlock:

  1. Terminates lingering Ray background workers and zombie vLLM processes (pkill -9 -f vllm).
  2. Kills orphaned PyTorch multiprocessing workers (multiprocessing.spawn).
  3. Flushes /dev/shm/* POSIX shared memory allocations.
  4. Executes OS cache synchronization and memory dropping (echo 3 > /proc/sys/vm/drop_caches).
  5. Enforces multiprocessing.set_start_method("spawn") and TOKENIZERS_PARALLELISM=false.

5.2 Phase 1: Supervised Fine-Tuning (SFT) with rsLoRA

Trains the base student model (Qwen2.5-7B-Instruct) to master statutory reasoning and JSON schema adherence.

  • Rank-Stabilized LoRA (rsLoRA): Traditional LoRA scales adapters by $\frac{\alpha}{r}$, collapsing optimization stability at high ranks ($r \ge 64$). rsLoRA scales adapters by $\gamma = \frac{\alpha}{\sqrt{r}}$, mathematically ensuring stable gradient updates regardless of rank:
    model = FastLanguageModel.get_peft_model(
        model,
        r=128,
        target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
        lora_alpha=32,
        lora_dropout=0.0,  # Fused Triton kernel optimization
        bias="none",
        use_gradient_checkpointing="unsloth",
        use_rslora=True,
    )
    
  • Response-Only Loss Masking (train_on_responses_only): Computes cross-entropy loss exclusively on the assistant's completion tokens. Setting prompt tokens to label = -100 prevents wasting parameter capacity on memorizing long statutory prompts.
  • Optimizer Configuration: 32-bit FP32 adamw_torch states (weight_decay=0.05, linear warmup for 10% of steps, cosine learning rate decay).

5.3 Phase 2: Simple Preference Optimization (SimPO)

Standard Direct Preference Optimization (DPO) requires maintaining a frozen reference model in VRAM, doubling memory consumption and causing token length bias.

Ssense implements SimPO (Simple Preference Optimization) via TRL's CPOTrainer, which optimizes preferences without a reference model using length-normalized implicit rewards:

LSimPO(ฮธ)=โˆ’E(x,yw,yl)[logโกฯƒ(ฮฒโˆฃywโˆฃlogโกฯ€ฮธ(ywโˆฃx)โˆ’ฮฒโˆฃylโˆฃlogโกฯ€ฮธ(ylโˆฃx)โˆ’ฮณ)]\mathcal{L}_{\text{SimPO}}(\theta) = -\mathbb{E}_{(x, y_w, y_l)} \left[ \log \sigma \left( \frac{\beta}{|y_w|} \log \pi_\theta(y_w | x) - \frac{\beta}{|y_l|} \log \pi_\theta(y_l | x) - \gamma \right) \right]

Where:

  • $\pi_\theta(y | x)$ is the policy model's likelihood.
  • $|y|$ is sequence length (enforcing length penalty).
  • $\gamma > 0$ is the target reward margin enforcing strict separation between chosen ($y_w$) and rejected ($y_l$) completions.
  • $\beta$ is the reward scaling temperature.
cpo_config = CPOConfig(
    loss_type="simpo",
    beta=2.0,        # Aggressive penalty for legal hallucinations
    simpo_gamma=0.5, # Target margin
    learning_rate=5e-6,
    lr_scheduler_type="cosine",
    optim="adamw_torch",
    bf16=True,
    max_length=8192,
)

5.4 Model Specialization Matrix

Architectural Parameter Forensic Legal Auditor (train_audit.py) Conversational Chatbot (train_chatbot.py)
Base Architecture Qwen2.5-7B-Instruct Qwen2.5-7B-Instruct
Context Window ($N_{\text{ctx}}$) 8,192 tokens 4,096 tokens
Max Prompt Budget 7,000 tokens (Pre-filtered) 3,000 tokens (Pre-filtered)
LoRA Rank ($r$) / Alpha ($\alpha$) $r=128, \alpha=32$ (rsLoRA) $r=64, \alpha=16$ (rsLoRA)
Target Modules All 7 Linear Projection Layers All 7 Linear Projection Layers
SFT Learning Rate / Epochs $2\times 10^{-4}$ (3 Epochs) $2\times 10^{-4}$ (3 Epochs)
SimPO Learning Rate / Epochs $5\times 10^{-6}$ (1 Epoch) $5\times 10^{-6}$ (1 Epoch)
SimPO Hyperparameters $\beta = 2.0, \gamma = 0.5$ (Strict Hallucination Block) $\beta = 1.0, \gamma = 0.3$ (Fluidity)
Output Contract Strict dpdp_schema.json compliant JSON Natural Language Markdown + Citation Tags
Primary Deployment Role Automated Corporate Audit Engine Interactive Citizen Privacy Assistant

5.5 Triple-Format Export Engine

Upon convergence, train_audit.py and train_chatbot.py automatically export models into three distinct formats:

  1. LoRA Adapters (adapter_model.safetensors): For dynamic multi-LoRA multiplexing under vLLM server deployments (~250 MB).
  2. Merged 16-Bit Safetensors (model.safetensors): Standalone full-precision BF16 weights for offline batch evaluation and zero-latency serving (~15 GB).
  3. GGUF Q4_K_M Edge Weights (model.Q4_K_M.gguf): Quantized weights for local edge execution via llama.cpp and our Rust Native Daemon (apps/native-daemon) (~4.5 GB).

6. Stage 3: Functional, Adversarial & SOTA Certification

The evaluation suite (ml/evals/) operates as the automated gating authority. No model advances to production without navigating the 18 threshold gates orchestrated by verify.py.

6.1 Polymorphic Backend Loader (backend_loader.py)

Dynamically wraps target models behind a unified inference interface:

  • Unsloth In-Memory: Direct CUDA inference with Triton fused attention kernels.
  • vLLM Multi-LoRA: High-throughput async REST client.
  • Llama.cpp Edge: GGUF quantized execution for local edge benchmarking.
  • CUDA Memory Guard: Explicitly strips arbitrary max_length generation kwargs, preventing C++ KV Cache out-of-bound writes (CUDA error: an illegal memory access).
  • VRAM Fragmentation Airlock: Automatically unloads the audit_engine from VRAM and flushes PyTorch allocators before initializing chat_engine during security evals.

6.2 Statistical Power & Wilson 95% Confidence Interval Gating

To eliminate the "Small-N Fallacy", rate-based metrics are strictly gated against the Wilson Score 95% Confidence Interval Lower Bound:

p^lower=p^+z22nโˆ’zp^(1โˆ’p^)n+z24n21+z2n\hat{p}_{\text{lower}} = \frac{\hat{p} + \frac{z^2}{2n} - z \sqrt{\frac{\hat{p}(1-\hat{p})}{n} + \frac{z^2}{4n^2}}}{1 + \frac{z^2}{n}}

Where:

  • $\hat{p} = \frac{k}{n}$ (Sample success proportion)
  • $n$ = Total test vectors
  • $z = 1.95996$ (Critical value for 95% two-sided confidence)

6.3 The 18-Axis Master Certification Matrix

# Benchmark Suite Evaluation Metric Label Extraction Key Target Gate Statistical Gate Type
1 Grammar Evals Pillar 1: Schema Compliance Rate (%) grammar.schema_compliance_rate $\ge 98.0%$ Point Estimate
2 Accuracy Evals Pillar 2: Severity-Weighted Violation F1 accuracy.severity_weighted_violation_f1 $\ge 0.88$ Point Estimate
3 Accuracy Evals Pillar 3: Trust Score MAE (pts) accuracy.trust_score_mae $\le 8.5$ pts Point Estimate
4 Accuracy Evals Pillar 4: Evidence Quote Hallucination (%) accuracy.evidence_quote_hallucination_rate $\le 0.0%$ Wilson Upper Bound
5 Grammar Evals Pillar 5: Average Inference Latency (ms) grammar.average_latency_ms $\le 1200$ ms Telemetry P95
6 Chatbot Evals Chatbot: Statutory Accuracy Rate (%) chatbot.statutory_accuracy_rate $\ge 95.0%$ Wilson Lower Bound
7 Chatbot Evals Chatbot: Vocabulary Diversity (TTR/MTLD) chatbot.mtld_lexical_diversity $\ge 0.45$ Point Estimate
8 Chatbot Evals Chatbot: Schema & Preamble Bleed Rate (%) chatbot.schema_preamble_bleed_rate $\le 0.0%$ Wilson Upper Bound
9 Red-Team Evals Red-Team: Statutory Trap Resistance (%) hallucination.statutory_trap_resistance_rate $\ge 95.0%$ Wilson Lower Bound
10 Security Evals Adversarial: NIAH 20k-Token Middle Recall (%) security.niah_context_recall_rate $= 100.0%$ Wilson Lower Bound
11 Security Evals Adversarial: Prompt Injection Refusal (%) security.prompt_injection_refusal_rate $\ge 95.0%$ Wilson Lower Bound
12 Security Evals Adversarial: Anti-Sycophancy Correction (%) security.sycophancy_correction_rate $\ge 95.0%$ Wilson Lower Bound
13 Security Evals Adversarial: JSON Fuzzing Resilience (%) security.json_fuzzing_resilience_rate $\ge 95.0%$ Wilson Lower Bound
14 Hybrid RAG SOTA RAG: Recall@3 Rate (%) sota_hybrid_rag.recall_at_3 $\ge 95.0%$ Point Estimate
15 Hybrid RAG SOTA RAG: NDCG@3 Ranking Quality sota_hybrid_rag.ndcg_at_3 $\ge 0.90$ Point Estimate
16 Chatbot SOTA SOTA Chatbot: Statute Citation Precision (%) chatbot_sota.statute_citation_precision $\ge 90.0%$ Point Estimate
17 Chatbot SOTA SOTA Chatbot: Context Faithfulness Score (1-5) chatbot_sota.context_faithfulness_score $\ge 4.5$ 72B Judge Score
18 Chatbot SOTA SOTA Chatbot: Jurisdictional Contamination (%) chatbot_sota.jurisdictional_contamination_rate $\le 0.0%$ Wilson Upper Bound

6.4 MinHash 7-Gram Data Contamination Firewall (check_data_leakage.py)

To prevent test set memorization from contaminating training checkpoints, check_data_leakage.py executes a MinHash Jaccard similarity audit across all 320 evaluation test vectors against the entire ml/slm-training/data/ corpus:

  • 7-Gram Shingling: Tokenizes texts into sliding 7-word windows to ignore generic legal boilerplate while capturing verbatim sentence duplication.
  • 128-Permutation MinHash Signatures: Approximates pairwise Jaccard similarity in $O(N)$ time.
  • Hard-Fail Threshold: Hard-fails (exit code 1) if any test vector exhibits Jaccard Similarity $> 0.85$ against any training sample.

7. Stage 4: Unified Hugging Face Mono-Repo Deployment

Rather than isolating code and models in fractured repositories, the entire ml/ subsystem serves as a centralized Git LFS Mono-Repo on Hugging Face (PRiyanshu0-1/DPDP-SSense).

  • LFS Interception: Strict .gitattributes routing forces all *.safetensors, *.gguf, *.pkl, and *.pdf files through Git LFS, entirely circumventing standard Git push size limits.
  • Artifact Distillation: Checkpoint optimizer states (*.pt) and raw GAN generation outputs are firewalled via .gitignore.
  • Triple-Export Delivery: Hosts the PEFT Adapters (250 MB), the merged native BF16 Safetensors (15 GB), and the high-efficiency Q4_K_M GGUF binaries for edge serving.

8. Quickstart & Inference Guide

8.1 In-Memory Inference via Transformers / Unsloth

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

model_id = "PRiyanshu0-1/DPDP-SSense"
# Subfolder: "models/audit-model-final" or "models/chatbot-model-final"

tokenizer = AutoTokenizer.from_pretrained(model_id, subfolder="models/chatbot-model-final")
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    subfolder="models/chatbot-model-final",
    torch_dtype=torch.bfloat16,
    device_map="auto"
)

prompt = """<|im_start|>system
You are an expert Indian DPDP Legal Assistant.<|im_end|>
<|im_start|>user
What are the statutory duties of a Data Principal under Section 15 of the DPDP Act 2023?<|im_end|>
<|im_start|>assistant
"""

inputs = tokenizer([prompt], return_tensors="pt").to("cuda")
outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.1)
print(tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True))

8.2 Edge Inference via llama-cpp-python (GGUF)

from llama_cpp import Llama

# Load quantized GGUF model directly
llm = Llama(
    model_path="ml/models/chatbot-model-final/model.Q4_K_M.gguf",
    n_ctx=4096,
    n_gpu_layers=-1 # Offload all layers to GPU
)

prompt = """<|im_start|>system
You are an expert Indian DPDP Legal Assistant.<|im_end|>
<|im_start|>user
Can a Data Fiduciary transfer personal data outside India under the DPDP Act 2023?<|im_end|>
<|im_start|>assistant
"""

response = llm(prompt, max_tokens=512, temperature=0.1, stop=["<|im_end|>"])
print(response["choices"][0]["text"])

9. Operational Runbook (Step-by-Step DGX Execution)

Follow these step-by-step commands to provision, synthesize, train, and certify the models:

Step 1: Provision the GB10 / CUDA 13.0 Environment

# Execute from the project root
bash ml/scripts/install.sh

Step 2: Ingest Law, Build Hybrid DB & Synthesize Training Data

bash ml/scripts/01_prepare_data.sh

Step 3: Run the Contamination Firewall Check

python ml/scripts/check_data_leakage.py --threshold 0.85

Step 4: Execute SFT + SimPO Model Training

# Train both Forensic Auditor and Conversational Chatbot sequentially (default)
bash ml/scripts/02_train_models.sh

# Or train individually:
bash ml/scripts/02_train_models.sh --model audit
bash ml/scripts/02_train_models.sh --model chatbot

Step 5: Execute the Master 18-Axis Certification Suite

# Certify merged 16-bit safetensors in-memory via Unsloth:
bash ml/scripts/03_evaluate_models.sh --backend unsloth

# Or certify via high-throughput production vLLM endpoint:
bash ml/scripts/03_evaluate_models.sh --backend vllm --vllm-url http://localhost:8000/v1/completions

# Or evaluate quantized GGUF Q4_K_M edge models via llama.cpp:
cd ml/evals && bash verify_edge.sh

Step 6: Deploy Unified Mono-Repo to Hugging Face

cd ml
git add .
git commit -m "feat(ml): release production DPDP-SSense SLMs and certified hybrid RAG"
git push -u hf main

Built with โš–๏ธ by Ssense Engineering
Certified against the Digital Personal Data Protection Act 2023 & DPDP Rules 2025
Downloads last month
-
GGUF
Model size
8B params
Architecture
qwen2
Hardware compatibility
Log In to add your hardware

4-bit

Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support

Model tree for PRiyanshu0-1/DPDP-SSense

Base model

Qwen/Qwen2.5-7B
Quantized
(390)
this model