MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / TEST

Function TEST

tensorflow/core/framework/tensor_testutil_test.cc:65–104  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

63}
64
65TEST(TensorTestUtilTest, ExpectTensorNearHalf) {
66 // Eigen::half has 1 sign bit, 5 exponent bits, and 10 mantissa bits.
67 // The exponent is offset at 15.
68 // https://en.wikipedia.org/wiki/Half-precision_floating-point_format
69 typedef Eigen::half T;
70#define HALF(x) static_cast<T>(x)
71
72 // Trivial cases: equalities.
73 EXPECT_TRUE(Expector<T>::Near(HALF(1.0f), HALF(1.0f), 0.0));
74 EXPECT_TRUE(Expector<T>::Near(HALF(0.0f), HALF(-0.0f), 0.0));
75 EXPECT_TRUE(Expector<T>::Near(HALF(3.141592f), HALF(3.141592f), 0.0));
76
77 // 0 10010 0001111110 -> 1150/128 = 8.984375 vs
78 // 0 10010 0001111111 -> 1151/128 = 8.9921875 (diff = 0.0078125)
79 EXPECT_TRUE(Expector<T>::Near(HALF(8.9875f), HALF(8.99f), 0.0078125));
80 EXPECT_FALSE(Expector<T>::Near(HALF(8.9875f), HALF(8.99f), 0.007));
81
82 // 0 11000 0110100000 -> 1440/2 = 720 vs
83 // 0 11000 0110100001 -> 1441/2 = 720.5 (diff = 0.5)
84 EXPECT_TRUE(Expector<T>::Near(HALF(720.2f), HALF(720.3f), 0.5));
85 EXPECT_FALSE(Expector<T>::Near(HALF(720.2f), HALF(720.3f), 0.4));
86
87 // 0 11001 0011010010 -> 1234 vs
88 // 0 11001 0011010011 -> 1235 (diff = 1)
89 // Rounds to even (1234.5 -> 1234).
90 EXPECT_TRUE(Expector<T>::Near(HALF(1234.f), HALF(1235.f), 1.0));
91 EXPECT_FALSE(Expector<T>::Near(HALF(1234.5f), HALF(1235.f), 0.5));
92 EXPECT_TRUE(Expector<T>::Near(HALF(1234.5f), HALF(1235.f), 1.0));
93
94 // 1 10000 0101101100 -> -1388/512 = -2.7109375 vs
95 // 1 10000 0101110001 -> -1393/512 = -2.720703125 (diff = 0.009765625)
96 EXPECT_TRUE(Expector<T>::Near(HALF(-2.71f), HALF(-2.72f), 0.01));
97
98#undef HALF
99
100 // Some of the cases failed because Eigen::half doesn't behave as expected.
101 // For example, (inf == inf) should have been true, but it returns false.
102 // TODO(penporn): uncomment this test once we fix Eigen::half
103 // TestEdgeCasesNear<T>();
104}
105
106TEST(TensorTestUtilTest, ExpectTensorNearFloat) {
107 // float has 1 sign bit, 8 exponent bits, and 23 mantissa bits.

Callers

nothing calls this directly

Calls 3

NearFunction · 0.85
IsCloseFunction · 0.85
epsilonFunction · 0.85

Tested by

no test coverage detected