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 | |
Georgios Pinitas | 4164814 | 2021-08-03 08:24:00 +0100 | [diff] [blame] | 30 | // TODO: Remove when all functions have been ported |
| 31 | #include "arm_compute/core/experimental/Types.h" |
| 32 | #include "arm_compute/runtime/IOperator.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 33 | |
Georgios Pinitas | 4164814 | 2021-08-03 08:24:00 +0100 | [diff] [blame] | 34 | #include "src/common/utils/Validate.h" |
| 35 | |
Georgios Pinitas | 06ac6e4 | 2021-07-05 08:08:52 +0100 | [diff] [blame] | 36 | #include <vector> |
| 37 | |
| 38 | struct AclOperator_ |
| 39 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 40 | arm_compute::detail::Header header{arm_compute::detail::ObjectType::Operator, nullptr}; |
Georgios Pinitas | 06ac6e4 | 2021-07-05 08:08:52 +0100 | [diff] [blame] | 41 | |
| 42 | protected: |
| 43 | AclOperator_() = default; |
| 44 | ~AclOperator_() = default; |
| 45 | }; |
| 46 | |
| 47 | namespace arm_compute |
| 48 | { |
| 49 | // Forward declarations |
| 50 | class ITensorPack; |
Georgios Pinitas | 4164814 | 2021-08-03 08:24:00 +0100 | [diff] [blame] | 51 | namespace experimental |
Georgios Pinitas | 06ac6e4 | 2021-07-05 08:08:52 +0100 | [diff] [blame] | 52 | { |
Georgios Pinitas | 4164814 | 2021-08-03 08:24:00 +0100 | [diff] [blame] | 53 | class IOperator; |
| 54 | } // namespace experimental |
| 55 | |
| 56 | using MemoryRequirements = experimental::MemoryRequirements; |
Georgios Pinitas | 06ac6e4 | 2021-07-05 08:08:52 +0100 | [diff] [blame] | 57 | |
| 58 | /** Base class specifying the operator interface */ |
| 59 | class IOperator : public AclOperator_ |
| 60 | { |
| 61 | public: |
| 62 | /** Explict Operator Constructor |
| 63 | * |
| 64 | * @param[in] ctx Context to be used by the operator |
| 65 | */ |
Georgios Pinitas | 4164814 | 2021-08-03 08:24:00 +0100 | [diff] [blame] | 66 | explicit IOperator(IContext *ctx); |
Georgios Pinitas | 06ac6e4 | 2021-07-05 08:08:52 +0100 | [diff] [blame] | 67 | /** Destructor */ |
Georgios Pinitas | 4164814 | 2021-08-03 08:24:00 +0100 | [diff] [blame] | 68 | virtual ~IOperator(); |
Georgios Pinitas | 06ac6e4 | 2021-07-05 08:08:52 +0100 | [diff] [blame] | 69 | /** Checks if an operator is valid |
| 70 | * |
| 71 | * @return True if successful otherwise false |
| 72 | */ |
Georgios Pinitas | 4164814 | 2021-08-03 08:24:00 +0100 | [diff] [blame] | 73 | bool is_valid() const; |
Georgios Pinitas | 06ac6e4 | 2021-07-05 08:08:52 +0100 | [diff] [blame] | 74 | /** Run the kernels contained in the function |
| 75 | * |
Georgios Pinitas | 4164814 | 2021-08-03 08:24:00 +0100 | [diff] [blame] | 76 | * @param[in] queue Queue to use |
Georgios Pinitas | 06ac6e4 | 2021-07-05 08:08:52 +0100 | [diff] [blame] | 77 | * @param[in] tensors Vector that contains the tensors to operate on |
| 78 | */ |
Georgios Pinitas | 4164814 | 2021-08-03 08:24:00 +0100 | [diff] [blame] | 79 | virtual StatusCode run(IQueue &queue, ITensorPack &tensors); |
| 80 | /** Run the kernels contained in the function |
| 81 | * |
| 82 | * @param[in] tensors Vector that contains the tensors to operate on |
| 83 | */ |
| 84 | virtual StatusCode run(ITensorPack &tensors); |
Georgios Pinitas | 06ac6e4 | 2021-07-05 08:08:52 +0100 | [diff] [blame] | 85 | /** Prepare the operator for execution |
| 86 | * |
| 87 | * Any one off pre-processing step required by the function is handled here |
| 88 | * |
Georgios Pinitas | 4164814 | 2021-08-03 08:24:00 +0100 | [diff] [blame] | 89 | * @param[in] tensors Vector that contains the preparation tensors. |
Georgios Pinitas | 06ac6e4 | 2021-07-05 08:08:52 +0100 | [diff] [blame] | 90 | * |
| 91 | * @note Prepare stage might not need all the function's buffers' backing memory to be available in order to execute |
| 92 | */ |
Georgios Pinitas | 4164814 | 2021-08-03 08:24:00 +0100 | [diff] [blame] | 93 | virtual StatusCode prepare(ITensorPack &tensors); |
Georgios Pinitas | 06ac6e4 | 2021-07-05 08:08:52 +0100 | [diff] [blame] | 94 | /** Return the memory requirements required by the workspace |
| 95 | */ |
Georgios Pinitas | 4164814 | 2021-08-03 08:24:00 +0100 | [diff] [blame] | 96 | virtual MemoryRequirements workspace() const; |
| 97 | |
| 98 | void set_internal_operator(std::unique_ptr<experimental::IOperator> op) |
Georgios Pinitas | 06ac6e4 | 2021-07-05 08:08:52 +0100 | [diff] [blame] | 99 | { |
Georgios Pinitas | 4164814 | 2021-08-03 08:24:00 +0100 | [diff] [blame] | 100 | _op = std::move(op); |
Georgios Pinitas | 06ac6e4 | 2021-07-05 08:08:52 +0100 | [diff] [blame] | 101 | } |
Georgios Pinitas | 4164814 | 2021-08-03 08:24:00 +0100 | [diff] [blame] | 102 | |
| 103 | private: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 104 | std::unique_ptr<experimental::IOperator> _op{nullptr}; |
Georgios Pinitas | 06ac6e4 | 2021-07-05 08:08:52 +0100 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | /** Extract internal representation of an Operator |
| 108 | * |
| 109 | * @param[in] op Opaque operator pointer |
| 110 | * |
| 111 | * @return The internal representation as an IOperator |
| 112 | */ |
| 113 | inline IOperator *get_internal(AclOperator op) |
| 114 | { |
| 115 | return static_cast<IOperator *>(op); |
| 116 | } |
| 117 | |
| 118 | namespace detail |
| 119 | { |
| 120 | /** Check if an internal operator is valid |
| 121 | * |
| 122 | * @param[in] op Internal operator to check |
| 123 | * |
| 124 | * @return A status code |
| 125 | */ |
| 126 | inline StatusCode validate_internal_operator(const IOperator *op) |
| 127 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 128 | if (op == nullptr || !op->is_valid()) |
Georgios Pinitas | 06ac6e4 | 2021-07-05 08:08:52 +0100 | [diff] [blame] | 129 | { |
| 130 | ARM_COMPUTE_LOG_ERROR_ACL("[IOperator]: Invalid operator object"); |
| 131 | return StatusCode::InvalidArgument; |
| 132 | } |
| 133 | return StatusCode::Success; |
| 134 | } |
| 135 | } // namespace detail |
| 136 | } // namespace arm_compute |
| 137 | #endif /* SRC_COMMON_IOPERATOR_H_ */ |