• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018-2020 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "src/core/CL/kernels/CLConvertFullyConnectedWeightsKernel.h"
25 
26 #include "arm_compute/core/CL/CLHelpers.h"
27 #include "arm_compute/core/CL/CLKernelLibrary.h"
28 #include "arm_compute/core/CL/ICLTensor.h"
29 #include "arm_compute/core/Helpers.h"
30 #include "src/core/CL/CLValidate.h"
31 #include "src/core/helpers/AutoConfiguration.h"
32 #include "src/core/helpers/WindowHelpers.h"
33 #include "support/StringSupport.h"
34 
35 namespace arm_compute
36 {
CLConvertFullyConnectedWeightsKernel()37 CLConvertFullyConnectedWeightsKernel::CLConvertFullyConnectedWeightsKernel()
38     : _input(nullptr), _output(nullptr)
39 {
40 }
41 
configure(const ICLTensor * input,ICLTensor * output,const TensorShape & original_input_shape,DataLayout data_layout)42 void CLConvertFullyConnectedWeightsKernel::configure(const ICLTensor *input, ICLTensor *output, const TensorShape &original_input_shape,
43                                                      DataLayout data_layout)
44 {
45     configure(CLKernelLibrary::get().get_compile_context(), input, output, original_input_shape, data_layout);
46 }
47 
configure(const CLCompileContext & compile_context,const ICLTensor * input,ICLTensor * output,const TensorShape & original_input_shape,DataLayout data_layout)48 void CLConvertFullyConnectedWeightsKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const TensorShape &original_input_shape,
49                                                      DataLayout data_layout)
50 {
51     ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
52 
53     // Output tensor auto initialisation if not yet initialized
54     auto_init_if_empty(*output->info(), *input->info()->clone());
55 
56     auto padding_info = get_padding_info({ input, output });
57 
58     ARM_COMPUTE_ERROR_THROW_ON(CLConvertFullyConnectedWeightsKernel::validate(input->info(), output->info(), original_input_shape, data_layout));
59 
60     _input  = input;
61     _output = output;
62 
63     const DataLayout input_data_layout = (data_layout == DataLayout::NCHW) ? DataLayout::NHWC : DataLayout::NCHW;
64 
65     const int width_idx   = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::WIDTH);
66     const int height_idx  = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::HEIGHT);
67     const int channel_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::CHANNEL);
68 
69     const unsigned int num_elems_per_input_plane = original_input_shape[width_idx] * original_input_shape[height_idx];
70     const unsigned int num_channels              = original_input_shape[channel_idx];
71 
72     const unsigned int factor_1 = (data_layout == DataLayout::NCHW) ? num_elems_per_input_plane : num_channels;
73     const unsigned int factor_2 = (data_layout == DataLayout::NCHW) ? num_channels : num_elems_per_input_plane;
74 
75     // Set build options
76     CLBuildOptions build_opts;
77     build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->info()->element_size()));
78     build_opts.add_option("-DFACTOR_1=" + support::cpp11::to_string(factor_1));
79     build_opts.add_option("-DFACTOR_2=" + support::cpp11::to_string(factor_2));
80 
81     // Create kernel
82     _kernel = create_kernel(compile_context, "convert_fc_weights", build_opts.options());
83 
84     // Configure kernel window
85     Window win = calculate_max_window(*input->info(), Steps());
86     ICLKernel::configure_internal(win);
87 
88     ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
89 }
90 
validate(const ITensorInfo * input,const ITensorInfo * output,const TensorShape & original_input_shape,DataLayout data_layout)91 Status CLConvertFullyConnectedWeightsKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const TensorShape &original_input_shape,
92                                                       DataLayout data_layout)
93 {
94     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
95     ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
96     ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
97     ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() != 2);
98     ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(1) != original_input_shape.total_size_lower(3));
99     ARM_COMPUTE_RETURN_ERROR_ON(data_layout == DataLayout::UNKNOWN);
100 
101     // Checks performed when output is configured
102     if(output->total_size() != 0)
103     {
104         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
105         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
106     }
107 
108     return Status{};
109 }
110 
run(const Window & window,cl::CommandQueue & queue)111 void CLConvertFullyConnectedWeightsKernel::run(const Window &window, cl::CommandQueue &queue)
112 {
113     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
114     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
115 
116     unsigned int idx = 0;
117     add_2D_tensor_argument(idx, _input, window);
118     add_2D_tensor_argument(idx, _output, window);
119     enqueue(queue, *this, window, lws_hint());
120 }
121 } // namespace arm_compute
122