Georgios Pinitas | 06ac6e4 | 2021-07-05 08:08:52 +0100 | [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_IOPERATOR_H_ |
| 25 | #define SRC_COMMON_IOPERATOR_H_ |
| 26 | |
| 27 | #include "src/common/IContext.h" |
| 28 | #include "src/common/IQueue.h" |
| 29 | |
| 30 | #include <vector> |
| 31 | |
| 32 | struct AclOperator_ |
| 33 | { |
| 34 | arm_compute::detail::Header header{ arm_compute::detail::ObjectType::Operator, nullptr }; |
| 35 | |
| 36 | protected: |
| 37 | AclOperator_() = default; |
| 38 | ~AclOperator_() = default; |
| 39 | }; |
| 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | // Forward declarations |
| 44 | class ITensorPack; |
| 45 | |
| 46 | /** Structure to capture internally memory requirements */ |
| 47 | struct MemoryInfo |
| 48 | { |
| 49 | MemoryInfo(AclTensorSlot slot_id, size_t size, size_t alignment) noexcept |
| 50 | : slot_id(slot_id), |
| 51 | size(size), |
| 52 | alignment(alignment) |
| 53 | { |
| 54 | } |
| 55 | AclTensorSlot slot_id; |
| 56 | size_t size; |
| 57 | size_t alignment; |
| 58 | }; |
| 59 | using MemoryRequirements = std::vector<MemoryInfo>; |
| 60 | |
| 61 | /** Base class specifying the operator interface */ |
| 62 | class IOperator : public AclOperator_ |
| 63 | { |
| 64 | public: |
| 65 | /** Explict Operator Constructor |
| 66 | * |
| 67 | * @param[in] ctx Context to be used by the operator |
| 68 | */ |
| 69 | explicit IOperator(IContext *ctx) |
| 70 | { |
| 71 | this->header.ctx = ctx; |
| 72 | this->header.ctx->inc_ref(); |
| 73 | } |
| 74 | |
| 75 | /** Destructor */ |
| 76 | virtual ~IOperator() |
| 77 | { |
| 78 | this->header.ctx->dec_ref(); |
| 79 | this->header.type = detail::ObjectType::Invalid; |
| 80 | }; |
| 81 | |
| 82 | /** Checks if an operator is valid |
| 83 | * |
| 84 | * @return True if successful otherwise false |
| 85 | */ |
| 86 | bool is_valid() const |
| 87 | { |
| 88 | return this->header.type == detail::ObjectType::Operator; |
| 89 | }; |
| 90 | |
| 91 | /** Run the kernels contained in the function |
| 92 | * |
| 93 | * @param[in] queue Queue to run a kernel on |
| 94 | * @param[in] tensors Vector that contains the tensors to operate on |
| 95 | */ |
| 96 | virtual StatusCode run(IQueue &queue, ITensorPack &tensors) = 0; |
| 97 | |
| 98 | /** Prepare the operator for execution |
| 99 | * |
| 100 | * Any one off pre-processing step required by the function is handled here |
| 101 | * |
| 102 | * @param[in] constants Vector that contains the constants tensors. |
| 103 | * |
| 104 | * @note Prepare stage might not need all the function's buffers' backing memory to be available in order to execute |
| 105 | */ |
| 106 | virtual StatusCode prepare(ITensorPack &constants) |
| 107 | { |
| 108 | ARM_COMPUTE_UNUSED(constants); |
| 109 | return StatusCode::Success; |
| 110 | } |
| 111 | |
| 112 | /** Return the memory requirements required by the workspace |
| 113 | */ |
| 114 | virtual MemoryRequirements workspace() const |
| 115 | { |
| 116 | return {}; |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | /** Extract internal representation of an Operator |
| 121 | * |
| 122 | * @param[in] op Opaque operator pointer |
| 123 | * |
| 124 | * @return The internal representation as an IOperator |
| 125 | */ |
| 126 | inline IOperator *get_internal(AclOperator op) |
| 127 | { |
| 128 | return static_cast<IOperator *>(op); |
| 129 | } |
| 130 | |
| 131 | namespace detail |
| 132 | { |
| 133 | /** Check if an internal operator is valid |
| 134 | * |
| 135 | * @param[in] op Internal operator to check |
| 136 | * |
| 137 | * @return A status code |
| 138 | */ |
| 139 | inline StatusCode validate_internal_operator(const IOperator *op) |
| 140 | { |
| 141 | if(op == nullptr || !op->is_valid()) |
| 142 | { |
| 143 | ARM_COMPUTE_LOG_ERROR_ACL("[IOperator]: Invalid operator object"); |
| 144 | return StatusCode::InvalidArgument; |
| 145 | } |
| 146 | return StatusCode::Success; |
| 147 | } |
| 148 | } // namespace detail |
| 149 | } // namespace arm_compute |
| 150 | #endif /* SRC_COMMON_IOPERATOR_H_ */ |