xref: /aosp_15_r20/external/ComputeLibrary/src/common/ITensorV2.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
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_ITENSOR_H_
25 #define SRC_COMMON_ITENSOR_H_
26 
27 #include "src/common/IContext.h"
28 #include "src/common/utils/Validate.h"
29 
30 struct AclTensor_
31 {
32     arm_compute::detail::Header header{ arm_compute::detail::ObjectType::Tensor, nullptr };
33 
34 protected:
35     AclTensor_()  = default;
36     ~AclTensor_() = default;
37 };
38 
39 namespace arm_compute
40 {
41 // Forward declaration
42 class ITensor;
43 
44 /** Base class specifying the tensor interface */
45 class ITensorV2 : public AclTensor_
46 {
47 public:
48     /** Explict Operator Constructor
49      *
50      * @param[in] ctx Context to be used by the operator
51      */
ITensorV2(IContext * ctx)52     explicit ITensorV2(IContext *ctx)
53         : AclTensor_()
54     {
55         ARM_COMPUTE_ASSERT_NOT_NULLPTR(ctx);
56         this->header.ctx = ctx;
57         this->header.ctx->inc_ref();
58     }
59     /** Destructor */
~ITensorV2()60     virtual ~ITensorV2()
61     {
62         this->header.ctx->dec_ref();
63         this->header.type = detail::ObjectType::Invalid;
64     };
65     /** Checks if a queue is valid
66      *
67      * @return True if successful otherwise false
68      */
is_valid()69     bool is_valid() const
70     {
71         return this->header.type == detail::ObjectType::Tensor;
72     };
73     /** Map tensor to a host pointer
74      *
75      * @return A pointer to the underlying backing memory if successful else nullptr
76      */
77     virtual void *map() = 0;
78     /** Unmap tensor
79      *
80      * @return AclStatus A status cod
81      */
82     virtual StatusCode unmap() = 0;
83     /** Import external memory handle
84      *
85      * @param[in] handle Memory to import
86      * @param[in] type   Type of imported memory
87      *
88      * @return Status code
89      */
90     virtual StatusCode import(void *handle, ImportMemoryType type) = 0;
91     /** Get the legacy tensor object
92      *
93      * @return The legacy underlying tensor object
94      */
95     virtual arm_compute::ITensor *tensor() const = 0;
96     /** Get the size of the tensor in byte
97      *
98      * @note The size isn't based on allocated memory, but based on information in its descriptor (dimensions, data type, etc.).
99      *
100      * @return The size of the tensor in byte
101      */
102     size_t get_size() const;
103     /** Get the descriptor of this tensor
104      *
105      * @return The descriptor describing the characteristics of this tensor
106      */
107     AclTensorDescriptor get_descriptor() const;
108 };
109 
110 /** Extract internal representation of a Tensor
111  *
112  * @param[in] tensor Opaque tensor pointer
113  *
114  * @return The internal representation as an ITensor
115  */
get_internal(AclTensor tensor)116 inline ITensorV2 *get_internal(AclTensor tensor)
117 {
118     return static_cast<ITensorV2 *>(tensor);
119 }
120 
121 namespace detail
122 {
123 /** Check if an internal tensor is valid
124  *
125  * @param[in] tensor Internal tensor to check
126  *
127  * @return A status code
128  */
validate_internal_tensor(const ITensorV2 * tensor)129 inline StatusCode validate_internal_tensor(const ITensorV2 *tensor)
130 {
131     if(tensor == nullptr || !tensor->is_valid())
132     {
133         ARM_COMPUTE_LOG_ERROR_ACL("[ITensorV2]: Invalid tensor object");
134         return StatusCode::InvalidArgument;
135     }
136     return StatusCode::Success;
137 }
138 } // namespace detail
139 } // namespace arm_compute
140 #endif /* SRC_COMMON_ITENSOR_H_ */
141