2 // Copyright (c) 2016-2019 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
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"
28 struct softmax_gpu : typed_primitive_gpu_impl<softmax> {
29 using parent = typed_primitive_gpu_impl<softmax>;
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());
37 auto& input = sm_params.inputs[0];
38 auto& output = sm_params.output;
39 const auto primitive = arg.get_primitive();
41 switch (primitive->dimension) {
42 case softmax::normalize_x:
43 sm_params.dim = kernel_selector::softmax_dim::X;
46 case softmax::normalize_y:
47 sm_params.dim = kernel_selector::softmax_dim::Y;
50 case softmax::normalize_fyx:
51 // Flatten fused with softmax
52 input = input.FlattenFeatureAndSpatials();
53 output = output.FlattenFeatureAndSpatials();
55 sm_params.dim = kernel_selector::softmax_dim::FEATURE;
58 case softmax::normalize_f:
59 sm_params.dim = kernel_selector::softmax_dim::FEATURE;
62 case softmax::normalize_z:
63 sm_params.dim = kernel_selector::softmax_dim::Z;
66 case softmax::normalize_all:
67 input = input.FlattenEverything();
68 output = output.FlattenEverything();
70 sm_params.dim = kernel_selector::softmax_dim::FEATURE;
74 throw std::runtime_error("Wrong API - no such softmax");
77 auto& kernel_selector = kernel_selector::softmax_kernel_selector::Instance();
78 auto best_kernels = kernel_selector.GetBestKernels(sm_params, sm_optional_params);
80 CLDNN_ERROR_BOOL(arg.id(),
81 "Best_kernel.empty()",
83 "Cannot find a proper kernel with this arguments");
85 auto softmax_node = new softmax_gpu(arg, best_kernels[0]);
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);