<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rithvik Panchapakesan on Medium]]></title>
        <description><![CDATA[Stories by Rithvik Panchapakesan on Medium]]></description>
        <link>https://medium.com/@rithvikp_?source=rss-5b66381327a5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9nYUMyGJTL8kuank98GEaA.jpeg</url>
            <title>Stories by Rithvik Panchapakesan on Medium</title>
            <link>https://medium.com/@rithvikp_?source=rss-5b66381327a5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 04:13:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rithvikp_/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The Art of Efficient Search: Scalar Quantization and Vectors]]></title>
            <link>https://medium.com/snowflake/the-art-of-efficient-search-scalar-quantization-and-vectors-f912fd2e9bb7?source=rss-5b66381327a5------2</link>
            <guid isPermaLink="false">https://medium.com/p/f912fd2e9bb7</guid>
            <category><![CDATA[performance-optimization]]></category>
            <category><![CDATA[retrieval-augmented-gen]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[vector-search]]></category>
            <category><![CDATA[snowflake]]></category>
            <dc:creator><![CDATA[Rithvik Panchapakesan]]></dc:creator>
            <pubDate>Mon, 22 Apr 2024 15:51:29 GMT</pubDate>
            <atom:updated>2024-04-22T15:51:29.639Z</atom:updated>
            <content:encoded><![CDATA[<p>Coauthored with <a href="https://medium.com/@danmei.xu_78667">Danmei Xu</a></p><p>Searching over unstructured data is a fundamental building block of many computer applications, from web search to retrieval-augmented generation (RAG) for chatbots! One of the most popular avenues for powering semantic search are <em>embeddings</em>, which try to distill complex data (in most search use-cases, text) into relatively compact <em>vectors of floating point numbers</em> that facilitate comparison between different data points. In previous blog posts, Snowflake AI Research, has dived into <a href="https://medium.com/snowflake/how-to-build-a-state-of-the-art-text-embedding-model-a8cd0c86a19e">how to create great embeddings</a> and even launched a <a href="https://www.snowflake.com/blog/introducing-snowflake-arctic-embed-snowflakes-state-of-the-art-text-embedding-family-of-models/">state-of-the-art family of embedding models</a> called <a href="https://huggingface.co/collections/Snowflake/arctic-embed-661fd57d50fab5fc314e4c18">arctic-embed</a>. But creating great embedding is not enough, we have to efficiently search over them too! This article dives into some compression techniques we are exploring to make embedding search efficient.</p><h3>Background</h3><p>Researchers are looking into both how to generate more representative embeddings and how to generate and search over these embeddings faster. The classic approach is to build a large, spatial data structure capable of generating <em>approximate nearest neighbors </em>given a query vector and a large set of embedding vectors. To catch a glimpse of the state of the art in this vibrant research area, please see this <a href="https://neurips.cc/virtual/2023/competition/66587">competition at NeurIPS 2023</a>. However, such data structures can be expensive to pre-compute and update, especially given complex query filters. For some applications, especially those with small corpuses, we simply need to search over a flat data structure.</p><p>This article focuses on this search problem where we have no spatial acceleration data structure and must search over a flat list of embeddings. For illustrative purposes, consider how to iterate over 1M vectors in memory as fast as possible. What tricks can we play and how fast can we go without affecting search quality? We’re going to dive into a solution called quantization and look at how it affects the accuracy and performance of vector search.</p><h3>Why is flat embedding search hard?</h3><p>By default, vectors are large and need serious engineering work to handle. Let’s consider representing vector elements as 32-bit floats, a common data type used for floating-point operations in general-purpose programming languages. A single 768-dimensional vector, which is the size of vectors produced by models such as e5-base-v2, will take up 768 × 4 bytes. This means that 1 million vectors will take up a little over 3 GB! Assuming a compute-optimized (vectorized/SIMD) search, if we try to just iterate through all our vectors and compute distances to find the closest match to some query vector, our distance computation is not even the bottleneck! In fact, we are memory bottlenecked in this situation: we are limited by the speed at which our machine’s memory can feed our CPU data to process. If our vectors were on disk, we’d be bottlenecked even further. We performed profiling to validate this theory and saw results that supported our hypothesis. Moreover, while system memory has an overall bandwidth limitation, individual cores only see a portion of that, which was reflected in our single-core results being slower than the multi-core ones, even if we weren’t technically CPU-bottlenecked! Now, when searching over vectors, you often don’t have to look at every single vector. The aforementioned spatial indices, as well as other, user-specified filters, allow us to prune the space of what we look at. However, even with these techniques, you may still have to traverse over many vectors, and their large sizes will add latency.</p><h3>Scalar quantization to the rescue</h3><p>If we were truly memory bottlenecked, reducing the amount of data we have to traverse when searching would directly lead to performance gains. One of the ways of doing that is with quantization. Quantization at its core, projects the elements of a vector onto a smaller set of values, reducing the size of each element. One form of quantization is called scalar quantization, which we’re going to explore today. Given we have a set of vectors to quantize, we can compute a scalar minimum and maximum value based on their elements (these two values could also be sampled or computed on another, representative, dataset). It may be helpful to ignore outliers when performing this computation.</p><p>Given this minimum and maximum, if we’re quantizing our 32 bit floats into 8 bit unsigned integers (uint8s), we’ll divide the range min–max into 256 (derived from the domain of uint8s) buckets and place our values into those. For each element of an original vector, the corresponding bucket’s index becomes the new element value in the quantized vector. Just like that, we’ve mapped each of our 32 bit elements to a corresponding value in [0, 255]. Stepping back, this process performs a type of “projection”: we’re coarsely reducing precision by assigning contiguous ranges of 32-bit float values to a single uint8. Elements which were similar in the original vector may now be exactly equal in the quantized vector, but elements with large disparities will likely still be different. Below is a diagram that illustrates this mapping, including the clipping of outliers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2NoGwbwBQ6klPGps" /></figure><p>Note that in order to reconstruct the approximate distance value that would have resulted from comparing the original 32-bit vectors, we will have to compute our distances with a different formula that takes our quantization into account. Our optimization also does come with costs: we lose information about the vector when performing this projection because we’re taking a number that could be in a very large domain and mapping it onto one of only 256 values. Our embeddings search could thus be more noisy, returning sub-optimal results. However, the benefit is a 4x reduction in the size of our vectors! Moreover, as we’ll explore later, the quality hit (ie. the observed degradation of the results returned when searching) is actually minimal as well! With these 8-bit elements, 1 million vectors now only take up around 0.77 GB. When performing an exhaustive search on an R6 instance on EC2, our search time drops from ~250 milliseconds to 80 milliseconds. Because of various overheads, we don’t see the full 4x drop, but we still observe a 3x speedup.</p><p>We experimented with uint8 quantization using both the Massive Text Embedding Benchmark (MTEB) and Snowflake’s internal retrieval tasks. First, we generated embeddings for a subset of MTEB retrieval tasks (a large and representative corpus) using the newly-released <a href="https://huggingface.co/Snowflake/snowflake-arctic-embed-m">snowflake-arctic-embed-m</a> model and computed the quantization minimum/maximum values using a simple Snowflake query. For simplicity, we didn’t remove outliers for this experiment.</p><pre>SELECT<br>ARRAY_MIN(V) AS min,<br>ARRAY_MAX(V) AS max<br>-- VECTOR_MAIN column is a array column containing 768-dim of text embeddings<br>-- using model of choice.<br>FROM (SELECT VECTOR_MAIN::ARRAY AS V FROM $TABLE_NAME)</pre><p>Using quantization parameters derived from MTEB, we then evaluated them against 6, out-of-domain, Snowflake-internal retrieval tasks. We used L2 distance to find the top 10 results for each of our queries in these retrieval tasks, without reranking. Given these best results, we then calculated the NDCG@10, which measures how close the first 10 retrieved results are to the ideal ranking of results based on their relevance to the query set. We observed that the impact of quantization on the quality of retrieved results was minimal! Even though we were compressing each of our vectors by a factor of 4, the impact on quality was less than <strong>0.4</strong> <strong>percent</strong>. While this reduction in quality may affect some workloads, for many, it’s well worth the 3x improvement in search speed. Below is a table summarizing our results.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7yl3OMnMLm-PaQalSGxSqA.png" /></figure><h3>Looking forwards</h3><p>These results are very exciting, but there’s still much more to do! There are many other forms of quantization (e.g., binary quantization which maps elements onto 0’s and 1’s) that allow us to explore the tradeoff between vector precision and size. The “best” choice will vary based on the workloads and target quality, but there are a lot of interesting options for us to explore here. In addition, the same benefits of quantization can apply to spatial data structures (ie. vector indices), and there’s a large amount of exploration around the intersection of these techniques. Lastly, while we’ve only covered applying quantization to search, the technique can apply more broadly, including to inference, where there can be significant performance benefits from reducing the size of model weights.</p><h3>Define the future of AI with us</h3><p>Snowflake AI Research is pushing the state-of-the-art so that some of the most consequential businesses and organizations in the world can better understand and interact with their data. We’re <a href="http://snowflake.com/careers">hiring</a>!</p><p>Try out Snowflake’s <a href="https://www.snowflake.com/blog/introducing-snowflake-arctic-embed-snowflakes-state-of-the-art-text-embedding-family-of-models/">state-of-the-art family of embedding models</a> either directly in our model garden, Snowflake Cortex, or by downloading them on HuggingFace at <a href="https://huggingface.co/collections/Snowflake/arctic-embed-661fd57d50fab5fc314e4c18">arctic-embed</a>.</p><h3>Acknowledgements</h3><p>We’d like to thank Adrien Treuille and Nicole Filippelli for their help editing this article, and Asim Shankar for his technical help and guidance.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f912fd2e9bb7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/snowflake/the-art-of-efficient-search-scalar-quantization-and-vectors-f912fd2e9bb7">The Art of Efficient Search: Scalar Quantization and Vectors</a> was originally published in <a href="https://medium.com/snowflake">Snowflake Builders Blog: Data Engineers, App Developers, AI, &amp; Data Science</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>