Compute Library
 21.11
ITensorV2.h
Go to the documentation of this file.
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"
29 
30 struct AclTensor_
31 {
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  */
52  explicit ITensorV2(IContext *ctx)
53  : AclTensor_()
54  {
56  this->header.ctx = ctx;
57  this->header.ctx->inc_ref();
58  }
59  /** Destructor */
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  */
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  */
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  */
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_ */
Base class specifying the tensor interface.
Definition: ITensorV2.h:45
#define ARM_COMPUTE_LOG_ERROR_ACL(msg)
Log an error message to the logger.
Definition: Log.h:77
StatusCode validate_internal_tensor(const ITensorV2 *tensor)
Check if an internal tensor is valid.
Definition: ITensorV2.h:129
Context interface.
Definition: IContext.h:50
decltype(strategy::transforms) typedef type
Interface for CPU tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
void unmap(T &tensor)
Unmaps a tensor if needed.
Definition: Utils.h:223
arm_compute::detail::Header header
Definition: ITensorV2.h:32
void dec_ref() const
Decrement context refcount.
Definition: IContext.h:76
StatusCode
Definition: Types.h:32
ImportMemoryType
Definition: Types.h:57
ITensorV2(IContext *ctx)
Explict Operator Constructor.
Definition: ITensorV2.h:52
bool is_valid() const
Checks if a queue is valid.
Definition: ITensorV2.h:69
void map(T &tensor, bool blocking)
Maps a tensor if needed.
Definition: Utils.h:212
virtual ~ITensorV2()
Destructor.
Definition: ITensorV2.h:60
void inc_ref() const
Increment context refcount.
Definition: IContext.h:71
ITensorV2 * get_internal(AclTensor tensor)
Extract internal representation of a Tensor.
Definition: ITensorV2.h:116
#define ARM_COMPUTE_ASSERT_NOT_NULLPTR(ptr)
Definition: Validate.h:38