Skip to content

Commit

Permalink
PR #17135: Add TypeParam to FP8E4M3DistanceTest
Browse files Browse the repository at this point in the history
Imported from GitHub PR #17135

Add TypeParam to FP8E4M3DistanceTest

This test suite will be extended with tsl::float8_e4m3 type in future
Copybara import of the project:

--
8afe5a9 by Alexander Pivovarov <[email protected]>:

Add TypeParam to FP8E4M3DistanceTest

Merging this change closes #17135

COPYBARA_INTEGRATE_REVIEW=#17135 from apivovarov:dedup_fp_util_test 8afe5a9
PiperOrigin-RevId: 675090814
  • Loading branch information
apivovarov authored and Google-ML-Automation committed Sep 16, 2024
1 parent c32e2b9 commit 95d5251
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions xla/fp_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,56 +111,56 @@ INSTANTIATE_TEST_SUITE_P(DoublePrecisionInputs, FixedValueTest,
0x1.fffffffffffffp-127,
0x1.aaaaaaaaaaaaap-127));

TEST(FPDistanceTest, F8E4M3FNDistance) {
// a & b are equal
EXPECT_EQ(CalculateDistanceInFloats<tsl::float8_e4m3fn>(
tsl::float8_e4m3fn(8.0), tsl::float8_e4m3fn(8.0)),
0);
// Test F8E4M3 floating-point types (F8E4M3FN)
template <typename T>
class FP8E4M3DistanceTest : public ::testing::Test {};

using F8E4M3Types = ::testing::Types<tsl::float8_e4m3fn>;
TYPED_TEST_SUITE(FP8E4M3DistanceTest, F8E4M3Types);

TYPED_TEST(FP8E4M3DistanceTest, F8E4M3Distance) {
// a & b are equal, distance should be 0
EXPECT_EQ(
CalculateDistanceInFloats<TypeParam>(TypeParam(8.0), TypeParam(8.0)), 0);

// a & b have the same exponents
EXPECT_EQ(CalculateDistanceInFloats<tsl::float8_e4m3fn>(
tsl::float8_e4m3fn(8.0), tsl::float8_e4m3fn(13)),
EXPECT_EQ(CalculateDistanceInFloats<TypeParam>(TypeParam(8.0), TypeParam(13)),
5);

// a & b have different exponents
EXPECT_EQ(CalculateDistanceInFloats<tsl::float8_e4m3fn>(
tsl::float8_e4m3fn(8.0), tsl::float8_e4m3fn(6.0)),
4);
EXPECT_EQ(
CalculateDistanceInFloats<TypeParam>(TypeParam(8.0), TypeParam(6.0)), 4);

// 1 from 0 in the positive direction
EXPECT_EQ(CalculateDistanceInFloats<tsl::float8_e4m3fn>(
std::numeric_limits<tsl::float8_e4m3fn>::denorm_min(),
tsl::float8_e4m3fn(0)),
EXPECT_EQ(CalculateDistanceInFloats<TypeParam>(
std::numeric_limits<TypeParam>::denorm_min(), TypeParam(0)),
1);

// 1 from 0 in the negative direction
EXPECT_EQ(CalculateDistanceInFloats<tsl::float8_e4m3fn>(
-std::numeric_limits<tsl::float8_e4m3fn>::denorm_min(),
tsl::float8_e4m3fn(0)),
EXPECT_EQ(CalculateDistanceInFloats<TypeParam>(
-std::numeric_limits<TypeParam>::denorm_min(), TypeParam(0)),
1);

// a & b have different signs
EXPECT_EQ(CalculateDistanceInFloats<tsl::float8_e4m3fn>(
-std::numeric_limits<tsl::float8_e4m3fn>::denorm_min(),
std::numeric_limits<tsl::float8_e4m3fn>::denorm_min()),
EXPECT_EQ(CalculateDistanceInFloats<TypeParam>(
-std::numeric_limits<TypeParam>::denorm_min(),
std::numeric_limits<TypeParam>::denorm_min()),
2);

// 1 non denorm from 0 in the positive direction
EXPECT_EQ(CalculateDistanceInFloats<tsl::float8_e4m3fn>(
std::numeric_limits<tsl::float8_e4m3fn>::min(),
tsl::float8_e4m3fn(0)),
EXPECT_EQ(CalculateDistanceInFloats<TypeParam>(
std::numeric_limits<TypeParam>::min(), TypeParam(0)),
8);

// 1 non denorm from 0 in the negative direction
EXPECT_EQ(CalculateDistanceInFloats<tsl::float8_e4m3fn>(
-std::numeric_limits<tsl::float8_e4m3fn>::min(),
tsl::float8_e4m3fn(0)),
EXPECT_EQ(CalculateDistanceInFloats<TypeParam>(
-std::numeric_limits<TypeParam>::min(), TypeParam(0)),
8);

// a & b have different signs
EXPECT_EQ(CalculateDistanceInFloats<tsl::float8_e4m3fn>(
-std::numeric_limits<tsl::float8_e4m3fn>::min(),
std::numeric_limits<tsl::float8_e4m3fn>::min()),
EXPECT_EQ(CalculateDistanceInFloats<TypeParam>(
-std::numeric_limits<TypeParam>::min(),
std::numeric_limits<TypeParam>::min()),
16);
}

Expand Down

0 comments on commit 95d5251

Please sign in to comment.