下面是我当前使用的代码。我正在比较由768个漂浮物组成的矢量和其他50k个漂浮物,大约需要800ms。我假设有一个更快的实现,无论是在C#中,还是在我可以使用的本机计算包中,但我很难找到它。谢谢!
// USAGE:
// vectors is IEnumerable<float[768]>
// vector is float[768]
vectors.DotProductSum(vector) * 100)
public static float DotProductSum(this IEnumerable<float> values, IEnumerable<float> other)
{
return values.Zip(other, (d1, d2) => d1 * d2).Sum();
}
我发现了一个非常快速的解决方案Faiss,在我的测试中,它能够在<5ms。我正在从中消费。NET,所以使用了FaissMask包装器库。要做到这一点,您需要大量的本机依赖项,可以通过构建faiss repo来获得这些依赖项。我没有找到包含依赖项的包。具体来说,我需要:
libgcc_s_seh-1.dll
libgfortran-3.dll
libopenblas.dll
libquadmath-0.dll
faiss.dll
faiss_c.dll
之后,代码非常简单:
using var index = new FaissMask.IndexFlat((int)embeddingSize, MetricType.MetricInnerProduct);
index.Add(vectors);
var queryResults = index.Search(queryVector, 10);