Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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/runtime/CL/tuners/BifrostTuner.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernels.h" |
| 28 | #include "arm_compute/core/utils/misc/Cast.h" |
| 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace tuners |
| 33 | { |
| 34 | namespace |
| 35 | { |
| 36 | /** Tunes a @ref CLDirectConvolutionLayerKernel for a bifrost target |
| 37 | * |
| 38 | * @param[in] k Kernels to tune |
| 39 | */ |
| 40 | void tune_direct_convolution_kernel(CLDirectConvolutionLayerKernel &k) |
| 41 | { |
| 42 | cl::NDRange lws_hint = k.lws_hint(); |
| 43 | |
| 44 | const GPUTarget gpu_target = k.get_target(); |
| 45 | const DataType dt = k._input->info()->data_type(); |
| 46 | const TensorShape weights_shape = k._weights->info()->tensor_shape(); |
| 47 | const TensorShape inputs_shape = k._input->info()->tensor_shape(); |
| 48 | const size_t kernel_size = weights_shape.x(); |
| 49 | const unsigned int stride_x = k._conv_stride_x; |
| 50 | const unsigned int stride_y = k._conv_stride_y; |
| 51 | |
| 52 | if(gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72) && (kernel_size <= 5) && (stride_x == 1) && (stride_y == 1) && (dt == DataType::F32)) |
| 53 | { |
| 54 | // Through extensive experimentation with over 30 representative tensor |
| 55 | // shapes, we found a small number of local work size configurations |
| 56 | // that result in nearly optimal execution times. Selecting the right |
| 57 | // lws for a given shape, however, required a complex decision tree, |
| 58 | // until we constructed a simple feature as described below. |
| 59 | // |
| 60 | // We started from the number of multiply-accumulate operations for a |
| 61 | // convolution layer, which is equal to the product of the input |
| 62 | // dimensions 0..2 and the weights dimensions 0..2. Unfortunately, |
| 63 | // this resulted in ties between distinct shapes that required distinct |
| 64 | // lws configurations. Replacing the width of the input with the kernel |
| 65 | // size, however, resulted in nearly optimal predictions. We use underscores |
| 66 | // in variable names to indicate when they are intentionally misleading. |
| 67 | const size_t product_of_weights_dimensions = weights_shape[0] * weights_shape[1] * weights_shape[2]; |
| 68 | const size_t product_of_input_dimensions_ = inputs_shape[0] * inputs_shape[1] * inputs_shape[2]; |
| 69 | const float mega_ops_ = 1e-6 * product_of_weights_dimensions * product_of_input_dimensions_; |
| 70 | |
| 71 | switch(kernel_size) |
| 72 | { |
| 73 | case 1: |
| 74 | { |
| 75 | if(mega_ops_ < 1.f) |
| 76 | { |
| 77 | lws_hint = cl::NDRange(1, 1, 8); |
| 78 | } |
| 79 | else if(mega_ops_ < 7.f) |
| 80 | { |
| 81 | lws_hint = cl::NDRange(1, 1, 4); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | lws_hint = cl::NDRange(1, 1, 2); |
| 86 | } |
| 87 | break; |
| 88 | } |
| 89 | case 3: |
| 90 | { |
| 91 | if(mega_ops_ < 1.f) |
| 92 | { |
| 93 | lws_hint = cl::NDRange(1, 1, 8); |
| 94 | } |
| 95 | else if(mega_ops_ < 13.f) |
| 96 | { |
| 97 | lws_hint = cl::NDRange(2, 1, 4); |
| 98 | } |
| 99 | else if(mega_ops_ < 50.f) |
| 100 | { |
| 101 | lws_hint = cl::NDRange(3, 1, 4); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | lws_hint = cl::NDRange(2, 1, 6); |
| 106 | } |
| 107 | break; |
| 108 | } |
| 109 | case 5: |
| 110 | { |
| 111 | if(mega_ops_ < 2.f || mega_ops_ > 80.f) |
| 112 | { |
| 113 | lws_hint = cl::NDRange(2, 1, 4); |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | lws_hint = cl::NDRange(2, 1, 8); |
| 118 | } |
| 119 | break; |
| 120 | } |
| 121 | default: |
| 122 | break; |
| 123 | } |
| 124 | k.set_lws_hint(lws_hint); |
| 125 | } |
| 126 | } |
| 127 | } // namespace |
| 128 | |
| 129 | void BifrostTuner::tune_kernel_static(ICLKernel &kernel) |
| 130 | { |
| 131 | // Continue on tuning if dynamic tuning |
| 132 | if(dynamic_cast<CLDirectConvolutionLayerKernel *>(&kernel) != nullptr) |
| 133 | { |
| 134 | tune_direct_convolution_kernel(*utils::cast::polymorphic_downcast<CLDirectConvolutionLayerKernel *>(&kernel)); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void BifrostTuner::tune_kernel_dynamic(ICLKernel &kernel) |
| 139 | { |
| 140 | ARM_COMPUTE_UNUSED(kernel); |
| 141 | } |
| 142 | } // namespace tuners |
| 143 | } // namespace arm_compute |