diff options
Diffstat (limited to 'src/core/TensorInfo.cpp')
-rw-r--r-- | src/core/TensorInfo.cpp | 180 |
1 files changed, 104 insertions, 76 deletions
diff --git a/src/core/TensorInfo.cpp b/src/core/TensorInfo.cpp index b86a4cf924..31bddbde40 100644 --- a/src/core/TensorInfo.cpp +++ b/src/core/TensorInfo.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2020 ARM Limited. + * Copyright (c) 2016-2023 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -24,40 +24,76 @@ #include "arm_compute/core/TensorInfo.h" #include "arm_compute/core/Error.h" -#include "arm_compute/core/HOGInfo.h" #include "arm_compute/core/Helpers.h" #include "arm_compute/core/TensorInfo.h" #include "arm_compute/core/Validate.h" -#include "support/MemorySupport.h" -using namespace arm_compute; +#include "src/core/helpers/Utils.h" +#include <memory> + +namespace arm_compute +{ TensorInfo::TensorInfo() - : _total_size(0), _offset_first_element_in_bytes(0), _strides_in_bytes(), _num_channels(0), _tensor_shape(), _data_type(DataType::UNKNOWN), _format(Format::UNKNOWN), _is_resizable{ true }, _is_dynamic{ false }, - _valid_region{ Coordinates(), _tensor_shape }, _padding{ 0 }, _quantization_info(), _data_layout(DataLayout::NCHW) + : _total_size(0), + _offset_first_element_in_bytes(0), + _strides_in_bytes(), + _num_channels(0), + _tensor_shape(), + _dims_state(), + _data_type(DataType::UNKNOWN), + _format(Format::UNKNOWN), + _is_resizable{true}, + _valid_region{Coordinates(), _tensor_shape}, + _padding{0}, + _quantization_info(), + _data_layout(DataLayout::NCHW), + _are_values_constant(true), + _id(invalid_tensor_id), + _lock_paddings(false) +{ +} + +TensorInfo::TensorInfo(const ITensorInfo &info) : TensorInfo() { + _total_size = info.total_size(); + _offset_first_element_in_bytes = info.offset_first_element_in_bytes(); + _strides_in_bytes = info.strides_in_bytes(); + _num_channels = info.num_channels(); + _tensor_shape = info.tensor_shape(); + _dims_state = info.tensor_dims_state(); + _data_type = info.data_type(); + _format = info.format(); + _is_resizable = info.is_resizable(); + _valid_region = info.valid_region(); + _padding = info.padding(); + _quantization_info = info.quantization_info(); + _data_layout = info.data_layout(); + _are_values_constant = info.are_values_constant(); + _id = info.id(); + _lock_paddings = info.lock_paddings(); } -TensorInfo::TensorInfo(const ITensorInfo &info) - : TensorInfo() +TensorInfo::TensorInfo(const TensorInfo &info) : TensorInfo() { _total_size = info.total_size(); _offset_first_element_in_bytes = info.offset_first_element_in_bytes(); _strides_in_bytes = info.strides_in_bytes(); _num_channels = info.num_channels(); _tensor_shape = info.tensor_shape(); + _dims_state = info.tensor_dims_state(); _data_type = info.data_type(); _format = info.format(); _is_resizable = info.is_resizable(); - _is_dynamic = info.is_dynamic(); _valid_region = info.valid_region(); _padding = info.padding(); _quantization_info = info.quantization_info(); _data_layout = info.data_layout(); + _are_values_constant = info.are_values_constant(); + _id = info.id(); + _lock_paddings = false; } - -TensorInfo::TensorInfo(Format format) - : TensorInfo(TensorShape(), format) +TensorInfo::TensorInfo(Format format) : TensorInfo(TensorShape(), format) { } @@ -66,25 +102,25 @@ TensorInfo::TensorInfo(unsigned int width, unsigned int height, Format format) { } -TensorInfo::TensorInfo(const TensorShape &tensor_shape, Format format) - : TensorInfo() +TensorInfo::TensorInfo(const TensorShape &tensor_shape, Format format) : TensorInfo() { init(tensor_shape, format); } -TensorInfo::TensorInfo(size_t num_channels, DataType data_type) - : TensorInfo() +TensorInfo::TensorInfo(size_t num_channels, DataType data_type) : TensorInfo() { init(TensorShape(), num_channels, data_type); } -TensorInfo::TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type) - : TensorInfo() +TensorInfo::TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type) : TensorInfo() { init(tensor_shape, num_channels, data_type); } -TensorInfo::TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, QuantizationInfo quantization_info) +TensorInfo::TensorInfo(const TensorShape &tensor_shape, + size_t num_channels, + DataType data_type, + QuantizationInfo quantization_info) : TensorInfo() { init(tensor_shape, num_channels, data_type); @@ -98,12 +134,6 @@ TensorInfo::TensorInfo(const TensorShape &tensor_shape, size_t num_channels, Dat _data_layout = data_layout; } -TensorInfo::TensorInfo(const HOGInfo &hog_info, unsigned int width, unsigned int height) - : TensorInfo() -{ - init(hog_info, width, height); -} - void TensorInfo::init(Format format) { init(TensorShape(), format); @@ -119,9 +149,11 @@ void TensorInfo::init(const TensorShape &tensor_shape, Format format) _format = format; } -void TensorInfo::init(const TensorShape &tensor_shape, Format format, - const Strides &strides_in_bytes, size_t offset_first_element_in_bytes, - size_t total_size_in_bytes) +void TensorInfo::init(const TensorShape &tensor_shape, + Format format, + const Strides &strides_in_bytes, + size_t offset_first_element_in_bytes, + size_t total_size_in_bytes) { size_t num_channels = num_channels_from_format(format); const DataType type = data_type_from_format(format); @@ -147,9 +179,12 @@ void TensorInfo::init(const TensorShape &tensor_shape, size_t num_channels, Data set_tensor_shape(tensor_shape); } -void TensorInfo::init(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, - const Strides &strides_in_bytes, size_t offset_first_element_in_bytes, - size_t total_size_in_bytes) +void TensorInfo::init(const TensorShape &tensor_shape, + size_t num_channels, + DataType data_type, + const Strides &strides_in_bytes, + size_t offset_first_element_in_bytes, + size_t total_size_in_bytes) { ARM_COMPUTE_ERROR_ON(num_channels == 0); @@ -161,21 +196,7 @@ void TensorInfo::init(const TensorShape &tensor_shape, size_t num_channels, Data _strides_in_bytes = strides_in_bytes; _total_size = total_size_in_bytes; - _valid_region = ValidRegion{ Coordinates(), _tensor_shape }; -} - -void TensorInfo::init(const HOGInfo &hog_info, unsigned int width, unsigned int height) -{ - // Number of cells for each block - const Size2D num_cells_per_block = hog_info.num_cells_per_block(); - - // Tensor Size = (Number of horizontal block positions) * (Number of vertical block positions) - const Size2D num_block_positions_per_img = hog_info.num_block_positions_per_image(Size2D(width, height)); - - // Number of tensor channels = (Number of cells per block) * (Number of bins per cell) - const size_t num_channels = num_cells_per_block.area() * hog_info.num_bins(); - - init(TensorShape(num_block_positions_per_img.width, num_block_positions_per_img.height), num_channels, DataType::F32); + _valid_region = ValidRegion{Coordinates(), _tensor_shape}; } size_t TensorInfo::init_auto_padding(const TensorShape &tensor_shape, Format format) @@ -198,27 +219,13 @@ size_t TensorInfo::init_auto_padding(const TensorShape &tensor_shape, size_t num _format = Format::UNKNOWN; _tensor_shape = tensor_shape; - _valid_region = ValidRegion{ Coordinates(), _tensor_shape }; + _valid_region = ValidRegion{Coordinates(), _tensor_shape}; auto_padding(); return _total_size; } -size_t TensorInfo::init_auto_padding(const HOGInfo &hog_info, unsigned int width, unsigned int height) -{ - // Number of cells for each block - const Size2D num_cells_per_block = hog_info.num_cells_per_block(); - - // Tensor Size = (Number of horizontal block positions) * (Number of vertical block positions) - const Size2D num_block_positions_per_img = hog_info.num_block_positions_per_image(Size2D(width, height)); - - // Number of tensor channels = (Number of cells per block) * (Number of bins per cell) - const size_t num_channels = num_cells_per_block.area() * hog_info.num_bins(); - - return init_auto_padding(TensorShape(num_block_positions_per_img.width, num_block_positions_per_img.height), num_channels, DataType::F32); -} - bool TensorInfo::auto_padding() { ARM_COMPUTE_ERROR_ON(!_is_resizable); @@ -243,11 +250,11 @@ std::tuple<Strides, size_t, size_t> TensorInfo::calculate_padding_requirements(c size_t required_total_size = 0; const size_t required_offset_first_element = padding.left * stride_x + padding.top * stride_y; - switch(_tensor_shape.num_dimensions()) + switch (_tensor_shape.num_dimensions()) { case 0: { - if(_tensor_shape.total_size() > 0) + if (_tensor_shape.total_size() > 0) { required_strides = Strides(stride_x, stride_x); required_total_size = stride_z; @@ -268,7 +275,8 @@ std::tuple<Strides, size_t, size_t> TensorInfo::calculate_padding_requirements(c const unsigned int idx_last_dimension = _tensor_shape.num_dimensions() - 1; - required_total_size = _tensor_shape[idx_last_dimension] * required_strides[idx_last_dimension]; + required_total_size = + static_cast<size_t>(_tensor_shape[idx_last_dimension]) * required_strides[idx_last_dimension]; break; } } @@ -276,31 +284,43 @@ std::tuple<Strides, size_t, size_t> TensorInfo::calculate_padding_requirements(c return std::make_tuple(required_strides, required_offset_first_element, required_total_size); } +ITensorInfo &TensorInfo::set_lock_paddings(bool flag) +{ + _lock_paddings = flag; + return *this; +} + +bool TensorInfo::lock_paddings() const +{ + return _lock_paddings; +} + bool TensorInfo::extend_padding(const PaddingSize &padding) { + ARM_COMPUTE_ERROR_ON(_lock_paddings); ARM_COMPUTE_ERROR_ON(!_is_resizable); bool updated = false; - if(padding.top > _padding.top) + if (padding.top > _padding.top) { _padding.top = padding.top; updated = true; } - if(padding.right > _padding.right) + if (padding.right > _padding.right) { _padding.right = padding.right; updated = true; } - if(padding.bottom > _padding.bottom) + if (padding.bottom > _padding.bottom) { _padding.bottom = padding.bottom; updated = true; } - if(padding.left > _padding.left) + if (padding.left > _padding.left) { _padding.left = padding.left; updated = true; @@ -313,7 +333,7 @@ bool TensorInfo::extend_padding(const PaddingSize &padding) std::unique_ptr<ITensorInfo> TensorInfo::clone() const { - return support::cpp14::make_unique<TensorInfo>(*this); + return std::make_unique<TensorInfo>(*this); } ITensorInfo &TensorInfo::set_data_type(DataType data_type) @@ -334,7 +354,7 @@ ITensorInfo &TensorInfo::set_format(Format format) { _format = format; - if(_data_type == DataType::UNKNOWN) + if (_data_type == DataType::UNKNOWN) { _num_channels = num_channels_from_format(format); _data_type = data_type_from_format(format); @@ -353,19 +373,25 @@ ITensorInfo &TensorInfo::set_tensor_shape(const TensorShape &shape) _offset_first_element_in_bytes = 0; _strides_in_bytes = compute_strides(*this); - if(_tensor_shape.num_dimensions() == 0) + if (_tensor_shape.num_dimensions() == 0) { _total_size = _strides_in_bytes[0]; } else { const unsigned int idx_last_dimension = _tensor_shape.num_dimensions() - 1; - _total_size = _tensor_shape[idx_last_dimension] * _strides_in_bytes[idx_last_dimension]; + _total_size = static_cast<size_t>(_tensor_shape[idx_last_dimension]) * _strides_in_bytes[idx_last_dimension]; } std::tie(_strides_in_bytes, _offset_first_element_in_bytes, _total_size) = calculate_padding_requirements(_padding); - _valid_region = ValidRegion{ Coordinates(), _tensor_shape }; + _valid_region = ValidRegion{Coordinates(), _tensor_shape}; + return *this; +} + +ITensorInfo &TensorInfo::set_tensor_dims_state(const TensorDimsState &state) +{ + _dims_state = state; return *this; } @@ -384,9 +410,10 @@ ITensorInfo &TensorInfo::set_data_layout(const DataLayout &data_layout) ITensorInfo &TensorInfo::reset_padding() { _padding = PaddingSize(); - if(((_format != Format::UNKNOWN) || (_data_type != DataType::UNKNOWN)) && _total_size != 0) + if (((_format != Format::UNKNOWN) || (_data_type != DataType::UNKNOWN)) && _total_size != 0) { - std::tie(_strides_in_bytes, _offset_first_element_in_bytes, _total_size) = calculate_padding_requirements(_padding); + std::tie(_strides_in_bytes, _offset_first_element_in_bytes, _total_size) = + calculate_padding_requirements(_padding); } return *this; } @@ -397,10 +424,11 @@ int32_t TensorInfo::offset_element_in_bytes(const Coordinates &pos) const int32_t offset = _offset_first_element_in_bytes; - for(size_t i = 0; i < _tensor_shape.num_dimensions(); ++i) + for (size_t i = 0; i < _tensor_shape.num_dimensions(); ++i) { offset += pos[i] * _strides_in_bytes[i]; } return offset; } +} // namespace arm_compute |