|
| 1 | +//===-- TestApiOps.cpp - OneDNN operations test -----------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This file is licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include <cmath> |
| 10 | +#include <gtest/gtest.h> |
| 11 | + |
| 12 | +#include <llvm/ADT/APFloat.h> |
| 13 | + |
| 14 | +#include "DnnlTestUtils.h" |
| 15 | +#include "graph/backend/elyzor/include/dnnl_graph_compiler.h" |
| 16 | + |
| 17 | +static void exec(const char *fileName, dnnl_graph_compiler_tensor *inputs, |
| 18 | + dnnl_graph_compiler_tensor *outputs) { |
| 19 | + auto json = readStrResource(fileName); |
| 20 | + const struct dnnl_graph_compiler *gc; |
| 21 | + const struct dnnl_graph_compiler_executable *exe; |
| 22 | + ASSERT_EQ(dnnl_graph_compiler_create(nullptr, &gc), dnnl_success); |
| 23 | + ASSERT_EQ(dnnl_graph_compiler_compile(gc, json.c_str(), &exe), dnnl_success); |
| 24 | + ASSERT_EQ(dnnl_graph_compiler_execute(gc, exe, inputs, outputs), |
| 25 | + dnnl_success); |
| 26 | + dnnl_graph_compiler_destroy_executable(gc, exe); |
| 27 | + dnnl_graph_compiler_destroy(gc); |
| 28 | +} |
| 29 | + |
| 30 | +TEST(TestApiOps, div) { |
| 31 | + dnnl_graph_compiler_tensor inputs[2]; |
| 32 | + dnnl_graph_compiler_tensor outputs[1]; |
| 33 | + int64_t dims[1] = {128}; |
| 34 | + std::float32_t arg1[128]; |
| 35 | + std::float32_t arg2[128]; |
| 36 | + std::float32_t arg3[128]; |
| 37 | + inputs[0] = {.id = 0, .ndims = 1, .dims = dims, .data = arg1}; |
| 38 | + inputs[1] = {.id = 1, .ndims = 1, .dims = dims, .data = arg2}; |
| 39 | + outputs[0] = {.id = 2, .ndims = 1, .dims = dims, .data = arg3}; |
| 40 | + for (auto i = 0; i < 128; i++) { |
| 41 | + arg1[i] = static_cast<std::float32_t>(std::pow(i, 2) / 128.f); |
| 42 | + arg2[i] = arg1[i] + 1; |
| 43 | + } |
| 44 | + |
| 45 | + exec("div.json", inputs, outputs); |
| 46 | + |
| 47 | + for (auto i = 0; i < 128; i++) { |
| 48 | + ASSERT_EQ(arg3[i], arg1[i] / arg2[i]); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +TEST(TestApiOps, matMul) { |
| 53 | + dnnl_graph_compiler_tensor inputs[3]; |
| 54 | + dnnl_graph_compiler_tensor outputs[1]; |
| 55 | + int64_t dimsA[2] = {128, 512}; |
| 56 | + int64_t dimsB[2] = {128, 64}; |
| 57 | + int64_t dimsBias[1] = {64}; |
| 58 | + int64_t dimsOut[2] = {512, 64}; |
| 59 | + std::float32_t argA[128][512]; |
| 60 | + std::float32_t argB[128][64]; |
| 61 | + std::float32_t argBias[64]; |
| 62 | + std::float32_t argOut[512][64]; |
| 63 | + inputs[0] = {.id = 0, .ndims = 2, .dims = dimsA, .data = argA}; |
| 64 | + inputs[1] = {.id = 1, .ndims = 2, .dims = dimsB, .data = argB}; |
| 65 | + inputs[2] = {.id = 2, .ndims = 1, .dims = dimsBias, .data = argBias}; |
| 66 | + outputs[0] = {.id = 3, .ndims = 2, .dims = dimsOut, .data = argOut}; |
| 67 | + |
| 68 | + // Initialize input tensors |
| 69 | + for (auto i = 0; i < 128; i++) { |
| 70 | + for (auto j = 0; j < 512; j++) { |
| 71 | + argA[i][j] = static_cast<std::float32_t>(i + j); |
| 72 | + } |
| 73 | + } |
| 74 | + for (auto i = 0; i < 128; i++) { |
| 75 | + for (auto j = 0; j < 64; j++) { |
| 76 | + argB[i][j] = static_cast<std::float32_t>(i - j); |
| 77 | + } |
| 78 | + } |
| 79 | + for (auto i = 0; i < 64; i++) { |
| 80 | + argBias[i] = static_cast<std::float32_t>(i); |
| 81 | + } |
| 82 | + |
| 83 | + exec("matmul.json", inputs, outputs); |
| 84 | + |
| 85 | + // Calculate expected output |
| 86 | + std::float32_t expected[512][64]; |
| 87 | + for (auto i = 0; i < 512; i++) { |
| 88 | + for (auto j = 0; j < 64; j++) { |
| 89 | + expected[i][j] = argBias[j]; |
| 90 | + for (auto k = 0; k < 128; k++) { |
| 91 | + expected[i][j] += argA[k][i] * argB[k][j]; // transpose_a = true |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Compare the results |
| 97 | + for (auto i = 0; i < 512; i++) { |
| 98 | + for (auto j = 0; j < 64; j++) { |
| 99 | + ASSERT_EQ(expected[i][j], argOut[i][j]); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +TEST(TestApiOps, mul) { |
| 105 | + dnnl_graph_compiler_tensor inputs[2]; |
| 106 | + dnnl_graph_compiler_tensor outputs[1]; |
| 107 | + int64_t dims[1] = {128}; |
| 108 | + std::float32_t arg1[128]; |
| 109 | + std::float32_t arg2[128]; |
| 110 | + std::float32_t arg3[128]; |
| 111 | + inputs[0] = {.id = 0, .ndims = 1, .dims = dims, .data = arg1}; |
| 112 | + inputs[1] = {.id = 1, .ndims = 1, .dims = dims, .data = arg2}; |
| 113 | + outputs[0] = {.id = 2, .ndims = 1, .dims = dims, .data = arg3}; |
| 114 | + for (auto i = 0; i < 128; i++) { |
| 115 | + arg1[i] = arg2[i] = static_cast<std::float32_t>(i); |
| 116 | + } |
| 117 | + |
| 118 | + exec("mul.json", inputs, outputs); |
| 119 | + |
| 120 | + for (auto i = 0; i < 128; i++) { |
| 121 | + ASSERT_EQ(arg3[i], static_cast<std::float32_t>(i * i)); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +TEST(TestApiOps, sub) { |
| 126 | + dnnl_graph_compiler_tensor inputs[2]; |
| 127 | + dnnl_graph_compiler_tensor outputs[1]; |
| 128 | + int64_t dims[1] = {128}; |
| 129 | + std::float32_t arg1[128]; |
| 130 | + std::float32_t arg2[128]; |
| 131 | + std::float32_t arg3[128]; |
| 132 | + inputs[0] = {.id = 0, .ndims = 1, .dims = dims, .data = arg1}; |
| 133 | + inputs[1] = {.id = 1, .ndims = 1, .dims = dims, .data = arg2}; |
| 134 | + outputs[0] = {.id = 2, .ndims = 1, .dims = dims, .data = arg3}; |
| 135 | + for (auto i = 0; i < 128; i++) { |
| 136 | + arg1[i] = static_cast<std::float32_t>(i); |
| 137 | + arg2[i] = arg1[i] * arg1[i]; |
| 138 | + } |
| 139 | + |
| 140 | + exec("sub.json", inputs, outputs); |
| 141 | + |
| 142 | + for (auto i = 0; i < 128; i++) { |
| 143 | + ASSERT_EQ(arg3[i], arg1[i] - arg2[i]); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +TEST(TestApiOps, pow) { |
| 148 | + dnnl_graph_compiler_tensor inputs[1]; |
| 149 | + dnnl_graph_compiler_tensor outputs[1]; |
| 150 | + int64_t dims[1] = {64}; |
| 151 | + std::float32_t arg1[64]; |
| 152 | + std::float32_t arg2[64]; |
| 153 | + inputs[0] = {.id = 0, .ndims = 1, .dims = dims, .data = arg1}; |
| 154 | + outputs[0] = {.id = 1, .ndims = 1, .dims = dims, .data = arg2}; |
| 155 | + for (auto i = 0; i < 64; i++) { |
| 156 | + arg1[i] = static_cast<std::float32_t>(i); |
| 157 | + } |
| 158 | + |
| 159 | + exec("pow.json", inputs, outputs); |
| 160 | + |
| 161 | + for (auto i = 0; i < 64; i++) { |
| 162 | + ASSERT_EQ(arg1[i] * arg1[i], arg2[i]); |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +TEST(TestApiOps, relu) { |
| 167 | + dnnl_graph_compiler_tensor inputs[1]; |
| 168 | + dnnl_graph_compiler_tensor outputs[1]; |
| 169 | + int64_t dims[1] = {128}; |
| 170 | + std::float32_t arg1[128]; |
| 171 | + std::float32_t arg2[128]; |
| 172 | + inputs[0] = {.id = 0, .ndims = 1, .dims = dims, .data = arg1}; |
| 173 | + outputs[0] = {.id = 1, .ndims = 1, .dims = dims, .data = arg2}; |
| 174 | + |
| 175 | + for (auto i = 0; i < 128; i++) { |
| 176 | + arg1[i] = static_cast<std::float32_t>(i - 64); |
| 177 | + } |
| 178 | + |
| 179 | + exec("relu.json", inputs, outputs); |
| 180 | + |
| 181 | + for (auto i = 0; i < 128; i++) { |
| 182 | + ASSERT_EQ(arg1[i] < 0 ? 0 : arg1[i], arg2[i]); |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +TEST(TestApiOps, reduceMean) { |
| 187 | + dnnl_graph_compiler_tensor inputs[1]; |
| 188 | + dnnl_graph_compiler_tensor outputs[1]; |
| 189 | + int64_t dims1[3] = {16, 64, 32}; |
| 190 | + int64_t dims2[3] = {16, 1, 32}; |
| 191 | + std::float32_t arg1[16][64][32]; |
| 192 | + std::float32_t arg2[16][1][32]; |
| 193 | + inputs[0] = {.id = 0, .ndims = 3, .dims = dims1, .data = arg1}; |
| 194 | + outputs[0] = {.id = 1, .ndims = 3, .dims = dims2, .data = arg2}; |
| 195 | + |
| 196 | + for (auto i = 0; i < 16; i++) { |
| 197 | + for (auto y = 0; y < 64; y++) { |
| 198 | + for (auto z = 0; z < 32; z++) { |
| 199 | + arg1[i][y][z] = static_cast<std::float32_t>(i * 64 * 32 + y * 32 + z); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + exec("reduce_mean.json", inputs, outputs); |
| 205 | + |
| 206 | + std::float32_t expected[16][1][32]; |
| 207 | + for (auto x = 0; x < 16; x++) { |
| 208 | + for (auto z = 0; z < 32; z++) { |
| 209 | + expected[x][0][z] = 0; |
| 210 | + for (auto y = 0; y < 64; y++) { |
| 211 | + expected[x][0][z] += arg1[x][y][z]; |
| 212 | + } |
| 213 | + expected[x][0][z] /= 64; |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + for (auto x = 0; x < 16; x++) { |
| 218 | + for (auto z = 0; z < 32; z++) { |
| 219 | + ASSERT_EQ(expected[x][0][z], arg2[x][0][z]); |
| 220 | + } |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +TEST(TestApiOps, reduceSum) { |
| 225 | + dnnl_graph_compiler_tensor inputs[1]; |
| 226 | + dnnl_graph_compiler_tensor outputs[1]; |
| 227 | + int64_t dims1[3] = {16, 64, 32}; |
| 228 | + int64_t dims2[3] = {16, 1, 32}; |
| 229 | + std::float32_t arg1[16][64][32]; |
| 230 | + std::float32_t arg2[16][1][32]; |
| 231 | + inputs[0] = {.id = 0, .ndims = 3, .dims = dims1, .data = arg1}; |
| 232 | + outputs[0] = {.id = 1, .ndims = 3, .dims = dims2, .data = arg2}; |
| 233 | + |
| 234 | + for (auto i = 0; i < 16; i++) { |
| 235 | + for (auto y = 0; y < 64; y++) { |
| 236 | + for (auto z = 0; z < 32; z++) { |
| 237 | + arg1[i][y][z] = static_cast<std::float32_t>(i * 64 * 32 + y * 32 + z); |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + exec("reduce_sum.json", inputs, outputs); |
| 243 | + |
| 244 | + std::float32_t expected[16][1][32]; |
| 245 | + for (auto x = 0; x < 16; x++) { |
| 246 | + for (auto z = 0; z < 32; z++) { |
| 247 | + expected[x][0][z] = 0; |
| 248 | + for (auto y = 0; y < 64; y++) { |
| 249 | + expected[x][0][z] += arg1[x][y][z]; |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + for (auto x = 0; x < 16; x++) { |
| 255 | + for (auto z = 0; z < 32; z++) { |
| 256 | + ASSERT_EQ(expected[x][0][z], arg2[x][0][z]); |
| 257 | + } |
| 258 | + } |
| 259 | +} |
| 260 | + |
| 261 | +TEST(TestApiOps, sigmoid) { |
| 262 | + dnnl_graph_compiler_tensor inputs[1]; |
| 263 | + dnnl_graph_compiler_tensor outputs[1]; |
| 264 | + int64_t dims[1] = {128}; |
| 265 | + std::float32_t arg1[128]; |
| 266 | + std::float32_t arg2[128]; |
| 267 | + inputs[0] = {.id = 0, .ndims = 1, .dims = dims, .data = arg1}; |
| 268 | + outputs[0] = {.id = 1, .ndims = 1, .dims = dims, .data = arg2}; |
| 269 | + for (auto i = 0; i < 128; i++) { |
| 270 | + arg1[i] = static_cast<std::float32_t>(i - 64); |
| 271 | + } |
| 272 | + |
| 273 | + exec("sigmoid.json", inputs, outputs); |
| 274 | + |
| 275 | + for (auto i = 0; i < 128; i++) { |
| 276 | + ASSERT_EQ(1.f / (1.f + std::exp(-arg1[i])), arg2[i]); |
| 277 | + } |
| 278 | +} |
| 279 | + |
| 280 | +TEST(TestApiOps, typecast) { |
| 281 | + dnnl_graph_compiler_tensor inputs[1]; |
| 282 | + dnnl_graph_compiler_tensor outputs[1]; |
| 283 | + int64_t dims[1] = {128}; |
| 284 | + std::float32_t arg1[128]; |
| 285 | + uint16_t arg2[128]; |
| 286 | + inputs[0] = {.id = 0, .ndims = 1, .dims = dims, .data = arg1}; |
| 287 | + outputs[0] = {.id = 1, .ndims = 1, .dims = dims, .data = arg2}; |
| 288 | + for (auto i = 0; i < 128; i++) { |
| 289 | + auto x = i - 64; |
| 290 | + arg1[i] = static_cast<std::float32_t>(x / (std::exp(-x))); |
| 291 | + } |
| 292 | + |
| 293 | + exec("typecast.json", inputs, outputs); |
| 294 | + |
| 295 | + for (auto i = 0; i < 128; i++) { |
| 296 | + llvm::APFloat f(arg1[i]); |
| 297 | + bool losesInfo; |
| 298 | + f.convert(llvm::APFloat::IEEEhalf(), llvm::APFloat::rmNearestTiesToEven, |
| 299 | + &losesInfo); |
| 300 | + ASSERT_EQ(static_cast<uint16_t>(f.bitcastToAPInt().getZExtValue()), |
| 301 | + arg2[i]); |
| 302 | + } |
| 303 | +} |
| 304 | + |
| 305 | +int main(int argc, char **argv) { |
| 306 | + ::testing::InitGoogleTest(&argc, argv); |
| 307 | + return RUN_ALL_TESTS(); |
| 308 | +} |
0 commit comments