forked from pytorch/FBGEMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FbgemmConvert.h
183 lines (160 loc) · 4.2 KB
/
FbgemmConvert.h
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* 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.
*/
#pragma once
#include <stdexcept>
#include "fbgemm/Types.h"
#include "fbgemm/Utils.h"
namespace fbgemm {
typedef uint16_t bfloat16;
/**
* @ Transform all entries in a matrix from fp32 to bfloat16: reference
* implementation.
*
*/
FBGEMM_API void
FloatToBfloat16_ref(const float* src, bfloat16* dst, size_t size);
/**
* @ Transform all entries in a matrix from bfloat16 to fp32: reference
* implementation.
*
*/
FBGEMM_API void
Bfloat16ToFloat_ref(const bfloat16* src, float* dst, size_t size);
/**
* @ Transform all entries in a matrix from fp32 to bfloat16: simd
* implementation.
*
*/
FBGEMM_API void
FloatToBfloat16_simd(const float* src, bfloat16* dst, size_t size);
/**
* @ Transform all entries in a matrix from bfloat16 to fp32: simd
* implementation.
*
*/
FBGEMM_API void
Bfloat16ToFloat_simd(const bfloat16* src, float* dst, size_t size);
/**
* @brief AVX2 implementation to convert fp32 numbers to bf16 numbers.
*
*/
FBGEMM_API void
FloatToBfloat16_avx2(const float* src, bfloat16* dst, size_t size);
/**
* @brief AVX512 implementation to convert fp32 numbers to bf16 numbers.
*
*/
FBGEMM_API void
FloatToBfloat16_avx512(const float* src, bfloat16* dst, size_t size);
/**
* @brief AVX2 implementation to convert bf16 numbers to fp32 numbers.
*
*/
FBGEMM_API void
Bfloat16ToFloat_avx2(const bfloat16* src, float* dst, size_t size);
/**
* @brief AVX512 implementation to convert bf16 numbers to fp32 numbers.
*
*/
FBGEMM_API void
Bfloat16ToFloat_avx512(const bfloat16* src, float* dst, size_t size);
/**
* @ Transform all entries in a matrix from fp32 to float16: reference
* implementation.
*
* @param do_clip if true we saturate to fp16 min and max instead of generating
* infinities.
*/
FBGEMM_API void FloatToFloat16_ref(
const float* src,
float16* dst,
size_t size,
bool do_clip = false);
/**
* @ Transform all entries in a matrix from float16 to fp32: reference
* implementation.
*
*/
FBGEMM_API void Float16ToFloat_ref(const float16* src, float* dst, size_t size);
/**
* @ Transform all entries in a matrix from fp32 to float16: simd
* implementation.
*
* @param do_clip if true we saturate to fp16 min and max instead of generating
* infinities.
*/
FBGEMM_API void FloatToFloat16_simd(
const float* src,
float16* dst,
size_t size,
bool do_clip = false);
/**
* @ Transform all entries in a matrix from float16 to fp32: simd
* implementation.
*
*/
FBGEMM_API void
Float16ToFloat_simd(const float16* src, float* dst, size_t size);
/**
* @brief AVX2 implementation to convert fp32 numbers to fp16 numbers.
*
*/
FBGEMM_API void FloatToFloat16_avx2(
const float* src,
float16* dst,
size_t size,
bool do_clip = false);
/**
* @brief AVX512 implementation to convert fp32 numbers to fp16 numbers.
*
*/
FBGEMM_API void FloatToFloat16_avx512(
const float* src,
float16* dst,
size_t size,
bool do_clip = false);
/**
* @brief AVX2 implementation to convert fp16 numbers to fp32 numbers.
*
*/
FBGEMM_API void
Float16ToFloat_avx2(const float16* src, float* dst, size_t size);
/**
* @brief AVX512 implementation to convert fp16 numbers to fp32 numbers.
*
*/
FBGEMM_API void
Float16ToFloat_avx512(const float16* src, float* dst, size_t size);
/**
* @brief Transform all entries in a matrix from fp32 to float16 and back to
* fp32.
*/
FBGEMM_API void RoundToFloat16(
const float* input,
float* output,
size_t size,
bool clamp = false,
bool clamp_denorms = false);
/**
* @brief Quantize float32 to float8. The code is a copy of float_to_hfp8() in
* fbgemm_gpu/quantize_ops_utils.h
*/
FBGEMM_API void FloatToFloat8_ref(
const float input,
uint8_t* output,
int exponent_bits,
int exponent_bias);
/**
* @brief Dequantize float8 to float32. The code is a copy of hf8_to_float() in
* fbgemm_gpu/quantize_ops_utils.h
*/
FBGEMM_API void Float8ToFloat_ref(
const uint8_t input,
float* output,
int exponent_bits,
int exponent_bias);
} // namespace fbgemm