Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | #ifndef __ARM_COMPUTE_TEST_RAW_TENSOR_H__ |
| 25 | #define __ARM_COMPUTE_TEST_RAW_TENSOR_H__ |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | |
| 30 | #include <cstddef> |
| 31 | #include <cstdint> |
| 32 | #include <memory> |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace test |
| 37 | { |
| 38 | /** Simple tensor object that stores elements in a consecutive chunk of memory. |
| 39 | * |
| 40 | * It can be created by either loading an image from a file which also |
| 41 | * initialises the content of the tensor or by explcitly specifying the size. |
| 42 | * The latter leaves the content uninitialised. |
| 43 | * |
| 44 | * Furthermore, the class provides methods to convert the tensor's values into |
| 45 | * different image format. |
| 46 | */ |
| 47 | class RawTensor final |
| 48 | { |
| 49 | public: |
| 50 | /** Create an uninitialised tensor of the given @p shape and @p format. |
| 51 | * |
| 52 | * @param[in] shape Shape of the new raw tensor. |
| 53 | * @param[in] format Format of the new raw tensor. |
| 54 | * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers |
| 55 | */ |
| 56 | RawTensor(TensorShape shape, Format format, int fixed_point_position = 0); |
| 57 | |
| 58 | /** Create an uninitialised tensor of the given @p shape and @p data type. |
| 59 | * |
| 60 | * @param[in] shape Shape of the new raw tensor. |
| 61 | * @param[in] data_type Data type of the new raw tensor. |
| 62 | * @param[in] num_channels (Optional) Number of channels (default = 1). |
| 63 | * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers (default = 0). |
| 64 | */ |
| 65 | RawTensor(TensorShape shape, DataType data_type, int num_channels = 1, int fixed_point_position = 0); |
| 66 | |
| 67 | /** Create a deep copy of the given @p tensor. |
| 68 | * |
| 69 | * @param[in] tensor To be copied tensor. |
| 70 | */ |
| 71 | RawTensor(const RawTensor &tensor); |
| 72 | |
| 73 | /** Create a deep copy of the given @p tensor. |
| 74 | * |
| 75 | * @param[in] tensor To be copied tensor. |
| 76 | */ |
| 77 | RawTensor &operator =(RawTensor tensor); |
| 78 | RawTensor(RawTensor &&) = default; |
| 79 | ~RawTensor() = default; |
| 80 | |
| 81 | using BufferType = uint8_t; |
| 82 | using Buffer = std::unique_ptr<BufferType[]>; |
| 83 | |
| 84 | /** Return value at @p offset in the buffer. |
| 85 | * |
| 86 | * @param[in] offset Offset within the buffer. |
| 87 | */ |
| 88 | BufferType &operator[](size_t offset); |
| 89 | |
| 90 | /** Return constant value at @p offset in the buffer. |
| 91 | * |
| 92 | * @param[in] offset Offset within the buffer. |
| 93 | */ |
| 94 | const BufferType &operator[](size_t offset) const; |
| 95 | |
| 96 | /** Shape of the tensor. */ |
| 97 | TensorShape shape() const; |
| 98 | |
| 99 | /** Size of each element in the tensor in bytes. */ |
| 100 | size_t element_size() const; |
| 101 | |
| 102 | /** Total size of the tensor in bytes. */ |
| 103 | size_t size() const; |
| 104 | |
| 105 | /** Image format of the tensor. */ |
| 106 | Format format() const; |
| 107 | |
| 108 | /** Data type of the tensor. */ |
| 109 | DataType data_type() const; |
| 110 | |
| 111 | /** Number of channels of the tensor. */ |
| 112 | int num_channels() const; |
| 113 | |
| 114 | /** Number of elements of the tensor. */ |
| 115 | int num_elements() const; |
| 116 | |
| 117 | /** The number of bits for the fractional part of the fixed point numbers. */ |
| 118 | int fixed_point_position() const; |
| 119 | |
| 120 | /** Constant pointer to the underlying buffer. */ |
| 121 | const BufferType *data() const; |
| 122 | |
| 123 | /** Pointer to the underlying buffer. */ |
| 124 | BufferType *data(); |
| 125 | |
| 126 | /** Read only access to the specified element. |
| 127 | * |
| 128 | * @param[in] coord Coordinates of the desired element. |
| 129 | * |
| 130 | * @return A pointer to the desired element. |
| 131 | */ |
| 132 | const BufferType *operator()(const Coordinates &coord) const; |
| 133 | |
| 134 | /** Access to the specified element. |
| 135 | * |
| 136 | * @param[in] coord Coordinates of the desired element. |
| 137 | * |
| 138 | * @return A pointer to the desired element. |
| 139 | */ |
| 140 | BufferType *operator()(const Coordinates &coord); |
| 141 | |
| 142 | /** Swaps the content of the provided tensors. |
| 143 | * |
| 144 | * @param[in, out] tensor1 Tensor to be swapped. |
| 145 | * @param[in, out] tensor2 Tensor to be swapped. |
| 146 | */ |
| 147 | friend void swap(RawTensor &tensor1, RawTensor &tensor2); |
| 148 | |
| 149 | private: |
| 150 | Buffer _buffer{ nullptr }; |
| 151 | TensorShape _shape{}; |
| 152 | Format _format{ Format::UNKNOWN }; |
| 153 | DataType _data_type{ DataType::UNKNOWN }; |
| 154 | int _num_channels{ 0 }; |
| 155 | int _fixed_point_position{ 0 }; |
| 156 | }; |
| 157 | } // namespace test |
| 158 | } // namespace arm_compute |
| 159 | #endif |