mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-05-19 21:32:23 +00:00
Switched the code to C++17. To build ONNX Runtime on old distros like CentOS 7, you need to install a newer GCC from additionary repos. If you build onnxruntime with the newer GCC, typically the result binary can't be distributed to other places because it depends on the new GCC's runtime libraries, something that the stock OS doesn't have. But on RHEL/CentOS, it can be better. We use Red Hat devtoolset 8/9/10 with CentOS7 building our code. The new library features(like std::filesystem) that not exists in the old C++ runtime will be statically linked into the applications with some restrictions: 1. GCC has dual ABI, but we can only use the old one. It means std::string is still copy-on-write and std::list::size() is still O(n). Also, if you build onnxruntime on CentOS 7 and link it with some binaries that were built on CentOS 8 or Ubuntu with the new ABI and export C++ symbols directly(instead of using a C API), the it won't work. 2. We still can't use std::optional. It is a limitation coming from macOS. We will solve it when we got macOS 11 build machines. It won't be too long. 3. Please avoid to use C++17 in CUDA files(*.cu). Also, the *.h files that they include(like core/framework/float16.h). This is Because CUDA 10.2 doesn't support C++17. You are welcome to use the new features in any *.cc files.
97 lines
No EOL
2.2 KiB
C++
97 lines
No EOL
2.2 KiB
C++
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
// Licensed under the MIT License.
|
|
#pragma once
|
|
|
|
#include "endian.h"
|
|
|
|
namespace onnxruntime
|
|
{
|
|
|
|
// MLFloat16
|
|
struct MLFloat16 {
|
|
uint16_t val;
|
|
|
|
MLFloat16() : val(0) {}
|
|
explicit MLFloat16(uint16_t x) : val(x) {}
|
|
explicit MLFloat16(float f);
|
|
|
|
float ToFloat() const;
|
|
|
|
operator float() const {
|
|
return ToFloat();
|
|
}
|
|
};
|
|
|
|
inline bool operator==(const MLFloat16& left, const MLFloat16& right) {
|
|
return left.val == right.val;
|
|
}
|
|
|
|
inline bool operator!=(const MLFloat16& left, const MLFloat16& right) {
|
|
return left.val != right.val;
|
|
}
|
|
|
|
inline bool operator<(const MLFloat16& left, const MLFloat16& right) {
|
|
return left.val < right.val;
|
|
}
|
|
|
|
//BFloat16
|
|
struct BFloat16 {
|
|
uint16_t val{0};
|
|
explicit BFloat16() = default;
|
|
explicit BFloat16(uint16_t v) : val(v) {}
|
|
explicit BFloat16(float v) {
|
|
ORT_IF_CONSTEXPR(endian::native == endian::little) {
|
|
std::memcpy(&val, reinterpret_cast<char*>(&v) + sizeof(uint16_t), sizeof(uint16_t));
|
|
} else {
|
|
std::memcpy(&val, &v, sizeof(uint16_t));
|
|
}
|
|
}
|
|
|
|
float ToFloat() const {
|
|
float result;
|
|
char* const first = reinterpret_cast<char*>(&result);
|
|
char* const second = first + sizeof(uint16_t);
|
|
ORT_IF_CONSTEXPR(endian::native == endian::little) {
|
|
std::memset(first, 0, sizeof(uint16_t));
|
|
std::memcpy(second, &val, sizeof(uint16_t));
|
|
} else {
|
|
std::memcpy(first, &val, sizeof(uint16_t));
|
|
std::memset(second, 0, sizeof(uint16_t));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
operator float() const {
|
|
return ToFloat();
|
|
}
|
|
};
|
|
|
|
inline void BFloat16ToFloat(const BFloat16* blf, float* flt, size_t size) {
|
|
auto src = blf;
|
|
auto d = flt;
|
|
for (; size != 0; ++src, ++d, --size) {
|
|
*d = src->ToFloat();
|
|
}
|
|
}
|
|
|
|
inline void FloatToBFloat16(const float* flt, BFloat16* blf, size_t size) {
|
|
auto src = flt;
|
|
auto d = blf;
|
|
for (; size != 0; ++src, ++d, --size) {
|
|
new (d) BFloat16(*src);
|
|
}
|
|
}
|
|
|
|
inline bool operator==(const BFloat16& left, const BFloat16& right) {
|
|
return left.val == right.val;
|
|
}
|
|
|
|
inline bool operator!=(const BFloat16& left, const BFloat16& right) {
|
|
return left.val != right.val;
|
|
}
|
|
|
|
inline bool operator<(const BFloat16& left, const BFloat16& right) {
|
|
return left.val < right.val;
|
|
}
|
|
|
|
} |