1/*******************************************************************************
2* Copyright 2020-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_REF_REDUCTION_HPP
18#define GPU_REF_REDUCTION_HPP
19
20#include "common/c_types_map.hpp"
21#include "common/primitive.hpp"
22#include "common/type_helpers.hpp"
23#include "common/utils.hpp"
24#include "gpu/compute/compute.hpp"
25#include "gpu/gpu_primitive.hpp"
26#include "gpu/gpu_reduction_pd.hpp"
27#include "gpu/gpu_resource.hpp"
28#include "gpu/primitive_conf.hpp"
29
30namespace dnnl {
31namespace impl {
32namespace gpu {
33namespace ocl {
34
35struct ref_reduction_t : public gpu_primitive_t {
36 using gpu_primitive_t::gpu_primitive_t;
37 struct pd_t : public gpu_reduction_pd_t {
38 using gpu_reduction_pd_t::gpu_reduction_pd_t;
39
40 DECLARE_COMMON_PD_T("reduction_ref:any", ref_reduction_t);
41
42 status_t init(engine_t *engine) {
43 using sm = primitive_attr_t::skip_mask_t;
44 const auto attr_skip_mask = sm::post_ops | sm::gpu_attr;
45
46 const bool ok = set_default_params() == status::success
47 && !memory_desc_ndims_ok(src_md(), dst_md())
48 && attr()->has_default_values(attr_skip_mask)
49 && post_ops_with_binary_ok(attr(), dst_md()->data_type, 5)
50 && attr_.set_default_formats(dst_md(0)) == status::success;
51 if (!ok) return status::unimplemented;
52
53 return init_conf(engine);
54 }
55
56 status_t init_conf(engine_t *engine);
57 status_t init_kernel_ctx(compute::kernel_ctx_t &kernel_ctx) const;
58
59 reduction_conf_t conf;
60 };
61
62 status_t init(engine_t *engine) override {
63 compute::kernel_ctx_t kernel_ctx;
64
65 status_t status = pd()->init_kernel_ctx(kernel_ctx);
66 CHECK(status);
67
68 status = create_kernel(engine, &kernel, "ref_reduce", kernel_ctx);
69 CHECK(status);
70
71 return status::success;
72 }
73
74 virtual status_t execute(const exec_ctx_t &ctx) const override {
75 return execute_ref(ctx);
76 }
77
78private:
79 status_t execute_ref(const exec_ctx_t &ctx) const;
80 const pd_t *pd() const { return (const pd_t *)primitive_t::pd().get(); }
81
82 compute::kernel_t kernel;
83};
84
85} // namespace ocl
86} // namespace gpu
87} // namespace impl
88} // namespace dnnl
89
90#endif
91