Vector Embeddings Reducer & Optimizer

Vector Database (RAG) Embeddings Dimension Reducer & Optimizer is a local-first developer tool. Simulate L2-normalized vector truncation, Int8 quantization, and 1-bit binary compression entirely on your device.

Presets

Paste vector floats in Vector A and Vector B to begin calculation. Or load a preset above to test the math.

/**
 * client-reducer.ts
 * Browser-safe embedding dimensionality reducer & optimizer.
 * Designed for Matryoshka Representation Learning (MRL) and local quantization.
 */

export function optimizeEmbedding(
  vector: number[],
  targetDim: number = 256,
  format: 'float32' | 'int8' | 'binary' = 'float32'
): {
  data: Float32Array | Int8Array | Uint8Array;
  scale?: number;
} {
  // 1. Matryoshka Truncation (Slice prefix)
  const sliced = vector.slice(0, targetDim);

  // 2. L2 Re-normalization (Ensure unit length)
  let sumSq = 0;
  for (let i = 0; i < targetDim; i++) {
    sumSq += sliced[i] * sliced[i];
  }
  const norm = Math.sqrt(sumSq);
  const normalized = new Float32Array(targetDim);
  if (norm > 0) {
    for (let i = 0; i < targetDim; i++) {
      normalized[i] = sliced[i] / norm;
    }
  }

  // 3. Apply Quantization Format
  if (format === 'float32') {
    return { data: normalized };
  }

  if (format === 'int8') {
    // Find absolute maximum value for scaling
    let maxVal = 0;
    for (let i = 0; i < targetDim; i++) {
      const abs = Math.abs(normalized[i]);
      if (abs > maxVal) maxVal = abs;
    }
    const scale = maxVal > 0 ? 127 / maxVal : 1;
    const int8Data = new Int8Array(targetDim);
    for (let i = 0; i < targetDim; i++) {
      int8Data[i] = Math.round(normalized[i] * scale);
    }
    return { data: int8Data, scale };
  }

  if (format === 'binary') {
    // Pack every 8 float values into a single byte
    const byteLen = Math.ceil(targetDim / 8);
    const binaryData = new Uint8Array(byteLen);
    for (let i = 0; i < targetDim; i++) {
      if (normalized[i] >= 0) {
        const byteIdx = Math.floor(i / 8);
        const bitIdx = i % 8;
        binaryData[byteIdx] |= (1 << bitIdx);
      }
    }
    return { data: binaryData };
  }

  throw new Error('Unsupported quantization format');
}

/**
 * Helper to calculate Hamming Distance between two Packed Binary Vectors
 */
export function hammingDistance(a: Uint8Array, b: Uint8Array): number {
  let distance = 0;
  for (let i = 0; i < a.length; i++) {
    let xor = a[i] ^ b[i];
    while (xor > 0) {
      if (xor & 1) distance++;
      xor >>= 1;
    }
  }
  return distance;
}

Instructions

  1. 1

    Provide two raw float array vectors (Vector A and Vector B) representing your high-dimensional embeddings.

  2. 2

    Select a target Matryoshka Representation Learning (MRL) truncation size.

  3. 3

    Configure the quantization format (Float32, Int8 Scalar, or Binary 1-bit).

  4. 4

    Analyze the Cosine Similarity retention rate, memory compression factors, and download optimized reducer code blocks.

Frequently Asked Questions

MRL embeddings (like OpenAI's text-embedding-3) are trained to contain their most important semantic information in the earliest dimensions. This allows you to truncate the vector (e.g., from 1536 to 256 dimensions) while retaining up to 95%+ of the original semantic accuracy, drastically reducing index size.
Int8 quantization maps Float32 values (-1.0 to 1.0) to integers (-128 to 127), reducing vector size by 4x. Binary (1-bit) quantization maps positive floats to 1 and negative floats to 0, reducing size by 32x. Binary quantization uses Hamming distance instead of Cosine distance for extremely fast search speeds, albeit with a slight drop in accuracy.
Engineering Guides

Master This Tool

Deep-dive guides and tutorials for advanced users.