Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 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 SRC_COMMON_ITENSOR_H_ |
| 25 | #define SRC_COMMON_ITENSOR_H_ |
| 26 | |
| 27 | #include "src/common/IContext.h" |
| 28 | #include "src/common/utils/Validate.h" |
| 29 | |
| 30 | struct AclTensor_ |
| 31 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 32 | arm_compute::detail::Header header{arm_compute::detail::ObjectType::Tensor, nullptr}; |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 33 | |
| 34 | protected: |
| 35 | AclTensor_() = default; |
| 36 | ~AclTensor_() = default; |
| 37 | }; |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | // Forward declaration |
| 42 | class ITensor; |
| 43 | |
| 44 | /** Base class specifying the tensor interface */ |
| 45 | class ITensorV2 : public AclTensor_ |
| 46 | { |
| 47 | public: |
| 48 | /** Explict Operator Constructor |
| 49 | * |
| 50 | * @param[in] ctx Context to be used by the operator |
| 51 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 52 | explicit ITensorV2(IContext *ctx) : AclTensor_() |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 53 | { |
| 54 | ARM_COMPUTE_ASSERT_NOT_NULLPTR(ctx); |
| 55 | this->header.ctx = ctx; |
| 56 | this->header.ctx->inc_ref(); |
| 57 | } |
| 58 | /** Destructor */ |
| 59 | virtual ~ITensorV2() |
| 60 | { |
| 61 | this->header.ctx->dec_ref(); |
| 62 | this->header.type = detail::ObjectType::Invalid; |
| 63 | }; |
| 64 | /** Checks if a queue is valid |
| 65 | * |
| 66 | * @return True if successful otherwise false |
| 67 | */ |
| 68 | bool is_valid() const |
| 69 | { |
| 70 | return this->header.type == detail::ObjectType::Tensor; |
| 71 | }; |
| 72 | /** Map tensor to a host pointer |
| 73 | * |
| 74 | * @return A pointer to the underlying backing memory if successful else nullptr |
| 75 | */ |
| 76 | virtual void *map() = 0; |
| 77 | /** Unmap tensor |
| 78 | * |
| 79 | * @return AclStatus A status cod |
| 80 | */ |
| 81 | virtual StatusCode unmap() = 0; |
| 82 | /** Import external memory handle |
| 83 | * |
| 84 | * @param[in] handle Memory to import |
| 85 | * @param[in] type Type of imported memory |
| 86 | * |
| 87 | * @return Status code |
| 88 | */ |
| 89 | virtual StatusCode import(void *handle, ImportMemoryType type) = 0; |
| 90 | /** Get the legacy tensor object |
| 91 | * |
| 92 | * @return The legacy underlying tensor object |
| 93 | */ |
Sang-Hoon Park | c6fcfb4 | 2021-03-31 15:18:16 +0100 | [diff] [blame] | 94 | virtual arm_compute::ITensor *tensor() const = 0; |
| 95 | /** Get the size of the tensor in byte |
| 96 | * |
| 97 | * @note The size isn't based on allocated memory, but based on information in its descriptor (dimensions, data type, etc.). |
| 98 | * |
| 99 | * @return The size of the tensor in byte |
| 100 | */ |
| 101 | size_t get_size() const; |
| 102 | /** Get the descriptor of this tensor |
| 103 | * |
| 104 | * @return The descriptor describing the characteristics of this tensor |
| 105 | */ |
| 106 | AclTensorDescriptor get_descriptor() const; |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | /** Extract internal representation of a Tensor |
| 110 | * |
| 111 | * @param[in] tensor Opaque tensor pointer |
| 112 | * |
| 113 | * @return The internal representation as an ITensor |
| 114 | */ |
| 115 | inline ITensorV2 *get_internal(AclTensor tensor) |
| 116 | { |
| 117 | return static_cast<ITensorV2 *>(tensor); |
| 118 | } |
| 119 | |
| 120 | namespace detail |
| 121 | { |
| 122 | /** Check if an internal tensor is valid |
| 123 | * |
| 124 | * @param[in] tensor Internal tensor to check |
| 125 | * |
| 126 | * @return A status code |
| 127 | */ |
| 128 | inline StatusCode validate_internal_tensor(const ITensorV2 *tensor) |
| 129 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 130 | if (tensor == nullptr || !tensor->is_valid()) |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 131 | { |
| 132 | ARM_COMPUTE_LOG_ERROR_ACL("[ITensorV2]: Invalid tensor object"); |
| 133 | return StatusCode::InvalidArgument; |
| 134 | } |
| 135 | return StatusCode::Success; |
| 136 | } |
| 137 | } // namespace detail |
| 138 | } // namespace arm_compute |
| 139 | #endif /* SRC_COMMON_ITENSOR_H_ */ |