gem5 v24.0.0.0
Loading...
Searching...
No Matches
mxfp.test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Advanced Micro Devices, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <gtest/gtest.h>
33
35
36template<typename T>
37bool test_raw_mxfp(T raw_mxfp, int bits)
38{
39 float tmp = float(raw_mxfp);
40 T from_float(tmp);
41
42 // Simply check that casting to float and back yields the same bit values.
43 // Exclude inf/NaN as those have multiple values in some MXFP types.
44 if (raw_mxfp.data != from_float.data &&
45 !std::isnan(tmp) && !std::isinf(tmp)) {
46 return false;
47 }
48
49 return true;
50}
51
52template<typename T>
53int test_type(int bits)
54{
55 T raw_mxfp;
56 int errors = 0;
57
58 int max_val = 1 << bits;
59 for (int val = 0; val < max_val; ++val) {
60 // Raw data is aligned to MSb in MXFP types. Shift into place.
61 raw_mxfp.data = val << (32 - bits);
62 if (!test_raw_mxfp(raw_mxfp, bits)) {
63 errors++;
64 }
65 }
66
67 return errors;
68}
69
70TEST(MxfpTest, MxBf16Test)
71{
73
74 int errors = test_type<T>(T::size());
75
76 EXPECT_EQ(errors, 0);
77}
78
79TEST(MxfpTest, MxFp16Test)
80{
82
83 int errors = test_type<T>(T::size());
84
85 EXPECT_EQ(errors, 0);
86}
87
88TEST(MxfpTest, MxBf8Test)
89{
91
92 int errors = test_type<T>(T::size());
93
94 EXPECT_EQ(errors, 0);
95}
96
97TEST(MxfpTest, MxFp8Test)
98{
99 using T = gem5::AMDGPU::mxfloat8;
100
101 int errors = test_type<T>(T::size());
102
103 EXPECT_EQ(errors, 0);
104}
TEST(MxfpTest, MxBf16Test)
Definition mxfp.test.cc:70
int test_type(int bits)
Definition mxfp.test.cc:53
bool test_raw_mxfp(T raw_mxfp, int bits)
Definition mxfp.test.cc:37
mxfp< fp8_e4m3_info > mxfloat8
Definition mxfp_types.hh:43
mxfp< fp16_e8m7_info > mxbfloat16
Definition mxfp_types.hh:45
mxfp< fp16_e5m10_info > mxfloat16
Definition mxfp_types.hh:46
mxfp< fp8_e5m2_info > mxbfloat8
Definition mxfp_types.hh:42
constexpr bool isinf(gem5::AMDGPU::fp16_e5m10_info a)
Definition fp16_e5m10.hh:78
constexpr bool isnan(gem5::AMDGPU::fp16_e5m10_info a)
Definition fp16_e5m10.hh:83

Generated on Tue Jun 18 2024 16:23:39 for gem5 by doxygen 1.11.0