xref: /aosp_15_r20/external/ComputeLibrary/src/common/TensorPack.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_ITENSORPACK_H_
25 #define SRC_COMMON_ITENSORPACK_H_
26 
27 #include "arm_compute/core/ITensorPack.h"
28 #include "src/common/IContext.h"
29 
30 struct AclTensorPack_
31 {
32     arm_compute::detail::Header header{ arm_compute::detail::ObjectType::TensorPack, nullptr };
33 
34 protected:
35     AclTensorPack_()  = default;
36     ~AclTensorPack_() = default;
37 };
38 
39 namespace arm_compute
40 {
41 // Forward declaration
42 class ITensor;
43 class ITensorV2;
44 
45 /** Tensor packing service
46  *
47  * Class is responsible for creating and managing a collection of tensors.
48  * Tensor packs can be passed to operators to be part of the mutable data of the execution.
49  */
50 class TensorPack : public AclTensorPack_
51 {
52 public:
53     /** Constructor
54      *
55      * @param[in] ctx Context to be used
56      */
57     explicit TensorPack(IContext *ctx);
58     /** Destructor */
59     ~TensorPack();
60     /** Add tensor to the pack
61      *
62      * @param[in] tensor  Tensor to add
63      * @param[in] slot_id Slot identification in respect to the operator of the tensor to add
64      *
65      * @return Status code
66      */
67     AclStatus add_tensor(ITensorV2 *tensor, int32_t slot_id);
68     /** Pack size accessor
69      *
70      * @return Number of tensors registered to the pack
71      */
72     size_t size() const;
73     /** Checks if pack is empty
74      *
75      * @return True if empty else false
76      */
77     bool empty() const;
78     /** Checks if an object is valid
79      *
80      * @return True if valid else false
81      */
82     bool is_valid() const;
83     /** Get tensor of a given id from the pac
84      *
85      * @param[in] slot_id Slot identification of tensor to extract
86      *
87      * @return The pointer to the tensor if exist and is non-const else nullptr
88      */
89     arm_compute::ITensor *get_tensor(int32_t slot_id);
90     /** Get legacy tensor pack
91      *
92      * @return Legacy tensor pack
93      */
94     arm_compute::ITensorPack &get_tensor_pack();
95 
96 private:
97     arm_compute::ITensorPack _pack; /**< Pack that currently redirects to the existing TensorPack */
98 };
99 
100 /** Extract internal representation of a TensoPack
101  *
102  * @param[in] pack Opaque tensor pack pointer
103  *
104  * @return The internal representation as an TensorPack
105  */
get_internal(AclTensorPack pack)106 inline TensorPack *get_internal(AclTensorPack pack)
107 {
108     return static_cast<TensorPack *>(pack);
109 }
110 
111 namespace detail
112 {
113 /** Check if an internal TensorPack is valid
114  *
115  * @param[in] pack Internal tensor pack to check
116  *
117  * @return A status code
118  */
validate_internal_pack(const TensorPack * pack)119 inline StatusCode validate_internal_pack(const TensorPack *pack)
120 {
121     if(pack == nullptr || !pack->is_valid())
122     {
123         ARM_COMPUTE_LOG_ERROR_ACL("[TensorPack]: Invalid tensor pack object");
124         return StatusCode::InvalidArgument;
125     }
126     return StatusCode::Success;
127 }
128 } // namespace detail
129 } // namespace arm_compute
130 #endif /* SRC_COMMON_ITENSORPACK_H_ */
131