diff --git a/tests/ut/test_type.cc b/tests/ut/test_type.cc index 8484bf616..70bc29f87 100644 --- a/tests/ut/test_type.cc +++ b/tests/ut/test_type.cc @@ -20,8 +20,8 @@ TEST_CASE("Test bf16 patch", "[bf16 patch]") { const int64_t nb = 1000, nq = 10; const int64_t dim = 128; - const auto train_ds = GenFloatDataSet(nb, dim); - const auto query_ds = GenFloatDataSet(nq, dim); + const auto train_ds = GenDataSet(nb, dim); + const auto query_ds = GenDataSet(nq, dim); auto train_tensor = reinterpret_cast(train_ds->GetTensor()); @@ -84,10 +84,10 @@ TEST_CASE("Test bf16 patch", "[bf16 patch]") { double ip_relative_error = ip_error_sum / ip_total_sum; double ip_ref_relative_error = ip_ref_error_sum / ip_total_sum; - REQUIRE(l2_relative_error < pow(2, -11.0)); - REQUIRE(l2_ref_relative_error < pow(2, -11.0)); - REQUIRE(ip_relative_error < pow(2, -11.0)); - REQUIRE(ip_ref_relative_error < pow(2, -11.0)); + REQUIRE(l2_relative_error < pow(2, -9.0)); + REQUIRE(l2_ref_relative_error < pow(2, -9.0)); + REQUIRE(ip_relative_error < pow(2, -9.0)); + REQUIRE(ip_ref_relative_error < pow(2, -9.0)); knowhere::KnowhereConfig::DisablePatchForComputeFP32AsBF16(); @@ -99,3 +99,27 @@ TEST_CASE("Test bf16 patch", "[bf16 patch]") { REQUIRE(ip_dist[i] == ip_dist_new[i]); } } + +template +void +check_data_type_accuracy(float accuracy) { + const int64_t nb = 100; + const int64_t dim = 16; + + auto fp32_base_ds = GenDataSet(nb, dim); + + auto type_base_ds = knowhere::data_type_conversion(*fp32_base_ds); + auto fp32_base_ds_2 = knowhere::data_type_conversion(*type_base_ds); + + auto bv1 = static_cast(fp32_base_ds->GetTensor()); + auto bv2 = static_cast(fp32_base_ds_2->GetTensor()); + + for (int64_t i = 0; i < nb * dim; i++) { + REQUIRE(std::abs(bv2[i] / bv1[i] - 1.0) < accuracy); + } +} + +TEST_CASE("Test Float16", "[fp16]") { + check_data_type_accuracy(0.001); + check_data_type_accuracy(0.01); +} diff --git a/tests/ut/utils.h b/tests/ut/utils.h index 3f15fee63..003928279 100644 --- a/tests/ut/utils.h +++ b/tests/ut/utils.h @@ -38,21 +38,10 @@ struct DisPairLess { inline knowhere::DataSetPtr GenDataSet(int rows, int dim, int seed = 42) { - std::mt19937 rng(seed); - std::uniform_int_distribution<> distrib(0.0, 100.0); - float* ts = new float[rows * dim]; - for (int i = 0; i < rows * dim; ++i) ts[i] = (float)distrib(rng); - auto ds = knowhere::GenDataSet(rows, dim, ts); - ds->SetIsOwner(true); - return ds; -} - -inline knowhere::DataSetPtr -GenFloatDataSet(int rows, int dim, int seed = 42) { std::mt19937 rng(seed); std::uniform_real_distribution<> distrib(0.0, 100.0); float* ts = new float[rows * dim]; - for (int i = 0; i < rows * dim; ++i) ts[i] = (float)distrib(rng); + for (int i = 0; i < rows * dim; ++i) ts[i] = distrib(rng); auto ds = knowhere::GenDataSet(rows, dim, ts); ds->SetIsOwner(true); return ds; @@ -149,7 +138,7 @@ GetKNNRecall(const knowhere::DataSet& ground_truth, const std::vector ids_0(gt_ids + i * gt_k, gt_ids + i * gt_k + gt_k); std::vector ids_1 = result[i];