Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / gpu / strided_slice_gpu.cpp
1 /*
2 // Copyright (c) 2019 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 "strided_slice_inst.h"
18 #include "primitive_gpu_base.h"
19 #include "implementation_map.h"
20 #include "kernel_selector_helper.h"
21 #include "strided_slice/strided_slice_kernel_ref.h"
22 #include "strided_slice/strided_slice_kernel_selector.h"
23 #include "error_handler.h"
24 #include "data_inst.h"
25
26 using namespace cldnn;
27
28 namespace cldnn
29 {
30 namespace gpu
31 {
32
33 struct strided_slice_gpu : typed_primitive_gpu_impl<strided_slice>
34 {
35     using parent = typed_primitive_gpu_impl<strided_slice>;
36     using parent::parent;
37 public:
38     static primitive_impl* create(const strided_slice_node& arg)
39     {
40         auto strided_slice_params = get_default_params<kernel_selector::strided_slice_params>(arg);
41         auto strided_slice_optional_params = get_default_optional_params<kernel_selector::strided_slice_optional_params>(arg.get_program());
42         const int32_t numberOfDims = 4;
43
44         auto complete_strided_slice_params = [&](std::vector<int32_t>& param) {
45             for (size_t i = param.size(); i < numberOfDims; ++i)
46                 param.push_back(1);
47         };
48
49         auto completeStridedSliceMasks = [&](std::vector<uint8_t>& mask) {
50             for (size_t i = mask.size(); i < numberOfDims; ++i)
51                 mask.push_back(0);
52         };
53
54         // Getting data from constant inputs. There are 3 args: Begin, End, Stride
55         for (size_t i = 1; i < arg.get_dependencies().size(); ++i) {
56             auto& input = arg.get_dependency(i).as<data>();
57             auto& mem = input.get_attached_memory();
58             int32_t* data = static_cast<int32_t*>(mem.lock());
59             std::vector<int32_t> vData = std::vector<int32_t>(data, data + input.get_output_layout().count());
60             complete_strided_slice_params(vData);
61             strided_slice_params.striding_params.push_back(vData);
62             mem.unlock();
63         }
64
65         strided_slice_params.end_mask = arg.get_primitive()->end_mask;
66         completeStridedSliceMasks(strided_slice_params.end_mask);
67         strided_slice_params.begin_mask = arg.get_primitive()->begin_mask;
68         completeStridedSliceMasks(strided_slice_params.begin_mask);
69         strided_slice_params.new_axis_mask = arg.get_primitive()->new_axis_mask;
70         strided_slice_params.shrink_axis_mask = arg.get_primitive()->shrink_axis_mask;
71         completeStridedSliceMasks(strided_slice_params.shrink_axis_mask);
72
73         auto& kernel_selector = kernel_selector::strided_slice_kernel_selector::Instance();
74         auto best_kernels = kernel_selector.GetBestKernels(strided_slice_params, strided_slice_optional_params);
75
76         CLDNN_ERROR_BOOL(arg.id(), "Best_kernel.empty()", best_kernels.empty(), "Cannot find a proper kernel with this arguments");
77
78         auto strided_slice = new strided_slice_gpu(arg, best_kernels[0]);
79
80         return strided_slice;
81     }
82 };
83
84 namespace
85 {
86     struct attach
87     {
88         attach()
89         {
90             auto val_fw = strided_slice_gpu::create;
91             implementation_map<strided_slice>::add(std::make_tuple(engine_types::ocl, data_types::f32, format::bfyx), val_fw);
92             implementation_map<strided_slice>::add(std::make_tuple(engine_types::ocl, data_types::f16, format::bfyx), val_fw);
93         }
94         ~attach() = default;
95     };
96     attach attach_impl;
97 }
98 } //namespace gpu
99 } //namespace cldnn