1 | /******************************************************************************* |
2 | * Copyright 2019-2022 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 GPU_OCL_REF_SHUFFLE_HPP |
18 | #define GPU_OCL_REF_SHUFFLE_HPP |
19 | |
20 | #include "common/c_types_map.hpp" |
21 | #include "common/primitive.hpp" |
22 | #include "gpu/compute/compute.hpp" |
23 | #include "gpu/gpu_primitive.hpp" |
24 | #include "gpu/gpu_resource.hpp" |
25 | #include "gpu/gpu_shuffle_pd.hpp" |
26 | #include "gpu/ocl/ocl_engine.hpp" |
27 | #include "gpu/ocl/ocl_stream.hpp" |
28 | #include "gpu/primitive_conf.hpp" |
29 | |
30 | namespace dnnl { |
31 | namespace impl { |
32 | namespace gpu { |
33 | namespace ocl { |
34 | |
35 | struct ref_shuffle_t : public gpu_primitive_t { |
36 | using gpu_primitive_t::gpu_primitive_t; |
37 | struct pd_t : public gpu_shuffle_pd_t { |
38 | using gpu_shuffle_pd_t::gpu_shuffle_pd_t; |
39 | |
40 | DECLARE_COMMON_PD_T("ocl:ref:any" , ref_shuffle_t); |
41 | |
42 | status_t init(engine_t *engine) { |
43 | using namespace format_tag; |
44 | auto *compute_engine |
45 | = utils::downcast<compute::compute_engine_t *>(engine); |
46 | |
47 | const auto &md_src = is_fwd() ? src_md() : diff_src_md(); |
48 | const auto &md_dst = is_fwd() ? dst_md() : diff_dst_md(); |
49 | const memory_desc_wrapper src_d(md_src); |
50 | const memory_desc_wrapper dst_d(md_dst); |
51 | |
52 | bool ok = src_d.data_type() == dst_d.data_type() |
53 | && attr()->has_default_values() |
54 | && !memory_desc_ndims_ok(src_d.md_, dst_d.md_) |
55 | && set_default_formats_common() && src_d == dst_d |
56 | && IMPLICATION(src_md()->data_type == data_type::f16, |
57 | compute_engine->mayiuse( |
58 | compute::device_ext_t::khr_fp16)); |
59 | if (!ok) return status::unimplemented; |
60 | |
61 | return init_conf(engine); |
62 | } |
63 | |
64 | status_t init_conf(engine_t *engine); |
65 | status_t init_kernel_ctx(compute::kernel_ctx_t &kernel_ctx) const; |
66 | |
67 | shuffle_conf_t conf; |
68 | offsets_t off; |
69 | }; |
70 | |
71 | status_t init(engine_t *engine) override { |
72 | compute::kernel_ctx_t kernel_ctx; |
73 | |
74 | status_t status = pd()->init_kernel_ctx(kernel_ctx); |
75 | if (status != status::success) return status; |
76 | |
77 | create_kernel(engine, &kernel_, "ref_shuffle" , kernel_ctx); |
78 | if (!kernel_) return status::runtime_error; |
79 | |
80 | return status::success; |
81 | } |
82 | |
83 | status_t execute(const exec_ctx_t &ctx) const override { |
84 | return execute_<format_tag::any>(ctx); |
85 | } |
86 | |
87 | private: |
88 | template <format_tag_t tag> |
89 | status_t execute_(const exec_ctx_t &ctx) const; |
90 | const pd_t *pd() const { return (const pd_t *)primitive_t::pd().get(); } |
91 | compute::kernel_t kernel_; |
92 | }; |
93 | |
94 | } // namespace ocl |
95 | } // namespace gpu |
96 | } // namespace impl |
97 | } // namespace dnnl |
98 | |
99 | #endif |
100 | |