1 | /******************************************************************************* |
2 | * Copyright 2017-2018 Intel Corporation |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | *******************************************************************************/ |
16 | |
17 | #ifndef CPU_SIMPLE_Q10N_HPP |
18 | #define CPU_SIMPLE_Q10N_HPP |
19 | |
20 | #include <assert.h> |
21 | |
22 | #include "c_types_map.hpp" |
23 | #include "math_utils.hpp" |
24 | #include "nstl.hpp" |
25 | #include "type_helpers.hpp" |
26 | #include "utils.hpp" |
27 | |
28 | namespace mkldnn { |
29 | namespace impl { |
30 | namespace cpu { |
31 | |
32 | using namespace mkldnn::impl::math; |
33 | |
34 | template <typename out_t> |
35 | inline out_t round_and_saturate(float f) |
36 | { return math::saturate<out_t>(out_round<int>(f)); } |
37 | |
38 | /* Quantization with alpha == 1 and beta == 0 */ |
39 | template <typename in_t, typename out_t, typename enabled = void> |
40 | struct qz_a1b0 { |
41 | out_t operator()(in_t in) |
42 | { return round_and_saturate<out_t>((float)in); } |
43 | }; |
44 | |
45 | template <typename in_t, typename out_t> |
46 | struct qz_a1b0<in_t, out_t, |
47 | typename utils::enable_if<true |
48 | && nstl::is_integral<in_t>::value |
49 | && !is_subset<in_t, out_t>::value |
50 | >::type> { |
51 | out_t operator()(in_t in) { return math::saturate<out_t>(in); } |
52 | }; |
53 | |
54 | template <typename in_t, typename out_t> |
55 | struct qz_a1b0<in_t, out_t, |
56 | typename utils::enable_if<is_subset<in_t, out_t>::value>::type> { |
57 | out_t operator()(in_t in) { return (out_t)in; } |
58 | }; |
59 | |
60 | /* Quantization with alpha == 1 */ |
61 | template <typename in_t, typename out_t> struct qz_a1 { |
62 | out_t operator()(in_t in, out_t out, float beta) |
63 | { return round_and_saturate<out_t>((float)in + beta * out); } |
64 | }; |
65 | |
66 | template <typename in_t> struct qz_a1<in_t, float> { |
67 | float operator()(in_t in, float out, float beta) |
68 | { return (float)in + beta * out; } |
69 | }; |
70 | |
71 | /* Quantization with beta == 0 */ |
72 | template <typename in_t, typename out_t> struct qz_b0 { |
73 | out_t operator()(in_t in, float alpha) |
74 | { return round_and_saturate<out_t>(alpha * in); } |
75 | }; |
76 | |
77 | template <typename in_t> struct qz_b0<in_t, float> { |
78 | float operator()(in_t in, float alpha) { return alpha * in; } |
79 | }; |
80 | |
81 | /* Quantization */ |
82 | template <typename in_t, typename out_t> struct qz { |
83 | out_t operator()(in_t in, out_t out, float alpha, float beta) { |
84 | return round_and_saturate<out_t>( |
85 | alpha * in + (beta ? beta * out : 0)); |
86 | } |
87 | }; |
88 | |
89 | template <typename in_t> struct qz<in_t, float> { |
90 | float operator()(in_t in, float out, float alpha, float beta) |
91 | { return alpha * in + (beta ? beta * out : 0); } |
92 | }; |
93 | |
94 | } |
95 | } |
96 | } |
97 | |
98 | #endif |
99 | |