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 "HalInterfaces.h"
30 #include "Utils.h"
31 
32 namespace android::nn {
33 
34 using namespace hal;
35 
create(uint32_t size,std::set<PreparedModelRole> roles,const Operand & operand)36 std::shared_ptr<ManagedBuffer> ManagedBuffer::create(uint32_t size,
37                                                      std::set<PreparedModelRole> roles,
38                                                      const Operand& operand) {
39     std::unique_ptr<uint8_t[]> buffer(new (std::nothrow) uint8_t[size]);
40     if (buffer == nullptr) {
41         return nullptr;
42     }
43     if (isExtensionOperandType(operand.type)) {
44         LOG(ERROR) << "ManagedBuffer cannot handle extension operands.";
45         return nullptr;
46     }
47     return std::make_shared<ManagedBuffer>(std::move(buffer), size, std::move(roles), operand);
48 }
49 
ManagedBuffer(std::unique_ptr<uint8_t[]> buffer,uint32_t size,std::set<PreparedModelRole> roles,const Operand & operand)50 ManagedBuffer::ManagedBuffer(std::unique_ptr<uint8_t[]> buffer, uint32_t size,
51                              std::set<PreparedModelRole> roles, const Operand& operand)
52     : kBuffer(std::move(buffer)),
53       kSize(size),
54       kRoles(std::move(roles)),
55       kOperandType(operand.type),
56       kInitialDimensions(operand.dimensions),
57       mUpdatedDimensions(operand.dimensions) {
58     CHECK(!isExtensionOperandType(kOperandType));
59 }
60 
validateRequest(uint32_t poolIndex,const Request & request,const IPreparedModel * preparedModel) const61 ErrorStatus ManagedBuffer::validateRequest(uint32_t poolIndex, const Request& request,
62                                            const IPreparedModel* preparedModel) const {
63     CHECK_LT(poolIndex, request.pools.size());
64     CHECK(request.pools[poolIndex].getDiscriminator() ==
65           Request::MemoryPool::hidl_discriminator::token);
66     std::lock_guard<std::mutex> guard(mMutex);
67 
68     bool usedAsInput = false, usedAsOutput = false;
69     for (uint32_t i = 0; i < request.inputs.size(); i++) {
70         if (request.inputs[i].hasNoValue) continue;
71         if (request.inputs[i].location.poolIndex != poolIndex) continue;
72         // Validate if the input role is specified during allocation.
73         if (kRoles.count({preparedModel, IOType::INPUT, i}) == 0) {
74             LOG(ERROR) << "ManagedBuffer::validateRequest -- invalid buffer role.";
75             return ErrorStatus::INVALID_ARGUMENT;
76         }
77         if (!mInitialized) {
78             LOG(ERROR) << "ManagedBuffer::validateRequest -- using uninitialized buffer as input "
79                           "request.";
80             return ErrorStatus::GENERAL_FAILURE;
81         }
82         auto combined = combineDimensions(mUpdatedDimensions, request.inputs[i].dimensions);
83         if (!combined.has_value()) {
84             LOG(ERROR) << "ManagedBuffer::validateRequest -- incompatible dimensions ("
85                        << toString(mUpdatedDimensions) << " vs "
86                        << toString(request.inputs[i].dimensions) << ")";
87             return ErrorStatus::INVALID_ARGUMENT;
88         }
89         usedAsInput = true;
90     }
91     for (uint32_t i = 0; i < request.outputs.size(); i++) {
92         if (request.outputs[i].hasNoValue) continue;
93         if (request.outputs[i].location.poolIndex != poolIndex) continue;
94         if (usedAsInput || usedAsOutput) {
95             LOG(ERROR) << "ManagedBuffer::validateRequest -- using the same device memory for "
96                           "input/output or multiple outputs";
97             return ErrorStatus::INVALID_ARGUMENT;
98         }
99         // Validate if the output role is specified during allocation.
100         if (kRoles.count({preparedModel, IOType::OUTPUT, i}) == 0) {
101             LOG(ERROR) << "ManagedBuffer::validateRequest -- invalid buffer role.";
102             return ErrorStatus::INVALID_ARGUMENT;
103         }
104         auto combined = combineDimensions(kInitialDimensions, request.outputs[i].dimensions);
105         if (!combined.has_value()) {
106             LOG(ERROR) << "ManagedBuffer::validateRequest -- incompatible dimensions ("
107                        << toString(kInitialDimensions) << " vs "
108                        << toString(request.outputs[i].dimensions) << ")";
109             return ErrorStatus::INVALID_ARGUMENT;
110         }
111         usedAsOutput = true;
112     }
113     return ErrorStatus::NONE;
114 }
115 
validateCopyFrom(const std::vector<uint32_t> & dimensions,uint32_t size) const116 ErrorStatus ManagedBuffer::validateCopyFrom(const std::vector<uint32_t>& dimensions,
117                                             uint32_t size) const {
118     if (size != kSize) {
119         LOG(ERROR) << "ManagedBuffer::validateCopyFrom -- invalid memory size: " << kSize << " vs "
120                    << size;
121         return ErrorStatus::INVALID_ARGUMENT;
122     }
123 
124     if (nonExtensionOperandTypeIsScalar(static_cast<int>(kOperandType))) {
125         if (!dimensions.empty()) {
126             LOG(ERROR)
127                     << "ManagedBuffer::validateCopyFrom -- invalid dimensions for scalar operand: "
128                     << toString(dimensions);
129             return ErrorStatus::INVALID_ARGUMENT;
130         }
131         return ErrorStatus::NONE;
132     }
133 
134     if (dimensions.empty()) {
135         if (tensorHasUnspecifiedDimensions(kOperandType, kInitialDimensions)) {
136             LOG(ERROR) << "ManagedBuffer::validateCopyFrom -- the initial dimensions are not fully "
137                           "specified and no dimension update is provided: "
138                        << toString(kInitialDimensions);
139             return ErrorStatus::INVALID_ARGUMENT;
140         }
141     } else {
142         if (tensorHasUnspecifiedDimensions(kOperandType, dimensions)) {
143             LOG(ERROR) << "ManagedBuffer::validateCopyFrom -- the updated dimensions are not fully "
144                           "specified: "
145                        << toString(dimensions);
146             return ErrorStatus::INVALID_ARGUMENT;
147         }
148     }
149 
150     const auto combined = combineDimensions(kInitialDimensions, dimensions);
151     if (!combined.has_value()) {
152         LOG(ERROR) << "ManagedBuffer::validateCopyFrom -- incompatible dimensions ("
153                    << toString(kInitialDimensions) << " vs " << toString(dimensions) << ")";
154         return ErrorStatus::INVALID_ARGUMENT;
155     }
156     return ErrorStatus::NONE;
157 }
158 
validateCopyTo(uint32_t size) const159 ErrorStatus ManagedBuffer::validateCopyTo(uint32_t size) const {
160     if (size != kSize) {
161         LOG(ERROR) << "ManagedBuffer::validateCopyTo -- invalid memory size: " << kSize << " vs "
162                    << size;
163         return ErrorStatus::INVALID_ARGUMENT;
164     }
165     std::lock_guard<std::mutex> guard(mMutex);
166     if (!mInitialized) {
167         LOG(ERROR) << "ManagedBuffer::validateCopyTo -- using uninitialized buffer as source.";
168         return ErrorStatus::GENERAL_FAILURE;
169     }
170     return ErrorStatus::NONE;
171 }
172 
updateDimensions(const std::vector<uint32_t> & dimensions)173 bool ManagedBuffer::updateDimensions(const std::vector<uint32_t>& dimensions) {
174     auto combined = combineDimensions(kInitialDimensions, dimensions);
175     if (!combined) {
176         LOG(ERROR) << "ManagedBuffer::updateDimensions -- incompatible dimensions ("
177                    << toString(kInitialDimensions) << " vs " << toString(dimensions) << ")";
178         return false;
179     }
180     std::lock_guard<std::mutex> guard(mMutex);
181     mUpdatedDimensions = std::move(combined.value());
182     return true;
183 }
184 
setInitialized(bool initialized)185 void ManagedBuffer::setInitialized(bool initialized) {
186     std::lock_guard<std::mutex> guard(mMutex);
187     mInitialized = initialized;
188 }
189 
add(std::shared_ptr<ManagedBuffer> buffer)190 std::unique_ptr<BufferTracker::Token> BufferTracker::add(std::shared_ptr<ManagedBuffer> buffer) {
191     if (buffer == nullptr) {
192         return nullptr;
193     }
194     std::lock_guard<std::mutex> guard(mMutex);
195     uint32_t token = 0;
196     if (mFreeTokens.empty()) {
197         token = mTokenToBuffers.size();
198         mTokenToBuffers.push_back(std::move(buffer));
199     } else {
200         token = mFreeTokens.top();
201         mFreeTokens.pop();
202         mTokenToBuffers[token] = std::move(buffer);
203     }
204     VLOG(MEMORY) << "BufferTracker::add -- new token = " << token;
205     return std::make_unique<Token>(token, shared_from_this());
206 }
207 
get(uint32_t token) const208 std::shared_ptr<ManagedBuffer> BufferTracker::get(uint32_t token) const {
209     std::lock_guard<std::mutex> guard(mMutex);
210     if (mTokenToBuffers.size() <= token || mTokenToBuffers[token] == nullptr) {
211         LOG(ERROR) << "BufferTracker::get -- unknown token " << token;
212         return nullptr;
213     }
214     return mTokenToBuffers[token];
215 }
216 
free(uint32_t token)217 void BufferTracker::free(uint32_t token) {
218     std::lock_guard<std::mutex> guard(mMutex);
219     CHECK_LT(token, mTokenToBuffers.size());
220     CHECK(mTokenToBuffers[token] != nullptr);
221     VLOG(MEMORY) << "BufferTracker::free -- release token = " << token;
222     mTokenToBuffers[token] = nullptr;
223     mFreeTokens.push(token);
224 }
225 
226 }  // namespace android::nn
227