Skip to content

Commit 510cec8

Browse files
committed
test: stabilize fp16 euclidean matrix comparison
1 parent 9a8e3f8 commit 510cec8

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

tests/ailego/math/euclidean_distance_matrix_fp16_test.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include <functional>
16+
#include <cstdint>
1617
#include <random>
1718
#include <string>
1819
#include <thread>
@@ -135,7 +136,8 @@ TEST(DistanceMatrix, SquaredEuclidean_General) {
135136

136137
template <size_t M, size_t N>
137138
void TestEuclideanMatrix(void) {
138-
std::mt19937 gen((std::random_device())());
139+
std::mt19937 gen(static_cast<uint32_t>(0x5EED1234u + M * 131u + N * 17u));
140+
constexpr int kFp16MatrixUlpTolerance = 20000;
139141

140142
const size_t batch_size = M;
141143
const size_t query_size = N;
@@ -174,13 +176,15 @@ void TestEuclideanMatrix(void) {
174176

175177
for (size_t i = 0; i < batch_size * query_size; ++i) {
176178
// EXPECT_FLOAT_EQ(result1[i], result2[i]);
177-
EXPECT_TRUE(MathHelper::IsAlmostEqual(result1[i], result2[i], 10000));
179+
EXPECT_TRUE(MathHelper::IsAlmostEqual(
180+
result1[i], result2[i], kFp16MatrixUlpTolerance));
178181
}
179182
}
180183

181184
template <size_t M, size_t N>
182185
void TestSquaredEuclideanMatrix(void) {
183-
std::mt19937 gen((std::random_device())());
186+
std::mt19937 gen(static_cast<uint32_t>(0x5EED5678u + M * 131u + N * 17u));
187+
constexpr int kFp16MatrixUlpTolerance = 20000;
184188

185189
const size_t batch_size = M;
186190
const size_t query_size = N;
@@ -219,7 +223,8 @@ void TestSquaredEuclideanMatrix(void) {
219223

220224
for (size_t i = 0; i < batch_size * query_size; ++i) {
221225
// EXPECT_FLOAT_EQ(result1[i], result2[i]);
222-
EXPECT_TRUE(MathHelper::IsAlmostEqual(result1[i], result2[i], 10000));
226+
EXPECT_TRUE(MathHelper::IsAlmostEqual(
227+
result1[i], result2[i], kFp16MatrixUlpTolerance));
223228
}
224229
}
225230

0 commit comments

Comments
 (0)