|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 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/Types.h" |
| 25 | +#include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 26 | +#include "arm_compute/runtime/NEON/NEFunctions.h" |
| 27 | + |
| 28 | +#include "utils/Utils.h" |
| 29 | + |
| 30 | +using namespace arm_compute; |
| 31 | +using namespace utils; |
| 32 | + |
| 33 | +class NEConvolutionExample : public Example |
| 34 | +{ |
| 35 | +public: |
| 36 | + bool do_setup(int argc, char **argv) override |
| 37 | + { |
| 38 | + TensorShape input_shape{32, 256, 256}; |
| 39 | + TensorShape weights_shape{32, 4, 4, 4}; |
| 40 | + TensorShape output_shape{4, 127, 127}; |
| 41 | + TensorInfo input_info{input_shape, 1, DataType::F16, DataLayout::NHWC}; |
| 42 | + TensorInfo weights_info{weights_shape, 1, DataType::F16, DataLayout::NHWC}; |
| 43 | + TensorInfo output_info{output_shape, 1, DataType::F16, DataLayout::NHWC}; |
| 44 | + PadStrideInfo ps_info{1, 1, 0, 0, DimensionRoundingType::FLOOR}; |
| 45 | + |
| 46 | + if (argc == 11) |
| 47 | + { |
| 48 | + try |
| 49 | + { |
| 50 | + const size_t input_x = std::stoul(argv[1]); |
| 51 | + const size_t input_y = std::stoul(argv[2]); |
| 52 | + const size_t input_z = std::stoul(argv[3]); |
| 53 | + const size_t kernel_size_x = std::stoul(argv[4]); |
| 54 | + const size_t kernel_size_y = std::stoul(argv[5]); |
| 55 | + const size_t output_channels = std::stoul(argv[6]); |
| 56 | + const uint32_t stride_x = static_cast<uint32_t>(std::stoul(argv[7])); |
| 57 | + const uint32_t stride_y = static_cast<uint32_t>(std::stoul(argv[8])); |
| 58 | + const uint32_t pad_x = static_cast<uint32_t>(std::stoul(argv[9])); |
| 59 | + const uint32_t pad_y = static_cast<uint32_t>(std::stoul(argv[10])); |
| 60 | + |
| 61 | + input_shape = TensorShape{input_z, input_x, input_y}; |
| 62 | + input_info = TensorInfo{input_shape, 1, DataType::F16, DataLayout::NHWC}; |
| 63 | + weights_shape = TensorShape{input_z, kernel_size_x, kernel_size_y, output_channels}; |
| 64 | + weights_info = TensorInfo{weights_shape, 1, DataType::F16, DataLayout::NHWC}; |
| 65 | + ps_info = PadStrideInfo{stride_x, stride_y, pad_x, pad_y, DimensionRoundingType::FLOOR}; |
| 66 | + output_shape = arm_compute::misc::shape_calculator::compute_deep_convolution_shape( |
| 67 | + input_info, weights_info, ps_info); |
| 68 | + output_info = TensorInfo{output_shape, 1, DataType::F16, DataLayout::NHWC}; |
| 69 | + } |
| 70 | + catch (const std::exception &e) |
| 71 | + { |
| 72 | + ARM_COMPUTE_ERROR(e.what()); |
| 73 | + return false; |
| 74 | + } |
| 75 | + } |
| 76 | + else if (argc != 1) |
| 77 | + { |
| 78 | + ARM_COMPUTE_ERROR( |
| 79 | + "Invalid number of arguments. Usage:\n" |
| 80 | + "<input_width> <input_height> <input_channels> <kernel_size_x> <kernel_size_y> <output_channels> " |
| 81 | + "<stride_x> <stride_y> <pad_x> <pad_y>\n"); |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + input.allocator()->init(input_info); |
| 86 | + weights.allocator()->init(weights_info); |
| 87 | + output.allocator()->init(output_info); |
| 88 | + |
| 89 | + auto status = NEConvolutionLayer::validate(input.info(), weights.info(), nullptr, output.info(), ps_info); |
| 90 | + if (status.error_code() != ErrorCode::OK) |
| 91 | + { |
| 92 | + ARM_COMPUTE_ERROR(status.error_description().c_str()); |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + conv.configure(&input, &weights, nullptr, &output, ps_info); |
| 97 | + input.allocator()->allocate(); |
| 98 | + weights.allocator()->allocate(); |
| 99 | + output.allocator()->allocate(); |
| 100 | + |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + void do_run() override |
| 105 | + { |
| 106 | + conv.run(); |
| 107 | + } |
| 108 | + |
| 109 | +private: |
| 110 | + NEConvolutionLayer conv{}; |
| 111 | + Tensor input{}, weights{}, output{}; |
| 112 | +}; |
| 113 | + |
| 114 | +/** Main program for convolution test |
| 115 | + * |
| 116 | + * @param[in] argc Number of arguments |
| 117 | + * @param[in] argv Arguments (input_width, input_height, input_channels, kernel_size_x, kernel_size_y, output_channels, stride_x, stride_y pad_x, pad_y) |
| 118 | + */ |
| 119 | +int main(int argc, char **argv) |
| 120 | +{ |
| 121 | + return utils::run_example<NEConvolutionExample>(argc, argv); |
| 122 | +} |
0 commit comments