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