Compute Library
 23.11
ITensorPack.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-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 ARM_COMPUTE_ITENSORPACK_H
25 #define ARM_COMPUTE_ITENSORPACK_H
26 
28 
29 #include <cstddef>
30 #include <cstdint>
31 #include <unordered_map>
32 
33 namespace arm_compute
34 {
35 // Forward declaration
36 class ITensor;
37 
38 /** Tensor packing service */
40 {
41 public:
42  struct PackElement
43  {
44  PackElement() = default;
45  PackElement(int id, ITensor *tensor) : id(id), tensor(tensor), ctensor(nullptr)
46  {
47  }
48  PackElement(int id, const ITensor *ctensor) : id(id), tensor(nullptr), ctensor(ctensor)
49  {
50  }
51 
52  int id{-1};
53  ITensor *tensor{nullptr};
54  const ITensor *ctensor{nullptr};
55  };
56 
57 public:
58  /** Default Constructor */
59  ITensorPack() = default;
60  /** Initializer list Constructor */
61  ITensorPack(std::initializer_list<PackElement> l);
62  /** Add tensor to the pack
63  *
64  * @param[in] id ID/type of the tensor to add
65  * @param[in] tensor Tensor to add
66  */
67  void add_tensor(int id, ITensor *tensor);
68 
69  /** Add const tensor to the pack
70  *
71  * @param[in] id ID/type of the tensor to add
72  * @param[in] tensor Tensor to add
73  */
74  void add_tensor(int id, const ITensor *tensor);
75 
76  /** Add const tensor to the pack
77  *
78  * @param[in] id ID/type of the tensor to add
79  * @param[in] tensor Tensor to add
80  */
81  void add_const_tensor(int id, const ITensor *tensor);
82  /** Get tensor of a given id from the pac
83  *
84  * @param[in] id ID of tensor to extract
85  *
86  * @return The pointer to the tensor if exist and is non-const else nullptr
87  */
88  ITensor *get_tensor(int id);
89  /** Get constant tensor of a given id
90  *
91  * @param[in] id ID of tensor to extract
92  *
93  * @return The pointer to the tensor if exist and is const else nullptr
94  */
95  const ITensor *get_const_tensor(int id) const;
96  /** Remove the tensor stored with the given id
97  *
98  * @param[in] id ID of tensor to remove
99  */
100  void remove_tensor(int id);
101  /** Pack size accessor
102  *
103  * @return Number of tensors registered to the pack
104  */
105  size_t size() const;
106  /** Checks if pack is empty
107  *
108  * @return True if empty else false
109  */
110  bool empty() const;
111 
112 private:
113  std::unordered_map<int, PackElement> _pack{}; /**< Container with the packed tensors */
114 };
115 } // namespace arm_compute
116 #endif /*ARM_COMPUTE_ITENSORPACK_H */
arm_compute::ITensorPack::PackElement::PackElement
PackElement(int id, const ITensor *ctensor)
Definition: ITensorPack.h:48
arm_compute::ITensorPack::remove_tensor
void remove_tensor(int id)
Remove the tensor stored with the given id.
Definition: ITensorPack.cpp:69
arm_compute::ITensorPack::PackElement::PackElement
PackElement()=default
arm_compute::ITensor
Interface for CPU tensor.
Definition: ITensor.h:36
arm_compute::ITensorPack::add_tensor
void add_tensor(int id, ITensor *tensor)
Add tensor to the pack.
Definition: ITensorPack.cpp:38
arm_compute::ITensorPack::get_tensor
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
Definition: ITensorPack.cpp:63
arm_compute::ITensorPack::add_const_tensor
void add_const_tensor(int id, const ITensor *tensor)
Add const tensor to the pack.
Definition: ITensorPack.cpp:48
arm_compute::ITensorPack::PackElement::tensor
ITensor * tensor
Definition: ITensorPack.h:53
Types.h
arm_compute::ITensorPack::empty
bool empty() const
Checks if pack is empty.
Definition: ITensorPack.cpp:79
arm_compute::ITensorPack::get_const_tensor
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:53
arm_compute::ITensorPack
Tensor packing service.
Definition: ITensorPack.h:39
arm_compute::ITensorPack::size
size_t size() const
Pack size accessor.
Definition: ITensorPack.cpp:74
tensor
CLTensor * tensor
Pointer to the auxiliary tensor.
Definition: ClWorkloadRuntime.cpp:67
arm_compute::ITensorPack::PackElement
Definition: ITensorPack.h:42
arm_compute::ITensorPack::PackElement::PackElement
PackElement(int id, ITensor *tensor)
Definition: ITensorPack.h:45
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::ITensorPack::PackElement::id
int id
Definition: ITensorPack.h:52
arm_compute::ITensorPack::PackElement::ctensor
const ITensor * ctensor
Definition: ITensorPack.h:54
arm_compute::ITensorPack::ITensorPack
ITensorPack()=default
Default Constructor.