|
| 1 | +//===- ImexRunnerUtils.cpp - IMEX Runtime Utilities -----------------------===// |
| 2 | +// |
| 3 | +// Copyright 2022 Intel Corporation |
| 4 | +// Part of the IMEX Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +/// |
| 10 | +/// \file |
| 11 | +/// This file includes runtime functions that can be called from mlir code |
| 12 | +/// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#include "imex/ExecutionEngine/ImexRunnerUtils.h" |
| 16 | +#include <cmath> |
| 17 | +#include <cstdlib> |
| 18 | +#include <cstring> |
| 19 | +#include <iostream> |
| 20 | +#include <string> |
| 21 | + |
| 22 | +// NOLINTBEGIN(*-identifier-naming) |
| 23 | + |
| 24 | +extern "C" void _mlir_ciface_printMemrefBF16(UnrankedMemRefType<bf16> *M) { |
| 25 | + impl::printMemRef(*M); |
| 26 | +} |
| 27 | + |
| 28 | +extern "C" void _mlir_ciface_printMemrefF16(UnrankedMemRefType<f16> *M) { |
| 29 | + impl::printMemRef(*M); |
| 30 | +} |
| 31 | + |
| 32 | +extern "C" void printMemrefBF16(int64_t rank, void *ptr) { |
| 33 | + UnrankedMemRefType<bf16> descriptor = {rank, ptr}; |
| 34 | + _mlir_ciface_printMemrefBF16(&descriptor); |
| 35 | +} |
| 36 | + |
| 37 | +extern "C" void printMemrefF16(int64_t rank, void *ptr) { |
| 38 | + UnrankedMemRefType<f16> descriptor = {rank, ptr}; |
| 39 | + _mlir_ciface_printMemrefF16(&descriptor); |
| 40 | +} |
| 41 | + |
| 42 | +// Copied f16 and bf16 conversion code from |
| 43 | +// https://github.com/llvm/llvm-project/blob/main/mlir/lib/ExecutionEngine/Float16bits.cpp |
| 44 | + |
| 45 | +// Union used to make the int/float aliasing explicit so we can access the raw |
| 46 | +// bits. |
| 47 | +union Float32Bits { |
| 48 | + uint32_t u; |
| 49 | + float f; |
| 50 | +}; |
| 51 | + |
| 52 | +const uint32_t kF32MantiBits = 23; |
| 53 | +const uint32_t kF32HalfMantiBitDiff = 13; |
| 54 | +const uint32_t kF32HalfBitDiff = 16; |
| 55 | +const Float32Bits kF32Magic = {113 << kF32MantiBits}; |
| 56 | +const uint32_t kF32HalfExpAdjust = (127 - 15) << kF32MantiBits; |
| 57 | + |
| 58 | +// Converts the 16 bit representation of a half precision value to a float |
| 59 | +// value. This implementation is adapted from Eigen. |
| 60 | +static float half2float(uint16_t halfValue) { |
| 61 | + const uint32_t shiftedExp = |
| 62 | + 0x7c00 << kF32HalfMantiBitDiff; // Exponent mask after shift. |
| 63 | + |
| 64 | + // Initialize the float representation with the exponent/mantissa bits. |
| 65 | + Float32Bits f = { |
| 66 | + static_cast<uint32_t>((halfValue & 0x7fff) << kF32HalfMantiBitDiff)}; |
| 67 | + const uint32_t exp = shiftedExp & f.u; |
| 68 | + f.u += kF32HalfExpAdjust; // Adjust the exponent |
| 69 | + |
| 70 | + // Handle exponent special cases. |
| 71 | + if (exp == shiftedExp) { |
| 72 | + // Inf/NaN |
| 73 | + f.u += kF32HalfExpAdjust; |
| 74 | + } else if (exp == 0) { |
| 75 | + // Zero/Denormal? |
| 76 | + f.u += 1 << kF32MantiBits; |
| 77 | + f.f -= kF32Magic.f; |
| 78 | + } |
| 79 | + |
| 80 | + f.u |= (halfValue & 0x8000) << kF32HalfBitDiff; // Sign bit. |
| 81 | + return f.f; |
| 82 | +} |
| 83 | + |
| 84 | +const uint32_t kF32BfMantiBitDiff = 16; |
| 85 | + |
| 86 | +// Converts the 16 bit representation of a bfloat value to a float value. This |
| 87 | +// implementation is adapted from Eigen. |
| 88 | +static float bfloat2float(uint16_t bfloatBits) { |
| 89 | + Float32Bits floatBits; |
| 90 | + floatBits.u = static_cast<uint32_t>(bfloatBits) << kF32BfMantiBitDiff; |
| 91 | + return floatBits.f; |
| 92 | +} |
| 93 | + |
| 94 | +// For information on how to Iterate over UnrankedMemRefType, start with |
| 95 | +// https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/ExecutionEngine/CRunnerUtils.h |
| 96 | +extern "C" bool _mlir_ciface_allcloseF16(UnrankedMemRefType<f16> *M, |
| 97 | + UnrankedMemRefType<float> *N) { |
| 98 | + // atol, rtol values copied from |
| 99 | + // https://numpy.org/doc/stable/reference/generated/numpy.allclose.html |
| 100 | + // values may need to adjusted in the future |
| 101 | + const float atol = 1e-08; |
| 102 | + const float rtol = 1e-05; |
| 103 | + DynamicMemRefType<f16> DM = DynamicMemRefType<f16>(*M); |
| 104 | + DynamicMemRefType<float> DN = DynamicMemRefType<float>(*N); |
| 105 | + DynamicMemRefIterator<f16> i = DM.begin(); |
| 106 | + DynamicMemRefIterator<float> j = DN.begin(); |
| 107 | + for (; i != DM.end() && j != DN.end(); ++i, ++j) { |
| 108 | + f16 lhs = *i; |
| 109 | + float rhs = *j; |
| 110 | + if (fabs(half2float(lhs.bits) - rhs) > atol + rtol * fabs(rhs)) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + } |
| 114 | + return true; |
| 115 | +} |
| 116 | + |
| 117 | +extern "C" bool _mlir_ciface_allcloseBF16(UnrankedMemRefType<bf16> *M, |
| 118 | + UnrankedMemRefType<float> *N) { |
| 119 | + // atol, rtol values copied from |
| 120 | + // https://numpy.org/doc/stable/reference/generated/numpy.allclose.html |
| 121 | + // values may need to adjusted in the future |
| 122 | + const float atol = 1e-08; |
| 123 | + const float rtol = 1e-05; |
| 124 | + DynamicMemRefType<bf16> DM = DynamicMemRefType<bf16>(*M); |
| 125 | + DynamicMemRefType<float> DN = DynamicMemRefType<float>(*N); |
| 126 | + DynamicMemRefIterator<bf16> i = DM.begin(); |
| 127 | + DynamicMemRefIterator<float> j = DN.begin(); |
| 128 | + for (; i != DM.end() && j != DN.end(); ++i, ++j) { |
| 129 | + bf16 lhs = *i; |
| 130 | + float rhs = *j; |
| 131 | + if (fabs(bfloat2float(lhs.bits) - rhs) > atol + rtol * fabs(rhs)) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + } |
| 135 | + return true; |
| 136 | +} |
| 137 | + |
| 138 | +extern "C" bool _mlir_ciface_allcloseF32(UnrankedMemRefType<float> *M, |
| 139 | + UnrankedMemRefType<float> *N) { |
| 140 | + // atol, rtol values copied from |
| 141 | + // https://numpy.org/doc/stable/reference/generated/numpy.allclose.html |
| 142 | + // values may need to adjusted in the future |
| 143 | + const float atol = 1e-08; |
| 144 | + const float rtol = 1e-05; |
| 145 | + DynamicMemRefType<float> DM = DynamicMemRefType<float>(*M); |
| 146 | + DynamicMemRefType<float> DN = DynamicMemRefType<float>(*N); |
| 147 | + DynamicMemRefIterator<float> i = DM.begin(); |
| 148 | + DynamicMemRefIterator<float> j = DN.begin(); |
| 149 | + for (; i != DM.end() && j != DN.end(); ++i, ++j) { |
| 150 | + float lhs = *i; |
| 151 | + float rhs = *j; |
| 152 | + if (fabs(lhs - rhs) > atol + rtol * fabs(rhs)) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + } |
| 156 | + return true; |
| 157 | +} |
| 158 | + |
| 159 | +extern "C" void _mlir_ciface_printAllcloseF16(UnrankedMemRefType<f16> *M, |
| 160 | + UnrankedMemRefType<float> *N) { |
| 161 | + if (_mlir_ciface_allcloseF16(M, N)) { |
| 162 | + std::cout << "[ALLCLOSE: TRUE]\n"; |
| 163 | + } else { |
| 164 | + std::cout << "[ALLCLOSE: FALSE]\n"; |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +extern "C" void _mlir_ciface_printAllcloseBF16(UnrankedMemRefType<bf16> *M, |
| 169 | + UnrankedMemRefType<float> *N) { |
| 170 | + if (_mlir_ciface_allcloseBF16(M, N)) { |
| 171 | + std::cout << "[ALLCLOSE: TRUE]\n"; |
| 172 | + } else { |
| 173 | + std::cout << "[ALLCLOSE: FALSE]\n"; |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +extern "C" void _mlir_ciface_printAllcloseF32(UnrankedMemRefType<float> *M, |
| 178 | + UnrankedMemRefType<float> *N) { |
| 179 | + if (_mlir_ciface_allcloseF32(M, N)) { |
| 180 | + std::cout << "[ALLCLOSE: TRUE]\n"; |
| 181 | + } else { |
| 182 | + std::cout << "[ALLCLOSE: FALSE]\n"; |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +// NOLINTEND(*-identifier-naming) |
0 commit comments