1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "BufferTracker.h"
18
19 #include <android-base/macros.h>
20
21 #include <memory>
22 #include <mutex>
23 #include <set>
24 #include <stack>
25 #include <utility>
26 #include <vector>
27
28 #include "CpuExecutor.h"
29 #include "LegacyUtils.h"
30 #include "nnapi/TypeUtils.h"
31 #include "nnapi/Validation.h"
32
33 namespace android::nn {
34
create(uint32_t size,std::set<PreparedModelRole> roles,const Operand & operand)35 std::shared_ptr<ManagedBuffer> ManagedBuffer::create(uint32_t size,
36 std::set<PreparedModelRole> roles,
37 const Operand& operand) {
38 std::unique_ptr<uint8_t[]> buffer(new (std::nothrow) uint8_t[size]);
39 if (buffer == nullptr) {
40 return nullptr;
41 }
42 if (isExtension(operand.type)) {
43 LOG(ERROR) << "ManagedBuffer cannot handle extension operands.";
44 return nullptr;
45 }
46 return std::make_shared<ManagedBuffer>(std::move(buffer), size, std::move(roles), operand);
47 }
48
ManagedBuffer(std::unique_ptr<uint8_t[]> buffer,uint32_t size,std::set<PreparedModelRole> roles,const Operand & operand)49 ManagedBuffer::ManagedBuffer(std::unique_ptr<uint8_t[]> buffer, uint32_t size,
50 std::set<PreparedModelRole> roles, const Operand& operand)
51 : kBuffer(std::move(buffer)),
52 kSize(size),
53 kRoles(std::move(roles)),
54 kOperandType(operand.type),
55 kInitialDimensions(operand.dimensions),
56 mUpdatedDimensions(operand.dimensions) {
57 CHECK(!isExtension(kOperandType));
58 }
59
validateRequest(uint32_t poolIndex,const Request & request,const IPreparedModel * preparedModel) const60 ErrorStatus ManagedBuffer::validateRequest(uint32_t poolIndex, const Request& request,
61 const IPreparedModel* preparedModel) const {
62 CHECK_LT(poolIndex, request.pools.size());
63 CHECK(std::holds_alternative<Request::MemoryDomainToken>(request.pools[poolIndex]));
64 std::lock_guard<std::mutex> guard(mMutex);
65
66 bool usedAsInput = false, usedAsOutput = false;
67 for (uint32_t i = 0; i < request.inputs.size(); i++) {
68 if (request.inputs[i].lifetime != Request::Argument::LifeTime::POOL) continue;
69 if (request.inputs[i].location.poolIndex != poolIndex) continue;
70 // Validate if the input role is specified during allocation.
71 if (kRoles.count({preparedModel, IOType::INPUT, i}) == 0) {
72 LOG(ERROR) << "ManagedBuffer::validateRequest -- invalid buffer role.";
73 return ErrorStatus::INVALID_ARGUMENT;
74 }
75 if (!mInitialized) {
76 LOG(ERROR) << "ManagedBuffer::validateRequest -- using uninitialized buffer as input "
77 "request.";
78 return ErrorStatus::GENERAL_FAILURE;
79 }
80 auto combined = combineDimensions(mUpdatedDimensions, request.inputs[i].dimensions);
81 if (!combined.has_value()) {
82 LOG(ERROR) << "ManagedBuffer::validateRequest -- incompatible dimensions ("
83 << toString(mUpdatedDimensions) << " vs "
84 << toString(request.inputs[i].dimensions) << ")";
85 return ErrorStatus::INVALID_ARGUMENT;
86 }
87 usedAsInput = true;
88 }
89 for (uint32_t i = 0; i < request.outputs.size(); i++) {
90 if (request.outputs[i].lifetime != Request::Argument::LifeTime::POOL) continue;
91 if (request.outputs[i].location.poolIndex != poolIndex) continue;
92 if (usedAsInput || usedAsOutput) {
93 LOG(ERROR) << "ManagedBuffer::validateRequest -- using the same device memory for "
94 "input/output or multiple outputs";
95 return ErrorStatus::INVALID_ARGUMENT;
96 }
97 // Validate if the output role is specified during allocation.
98 if (kRoles.count({preparedModel, IOType::OUTPUT, i}) == 0) {
99 LOG(ERROR) << "ManagedBuffer::validateRequest -- invalid buffer role.";
100 return ErrorStatus::INVALID_ARGUMENT;
101 }
102 auto combined = combineDimensions(kInitialDimensions, request.outputs[i].dimensions);
103 if (!combined.has_value()) {
104 LOG(ERROR) << "ManagedBuffer::validateRequest -- incompatible dimensions ("
105 << toString(kInitialDimensions) << " vs "
106 << toString(request.outputs[i].dimensions) << ")";
107 return ErrorStatus::INVALID_ARGUMENT;
108 }
109 usedAsOutput = true;
110 }
111 return ErrorStatus::NONE;
112 }
113
validateCopyFrom(const std::vector<uint32_t> & dimensions,uint32_t size) const114 ErrorStatus ManagedBuffer::validateCopyFrom(const std::vector<uint32_t>& dimensions,
115 uint32_t size) const {
116 if (size != kSize) {
117 LOG(ERROR) << "ManagedBuffer::validateCopyFrom -- invalid memory size: " << kSize << " vs "
118 << size;
119 return ErrorStatus::INVALID_ARGUMENT;
120 }
121
122 if (nonExtensionOperandTypeIsScalar(static_cast<int>(kOperandType))) {
123 if (!dimensions.empty()) {
124 LOG(ERROR)
125 << "ManagedBuffer::validateCopyFrom -- invalid dimensions for scalar operand: "
126 << toString(dimensions);
127 return ErrorStatus::INVALID_ARGUMENT;
128 }
129 return ErrorStatus::NONE;
130 }
131
132 if (dimensions.empty()) {
133 if (tensorHasUnspecifiedDimensions(kOperandType, kInitialDimensions)) {
134 LOG(ERROR) << "ManagedBuffer::validateCopyFrom -- the initial dimensions are not fully "
135 "specified and no dimension update is provided: "
136 << toString(kInitialDimensions);
137 return ErrorStatus::INVALID_ARGUMENT;
138 }
139 } else {
140 if (tensorHasUnspecifiedDimensions(kOperandType, dimensions)) {
141 LOG(ERROR) << "ManagedBuffer::validateCopyFrom -- the updated dimensions are not fully "
142 "specified: "
143 << toString(dimensions);
144 return ErrorStatus::INVALID_ARGUMENT;
145 }
146 }
147
148 const auto combined = combineDimensions(kInitialDimensions, dimensions);
149 if (!combined.has_value()) {
150 LOG(ERROR) << "ManagedBuffer::validateCopyFrom -- incompatible dimensions ("
151 << toString(kInitialDimensions) << " vs " << toString(dimensions) << ")";
152 return ErrorStatus::INVALID_ARGUMENT;
153 }
154 return ErrorStatus::NONE;
155 }
156
validateCopyTo(uint32_t size) const157 ErrorStatus ManagedBuffer::validateCopyTo(uint32_t size) const {
158 if (size != kSize) {
159 LOG(ERROR) << "ManagedBuffer::validateCopyTo -- invalid memory size: " << kSize << " vs "
160 << size;
161 return ErrorStatus::INVALID_ARGUMENT;
162 }
163 std::lock_guard<std::mutex> guard(mMutex);
164 if (!mInitialized) {
165 LOG(ERROR) << "ManagedBuffer::validateCopyTo -- using uninitialized buffer as source.";
166 return ErrorStatus::GENERAL_FAILURE;
167 }
168 return ErrorStatus::NONE;
169 }
170
updateDimensions(const std::vector<uint32_t> & dimensions)171 bool ManagedBuffer::updateDimensions(const std::vector<uint32_t>& dimensions) {
172 auto combined = combineDimensions(kInitialDimensions, dimensions);
173 if (!combined.has_value()) {
174 LOG(ERROR) << "ManagedBuffer::updateDimensions -- incompatible dimensions ("
175 << toString(kInitialDimensions) << " vs " << toString(dimensions) << ")";
176 return false;
177 }
178 std::lock_guard<std::mutex> guard(mMutex);
179 mUpdatedDimensions = std::move(combined.value());
180 return true;
181 }
182
setInitialized(bool initialized)183 void ManagedBuffer::setInitialized(bool initialized) {
184 std::lock_guard<std::mutex> guard(mMutex);
185 mInitialized = initialized;
186 }
187
BufferTracker()188 BufferTracker::BufferTracker() {
189 constexpr size_t kPreallocatedElements = 1024;
190 using StackSpace = std::vector<Request::MemoryDomainToken>;
191 using Stack = std::stack<Request::MemoryDomainToken, StackSpace>;
192 StackSpace stackSpace;
193 stackSpace.reserve(kPreallocatedElements);
194 mFreeTokens = Stack(std::move(stackSpace));
195 mTokenToBuffers.reserve(kPreallocatedElements);
196 mTokenToBuffers.emplace_back();
197 }
198
add(std::shared_ptr<ManagedBuffer> buffer)199 std::unique_ptr<BufferTracker::Token> BufferTracker::add(std::shared_ptr<ManagedBuffer> buffer) {
200 if (buffer == nullptr) {
201 return nullptr;
202 }
203 std::lock_guard<std::mutex> guard(mMutex);
204 auto token = Request::MemoryDomainToken{0};
205 if (mFreeTokens.empty()) {
206 token = static_cast<Request::MemoryDomainToken>(mTokenToBuffers.size());
207 mTokenToBuffers.push_back(std::move(buffer));
208 } else {
209 token = mFreeTokens.top();
210 mFreeTokens.pop();
211 const auto index = static_cast<uint32_t>(token);
212 mTokenToBuffers[index] = std::move(buffer);
213 }
214 VLOG(MEMORY) << "BufferTracker::add -- new token = " << token;
215 return std::make_unique<Token>(token, shared_from_this());
216 }
217
get(Request::MemoryDomainToken token) const218 std::shared_ptr<ManagedBuffer> BufferTracker::get(Request::MemoryDomainToken token) const {
219 std::lock_guard<std::mutex> guard(mMutex);
220 const auto index = static_cast<uint32_t>(token);
221 if (mTokenToBuffers.size() <= index || mTokenToBuffers[index] == nullptr) {
222 LOG(ERROR) << "BufferTracker::get -- unknown token " << token;
223 return nullptr;
224 }
225 return mTokenToBuffers[index];
226 }
227
free(Request::MemoryDomainToken token)228 void BufferTracker::free(Request::MemoryDomainToken token) {
229 std::lock_guard<std::mutex> guard(mMutex);
230 const auto index = static_cast<uint32_t>(token);
231 CHECK_LT(index, mTokenToBuffers.size());
232 CHECK(mTokenToBuffers[index] != nullptr);
233 VLOG(MEMORY) << "BufferTracker::free -- release token = " << token;
234 mTokenToBuffers[index] = nullptr;
235 mFreeTokens.push(token);
236 }
237
238 } // namespace android::nn
239