blob: 1b65a09e0d8bf07197a9e5ebf848b18858352bc6 [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"
33#include "src/common/utils/Validate.h"
34
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010035#include <vector>
36
37struct AclOperator_
38{
39 arm_compute::detail::Header header{ arm_compute::detail::ObjectType::Operator, nullptr };
40
41protected:
42 AclOperator_() = default;
43 ~AclOperator_() = default;
44};
45
46namespace arm_compute
47{
48// Forward declarations
49class ITensorPack;
Georgios Pinitas41648142021-08-03 08:24:00 +010050namespace experimental
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010051{
Georgios Pinitas41648142021-08-03 08:24:00 +010052class IOperator;
53} // namespace experimental
54
55using MemoryRequirements = experimental::MemoryRequirements;
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010056
57/** Base class specifying the operator interface */
58class IOperator : public AclOperator_
59{
60public:
61 /** Explict Operator Constructor
62 *
63 * @param[in] ctx Context to be used by the operator
64 */
Georgios Pinitas41648142021-08-03 08:24:00 +010065 explicit IOperator(IContext *ctx);
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010066 /** Destructor */
Georgios Pinitas41648142021-08-03 08:24:00 +010067 virtual ~IOperator();
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010068 /** Checks if an operator is valid
69 *
70 * @return True if successful otherwise false
71 */
Georgios Pinitas41648142021-08-03 08:24:00 +010072 bool is_valid() const;
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010073 /** Run the kernels contained in the function
74 *
Georgios Pinitas41648142021-08-03 08:24:00 +010075 * @param[in] queue Queue to use
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010076 * @param[in] tensors Vector that contains the tensors to operate on
77 */
Georgios Pinitas41648142021-08-03 08:24:00 +010078 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);
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010084 /** Prepare the operator for execution
85 *
86 * Any one off pre-processing step required by the function is handled here
87 *
Georgios Pinitas41648142021-08-03 08:24:00 +010088 * @param[in] tensors Vector that contains the preparation tensors.
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010089 *
90 * @note Prepare stage might not need all the function's buffers' backing memory to be available in order to execute
91 */
Georgios Pinitas41648142021-08-03 08:24:00 +010092 virtual StatusCode prepare(ITensorPack &tensors);
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010093 /** Return the memory requirements required by the workspace
94 */
Georgios Pinitas41648142021-08-03 08:24:00 +010095 virtual MemoryRequirements workspace() const;
96
97 void set_internal_operator(std::unique_ptr<experimental::IOperator> op)
Georgios Pinitas06ac6e42021-07-05 08:08:52 +010098 {
Georgios Pinitas41648142021-08-03 08:24:00 +010099 _op = std::move(op);
Georgios Pinitas06ac6e42021-07-05 08:08:52 +0100100 }
Georgios Pinitas41648142021-08-03 08:24:00 +0100101
102private:
103 std::unique_ptr<experimental::IOperator> _op{ nullptr };
Georgios Pinitas06ac6e42021-07-05 08:08:52 +0100104};
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 */
112inline IOperator *get_internal(AclOperator op)
113{
114 return static_cast<IOperator *>(op);
115}
116
117namespace 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 */
125inline 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_ */