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