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