blob: 58b17ff70eea44e31b5a63a55be326db0d9199f8 [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#include "arm_compute/AclEntrypoints.h"
25#include "src/common/ITensor.h"
26#include "src/common/utils/Macros.h"
27
28namespace
29{
30/**< Maximum allowed dimensions by Compute Library */
31constexpr int32_t max_allowed_dims = 6;
32
33/** Check if a descriptor is valid
34 *
35 * @param desc Descriptor to validate
36 *
37 * @return true in case of success else false
38 */
39bool is_desc_valid(const AclTensorDescriptor &desc)
40{
41 if(desc.data_type > AclFloat32)
42 {
43 ARM_COMPUTE_LOG_ERROR_ACL("[AclCreateTensor]: Unknown data type!");
44 return false;
45 }
46 if(desc.ndims > max_allowed_dims)
47 {
48 ARM_COMPUTE_LOG_ERROR_ACL("[AclCreateTensor]: Dimensions surpass the maximum allowed value!");
49 return false;
50 }
51 if(desc.ndims > 0 && desc.shape == nullptr)
52 {
53 ARM_COMPUTE_LOG_ERROR_ACL("[AclCreateTensor]: Dimensions values are empty while dimensionality is > 0!");
54 return false;
55 }
56 return true;
57}
58} // namespace
59
60extern "C" AclStatus AclCreateTensor(AclTensor *external_tensor,
61 AclContext external_ctx,
62 const AclTensorDescriptor *desc,
63 bool allocate)
64{
65 using namespace arm_compute;
66
67 IContext *ctx = get_internal(external_ctx);
68
69 StatusCode status = detail::validate_internal_context(ctx);
70 ARM_COMPUTE_RETURN_CENUM_ON_FAILURE(status);
71
72 if(desc == nullptr || !is_desc_valid(*desc))
73 {
74 ARM_COMPUTE_LOG_ERROR_ACL("[AclCreateTensor]: Descriptor is invalid!");
75 return AclInvalidArgument;
76 }
77
78 auto tensor = ctx->create_tensor(*desc, allocate);
79 if(tensor == nullptr)
80 {
81 ARM_COMPUTE_LOG_ERROR_ACL("[AclCreateTensor]: Couldn't allocate internal resources for tensor creation!");
82 return AclOutOfMemory;
83 }
84 *external_tensor = tensor;
85
86 return AclSuccess;
87}
88
89extern "C" AclStatus AclMapTensor(AclTensor external_tensor, void **handle)
90{
91 using namespace arm_compute;
92
93 auto tensor = get_internal(external_tensor);
94 StatusCode status = detail::validate_internal_tensor(tensor);
95 ARM_COMPUTE_RETURN_CENUM_ON_FAILURE(status);
96
97 if(handle == nullptr)
98 {
99 ARM_COMPUTE_LOG_ERROR_ACL("[AclMapTensor]: Handle object is nullptr!");
100 return AclInvalidArgument;
101 }
102
103 *handle = tensor->map();
104
105 return AclSuccess;
106}
107
108extern "C" AclStatus AclUnmapTensor(AclTensor external_tensor, void *handle)
109{
110 ARM_COMPUTE_UNUSED(handle);
111
112 using namespace arm_compute;
113
114 auto tensor = get_internal(external_tensor);
115 StatusCode status = detail::validate_internal_tensor(tensor);
116 ARM_COMPUTE_RETURN_CENUM_ON_FAILURE(status);
117
118 status = tensor->unmap();
119 return AclSuccess;
120}
121
122extern "C" AclStatus AclTensorImport(AclTensor external_tensor, void *handle, AclImportMemoryType type)
123{
124 using namespace arm_compute;
125
126 auto tensor = get_internal(external_tensor);
127 StatusCode status = detail::validate_internal_tensor(tensor);
128 ARM_COMPUTE_RETURN_CENUM_ON_FAILURE(status);
129
130 status = tensor->import(handle, utils::as_enum<ImportMemoryType>(type));
131 ARM_COMPUTE_RETURN_CENUM_ON_FAILURE(status);
132
133 return AclSuccess;
134}
135
136extern "C" AclStatus AclDestroyTensor(AclTensor external_tensor)
137{
138 using namespace arm_compute;
139
140 auto tensor = get_internal(external_tensor);
141
142 StatusCode status = detail::validate_internal_tensor(tensor);
143 ARM_COMPUTE_RETURN_CENUM_ON_FAILURE(status);
144
145 delete tensor;
146
147 return AclSuccess;
148}