-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathcfg.cpp
99 lines (88 loc) · 2.74 KB
/
cfg.cpp
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
/*******************************************************************************
* Copyright 2018-2025 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dnnl_common.hpp"
#include "oneapi/dnnl/dnnl.h"
#include "reorder.hpp"
namespace reorder {
const float int_max_exact = 1 << 24;
const float f16_max_exact = 1 << 11;
const float f4_max_exact = 1 << 3;
#define REG(dt, min, max) \
const dt_conf_t CONCAT2(_conf_, dt) = {CONCAT2(dnnl_, dt), min, max}; \
const dt_conf_t *CONCAT2(conf_, dt) = &CONCAT2(_conf_, dt);
REG(f32, -int_max_exact, int_max_exact);
REG(f64, -int_max_exact, int_max_exact);
REG(f16, -f16_max_exact, f16_max_exact);
REG(bf16, -int_max_exact, int_max_exact);
REG(f8_e5m2, -f16_max_exact, f16_max_exact);
REG(f8_e4m3, -f16_max_exact, f16_max_exact);
REG(f4_e2m1, -f16_max_exact, f16_max_exact);
REG(f4_e3m0, -f4_max_exact, f4_max_exact);
// Do not exceed min/max float value representable in integer. Otherwise, we get
// a correctness issue caused by different computations or roudings in the naive
// reference and the library. One of those can be zero-point subtracting which
// leads to underflow or overflow.
REG(s32, -BENCHDNN_S32_TO_F32_SAT_CONST, BENCHDNN_S32_TO_F32_SAT_CONST);
REG(s8, INT8_MIN, INT8_MAX);
REG(u8, 0, UINT8_MAX);
REG(s4, -7, 8);
REG(u4, 0, 15);
#undef REG
const dt_conf_t *dt2cfg(dnnl_data_type_t dt) {
#define CASE(cfg) \
if (CONCAT2(dnnl_, cfg) == dt) return CONCAT2(conf_, cfg)
CASE(f32);
CASE(f64);
CASE(f16);
CASE(bf16);
CASE(f8_e5m2);
CASE(f8_e4m3);
CASE(f4_e2m1);
CASE(f4_e3m0);
CASE(s32);
CASE(s8);
CASE(u8);
CASE(s4);
CASE(u4);
#undef CASE
SAFE_V(FAIL);
return conf_f32;
}
dnnl_data_type_t cfg2dt(const dt_conf_t *cfg) {
#define CASE(_cfg) \
if (cfg == CONCAT2(conf_, _cfg)) return CONCAT2(dnnl_, _cfg)
CASE(f32);
CASE(f64);
CASE(f16);
CASE(bf16);
CASE(f8_e5m2);
CASE(f8_e4m3);
CASE(f4_e2m1);
CASE(f4_e3m0);
CASE(s32);
CASE(s8);
CASE(u8);
CASE(s4);
CASE(u4);
#undef CASE
SAFE_V(FAIL);
return dnnl_f32;
}
} // namespace reorder