Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / gpu / activation_grad_gpu.cpp
1 /*
2 // Copyright (c) 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 #include "activation_grad_inst.h"
18 #include "primitive_gpu_base.h"
19 #include "implementation_map.h"
20 #include "error_handler.h"
21 #include "kernel_selector_helper.h"
22 #include "activation/activation_kernel_selector.h"
23 #include "activation/activation_kernel_base.h"
24 #include "api/CPP/activation_grad.hpp"
25
26 namespace cldnn { namespace gpu {
27
28
29 struct activation_grad_gpu : typed_primitive_gpu_impl<activation_grad>
30 {
31     using parent = typed_primitive_gpu_impl<activation_grad>;
32     using parent::parent;
33
34     virtual kernel::kernel_arguments_data get_arguments(typed_primitive_inst<activation_grad>& instance, int32_t split) const override
35     {
36         kernel::kernel_arguments_data args = parent::get_arguments(instance, split);
37
38         if (_outer.is_parameterized())
39         {
40             args.slope = &instance.slope_memory();
41         }
42
43         return args;
44     }
45
46     static primitive_impl* create(const activation_grad_node& arg) 
47     { 
48         auto activation_grad_params = get_default_params<kernel_selector::activation_params>(arg);
49         auto activation_grad_optional_params = get_default_optional_params<kernel_selector::activation_optional_params>(arg.get_program());
50
51         const auto& primitive = arg.get_primitive();
52
53         activation_grad_params.gradient = true;
54         activation_grad_params.inputs.push_back(convert_data_tensor(arg.get_dependency(1).get_output_layout()));
55         activation_grad_params.activation.function = get_kernel_selector_activation_grad_param(primitive->activation_grad_func);
56         activation_grad_params.activation.m = primitive->additional_params.a;
57         activation_grad_params.activation.n = primitive->additional_params.b;
58
59         if (arg.is_parameterized())
60         {
61             const auto& slope_layout = arg.slope_input().get_output_layout();
62             const auto& output_layout = arg.get_output_layout();
63
64             const auto params_num = kernel_selector::GetActivationAdditionalParamsNumber(activation_grad_params.activation.function);
65
66             CLDNN_ERROR_LESS_THAN(arg.id(), "Slope layout size count", slope_layout.size.count(), "output_layout.size.feature[0] * params_num", static_cast<size_t>(output_layout.size.feature[0] * params_num), "Error - not enough data inside additional params buffer");
67
68             activation_grad_params.inputActivationParams.push_back(convert_data_tensor(slope_layout));
69         }
70
71         auto& kernel_selector = kernel_selector::activation_kernel_selector::Instance();
72         auto best_kernels = kernel_selector.GetBestKernels(activation_grad_params, activation_grad_optional_params);
73         CLDNN_ERROR_BOOL(arg.id(), "Best_kernel.empty()", best_kernels.empty(), "Cannot find a proper kernel with this arguments");
74
75         auto activation_grad = new activation_grad_gpu(arg, best_kernels[0]);
76
77         return activation_grad;
78     };
79 };
80
81
82 namespace {
83     struct attach {
84         attach() {
85             auto val_fw = activation_grad_gpu::create;
86     
87             implementation_map<activation_grad>::add({
88                 { std::make_tuple(engine_types::ocl, data_types::f32, format::yxfb), val_fw},
89                 { std::make_tuple(engine_types::ocl, data_types::f16, format::yxfb), val_fw},
90                 { std::make_tuple(engine_types::ocl, data_types::f32, format::bfyx), val_fw },
91                 { std::make_tuple(engine_types::ocl, data_types::f16, format::bfyx), val_fw },
92                 { std::make_tuple(engine_types::ocl, data_types::f32, format::byxf), val_fw },
93                 { std::make_tuple(engine_types::ocl, data_types::f16, format::byxf), val_fw },
94             });
95         }
96         ~attach() {}
97     };
98     attach attach_impl;
99 }
100 } }