2 // Copyright (c) 2016 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"
25 namespace cldnn { namespace gpu {
28 struct softmax_gpu : typed_primitive_gpu_impl<softmax>
30 using parent = typed_primitive_gpu_impl<softmax>;
33 static primitive_impl* create(const softmax_node& arg)
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());
38 auto& input = sm_params.inputs[0];
39 auto& output = sm_params.output;
40 const auto primitive = arg.get_primitive();
42 switch (primitive->dimension)
44 case softmax::normalize_x:
45 sm_params.dim = kernel_selector::softmax_dim::X;
48 case softmax::normalize_y:
49 sm_params.dim = kernel_selector::softmax_dim::Y;
52 case softmax::normalize_fyx:
53 // Flatten fused with softmax
54 input = input.FlattenFeatureAndSpatials();
55 output = output.FlattenFeatureAndSpatials();
57 sm_params.dim = kernel_selector::softmax_dim::FEATURE;
60 case softmax::normalize_f:
61 sm_params.dim = kernel_selector::softmax_dim::FEATURE;
65 throw std::runtime_error("Wrong API - no such softmax");
68 auto& kernel_selector = kernel_selector::softmax_kernel_selector::Instance();
69 auto best_kernels = kernel_selector.GetBestKernels(sm_params, sm_optional_params);
71 CLDNN_ERROR_BOOL(arg.id(), "Best_kernel.empty()", best_kernels.empty(), "Cannot find a proper kernel with this arguments");
73 auto softmax_node = new softmax_gpu(arg, best_kernels[0]);
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);