Optimizador de Dimensiones de Vectores

Vector Database (RAG) Embeddings Dimension Reducer & Optimizer es una herramienta local de simulación de vectores. Realice truncaciones normalizadas L2 y cuantificaciones a 1-bit directamente en su navegador.

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;
}

Instrucciones

  1. 1

    Ingrese dos vectores en formato de matriz float JSON (Vector A y Vector B).

  2. 2

    Seleccione el tamaño de truncación MRL (Original, 512, 256 o 128).

  3. 3

    Configure el formato de cuantificación (Float32, Int8 Escalar o Binario de 1-bit).

  4. 4

    Analice la retención de similitud de coseno, el factor de compresión de memoria y descargue los bloques de código.

Preguntas Frecuentes

Los embeddings MRL (como text-embedding-3 de OpenAI) se entrenan para concentrar la información semántica más importante en las primeras dimensiones. Esto permite truncar el vector (por ejemplo, de 1536 a 256 dimensiones) reteniendo más del 95% de la precisión original y reduciendo drásticamente el espacio de almacenamiento.
La cuantificación Int8 mapea valores flotantes a enteros de 8 bits, reduciendo el tamaño 4x. La cuantificación binaria mapea valores positivos a 1 y negativos a 0, reduciendo el tamaño 32x. La cuantificación binaria permite búsquedas ultrarrápidas mediante distancia Hamming en lugar de coseno.
Engineering Guides

Domina Esta Herramienta

Guías detalladas y tutoriales para usuarios avanzados.