From 82e70a12dc3bf8309c43620c08a2c4ff05a6d13e Mon Sep 17 00:00:00 2001 From: Moritz Pflanzer Date: Tue, 8 Aug 2017 16:20:45 +0100 Subject: COMPMID-415: Improve SimpleTensor and RawTensor Change-Id: I7a5f970b3c04b925682fd9f0ece3254478dc96f7 Reviewed-on: http://mpd-gerrit.cambridge.arm.com/83343 Reviewed-by: Anthony Barbier Tested-by: Kaizen --- tests/AssetsLibrary.h | 8 +- tests/RawTensor.cpp | 140 +-------- tests/RawTensor.h | 90 +----- tests/SimpleTensor.h | 344 +++++++++++++++++++++++ tests/validation/Validation.h | 2 +- tests/validation_new/CPP/ActivationLayer.h | 2 +- tests/validation_new/CPP/BitwiseAnd.h | 2 +- tests/validation_new/CPP/BitwiseNot.h | 2 +- tests/validation_new/CPP/BitwiseOr.h | 2 +- tests/validation_new/CPP/BitwiseXor.h | 2 +- tests/validation_new/CPP/ConvolutionLayer.h | 2 +- tests/validation_new/CPP/DepthConcatenateLayer.h | 2 +- tests/validation_new/CPP/Floor.h | 2 +- tests/validation_new/CPP/GEMM.h | 2 +- tests/validation_new/CPP/L2Normalize.h | 2 +- tests/validation_new/CPP/NormalizationLayer.h | 2 +- tests/validation_new/CPP/ReductionOperation.h | 2 +- tests/validation_new/CPP/SoftmaxLayer.h | 2 +- tests/validation_new/SimpleTensor.h | 341 ---------------------- 19 files changed, 379 insertions(+), 572 deletions(-) create mode 100644 tests/SimpleTensor.h delete mode 100644 tests/validation_new/SimpleTensor.h diff --git a/tests/AssetsLibrary.h b/tests/AssetsLibrary.h index 18ffd773c8..2993662dc7 100644 --- a/tests/AssetsLibrary.h +++ b/tests/AssetsLibrary.h @@ -427,8 +427,8 @@ void AssetsLibrary::fill(T &&tensor, const std::string &name, Format format) con { const Coordinates id = index2coord(raw.shape(), offset / raw.element_size()); - const RawTensor::BufferType *const raw_ptr = raw.data() + offset; - const auto out_ptr = static_cast(tensor(id)); + const RawTensor::value_type *const raw_ptr = raw.data() + offset; + const auto out_ptr = static_cast(tensor(id)); std::copy_n(raw_ptr, raw.element_size(), out_ptr); } } @@ -448,8 +448,8 @@ void AssetsLibrary::fill(T &&tensor, const std::string &name, Format format, Cha { const Coordinates id = index2coord(raw.shape(), offset / raw.element_size()); - const RawTensor::BufferType *const raw_ptr = raw.data() + offset; - const auto out_ptr = static_cast(tensor(id)); + const RawTensor::value_type *const raw_ptr = raw.data() + offset; + const auto out_ptr = static_cast(tensor(id)); std::copy_n(raw_ptr, raw.element_size(), out_ptr); } } diff --git a/tests/RawTensor.cpp b/tests/RawTensor.cpp index e6b320fcb2..bc2747d2a1 100644 --- a/tests/RawTensor.cpp +++ b/tests/RawTensor.cpp @@ -23,48 +23,28 @@ */ #include "RawTensor.h" -#include "Utils.h" - -#include "arm_compute/core/Utils.h" -#include "support/ToolchainSupport.h" - -#include -#include -#include -#include -#include - namespace arm_compute { namespace test { RawTensor::RawTensor(TensorShape shape, Format format, int fixed_point_position) - : _buffer(nullptr), - _shape(shape), - _format(format), - _fixed_point_position(fixed_point_position) + : SimpleTensor(shape, format, fixed_point_position) { - _buffer = support::cpp14::make_unique(size()); + _buffer = support::cpp14::make_unique(SimpleTensor::num_elements() * SimpleTensor::num_channels() * SimpleTensor::element_size()); } RawTensor::RawTensor(TensorShape shape, DataType data_type, int num_channels, int fixed_point_position) - : _buffer(nullptr), - _shape(shape), - _data_type(data_type), - _num_channels(num_channels), - _fixed_point_position(fixed_point_position) + : SimpleTensor(shape, data_type, num_channels, fixed_point_position) { - _buffer = support::cpp14::make_unique(size()); + _buffer = support::cpp14::make_unique(SimpleTensor::num_elements() * SimpleTensor::num_channels() * SimpleTensor::element_size()); } RawTensor::RawTensor(const RawTensor &tensor) - : _buffer(nullptr), - _shape(tensor.shape()), - _format(tensor.format()), - _fixed_point_position(tensor.fixed_point_position()) + : SimpleTensor(tensor.shape(), tensor.data_type(), tensor.num_channels(), tensor.fixed_point_position()) { - _buffer = support::cpp14::make_unique(tensor.size()); - std::copy(tensor.data(), tensor.data() + size(), _buffer.get()); + _format = tensor.format(); + _buffer = support::cpp14::make_unique(num_elements() * num_channels() * element_size()); + std::copy_n(tensor.data(), num_elements() * num_channels() * element_size(), _buffer.get()); } RawTensor &RawTensor::operator=(RawTensor tensor) @@ -74,114 +54,14 @@ RawTensor &RawTensor::operator=(RawTensor tensor) return *this; } -RawTensor::BufferType &RawTensor::operator[](size_t offset) -{ - return _buffer[offset]; -} - -const RawTensor::BufferType &RawTensor::operator[](size_t offset) const -{ - return _buffer[offset]; -} - -TensorShape RawTensor::shape() const -{ - return _shape; -} - -size_t RawTensor::element_size() const -{ - return num_channels() * element_size_from_data_type(data_type()); -} - -int RawTensor::fixed_point_position() const -{ - return _fixed_point_position; -} - -size_t RawTensor::size() const -{ - const size_t size = std::accumulate(_shape.cbegin(), _shape.cend(), 1, std::multiplies()); - return size * element_size(); -} - -Format RawTensor::format() const -{ - return _format; -} - -DataType RawTensor::data_type() const -{ - if(_format != Format::UNKNOWN) - { - return data_type_from_format(_format); - } - else - { - return _data_type; - } -} - -int RawTensor::num_channels() const -{ - switch(_format) - { - case Format::U8: - case Format::S16: - case Format::U16: - case Format::S32: - case Format::U32: - case Format::F32: - return 1; - case Format::RGB888: - return 3; - case Format::UNKNOWN: - return _num_channels; - default: - ARM_COMPUTE_ERROR("NOT SUPPORTED!"); - } -} - -int RawTensor::num_elements() const -{ - return _shape.total_size(); -} - -PaddingSize RawTensor::padding() const -{ - return PaddingSize(0); -} - -const RawTensor::BufferType *RawTensor::data() const -{ - return _buffer.get(); -} - -RawTensor::BufferType *RawTensor::data() -{ - return _buffer.get(); -} - -const RawTensor::BufferType *RawTensor::operator()(const Coordinates &coord) const +const void *RawTensor::operator()(const Coordinates &coord) const { return _buffer.get() + coord2index(_shape, coord) * element_size(); } -RawTensor::BufferType *RawTensor::operator()(const Coordinates &coord) +void *RawTensor::operator()(const Coordinates &coord) { return _buffer.get() + coord2index(_shape, coord) * element_size(); } - -void swap(RawTensor &tensor1, RawTensor &tensor2) -{ - // Use unqualified call to swap to enable ADL. But make std::swap available - // as backup. - using std::swap; - swap(tensor1._shape, tensor2._shape); - swap(tensor1._format, tensor2._format); - swap(tensor1._data_type, tensor2._data_type); - swap(tensor1._num_channels, tensor2._num_channels); - swap(tensor1._buffer, tensor2._buffer); -} } // namespace test } // namespace arm_compute diff --git a/tests/RawTensor.h b/tests/RawTensor.h index 9d65e4f319..f1a9af37c9 100644 --- a/tests/RawTensor.h +++ b/tests/RawTensor.h @@ -24,27 +24,18 @@ #ifndef __ARM_COMPUTE_TEST_RAW_TENSOR_H__ #define __ARM_COMPUTE_TEST_RAW_TENSOR_H__ -#include "arm_compute/core/TensorShape.h" -#include "arm_compute/core/Types.h" - -#include -#include -#include +#include "SimpleTensor.h" namespace arm_compute { namespace test { -/** Simple tensor object that stores elements in a consecutive chunk of memory. - * - * It can be created by either loading an image from a file which also - * initialises the content of the tensor or by explcitly specifying the size. - * The latter leaves the content uninitialised. +/** Subclass of SimpleTensor using uint8_t as value type. * - * Furthermore, the class provides methods to convert the tensor's values into - * different image format. + * Access operations (except for operator[]) will be based on the data type to + * copy the right number of elements. */ -class RawTensor final +class RawTensor : public SimpleTensor { public: /** Create an uninitialised tensor of the given @p shape and @p format. @@ -70,69 +61,17 @@ public: */ RawTensor(const RawTensor &tensor); - /** Create a deep copy of the given @p tensor. - * - * @param[in] tensor To be copied tensor. - */ RawTensor &operator =(RawTensor tensor); RawTensor(RawTensor &&) = default; ~RawTensor() = default; - using BufferType = uint8_t; - using Buffer = std::unique_ptr; - - /** Return value at @p offset in the buffer. - * - * @param[in] offset Offset within the buffer. - */ - BufferType &operator[](size_t offset); - - /** Return constant value at @p offset in the buffer. - * - * @param[in] offset Offset within the buffer. - */ - const BufferType &operator[](size_t offset) const; - - /** Shape of the tensor. */ - TensorShape shape() const; - - /** Size of each element in the tensor in bytes. */ - size_t element_size() const; - - /** Total size of the tensor in bytes. */ - size_t size() const; - - /** Image format of the tensor. */ - Format format() const; - - /** Data type of the tensor. */ - DataType data_type() const; - - /** Number of channels of the tensor. */ - int num_channels() const; - - /** Number of elements of the tensor. */ - int num_elements() const; - - /** Available padding around the tensor. */ - PaddingSize padding() const; - - /** The number of bits for the fractional part of the fixed point numbers. */ - int fixed_point_position() const; - - /** Constant pointer to the underlying buffer. */ - const BufferType *data() const; - - /** Pointer to the underlying buffer. */ - BufferType *data(); - /** Read only access to the specified element. * * @param[in] coord Coordinates of the desired element. * * @return A pointer to the desired element. */ - const BufferType *operator()(const Coordinates &coord) const; + const void *operator()(const Coordinates &coord) const override; /** Access to the specified element. * @@ -140,22 +79,7 @@ public: * * @return A pointer to the desired element. */ - BufferType *operator()(const Coordinates &coord); - - /** Swaps the content of the provided tensors. - * - * @param[in, out] tensor1 Tensor to be swapped. - * @param[in, out] tensor2 Tensor to be swapped. - */ - friend void swap(RawTensor &tensor1, RawTensor &tensor2); - -private: - Buffer _buffer{ nullptr }; - TensorShape _shape{}; - Format _format{ Format::UNKNOWN }; - DataType _data_type{ DataType::UNKNOWN }; - int _num_channels{ 0 }; - int _fixed_point_position{ 0 }; + void *operator()(const Coordinates &coord) override; }; } // namespace test } // namespace arm_compute diff --git a/tests/SimpleTensor.h b/tests/SimpleTensor.h new file mode 100644 index 0000000000..ea955fa730 --- /dev/null +++ b/tests/SimpleTensor.h @@ -0,0 +1,344 @@ +/* + * Copyright (c) 2017 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_TEST_SIMPLE_TENSOR_H__ +#define __ARM_COMPUTE_TEST_SIMPLE_TENSOR_H__ + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Utils.h" +#include "support/ToolchainSupport.h" +#include "tests/IAccessor.h" +#include "tests/Utils.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace arm_compute +{ +namespace test +{ +/** Simple tensor object that stores elements in a consecutive chunk of memory. + * + * It can be created by either loading an image from a file which also + * initialises the content of the tensor or by explcitly specifying the size. + * The latter leaves the content uninitialised. + * + * Furthermore, the class provides methods to convert the tensor's values into + * different image format. + */ +template +class SimpleTensor : public IAccessor +{ +public: + /** Create an uninitialised tensor. */ + SimpleTensor() = default; + + /** Create an uninitialised tensor of the given @p shape and @p format. + * + * @param[in] shape Shape of the new raw tensor. + * @param[in] format Format of the new raw tensor. + * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers + */ + SimpleTensor(TensorShape shape, Format format, int fixed_point_position = 0); + + /** Create an uninitialised tensor of the given @p shape and @p data type. + * + * @param[in] shape Shape of the new raw tensor. + * @param[in] data_type Data type of the new raw tensor. + * @param[in] num_channels (Optional) Number of channels (default = 1). + * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers (default = 0). + */ + SimpleTensor(TensorShape shape, DataType data_type, int num_channels = 1, int fixed_point_position = 0); + + /** Create a deep copy of the given @p tensor. + * + * @param[in] tensor To be copied tensor. + */ + SimpleTensor(const SimpleTensor &tensor); + + /** Create a deep copy of the given @p tensor. + * + * @param[in] tensor To be copied tensor. + */ + SimpleTensor &operator =(SimpleTensor tensor); + SimpleTensor(SimpleTensor &&) = default; + ~SimpleTensor() = default; + + using value_type = T; + using Buffer = std::unique_ptr; + + /** Return value at @p offset in the buffer. + * + * @param[in] offset Offset within the buffer. + */ + T &operator[](size_t offset); + + /** Return constant value at @p offset in the buffer. + * + * @param[in] offset Offset within the buffer. + */ + const T &operator[](size_t offset) const; + + /** Shape of the tensor. */ + TensorShape shape() const override; + + /** Size of each element in the tensor in bytes. */ + size_t element_size() const override; + + /** Total size of the tensor in bytes. */ + size_t size() const override; + + /** Image format of the tensor. */ + Format format() const override; + + /** Data type of the tensor. */ + DataType data_type() const override; + + /** Number of channels of the tensor. */ + int num_channels() const override; + + /** Number of elements of the tensor. */ + int num_elements() const override; + + /** Available padding around the tensor. */ + PaddingSize padding() const override; + + /** The number of bits for the fractional part of the fixed point numbers. */ + int fixed_point_position() const override; + + /** Constant pointer to the underlying buffer. */ + const T *data() const; + + /** Pointer to the underlying buffer. */ + T *data(); + + /** Read only access to the specified element. + * + * @param[in] coord Coordinates of the desired element. + * + * @return A pointer to the desired element. + */ + const void *operator()(const Coordinates &coord) const override; + + /** Access to the specified element. + * + * @param[in] coord Coordinates of the desired element. + * + * @return A pointer to the desired element. + */ + void *operator()(const Coordinates &coord) override; + + /** Swaps the content of the provided tensors. + * + * @param[in, out] tensor1 Tensor to be swapped. + * @param[in, out] tensor2 Tensor to be swapped. + */ + template + friend void swap(SimpleTensor &tensor1, SimpleTensor &tensor2); + +protected: + Buffer _buffer{ nullptr }; + TensorShape _shape{}; + Format _format{ Format::UNKNOWN }; + DataType _data_type{ DataType::UNKNOWN }; + int _num_channels{ 0 }; + int _fixed_point_position{ 0 }; +}; + +template +SimpleTensor::SimpleTensor(TensorShape shape, Format format, int fixed_point_position) + : _buffer(nullptr), + _shape(shape), + _format(format), + _fixed_point_position(fixed_point_position) +{ + _buffer = support::cpp14::make_unique(num_elements() * num_channels()); +} + +template +SimpleTensor::SimpleTensor(TensorShape shape, DataType data_type, int num_channels, int fixed_point_position) + : _buffer(nullptr), + _shape(shape), + _data_type(data_type), + _num_channels(num_channels), + _fixed_point_position(fixed_point_position) +{ + _buffer = support::cpp14::make_unique(num_elements() * this->num_channels()); +} + +template +SimpleTensor::SimpleTensor(const SimpleTensor &tensor) + : _buffer(nullptr), + _shape(tensor.shape()), + _format(tensor.format()), + _data_type(tensor.data_type()), + _num_channels(tensor.num_channels()), + _fixed_point_position(tensor.fixed_point_position()) +{ + _buffer = support::cpp14::make_unique(tensor.num_elements() * num_channels()); + std::copy_n(tensor.data(), num_elements() * num_channels(), _buffer.get()); +} + +template +SimpleTensor &SimpleTensor::operator=(SimpleTensor tensor) +{ + swap(*this, tensor); + + return *this; +} + +template +T &SimpleTensor::operator[](size_t offset) +{ + return _buffer[offset]; +} + +template +const T &SimpleTensor::operator[](size_t offset) const +{ + return _buffer[offset]; +} + +template +TensorShape SimpleTensor::shape() const +{ + return _shape; +} + +template +size_t SimpleTensor::element_size() const +{ + return num_channels() * element_size_from_data_type(data_type()); +} + +template +int SimpleTensor::fixed_point_position() const +{ + return _fixed_point_position; +} + +template +size_t SimpleTensor::size() const +{ + const size_t size = std::accumulate(_shape.cbegin(), _shape.cend(), 1, std::multiplies()); + return size * element_size(); +} + +template +Format SimpleTensor::format() const +{ + return _format; +} + +template +DataType SimpleTensor::data_type() const +{ + if(_format != Format::UNKNOWN) + { + return data_type_from_format(_format); + } + else + { + return _data_type; + } +} + +template +int SimpleTensor::num_channels() const +{ + switch(_format) + { + case Format::U8: + case Format::S16: + case Format::U16: + case Format::S32: + case Format::U32: + case Format::F32: + return 1; + case Format::RGB888: + return 3; + case Format::UNKNOWN: + return _num_channels; + default: + ARM_COMPUTE_ERROR("NOT SUPPORTED!"); + } +} + +template +int SimpleTensor::num_elements() const +{ + return _shape.total_size(); +} + +template +PaddingSize SimpleTensor::padding() const +{ + return PaddingSize(0); +} + +template +const T *SimpleTensor::data() const +{ + return _buffer.get(); +} + +template +T *SimpleTensor::data() +{ + return _buffer.get(); +} + +template +const void *SimpleTensor::operator()(const Coordinates &coord) const +{ + return _buffer.get() + coord2index(_shape, coord); +} + +template +void *SimpleTensor::operator()(const Coordinates &coord) +{ + return _buffer.get() + coord2index(_shape, coord); +} + +template +void swap(SimpleTensor &tensor1, SimpleTensor &tensor2) +{ + // Use unqualified call to swap to enable ADL. But make std::swap available + // as backup. + using std::swap; + swap(tensor1._shape, tensor2._shape); + swap(tensor1._format, tensor2._format); + swap(tensor1._data_type, tensor2._data_type); + swap(tensor1._num_channels, tensor2._num_channels); + swap(tensor1._buffer, tensor2._buffer); +} +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_SIMPLE_TENSOR_H__ */ diff --git a/tests/validation/Validation.h b/tests/validation/Validation.h index 993a3c360e..f95859d029 100644 --- a/tests/validation/Validation.h +++ b/tests/validation/Validation.h @@ -26,6 +26,7 @@ #include "arm_compute/core/Types.h" #include "arm_compute/runtime/Array.h" +#include "tests/RawTensor.h" #include "boost_wrapper.h" @@ -37,7 +38,6 @@ class Tensor; namespace test { -class RawTensor; class IAccessor; namespace validation diff --git a/tests/validation_new/CPP/ActivationLayer.h b/tests/validation_new/CPP/ActivationLayer.h index 5f4ef46827..3f10a2f360 100644 --- a/tests/validation_new/CPP/ActivationLayer.h +++ b/tests/validation_new/CPP/ActivationLayer.h @@ -24,8 +24,8 @@ #ifndef __ARM_COMPUTE_TEST_ACTIVATION_LAYER_H__ #define __ARM_COMPUTE_TEST_ACTIVATION_LAYER_H__ +#include "tests/SimpleTensor.h" #include "tests/validation_new/Helpers.h" -#include "tests/validation_new/SimpleTensor.h" namespace arm_compute { diff --git a/tests/validation_new/CPP/BitwiseAnd.h b/tests/validation_new/CPP/BitwiseAnd.h index 07fda90590..eba2fd695f 100644 --- a/tests/validation_new/CPP/BitwiseAnd.h +++ b/tests/validation_new/CPP/BitwiseAnd.h @@ -24,7 +24,7 @@ #ifndef __ARM_COMPUTE_TEST_BITWISE_AND_H__ #define __ARM_COMPUTE_TEST_BITWISE_AND_H__ -#include "tests/validation_new/SimpleTensor.h" +#include "tests/SimpleTensor.h" namespace arm_compute { diff --git a/tests/validation_new/CPP/BitwiseNot.h b/tests/validation_new/CPP/BitwiseNot.h index 36aa50d75c..b4206f9388 100644 --- a/tests/validation_new/CPP/BitwiseNot.h +++ b/tests/validation_new/CPP/BitwiseNot.h @@ -24,7 +24,7 @@ #ifndef __ARM_COMPUTE_TEST_BITWISE_NOT_H__ #define __ARM_COMPUTE_TEST_BITWISE_NOT_H__ -#include "tests/validation_new/SimpleTensor.h" +#include "tests/SimpleTensor.h" namespace arm_compute { diff --git a/tests/validation_new/CPP/BitwiseOr.h b/tests/validation_new/CPP/BitwiseOr.h index 727551132a..39158cb411 100644 --- a/tests/validation_new/CPP/BitwiseOr.h +++ b/tests/validation_new/CPP/BitwiseOr.h @@ -24,7 +24,7 @@ #ifndef __ARM_COMPUTE_TEST_BITWISE_OR_H__ #define __ARM_COMPUTE_TEST_BITWISE_OR_H__ -#include "tests/validation_new/SimpleTensor.h" +#include "tests/SimpleTensor.h" namespace arm_compute { diff --git a/tests/validation_new/CPP/BitwiseXor.h b/tests/validation_new/CPP/BitwiseXor.h index 45a2e0323b..3e7721e843 100644 --- a/tests/validation_new/CPP/BitwiseXor.h +++ b/tests/validation_new/CPP/BitwiseXor.h @@ -24,7 +24,7 @@ #ifndef __ARM_COMPUTE_TEST_BITWISE_XOR_H__ #define __ARM_COMPUTE_TEST_BITWISE_XOR_H__ -#include "tests/validation_new/SimpleTensor.h" +#include "tests/SimpleTensor.h" namespace arm_compute { diff --git a/tests/validation_new/CPP/ConvolutionLayer.h b/tests/validation_new/CPP/ConvolutionLayer.h index fd46567910..a7c9e086a4 100644 --- a/tests/validation_new/CPP/ConvolutionLayer.h +++ b/tests/validation_new/CPP/ConvolutionLayer.h @@ -24,8 +24,8 @@ #ifndef __ARM_COMPUTE_TEST_CONVOLUTION_LAYER_H__ #define __ARM_COMPUTE_TEST_CONVOLUTION_LAYER_H__ +#include "tests/SimpleTensor.h" #include "tests/validation_new/Helpers.h" -#include "tests/validation_new/SimpleTensor.h" namespace arm_compute { diff --git a/tests/validation_new/CPP/DepthConcatenateLayer.h b/tests/validation_new/CPP/DepthConcatenateLayer.h index b67eb555eb..3c486a8015 100644 --- a/tests/validation_new/CPP/DepthConcatenateLayer.h +++ b/tests/validation_new/CPP/DepthConcatenateLayer.h @@ -24,7 +24,7 @@ #ifndef __ARM_COMPUTE_TEST_DEPTHCONCATENATE_LAYER_H__ #define __ARM_COMPUTE_TEST_DEPTHCONCATENATE_LAYER_H__ -#include "tests/validation_new/SimpleTensor.h" +#include "tests/SimpleTensor.h" #include diff --git a/tests/validation_new/CPP/Floor.h b/tests/validation_new/CPP/Floor.h index 8a6c3bcff6..30b8118622 100644 --- a/tests/validation_new/CPP/Floor.h +++ b/tests/validation_new/CPP/Floor.h @@ -24,8 +24,8 @@ #ifndef __ARM_COMPUTE_TEST_FLOOR_H__ #define __ARM_COMPUTE_TEST_FLOOR_H__ +#include "tests/SimpleTensor.h" #include "tests/validation_new/Helpers.h" -#include "tests/validation_new/SimpleTensor.h" namespace arm_compute { diff --git a/tests/validation_new/CPP/GEMM.h b/tests/validation_new/CPP/GEMM.h index ff79c7b546..7fea2a9862 100644 --- a/tests/validation_new/CPP/GEMM.h +++ b/tests/validation_new/CPP/GEMM.h @@ -24,8 +24,8 @@ #ifndef __ARM_COMPUTE_TEST_GEMM_H__ #define __ARM_COMPUTE_TEST_GEMM_H__ +#include "tests/SimpleTensor.h" #include "tests/validation_new/Helpers.h" -#include "tests/validation_new/SimpleTensor.h" namespace arm_compute { diff --git a/tests/validation_new/CPP/L2Normalize.h b/tests/validation_new/CPP/L2Normalize.h index 3e81b9a644..da36a73bba 100644 --- a/tests/validation_new/CPP/L2Normalize.h +++ b/tests/validation_new/CPP/L2Normalize.h @@ -24,8 +24,8 @@ #ifndef __ARM_COMPUTE_TEST_L2NORMALIZE_H__ #define __ARM_COMPUTE_TEST_L2NORMALIZE_H__ +#include "tests/SimpleTensor.h" #include "tests/validation_new/Helpers.h" -#include "tests/validation_new/SimpleTensor.h" namespace arm_compute { diff --git a/tests/validation_new/CPP/NormalizationLayer.h b/tests/validation_new/CPP/NormalizationLayer.h index 54284b1d50..bdd87545ca 100644 --- a/tests/validation_new/CPP/NormalizationLayer.h +++ b/tests/validation_new/CPP/NormalizationLayer.h @@ -24,8 +24,8 @@ #ifndef __ARM_COMPUTE_TEST_NORMALIZATION_LAYER_H__ #define __ARM_COMPUTE_TEST_NORMALIZATION_LAYER_H__ +#include "tests/SimpleTensor.h" #include "tests/validation_new/Helpers.h" -#include "tests/validation_new/SimpleTensor.h" namespace arm_compute { diff --git a/tests/validation_new/CPP/ReductionOperation.h b/tests/validation_new/CPP/ReductionOperation.h index 43a700d80a..500a8162fd 100644 --- a/tests/validation_new/CPP/ReductionOperation.h +++ b/tests/validation_new/CPP/ReductionOperation.h @@ -24,8 +24,8 @@ #ifndef __ARM_COMPUTE_TEST_REDUCTION_OPERATION_H__ #define __ARM_COMPUTE_TEST_REDUCTION_OPERATION_H__ +#include "tests/SimpleTensor.h" #include "tests/validation_new/Helpers.h" -#include "tests/validation_new/SimpleTensor.h" namespace arm_compute { diff --git a/tests/validation_new/CPP/SoftmaxLayer.h b/tests/validation_new/CPP/SoftmaxLayer.h index 28a532ee96..cc52f3cf51 100644 --- a/tests/validation_new/CPP/SoftmaxLayer.h +++ b/tests/validation_new/CPP/SoftmaxLayer.h @@ -24,8 +24,8 @@ #ifndef __ARM_COMPUTE_TEST_SOFTMAX_LAYER_H__ #define __ARM_COMPUTE_TEST_SOFTMAX_LAYER_H__ +#include "tests/SimpleTensor.h" #include "tests/validation_new/Helpers.h" -#include "tests/validation_new/SimpleTensor.h" namespace arm_compute { diff --git a/tests/validation_new/SimpleTensor.h b/tests/validation_new/SimpleTensor.h deleted file mode 100644 index 61d6f1cd04..0000000000 --- a/tests/validation_new/SimpleTensor.h +++ /dev/null @@ -1,341 +0,0 @@ -/* - * Copyright (c) 2017 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_TEST_SIMPLE_TENSOR_H__ -#define __ARM_COMPUTE_TEST_SIMPLE_TENSOR_H__ - -#include "arm_compute/core/TensorShape.h" -#include "arm_compute/core/Types.h" -#include "arm_compute/core/Utils.h" -#include "support/ToolchainSupport.h" -#include "tests/IAccessor.h" -#include "tests/Utils.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -namespace arm_compute -{ -namespace test -{ -/** Simple tensor object that stores elements in a consecutive chunk of memory. - * - * It can be created by either loading an image from a file which also - * initialises the content of the tensor or by explcitly specifying the size. - * The latter leaves the content uninitialised. - * - * Furthermore, the class provides methods to convert the tensor's values into - * different image format. - */ -template -class SimpleTensor final : public IAccessor -{ -public: - /** Create an uninitialised tensor. */ - SimpleTensor() = default; - - /** Create an uninitialised tensor of the given @p shape and @p format. - * - * @param[in] shape Shape of the new raw tensor. - * @param[in] format Format of the new raw tensor. - * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers - */ - SimpleTensor(TensorShape shape, Format format, int fixed_point_position = 0); - - /** Create an uninitialised tensor of the given @p shape and @p data type. - * - * @param[in] shape Shape of the new raw tensor. - * @param[in] data_type Data type of the new raw tensor. - * @param[in] num_channels (Optional) Number of channels (default = 1). - * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers (default = 0). - */ - SimpleTensor(TensorShape shape, DataType data_type, int num_channels = 1, int fixed_point_position = 0); - - /** Create a deep copy of the given @p tensor. - * - * @param[in] tensor To be copied tensor. - */ - SimpleTensor(const SimpleTensor &tensor); - - /** Create a deep copy of the given @p tensor. - * - * @param[in] tensor To be copied tensor. - */ - SimpleTensor &operator =(SimpleTensor tensor); - SimpleTensor(SimpleTensor &&) = default; - ~SimpleTensor() = default; - - using value_type = T; - using Buffer = std::unique_ptr; - - /** Return value at @p offset in the buffer. - * - * @param[in] offset Offset within the buffer. - */ - T &operator[](size_t offset); - - /** Return constant value at @p offset in the buffer. - * - * @param[in] offset Offset within the buffer. - */ - const T &operator[](size_t offset) const; - - /** Shape of the tensor. */ - TensorShape shape() const override; - - /** Size of each element in the tensor in bytes. */ - size_t element_size() const override; - - /** Total size of the tensor in bytes. */ - size_t size() const override; - - /** Image format of the tensor. */ - Format format() const override; - - /** Data type of the tensor. */ - DataType data_type() const override; - - /** Number of channels of the tensor. */ - int num_channels() const override; - - /** Number of elements of the tensor. */ - int num_elements() const override; - - /** Available padding around the tensor. */ - PaddingSize padding() const override; - - /** The number of bits for the fractional part of the fixed point numbers. */ - int fixed_point_position() const override; - - /** Constant pointer to the underlying buffer. */ - const T *data() const; - - /** Pointer to the underlying buffer. */ - T *data(); - - /** Read only access to the specified element. - * - * @param[in] coord Coordinates of the desired element. - * - * @return A pointer to the desired element. - */ - const void *operator()(const Coordinates &coord) const override; - - /** Access to the specified element. - * - * @param[in] coord Coordinates of the desired element. - * - * @return A pointer to the desired element. - */ - void *operator()(const Coordinates &coord) override; - - /** Swaps the content of the provided tensors. - * - * @param[in, out] tensor1 Tensor to be swapped. - * @param[in, out] tensor2 Tensor to be swapped. - */ - template - friend void swap(SimpleTensor &tensor1, SimpleTensor &tensor2); - -private: - Buffer _buffer{ nullptr }; - TensorShape _shape{}; - Format _format{ Format::UNKNOWN }; - DataType _data_type{ DataType::UNKNOWN }; - int _num_channels{ 0 }; - int _fixed_point_position{ 0 }; -}; - -template -SimpleTensor::SimpleTensor(TensorShape shape, Format format, int fixed_point_position) - : _buffer(nullptr), - _shape(shape), - _format(format), - _fixed_point_position(fixed_point_position) -{ - _buffer = support::cpp14::make_unique(num_elements() * num_channels()); -} - -template -SimpleTensor::SimpleTensor(TensorShape shape, DataType data_type, int num_channels, int fixed_point_position) - : _buffer(nullptr), - _shape(shape), - _data_type(data_type), - _num_channels(num_channels), - _fixed_point_position(fixed_point_position) -{ - _buffer = support::cpp14::make_unique(num_elements() * this->num_channels()); -} - -template -SimpleTensor::SimpleTensor(const SimpleTensor &tensor) - : _buffer(nullptr), - _shape(tensor.shape()), - _format(tensor.format()), - _fixed_point_position(tensor.fixed_point_position()) -{ - _buffer = support::cpp14::make_unique(tensor.num_elements() * num_channels()); - std::copy_n(tensor.data(), num_elements() * num_channels(), _buffer.get()); -} - -template -SimpleTensor &SimpleTensor::operator=(SimpleTensor tensor) -{ - swap(*this, tensor); - - return *this; -} - -template -T &SimpleTensor::operator[](size_t offset) -{ - return _buffer[offset]; -} - -template -const T &SimpleTensor::operator[](size_t offset) const -{ - return _buffer[offset]; -} - -template -TensorShape SimpleTensor::shape() const -{ - return _shape; -} - -template -size_t SimpleTensor::element_size() const -{ - return num_channels() * element_size_from_data_type(data_type()); -} - -template -int SimpleTensor::fixed_point_position() const -{ - return _fixed_point_position; -} - -template -size_t SimpleTensor::size() const -{ - const size_t size = std::accumulate(_shape.cbegin(), _shape.cend(), 1, std::multiplies()); - return size * element_size(); -} - -template -Format SimpleTensor::format() const -{ - return _format; -} - -template -DataType SimpleTensor::data_type() const -{ - if(_format != Format::UNKNOWN) - { - return data_type_from_format(_format); - } - else - { - return _data_type; - } -} - -template -int SimpleTensor::num_channels() const -{ - switch(_format) - { - case Format::U8: - case Format::S16: - case Format::U16: - case Format::S32: - case Format::U32: - return 1; - case Format::RGB888: - return 3; - case Format::UNKNOWN: - return _num_channels; - default: - ARM_COMPUTE_ERROR("NOT SUPPORTED!"); - } -} - -template -int SimpleTensor::num_elements() const -{ - return _shape.total_size(); -} - -template -PaddingSize SimpleTensor::padding() const -{ - return PaddingSize(0); -} - -template -const T *SimpleTensor::data() const -{ - return _buffer.get(); -} - -template -T *SimpleTensor::data() -{ - return _buffer.get(); -} - -template -const void *SimpleTensor::operator()(const Coordinates &coord) const -{ - return _buffer.get() + coord2index(_shape, coord); -} - -template -void *SimpleTensor::operator()(const Coordinates &coord) -{ - return _buffer.get() + coord2index(_shape, coord); -} - -template -void swap(SimpleTensor &tensor1, SimpleTensor &tensor2) -{ - // Use unqualified call to swap to enable ADL. But make std::swap available - // as backup. - using std::swap; - swap(tensor1._shape, tensor2._shape); - swap(tensor1._format, tensor2._format); - swap(tensor1._data_type, tensor2._data_type); - swap(tensor1._num_channels, tensor2._num_channels); - swap(tensor1._buffer, tensor2._buffer); -} -} // namespace test -} // namespace arm_compute -#endif /* __ARM_COMPUTE_TEST_SIMPLE_TENSOR_H__ */ -- cgit v1.2.1