liveBook · Manning

Quantization

As explained in AI-Powered Search

Quantization is the transformation of numerical values into a smaller set of discrete values to reduce representation precision and resource requirements.

A semantic-search index can become expensive when every document has a large embedding. Quantization reduces the precision of the numbers in those vectors so they take less space and can be cheaper to search. A 1,024-dimensional vector stored as 32-bit floats occupies 4,096 bytes; converting each feature to an 8-bit integer can reduce that storage by 75%. The trade-off is that less precise vectors may change which results are retrieved. section 13.7

Choose a precision and measure the effect rather than assuming compression preserves relevance:

  • Scalar quantization maps each vector value independently to a lower-precision representation. A 16-bit float uses half the space of a 32-bit float; Int8 mapping can compress further, using a range such as −128 to 127. section 13.7.1
  • Compare index size, search time, and recall against a full-precision index. Search a larger candidate set with quantized vectors, then rerank candidates using full-precision vectors when recall needs improvement. section 13.7.1 section 13.7.5

Int8 search for outdoor posts

Generate embeddings for post titles and bodies, build a full-precision FAISS index, and encode queries such as “tent poles” and “hiking trails.” Quantize document embeddings with `quantize_embeddings(doc_embeddings, precision="int8")`; quantize query embeddings with the document embeddings supplied for calibration. In the reported run, the Int8 index occupied 18.91 MB versus 75.6 MB for full precision. Its recall was 0.9289, rising to 1.0 after reranking. Measure speed separately: the reported timings do not establish that Int8 is faster in every run. section 13.7.1

Listing 13.23 Creating an Int8 quantized embeddings index using FAISS
def index_int8_embeddings(doc_embeddings, name):
  int8_embeddings = quantize_embeddings(  #1
                      doc_embeddings, precision="int8")  #1
  print("Int8 embeddings shape:", int8_embeddings.shape)
  index = faiss.IndexFlatIP(int8_embeddings.shape[1]) #2
  index.add(int8_embeddings)  #3
  faiss.write_index(index, name) #4
  return index

int8_index_name = "int8_embeddings"
int8_index = index_int8_embeddings(doc_embeddings, int8_index_name)

quantized_queries = quantize_embeddings(  #5
  query_embeddings,  #5
  calibration_embeddings=doc_embeddings,  #5
  precision="int8")  #5

evaluate_search(full_index, int8_index,  #6
                int8_index_name, query_embeddings,  #6
                quantized_queries)  #6
evaluate_rerank_search(full_index, int8_index,  #7
          query_embeddings, quantized_queries)  #7

The Python listing shows how document and query embeddings are quantized, indexed in FAISS, evaluated against full precision, and passed to a reranking evaluation. section 13.7.1

Quantization can also be combined with approximate nearest-neighbor search, reduced-dimension Matryoshka Representation Learning embeddings, and reranking. More aggressive compression can substantially reduce index size and query time, but may leave a recall gap even after reranking. Engine support varies: some engines quantize internally, while others require externally quantized vectors and a matching index format. section 13.7.5

How AI-Powered Search covers it

How deep it goes
Works through scalar precision mapping and an Int8 indexing benchmark, then shows combined optimizations without detailing product quantization.
Examples
Uses outdoor-post embeddings, sample search queries, FAISS indexes, and measured index size and recall.
What it assumes
Assumes familiarity with query and document embeddings, but introduces reduced numerical precision through float-to-Int8 examples.
What you can do after
Apply it via Sentence Transformers and FAISS to build an Int8 index and compare its size and recall with full precision.
How it connects
Connects quantization to embeddings, approximate nearest-neighbor search, Matryoshka Representation Learning, and full-precision reranking.

Also explained in

Compare how 13 books explain quantization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI-Powered Search. The section links point to the parts of the book it draws on.