1 | /******************************************************************************* |
2 | * Copyright 2017-2022 Intel Corporation |
3 | * Copyright 2020 FUJITSU LIMITED |
4 | * |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | * you may not use this file except in compliance with the License. |
7 | * You may obtain a copy of the License at |
8 | * |
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | * |
11 | * Unless required by applicable law or agreed to in writing, software |
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | * See the License for the specific language governing permissions and |
15 | * limitations under the License. |
16 | *******************************************************************************/ |
17 | |
18 | #include "cpu/reorder/cpu_reorder.hpp" |
19 | |
20 | namespace dnnl { |
21 | namespace impl { |
22 | namespace cpu { |
23 | |
24 | /* regular reorders */ |
25 | static const std::map<reorder_impl_key_t, const void *> & |
26 | regular_impl_list_map() { |
27 | static const std::map<reorder_impl_key_t, const void *> the_map = { |
28 | {{f32, bf16, 0}, ®ular_f32_bf16_impl_list_map()}, |
29 | {{f32, f16, 0}, ®ular_f32_f16_impl_list_map()}, |
30 | {{f32, f32, 0}, ®ular_f32_f32_impl_list_map()}, |
31 | {{f32, s32, 0}, ®ular_f32_s32_impl_list_map()}, |
32 | {{f32, s8, 0}, ®ular_f32_s8_impl_list_map()}, |
33 | {{f32, u8, 0}, ®ular_f32_u8_impl_list_map()}, |
34 | {{bf16, data_type::undef, 0}, ®ular_bf16_impl_list_map()}, |
35 | {{f16, data_type::undef, 0}, ®ular_f16_impl_list_map()}, |
36 | {{s32, data_type::undef, 0}, ®ular_s32_impl_list_map()}, |
37 | {{s8, data_type::undef, 0}, ®ular_s8_impl_list_map()}, |
38 | {{u8, data_type::undef, 0}, ®ular_u8_impl_list_map()}, |
39 | }; |
40 | return the_map; |
41 | } |
42 | |
43 | /* conv reorders w/ compensation */ |
44 | static const std::map<reorder_impl_key_t, const void *> & |
45 | comp_s8s8_impl_list_map() { |
46 | static const std::map<reorder_impl_key_t, const void *> the_map = { |
47 | {{f32, s8, 0}, &comp_f32_s8_impl_list_map()}, |
48 | {{bf16, s8, 0}, &comp_bf16_s8_impl_list_map()}, |
49 | {{s8, s8, 0}, &comp_s8_s8_impl_list_map()}, |
50 | }; |
51 | return the_map; |
52 | } |
53 | |
54 | const impl_list_item_t *cpu_engine_impl_list_t::get_reorder_implementation_list( |
55 | const memory_desc_t *src_md, const memory_desc_t *dst_md) { |
56 | reorder_impl_key_t dt_pair {src_md->data_type, dst_md->data_type, 0}; |
57 | const bool do_comp_s8s8 = dst_md->extra.flags |
58 | & (memory_extra_flags::compensation_conv_s8s8 |
59 | | memory_extra_flags::compensation_conv_asymmetric_src); |
60 | const auto &map = do_comp_s8s8 ? comp_s8s8_impl_list_map() |
61 | : regular_impl_list_map(); |
62 | |
63 | static const impl_list_item_t empty_list[] = {nullptr}; |
64 | auto top_map_it = map.find(dt_pair); |
65 | if (top_map_it == map.end()) { |
66 | dt_pair.dst_dt = data_type::undef; |
67 | top_map_it = map.find(dt_pair); |
68 | if (top_map_it == map.end()) return empty_list; |
69 | } |
70 | |
71 | const impl_list_map_t *p_impl_list |
72 | = (const impl_list_map_t *)top_map_it->second; |
73 | reorder_impl_key_t key {dt_pair.src_dt, dt_pair.dst_dt, src_md->ndims}; |
74 | |
75 | { |
76 | const auto it = p_impl_list->find(key); |
77 | if (it != p_impl_list->cend()) return it->second.data(); |
78 | } |
79 | |
80 | { |
81 | key.ndims = 0; |
82 | const auto it = p_impl_list->find(key); |
83 | if (it != p_impl_list->cend()) return it->second.data(); |
84 | } |
85 | |
86 | return empty_list; |
87 | } |
88 | |
89 | } // namespace cpu |
90 | } // namespace impl |
91 | } // namespace dnnl |
92 | |