From e4563a032aaa71de5efdb83fc04ff2933338e02d Mon Sep 17 00:00:00 2001 From: Adnan AlSinan Date: Wed, 1 Sep 2021 15:32:03 +0100 Subject: Adds Conv3d reference implementation support. Expands the interface with the following items: - Size3D Class. - Conv3dInfo Struct. - Padding3D Struct. - Add 'NDHWC' to supported Tensor Data Layouts. - Add function to compute expected size of Conv3d. Resolves COMPMID-4658 & COMPMID-4657 Signed-off-by: Adnan AlSinan Change-Id: Ic7452c48461eedaa38eaf3ac458f54b031e7dfa8 Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/6187 Reviewed-by: Giorgio Arena Reviewed-by: Gian Marco Iodice Tested-by: Arm Jenkins --- arm_compute/core/Size3D.h | 106 ++++++++++++++++++++++++++ arm_compute/core/Types.h | 15 +++- arm_compute/core/utils/misc/ShapeCalculator.h | 66 ++++++++++++++++ 3 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 arm_compute/core/Size3D.h (limited to 'arm_compute/core') diff --git a/arm_compute/core/Size3D.h b/arm_compute/core/Size3D.h new file mode 100644 index 0000000000..1d8febe3ce --- /dev/null +++ b/arm_compute/core/Size3D.h @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2021 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_SIZE3D_H +#define ARM_COMPUTE_SIZE3D_H + +#include + +namespace arm_compute +{ +/** Class for specifying the size of a 3D shape or object */ +class Size3D +{ +public: + /** Default constructor */ + Size3D() = default; + /** Constructor. Initializes "width", "height" and "depth" respectively with "w", "h" and "d" + * + * @param[in] w Width of the 3D shape or object + * @param[in] h Height of the 3D shape or object + * @param[in] d Depth of the 3D shape or object + */ + Size3D(size_t w, size_t h, size_t d) + : width(w), height(h), depth(d) + { + } + + /** Convert the values stored to string + * + * @return string of (width x height x depth). + */ + std::string to_string() const; + + /** Semantic accessor for width as x. + * + * @return x. + */ + size_t x() const + { + return width; + } + + /** Semantic accessor for height as y. + * + * @return y. + */ + size_t y() const + { + return height; + } + + /** Semantic accessor for depth as z. + * + * @return z. + */ + size_t z() const + { + return depth; + } + +public: + size_t width = {}; /**< Width of the 3D shape or object */ + size_t height = {}; /**< Height of the 3D shape or object */ + size_t depth = {}; /**< Depth of the 3D shape or object */ +}; + +/** Operator to compare two Size3D objects to be equal + * + * @param[in] lhs Left-hand side Size3D object. + * @param[in] rhs Right-hand side Size3D object. + * + * @return True if two instances have the same width, height and depth + */ +bool operator==(const Size3D &lhs, const Size3D &rhs); + +/** Operator to compare two Size3D objects to be different + * + * @param[in] lhs Left-hand side Size3D object. + * @param[in] rhs Right-hand side Size3D object. + * + * @return True if two instances have a difference in width, height or depth + */ +bool operator!=(const Size3D &lhs, const Size3D &rhs); + +} // namespace arm_compute +#endif /* ARM_COMPUTE_SIZE3D_H */ diff --git a/arm_compute/core/Types.h b/arm_compute/core/Types.h index 36b77b8224..31199e138b 100644 --- a/arm_compute/core/Types.h +++ b/arm_compute/core/Types.h @@ -27,6 +27,7 @@ #include "arm_compute/core/Coordinates.h" #include "arm_compute/core/QuantizationInfo.h" #include "arm_compute/core/Size2D.h" +#include "arm_compute/core/Size3D.h" #include "arm_compute/core/Strides.h" #include "arm_compute/core/TensorShape.h" #include "arm_compute/core/utils/misc/Macros.h" @@ -112,7 +113,8 @@ enum class DataLayout { UNKNOWN, /**< Unknown data layout */ NCHW, /**< Num samples, channels, height, width */ - NHWC /**< Num samples, height, width, channels */ + NHWC, /**< Num samples, height, width, channels */ + NDHWC /**< Num samples, depth, height, width, channels */ }; /** [DataLayout enum definition] **/ @@ -760,6 +762,17 @@ private: DimensionRoundingType _round_type; }; +/** Padding information for 3D operations like Conv3d */ +struct Padding3D +{ + size_t left = { 0 }; /**< Padding across the width dimenstion on the left, in elements. */ + size_t right = { 0 }; /**< Padding across the width dimenstion on the right, in elements. */ + size_t top = { 0 }; /**< Padding across the height dimenstion on the top, in elements. */ + size_t bottom = { 0 }; /**< Padding across the height dimenstion on the bottom, in elements. */ + size_t front = { 0 }; /**< Padding across the depth dimenstion on the front, in elements. */ + size_t back = { 0 }; /**< Padding across the depth dimenstion on the back, in elements. */ +}; + /** PriorBox layer info */ class PriorBoxLayerInfo final { diff --git a/arm_compute/core/utils/misc/ShapeCalculator.h b/arm_compute/core/utils/misc/ShapeCalculator.h index d0dc202f91..f18f5b7a42 100644 --- a/arm_compute/core/utils/misc/ShapeCalculator.h +++ b/arm_compute/core/utils/misc/ShapeCalculator.h @@ -28,6 +28,7 @@ #include "arm_compute/core/ITensorInfo.h" #include "arm_compute/core/KernelDescriptors.h" #include "arm_compute/core/Utils.h" +#include "arm_compute/runtime/FunctionDescriptors.h" #include "arm_compute/core/utils/helpers/tensor_transform.h" @@ -1383,6 +1384,71 @@ inline TensorShape compute_stack_shape(const ITensorInfo &a, unsigned int axis, return shape_out; } +/** Calculate the output shape of 3d Convolution + * + * @param[in] src Input tensor shape + * @param[in] weights Weights tensor shape + * @param[in] conv3d_info 3d Convolution Parameters object + * + * @return the calculated shape + */ +inline TensorShape compute_conv3d_shape(const TensorShape &src, const TensorShape &weights, const Conv3dInfo &conv3d_info) +{ + // Weight tensor shape indices (D H W Cin Cout) + constexpr unsigned int weights_depth_dim = 4u; + constexpr unsigned int weights_height_dim = 3u; + constexpr unsigned int weights_width_dim = 2u; + constexpr unsigned int weights_CHout_dim = 0u; + + // Source/Destination Tensor shape indices (N D H W C) + constexpr unsigned int batch_dim = 4u; + constexpr unsigned int depth_dim = 3u; + constexpr unsigned int height_dim = 2u; + constexpr unsigned int width_dim = 1u; + constexpr unsigned int channel_dim = 0u; + + TensorShape output_shape{ src }; + const size_t pad_left = conv3d_info.padding.left; + const size_t pad_right = conv3d_info.padding.right; + const size_t pad_top = conv3d_info.padding.top; + const size_t pad_bottom = conv3d_info.padding.bottom; + const size_t pad_front = conv3d_info.padding.front; + const size_t pad_back = conv3d_info.padding.back; + const size_t dilation_x = conv3d_info.dilation.width; + const size_t dilation_y = conv3d_info.dilation.height; + const size_t dilation_z = conv3d_info.dilation.depth; + const size_t stride_x = conv3d_info.stride.x(); + const size_t stride_y = conv3d_info.stride.y(); + const size_t stride_z = conv3d_info.stride.z(); + + int output_width_size = 0; + int output_height_size = 0; + int output_depth_size = 0; + + switch(conv3d_info.round_type) + { + case DimensionRoundingType::FLOOR: + output_width_size = static_cast(std::floor((static_cast(src[width_dim] + pad_left + pad_right - (dilation_x * (weights[weights_width_dim] - 1) + 1)) / stride_x) + 1)); + output_height_size = static_cast(std::floor((static_cast(src[height_dim] + pad_top + pad_bottom - (dilation_y * (weights[weights_height_dim] - 1) + 1)) / stride_y) + 1)); + output_depth_size = static_cast(std::floor((static_cast(src[depth_dim] + pad_front + pad_back - (dilation_z * (weights[weights_depth_dim] - 1) + 1)) / stride_z) + 1)); + break; + case DimensionRoundingType::CEIL: + output_width_size = static_cast(std::ceil((static_cast(src[width_dim] + pad_left + pad_right - (dilation_x * (weights[weights_width_dim] - 1) + 1)) / stride_x) + 1)); + output_height_size = static_cast(std::ceil((static_cast(src[height_dim] + pad_top + pad_bottom - (dilation_y * (weights[weights_height_dim] - 1) + 1)) / stride_y) + 1)); + output_depth_size = static_cast(std::ceil((static_cast(src[depth_dim] + pad_front + pad_back - (dilation_z * (weights[weights_depth_dim] - 1) + 1)) / stride_z) + 1)); + break; + default: + ARM_COMPUTE_ERROR("Unsupported rounding type"); + } + + output_shape.set(batch_dim, src[batch_dim]); + output_shape.set(width_dim, output_width_size); + output_shape.set(height_dim, output_height_size); + output_shape.set(depth_dim, output_depth_size); + output_shape.set(channel_dim, weights[weights_CHout_dim]); + return output_shape; +} + inline TensorShape compute_gather_shape(const TensorShape &input_shape, const TensorShape &indices_shape, uint32_t actual_axis) { ARM_COMPUTE_ERROR_ON(indices_shape.num_dimensions() > 1); -- cgit v1.2.1