Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / gpu / arg_max_min_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 "arg_max_min_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 "arg_max_min/arg_max_min_kernel_selector.h"
23 #include "arg_max_min/arg_max_min_kernel_base.h"
24 #include "kernel_runner.h"
25
26 namespace cldnn {
27         namespace gpu {
28
29                 struct arg_max_min_gpu : typed_primitive_gpu_impl<arg_max_min>
30                 {
31                         using parent = typed_primitive_gpu_impl<arg_max_min>;
32                         using parent::parent;
33
34                 protected:
35
36                         virtual kernel::kernel_arguments_data get_arguments(typed_primitive_inst<arg_max_min>& instance, int32_t) const override
37                         {
38                                 kernel::kernel_arguments_data args = parent::get_arguments(instance, 0);
39
40                                 return args;
41                         }
42
43                 public:
44
45                         static primitive_impl* create(const arg_max_min_node &arg)
46                         {
47                                 const auto& primitive = arg.get_primitive();
48
49                                 const auto& axis = primitive->axis;
50                                 const auto& top_k = primitive->top_k;
51                                 const auto& out_type = primitive->output_type;
52                                 const auto& with_axis = primitive->with_axis;
53
54                                 auto argm_params = get_default_params<kernel_selector::arg_max_min_params>(arg);
55                                 auto argm_optional_params = get_default_optional_params<kernel_selector::arg_max_min_optional_params>(arg.get_program());
56
57                                 argm_params.topK = top_k;
58                                 if (with_axis) {
59                                         switch (axis)
60                                         {
61                     case arg_max_min::batch:
62                                                 argm_params.argMaxMinAxis = kernel_selector::argm_axis::BATCH;
63                         break;
64                                         case arg_max_min::feature:
65                                                 argm_params.argMaxMinAxis = kernel_selector::argm_axis::FEATURE;
66                         break;
67                     case arg_max_min::x:
68                                                 argm_params.argMaxMinAxis = kernel_selector::argm_axis::X;
69                         break;
70                     case arg_max_min::y:
71                                                 argm_params.argMaxMinAxis = kernel_selector::argm_axis::Y;
72                         break;
73                     default:
74                                                 break;
75                                         }
76                                 }
77
78                                 if (out_type == primitive->max)
79                                         argm_params.argMaxMinOut = kernel_selector::argm_output::MAX;
80                                 else
81                                         argm_params.argMaxMinOut = kernel_selector::argm_output::MIN;
82                                 auto& kernel_selector = kernel_selector::arg_max_min_kernel_selector::Instance();
83
84                                 kernel_selector::KernelsData best_kernels = kernel_selector.GetBestKernels(argm_params, argm_optional_params);
85
86                                 CLDNN_ERROR_BOOL(arg.id(), "Best_kernel.empty()", best_kernels.empty(), "Cannot find a proper kernel with this arguments");
87
88                                 auto conv = new arg_max_min_gpu(arg, best_kernels[0]);
89
90                                 return conv;
91                         }
92                 };
93
94                 namespace {
95                         struct attach {
96                                 attach() {
97                                         implementation_map<arg_max_min>::add(std::make_tuple(engine_types::ocl, data_types::f32, format::bfyx), arg_max_min_gpu::create);
98                                         implementation_map<arg_max_min>::add(std::make_tuple(engine_types::ocl, data_types::f16, format::bfyx), arg_max_min_gpu::create);
99                                         implementation_map<arg_max_min>::add(std::make_tuple(engine_types::ocl, data_types::i8, format::bfyx), arg_max_min_gpu::create);
100                                         implementation_map<arg_max_min>::add(std::make_tuple(engine_types::ocl, data_types::f32, format::yxfb), arg_max_min_gpu::create);
101                                         implementation_map<arg_max_min>::add(std::make_tuple(engine_types::ocl, data_types::f16, format::yxfb), arg_max_min_gpu::create);
102                                         implementation_map<arg_max_min>::add(std::make_tuple(engine_types::ocl, data_types::i8, format::yxfb), arg_max_min_gpu::create);
103                                 }
104                                 ~attach() {}
105                         };
106                         attach attach_impl;
107                 }
108         }
109 }