diff --git a/faiss/gpu/test/TestGpuIndexFlat.cpp b/faiss/gpu/test/TestGpuIndexFlat.cpp index fdb8ce1d3..a566732ed 100644 --- a/faiss/gpu/test/TestGpuIndexFlat.cpp +++ b/faiss/gpu/test/TestGpuIndexFlat.cpp @@ -821,6 +821,14 @@ std::vector toFp16(const std::vector& v) { return out; } +std::vector fromFp16(const std::vector& v) { + std::vector out(v.size()); + std::transform(v.begin(), v.end(), out.begin(), [](uint16_t h) { + return faiss::decode_fp16(h); + }); + return out; +} + // A copy of host data on a GPU device struct DeviceCopy { DeviceCopy(int device, const void* src, size_t bytes) { @@ -849,7 +857,7 @@ void testSearchExFloat16(faiss::gpu::GpuIndex& index) { auto queries = faiss::gpu::randVecs(numQuery, index.d); auto queries16 = toFp16(queries); - auto roundedQueries = faiss::gpu::roundToHalf(queries); + auto roundedQueries = fromFp16(queries16); std::vector refDistances(numQuery * k); std::vector refLabels(numQuery * k); @@ -955,7 +963,7 @@ TEST(TestGpuIndexFlat, ClusteringTrainExFloat16) { int numVecs = 2000; auto vecs = faiss::gpu::randVecs(numVecs, dim); auto vecs16 = toFp16(vecs); - auto roundedVecs = faiss::gpu::roundToHalf(vecs); + auto roundedVecs = fromFp16(vecs16); faiss::ClusteringParameters cp; cp.niter = 10; diff --git a/tests/test_ivf_index.cpp b/tests/test_ivf_index.cpp index 9e1c3a9e5..814bc07b0 100644 --- a/tests/test_ivf_index.cpp +++ b/tests/test_ivf_index.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -1165,8 +1166,14 @@ TEST(IndexFlat, search_ex_float16_native_matches_float32) { hi.data()); EXPECT_EQ(hi, fi) << "metric=" << metric << " d=" << d << " k=" << k; - EXPECT_EQ(hd, fd) - << "metric=" << metric << " d=" << d << " k=" << k; + for (size_t i = 0; i < fd.size(); ++i) { + EXPECT_NEAR( + hd[i], + fd[i], + 1e-5f * std::max(1.0f, std::fabs(fd[i]))) + << "metric=" << metric << " d=" << d << " k=" << k + << " result=" << i; + } } } }