
🧠 记忆锚点:BQ 把维度压成比特,极快但损失大;PQ 分段查码本,压缩与精度更均衡。
💡 答案要点
Binary Quantization = 把 float32 向量压缩成二进制(0/1)位向量,实现 32 倍压缩
量化原理:
python
import numpy as np
def binary_quantize(vector: np.ndarray) -> np.ndarray:
"""大于均值的维度为 1,否则为 0"""
mean = vector.mean()
return (vector > mean).astype(np.uint8)
# 相似度:XOR + popcount(位运算,比浮点计算快 10 倍以上)
def hamming_similarity(a: np.ndarray, b: np.ndarray) -> float:
return (a == b).mean()三种量化方案对比:
| 维度 | 原始 float32 | PQ(乘积量化) | Binary Quantization |
|---|---|---|---|
| 存储(1536维) | 6144 字节 | 16-96 字节 | 192 字节 |
| 压缩比 | 1x | 64-384x | 32x |
| 计算加速 | 基准 | SIMD | AVX-512 位运算(最快) |
| 精度损失 | 无 | 中等 | 较大 |
| 适用场景 | 精度优先 | 内存受限 | 速度优先超大规模 |
搭配 Matryoshka Embeddings(OpenAI text-embedding-3):
python
# 先截断到 512 维,再做 BQ → 只有 64 字节!比原始节省 96 倍
response = client.embeddings.create(
model="text-embedding-3-large",
input="测试文本",
dimensions=512 # Matryoshka 截断
)典型两阶段检索用法:
- 粗召回:BQ 向量超快速检索 Top-500(位运算极快)
- 精排:原始 float32 对 Top-500 重新打分 → 返回 Top-20
面试话术:
"二进制量化是最激进的向量压缩:float32 变成 0/1,压缩 32 倍,XOR+popcount 位运算比 SIMD 浮点快 10 倍以上。代价是精度损失较大,通常配合两阶段检索——先 BQ 粗召回,再原始向量精排。搭配 OpenAI Matryoshka 的 512 维截断,最终只有 64 字节,非常适合超大规模搜索。"