blob: e86e11fe25d547eb535013cbab9fa154f664d04d [file] [log] [blame]
Georgios Pinitas06ac6e42021-07-05 08:08:52 +01001/*
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 Pinitas41648142021-08-03 08:24:00 +010030// TODO: Remove when all functions have been ported
31#include "arm_compute/core/experimental/Types.h"
32#include "arm_compute/runtime/IOperator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033
Georgios Pinitas41648142021-08-03 08:24:00 +010034#include "src/common/utils/Validate.h"
35
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010036#include <vector>
37
38struct AclOperator_
39{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010040 arm_compute::detail::Header header{arm_compute::detail::ObjectType::Operator, nullptr};
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010041
42protected:
43 AclOperator_() = default;
44 ~AclOperator_() = default;
45};
46
47namespace arm_compute
48{
49// Forward declarations
50class ITensorPack;
Georgios Pinitas41648142021-08-03 08:24:00 +010051namespace experimental
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010052{
Georgios Pinitas41648142021-08-03 08:24:00 +010053class IOperator;
54} // namespace experimental
55
56using MemoryRequirements = experimental::MemoryRequirements;
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010057
58/** Base class specifying the operator interface */
59class IOperator : public AclOperator_
60{
61public:
62 /** Explict Operator Constructor
63 *
64 * @param[in] ctx Context to be used by the operator
65 */
Georgios Pinitas41648142021-08-03 08:24:00 +010066 explicit IOperator(IContext *ctx);
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010067 /** Destructor */
Georgios Pinitas41648142021-08-03 08:24:00 +010068 virtual ~IOperator();
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010069 /** Checks if an operator is valid
70 *
71 * @return True if successful otherwise false
72 */
Georgios Pinitas41648142021-08-03 08:24:00 +010073 bool is_valid() const;
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010074 /** Run the kernels contained in the function
75 *
Georgios Pinitas41648142021-08-03 08:24:00 +010076 * @param[in] queue Queue to use
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010077 * @param[in] tensors Vector that contains the tensors to operate on
78 */
Georgios Pinitas41648142021-08-03 08:24:00 +010079 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 Pinitas06ac6e42021-07-05 08:08:52 +010085 /** Prepare the operator for execution
86 *
87 * Any one off pre-processing step required by the function is handled here
88 *
Georgios Pinitas41648142021-08-03 08:24:00 +010089 * @param[in] tensors Vector that contains the preparation tensors.
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010090 *
91 * @note Prepare stage might not need all the function's buffers' backing memory to be available in order to execute
92 */
Georgios Pinitas41648142021-08-03 08:24:00 +010093 virtual StatusCode prepare(ITensorPack &tensors);
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010094 /** Return the memory requirements required by the workspace
95 */
Georgios Pinitas41648142021-08-03 08:24:00 +010096 virtual MemoryRequirements workspace() const;
97
98 void set_internal_operator(std::unique_ptr<experimental::IOperator> op)
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010099 {
Georgios Pinitas41648142021-08-03 08:24:00 +0100100 _op = std::move(op);
Georgios Pinitas06ac6e42021-07-05 08:08:52 +0100101 }
Georgios Pinitas41648142021-08-03 08:24:00 +0100102
103private:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100104 std::unique_ptr<experimental::IOperator> _op{nullptr};
Georgios Pinitas06ac6e42021-07-05 08:08:52 +0100105};
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 */
113inline IOperator *get_internal(AclOperator op)
114{
115 return static_cast<IOperator *>(op);
116}
117
118namespace 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 */
126inline StatusCode validate_internal_operator(const IOperator *op)
127{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100128 if (op == nullptr || !op->is_valid())
Georgios Pinitas06ac6e42021-07-05 08:08:52 +0100129 {
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_ */