forked from pytorch/FBGEMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransposeTest.cc
129 lines (115 loc) · 3.47 KB
/
TransposeTest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <random>
#include <type_traits>
#include <vector>
#include <gtest/gtest.h>
#include "fbgemm/Utils.h"
using namespace std;
using namespace fbgemm;
template <typename T>
::testing::AssertionResult compare_tranpose_results(
vector<T> expected,
vector<T> acutal,
int m,
int n,
int ld_src,
int ld_dst) {
std::stringstream ss;
if (is_same<T, float>::value) {
ss << " float results ";
} else if (is_same<T, uint8_t>::value) {
ss << " i8 results ";
} else {
ss << " i16 results ";
}
ss << " mismatch at ";
bool match = true;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int exp = expected[i * ld_src + j];
int act = acutal[i + j * ld_dst];
if (exp != act) {
ss << "(" << i << ", " << j << "). ref " << exp << " actual " << act;
match = false;
}
}
}
if (match)
return ::testing::AssertionSuccess();
else
return ::testing::AssertionFailure() << "results differ: " << ss.str();
}
TEST(TransposeTest, TransposeTest) {
// Generate shapes to test
vector<tuple<int, int, int, int>> shapes;
uniform_int_distribution<int> dist(0, 32);
default_random_engine generator;
for (int i = 0; i < 1024; ++i) {
int m = dist(generator);
int n = dist(generator);
int ld_src = n + dist(generator);
int ld_dst = m + dist(generator);
shapes.push_back(make_tuple(m, n, ld_src, ld_dst));
}
for (int i = 0; i < 1024; ++i) {
int m = dist(generator);
int n = 2;
int ld_src = n;
int ld_dst = m + dist(generator);
shapes.push_back(make_tuple(m, n, ld_src, ld_dst));
}
for (int i = 0; i < 1024; ++i) {
int m = dist(generator);
int n = 4;
int ld_src = n;
int ld_dst = m + dist(generator);
shapes.push_back(make_tuple(m, n, ld_src, ld_dst));
}
for (int i = 0; i < 1024; ++i) {
int m = 2;
int n = dist(generator);
int ld_src = n + dist(generator);
int ld_dst = m;
shapes.push_back(make_tuple(m, n, ld_src, ld_dst));
}
for (int i = 0; i < 1024; ++i) {
int m = 4;
int n = dist(generator);
int ld_src = n + dist(generator);
int ld_dst = m;
shapes.push_back(make_tuple(m, n, ld_src, ld_dst));
}
for (const auto& shape : shapes) {
int m, n, ld_src, ld_dst;
tie(m, n, ld_src, ld_dst) = shape;
// float test
vector<float> a(m * ld_src);
vector<float> b(n * ld_dst);
generate(
a.begin(), a.end(), [&dist, &generator] { return dist(generator); });
transpose_simd(m, n, a.data(), ld_src, b.data(), ld_dst);
EXPECT_TRUE(compare_tranpose_results(a, b, m, n, ld_src, ld_dst));
// i8 test
vector<uint8_t> a_i8(m * ld_src);
vector<uint8_t> b_i8(n * ld_dst);
generate(a_i8.begin(), a_i8.end(), [&dist, &generator] {
return dist(generator);
});
transpose_simd(m, n, a_i8.data(), ld_src, b_i8.data(), ld_dst);
EXPECT_TRUE(compare_tranpose_results(a_i8, b_i8, m, n, ld_src, ld_dst));
// i16 test
vector<uint16_t> a_i16(m * ld_src);
vector<uint16_t> b_i16(n * ld_dst);
generate(a_i16.begin(), a_i16.end(), [&dist, &generator] {
return dist(generator);
});
transpose_simd(m, n, a_i16.data(), ld_src, b_i16.data(), ld_dst);
EXPECT_TRUE(compare_tranpose_results(a_i16, b_i16, m, n, ld_src, ld_dst));
}
}