VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 15: The ML coding round

Lesson 15.2 of 12 in this module Unrolled Pro

Cosine-similarity search

Return the top k corpus embeddings most similar to each query by cosine similarity, ranked. From loops that recompute every vector norm for every pair and sort all n scores (O(m · n · d) interpreted work plus O(n log n) per query), to normalising the corpus once, one matrix product for all scores, and argpartition followed by sorting only the k winners (O(n + k log k) per query). Then zero vectors, metadata filters, memory with float16 and int8, approximate indexes, and finding near-duplicate examples in a dataset.

about 10 min 3 Python examples 2 quick checks

Problem. Given a corpus of n embeddings E (n × d) and m query embeddings Q (m × d), return for each query the indices of the k corpus vectors with the highest cosine similarity, in descending order of similarity.

text
cos(q, e) = (q · e) / (‖q‖ · ‖e‖)        ranges from −1 (opposite) to 1 (same direction)

E = [[1, 0], [0, 1], [1, 1], [−1, 0]],   Q = [[2, 1]],   k = 2   →   [[2, 0]]
         cos: 0.894, 0.447, 0.949, −0.894

Cosine similarity ignores length and compares direction, which is what embedding models encode meaning in. The computation is a dot product divided by two norms. The optimisations: compute the corpus norms once (not once per query), do all dot products as one matrix product, and only sort the k scores you return.

ML context. This is the retrieval step of RAG, semantic search, recommendation by item embeddings and few-shot example selection: embed the query, find the nearest corpus vectors. Vector databases implement exactly this, with approximate indexes once n is large (Change C). Run on the corpus against itself, it finds near-duplicates in a training set (Change D).


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Cosine-similarity search"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee