1 /*
2  * Copyright (c) 2022 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 "arm_compute/core/CL/CLHelpers.h"
25 #include "arm_compute/core/TensorInfo.h"
26 #include "arm_compute/core/TensorShape.h"
27 
28 namespace arm_compute
29 {
30 namespace cl_dwc
31 {
use_cl_image_for_weights(const ITensorInfo * weights,unsigned int depth_multiplier)32 bool use_cl_image_for_weights(const ITensorInfo *weights, unsigned int depth_multiplier)
33 {
34     // Check whether we can use the cl image with the weights.
35     if(!export_to_cl_image(weights))
36     {
37         return false;
38     }
39 
40     const size_t idx_w    = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::WIDTH);
41     const size_t idx_h    = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::HEIGHT);
42     const size_t kernel_w = weights->tensor_shape()[idx_w];
43     const size_t kernel_h = weights->tensor_shape()[idx_h];
44 
45     // If we can use the cl image storage with the weights, we prefer to use the cl buffer storage in the following cases for performance reasons:
46     // 1- When the kernel size is 1x1
47     // 2- When the depth multiplier is greater than 1 and not multiple of 4.
48     if((kernel_w == 1) && (kernel_h == 1))
49     {
50         return false;
51     }
52 
53     if((depth_multiplier > 1) && (depth_multiplier % 4) != 0)
54     {
55         return false;
56     }
57 
58     return true;
59 }
60 } // namespace cl_dwc
61 } // namespace arm_compute