fba473906cf0349a0f1572f3c623abf6aad37e74
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / gpu / softmax_gpu.cpp
1 /*
2 // Copyright (c) 2016-2019 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 "softmax_inst.h"
18 #include "primitive_gpu_base.h"
19 #include "implementation_map.h"
20 #include "kernel_selector_helper.h"
21 #include "softmax/softmax_kernel_selector.h"
22 #include "softmax/softmax_kernel_base.h"
23 #include "error_handler.h"
24
25 namespace cldnn {
26 namespace gpu {
27
28 struct softmax_gpu : typed_primitive_gpu_impl<softmax> {
29     using parent = typed_primitive_gpu_impl<softmax>;
30     using parent::parent;
31
32     static primitive_impl* create(const softmax_node& arg) {
33         auto sm_params = get_default_params<kernel_selector::softmax_params>(arg);
34         auto sm_optional_params =
35             get_default_optional_params<kernel_selector::softmax_optional_params>(arg.get_program());
36
37         auto& input = sm_params.inputs[0];
38         auto& output = sm_params.output;
39         const auto primitive = arg.get_primitive();
40
41         switch (primitive->dimension) {
42             case softmax::normalize_x:
43                 sm_params.dim = kernel_selector::softmax_dim::X;
44                 break;
45
46             case softmax::normalize_y:
47                 sm_params.dim = kernel_selector::softmax_dim::Y;
48                 break;
49
50             case softmax::normalize_fyx:
51                 // Flatten fused with softmax
52                 input = input.FlattenFeatureAndSpatials();
53                 output = output.FlattenFeatureAndSpatials();
54
55                 sm_params.dim = kernel_selector::softmax_dim::FEATURE;
56                 break;
57
58             case softmax::normalize_f:
59                 sm_params.dim = kernel_selector::softmax_dim::FEATURE;
60                 break;
61
62             case softmax::normalize_z:
63                 sm_params.dim = kernel_selector::softmax_dim::Z;
64                 break;
65
66             case softmax::normalize_all:
67                 input = input.FlattenEverything();
68                 output = output.FlattenEverything();
69
70                 sm_params.dim = kernel_selector::softmax_dim::FEATURE;
71                 break;
72
73             default:
74                 throw std::runtime_error("Wrong API - no such softmax");
75         }
76
77         auto& kernel_selector = kernel_selector::softmax_kernel_selector::Instance();
78         auto best_kernels = kernel_selector.GetBestKernels(sm_params, sm_optional_params);
79
80         CLDNN_ERROR_BOOL(arg.id(),
81                          "Best_kernel.empty()",
82                          best_kernels.empty(),
83                          "Cannot find a proper kernel with this arguments");
84
85         auto softmax_node = new softmax_gpu(arg, best_kernels[0]);
86
87         return softmax_node;
88     }
89 };
90
91 namespace {
92 struct attach {
93     attach() {
94         auto val_fw = softmax_gpu::create;
95         implementation_map<softmax>::add(std::make_tuple(engine_types::ocl, data_types::f32, format::yxfb), val_fw);
96         implementation_map<softmax>::add(std::make_tuple(engine_types::ocl, data_types::f16, format::yxfb), val_fw);
97         implementation_map<softmax>::add(std::make_tuple(engine_types::ocl, data_types::f32, format::bfyx), val_fw);
98         implementation_map<softmax>::add(std::make_tuple(engine_types::ocl, data_types::f16, format::bfyx), val_fw);
99         implementation_map<softmax>::add(std::make_tuple(engine_types::ocl, data_types::f32, format::byxf), val_fw);
100         implementation_map<softmax>::add(std::make_tuple(engine_types::ocl, data_types::f16, format::byxf), val_fw);
101         implementation_map<softmax>::add(std::make_tuple(engine_types::ocl, data_types::f32, format::bfzyx), val_fw);
102         implementation_map<softmax>::add(std::make_tuple(engine_types::ocl, data_types::f16, format::bfzyx), val_fw);
103     }
104     ~attach() {}
105 };
106
107 attach attach_impl;
108 }  // namespace
109
110 }  // namespace gpu
111 }  // namespace cldnn