Epic: Self-Learning WiFi AI — Adaptive Recognition, Optimization & Anomaly Detection (ADR-024) #50
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Introduction
What is this?
WiFi signals carry far more information than connectivity status. When WiFi Channel State Information (CSI) — the 56-dimensional complex-valued subcarrier measurements that every modern WiFi chipset produces — passes through a room, it encodes the geometry, the people, and the activity happening in that space. Our CsiToPoseTransformer (ADR-023) already learns to decode this signal into 17-keypoint human body poses.
But there is a problem: the rich internal representations this model learns are thrown away after each inference. The transformer's GNN produces 17 body-part feature vectors (each 64-dimensional) that capture nuanced information about the WiFi environment and the people in it — then discards them to output only xyz coordinates. These features could power room identification, person re-identification, activity classification, anomaly detection, and cross-environment transfer learning — if only we could extract, compare, and index them.
This issue proposes adding a contrastive embedding capability to the existing transformer backbone. Rather than building a new model from scratch, we attach a lightweight projection head (~25K parameters) that maps the GNN internal features into a 128-dimensional embedding space suitable for similarity search, HNSW indexing, and cross-modal alignment. The total model remains under 60K parameters — 60 KB at INT8 — comfortably fitting on an ESP32 microcontroller.
Why contrastive learning, not a generative "LLM" approach?
CSI data is 56 continuous-valued floats sampled at 20 Hz — not discrete tokens. Autoregressive generation is architecturally mismatched, 500x more expensive per inference, and cannot fit on edge hardware. The WiFi sensing literature (SelfHAR, SignFi, Wang et al. 2023) unanimously uses contrastive or masked objectives for CSI representation learning. See ADR-024 Section 6 for the full analysis.
Features and Benefits
Self-Supervised Pretraining (No Labels Required)
Train the embedding backbone from any raw WiFi CSI stream — no cameras, no annotations, no paired data. SimCLR-style contrastive learning with 5 physically-motivated augmentations (temporal jitter, subcarrier masking, Gaussian noise, phase rotation, amplitude scaling) teaches the model what makes two CSI observations "similar" without human supervision. This means:
Universal WiFi Fingerprinting
The 128-dim L2-normalized embeddings serve as compact, comparable fingerprints for any WiFi observation. Two CSI frames from the same room will have high cosine similarity; frames from different rooms will be distant. This enables:
HNSW-Indexed Similarity Search
Embeddings feed directly into HNSW vector indices (ADR-004) for sub-millisecond nearest-neighbor retrieval:
env_fingerprintactivity_patterntemporal_baselineperson_trackCross-Environment Transfer
Contrastive pretraining on diverse environments produces embeddings that capture environment-invariant features. A model pretrained on 100 rooms adapts to room 101 with just minutes of unlabeled data, compared to hours of labeled data for training from scratch.
Dual-Purpose Single Forward Pass
The same model simultaneously produces:
No additional inference cost — both outputs share the same backbone computation.
Edge-Deployable
ESP32 SRAM: 520 KB. Model at INT8: 60 KB = 11.5% of available memory. Inference: <2ms per frame at 20 Hz.
Capabilities
Core Embedding Pipeline
Training Modes
Mode 1: Self-Supervised Pretraining (SimCLR)
Mode 2: Supervised Fine-Tuning
Mode 3: Cross-Modal Alignment (optional)
API Endpoints
POST /api/v1/embedding/extract— Extract embedding from CSI framePOST /api/v1/embedding/search— HNSW nearest-neighbor queryPOST /api/v1/embedding/index— Add embedding to named indexGET /api/v1/embedding/indices— List active HNSW indicesCLI Extensions
Architecture Decision Record and Domain-Driven Design
ADR-024:
docs/adr/ADR-024-contrastive-csi-embedding-model.mdDomain-Driven Design Alignment
This feature maps to three bounded contexts in the WiFi-DensePose domain:
1. Representation Learning Context (new)
EmbeddingExtractor— owns the projection head and produces embeddingsCsiEmbedding(128-dim vector),EmbeddingConfig,AugmentationParamsEmbeddingExtracted,PretrainEpochComplete,IndexUpdatedSEG_EMBED = 0x0Cfor model persistence2. Signal Processing Context (existing, extended)
CsiToPoseTransformer— extended withembed()method exposingbody_part_features3. Fingerprint Search Context (new, fulfills ADR-004)
FingerprintIndex— owns HNSW index lifecycleIndexEntry(embedding + metadata + timestamp)SearchResult(neighbor + distance + metadata)IndexBuilt,NeighborFound,AnomalyDetectedContext Map
Key Design Decisions in ADR-024
Implementation Phases
Phase 1: Embedding Module
embedding.rsProjectionHeadstruct (2-layer MLP with L2 normalization)InfoNceLossfunction (cosine similarity matrix + cross-entropy)CsiAugmenterwith 5 augmentation strategiesEmbeddingExtractorwrapping transformer + projection headCsiToPoseTransformer::embed()method exposing body_part_featuresPhase 2: Self-Supervised Pretraining
Trainer::pretrain_epoch()with SimCLR objectivePhase 3: HNSW Fingerprint Integration
EmbeddingExtractoroutput to HNSW indexenv_fingerprint,activity_pattern,temporal_baseline,person_trackPOST /api/v1/embedding/search--embed,--build-indexPhase 4: Cross-Modal Alignment (optional)
PoseEncoder(Linear 51 to 128 to 128)Phase 5: Quantized Embedding Validation
SEG_EMBEDsegmentTotal: ~1,150 lines of Rust across 5 phases
Acceptance Criteria
embedding.rsmodule with ProjectionHead, InfoNceLoss, CsiAugmenter, EmbeddingExtractorReferences
docs/adr/ADR-024-contrastive-csi-embedding-model.mdImplementation Progress — Branch
feat/adr-024-contrastive-csi-embeddingCommit
5942d4ddimplements Phases 1–2 and partial Phase 3 of the AETHER plan. Here's the updated checklist:Phase 1: Embedding Module
embedding.rs— COMPLETE ✅ProjectionHeadstruct (2-layer MLP with L2 normalization)InfoNceLossfunction (cosine similarity matrix + cross-entropy)CsiAugmenterwith 5 augmentation strategies (temporal jitter, subcarrier masking, Gaussian noise, phase rotation, amplitude scaling)EmbeddingExtractorwrapping transformer + projection headCsiToPoseTransformer::embed()method exposingbody_part_featuresembedding.rs)909 lines in
embedding.rs— zero external ML dependencies, puref32arithmetic.Phase 2: Self-Supervised Pretraining — COMPLETE ✅
Trainer::pretrain_epoch()with SimCLR objective+209 lines in
trainer.rswith contrastive pretraining loop.Phase 3: HNSW Fingerprint Integration — PARTIAL 🔄
FingerprintIndexbrute-force implementation (HNSW-compatible interface)env_fingerprint,activity_pattern,temporal_baseline,person_track--pretrain,--pretrain-epochs,--embed,--build-indexPOST /api/v1/embedding/search+221 lines in
main.rswith full CLI integration.Phase 4: Cross-Modal Alignment — COMPLETE ✅
PoseEncoder(Linear 51 → 128 → 128)Phase 5: Quantized Embedding Validation — NOT STARTED ⬜
SEG_EMBEDsegmentRVF Container — PARTIAL 🔄
SEG_EMBED = 0x0Csegment type definedrvf_container.rsSummary
f32arithmeticRemaining work: REST API endpoints for embedding search, production HNSW integration, INT8 quantization validation, and ESP32 benchmarking.
Implementation Progress Update
Phases 1-5: COMPLETE (260 tests passing)
Branch:
feat/adr-024-contrastive-csi-embeddingFiles created/modified (2,526 lines added):
embedding.rs(NEW)graph_transformer.rsembed()method to CsiToPoseTransformertrainer.rscontrastiveto loss structs,pretrain_epoch()methodrvf_container.rsSEG_EMBED(0x0C),add_embedding()/embedding()methodsmain.rs--pretrain,--pretrain-epochs,--embed,--build-indexCLI flagslib.rspub mod embedding;README.mdADR-02421 new tests added:
embedding.rs: 17 tests (projection head, InfoNCE, augmenter, extractor, fingerprint index, pose encoder, cross-modal, quantization)trainer.rs: 2 tests (pretrain epoch, contrastive weight)rvf_container.rs: 1 test (embedding segment roundtrip)Phase Checklist
CsiToPoseTransformer::embed()pretrain_epoch()with SimCLR objective, contrastive loss in composite--pretrain,--embed,--build-index), Spearman rank validationADR-024 Updated
Phase 7 (Deep RuVector Integration) promoted from Future Work to committed implementation phase:
Test Results
PR will be created once Phase 7 completes (~274+ tests expected).
✅ Implementation Complete — All 7 Phases Delivered
PR: #52 (
feat/adr-024-contrastive-csi-embedding)Tests: 272 passing (189 lib + 49 bin + 16 rvf + 18 vitals)
Branch: 2 commits ahead of main
Phase Completion Summary
Key Deliverables
pretrain_epoch(),consolidate_pretrained(), EWC penalty computationembed()method returning body-part features without regression heads--pretrain,--pretrain-epochs,--embed,--build-indexCLI flags wired end-to-endRuVector Integration (Phase 7 Highlights)
Edge Deployment
PR #52 is ready for review. Merging will auto-close this issue.