Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / gpu / roi_pooling_gpu.cpp
1 /*
2 // Copyright (c) 2017-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 "roi_pooling_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 "roi_pooling/roi_pooling_kernel_selector.h"
23 #include "roi_pooling/roi_pooling_kernel_ref.h"
24
25 namespace cldnn { namespace gpu {
26
27 namespace {
28     static inline bool hasSingleBatchOutput(const program_node & node)
29     {
30         const auto & batch = node.get_output_layout().size.batch;
31
32         return batch.empty() || (batch.size() == 1 && batch[0] == 1);
33     }
34
35     kernel_selector::pool_type cldnn_2_pool_type(pooling_mode mode)
36     {
37         switch (mode)
38         {
39             case pooling_mode::max:
40                 return kernel_selector::pool_type::MAX;
41             case pooling_mode::average:
42                 return kernel_selector::pool_type::AVG;
43             case pooling_mode::average_no_padding:
44                 return kernel_selector::pool_type::AVG;
45             case pooling_mode::bilinear:
46                 return kernel_selector::pool_type::BILINEAR;
47             default:
48                 assert(0);
49                 return kernel_selector::pool_type::MAX;
50         }
51     }
52 }
53
54
55 struct roi_pooling_gpu : typed_primitive_gpu_impl<roi_pooling>
56 {
57     using parent = typed_primitive_gpu_impl<roi_pooling>;
58     using parent::parent;
59
60 protected:
61
62     virtual kernel::kernel_arguments_data get_arguments(typed_primitive_inst<roi_pooling>& instance, int32_t) const override
63     {
64         kernel::kernel_arguments_data args;
65
66         args.inputs = { &instance.input_memory(), &instance.rois_memory() };
67         args.output = &instance.output_memory();
68
69         return args;
70     }
71
72 public:
73
74     static primitive_impl* create(const roi_pooling_node& arg)
75     {
76         const auto& input_layout    = arg.input().get_output_layout();
77         const auto& output_layout   = arg.get_output_layout();
78         const auto& rois_layout     = arg.rois().get_output_layout();
79         const auto& primitive       = arg.get_primitive();
80
81         const auto padding_filling_value = output_layout.data_padding.filling_value();
82
83         CLDNN_ERROR_NOT_EQUAL(arg.id(), "roi_pooling padding filling value", padding_filling_value, "padding mode", 0.0f, "Unknown padding mode in roi_pooling.");
84         CLDNN_ERROR_NOT_PROPER_FORMAT(arg.id(), "Input_layout.format", input_layout.format.value, "output_layout.format", output_layout.format);
85
86         auto roi_params = get_default_params<kernel_selector::roi_pooling_params>(arg);
87         auto roi_optional_params = get_default_optional_params<kernel_selector::roi_pooling_optional_params>(arg.get_program());
88
89         const auto& out = roi_params.output;
90
91         const auto roi_bfyx = convert_data_tensor(rois_layout);
92         const auto roi_bf = roi_bfyx.FlattenFeatureAndSpatials();
93         roi_params.inputs.push_back(roi_bf);
94         roi_params.output = { out.GetDims(), out.GetDType(), kernel_selector::data_layout::brfyx, out.GetViewOffset(), out.PhysicalSize(), out.GetPaddedVal() }; // TOOD: it's an hack - cldnn doesn't support roi pooling with batching
95         roi_params.mode               = cldnn_2_pool_type(primitive->mode);
96         roi_params.position_sensitive = primitive->position_sensitive;
97         roi_params.pooledWidth        = primitive->pooled_width;
98         roi_params.pooledHeight       = primitive->pooled_height;
99         roi_params.spatialScale       = primitive->spatial_scale;
100         roi_params.spatial_bins_x     = primitive->spatial_bins_x;
101         roi_params.spatial_bins_y     = primitive->spatial_bins_y;
102
103         auto& kernel_selector = kernel_selector::roi_pooling_kernel_selector::Instance();
104         auto best_kernels = kernel_selector.GetBestKernels(roi_params, roi_optional_params);
105
106         CLDNN_ERROR_BOOL(arg.id(), "Best_kernel.empty()", best_kernels.empty(), "Cannot find a proper kernel with this arguments");
107
108         auto roi_pool = new roi_pooling_gpu(arg, best_kernels[0]);
109
110         return roi_pool;
111     }
112 };
113
114 namespace {
115     struct attach
116     {
117         attach()
118         {
119             implementation_map<roi_pooling>::add(std::make_tuple(engine_types::ocl, data_types::f16, format::bfyx), roi_pooling_gpu::create);
120             implementation_map<roi_pooling>::add(std::make_tuple(engine_types::ocl, data_types::f32, format::bfyx), roi_pooling_gpu::create);
121         }
122
123         ~attach() {}
124     };
125
126     attach attach_impl;
127 }
128 } }