blob: c0498eb8765283cb01f65dbe9ac96de7509d8f95 [file] [log] [blame]
Michael Butlera685c3d2020-02-22 22:37:59 -08001/*
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 "Conversions.h"
18
19#include <android-base/logging.h>
20#include <android/hardware/neuralnetworks/1.0/types.h>
21#include <nnapi/OperandTypes.h>
22#include <nnapi/OperationTypes.h>
23#include <nnapi/Result.h>
24#include <nnapi/SharedMemory.h>
Michael Butler32acc062020-11-22 19:36:30 -080025#include <nnapi/TypeUtils.h>
Michael Butlera685c3d2020-02-22 22:37:59 -080026#include <nnapi/Types.h>
Michael Butler32acc062020-11-22 19:36:30 -080027#include <nnapi/Validation.h>
Michael Butlera685c3d2020-02-22 22:37:59 -080028#include <nnapi/hal/CommonUtils.h>
29
30#include <algorithm>
31#include <functional>
32#include <iterator>
33#include <memory>
34#include <type_traits>
35#include <utility>
36#include <variant>
37
Michael Butler08ee3f92021-02-03 15:15:43 -080038#include "Utils.h"
39
Michael Butlera685c3d2020-02-22 22:37:59 -080040namespace {
41
42template <typename Type>
43constexpr std::underlying_type_t<Type> underlyingType(Type value) {
44 return static_cast<std::underlying_type_t<Type>>(value);
45}
46
47} // namespace
48
49namespace android::nn {
50namespace {
51
52using hardware::hidl_memory;
53using hardware::hidl_vec;
54
55template <typename Input>
Michael Butler08ee3f92021-02-03 15:15:43 -080056using UnvalidatedConvertOutput =
Michael Butler32acc062020-11-22 19:36:30 -080057 std::decay_t<decltype(unvalidatedConvert(std::declval<Input>()).value())>;
Michael Butlera685c3d2020-02-22 22:37:59 -080058
59template <typename Type>
Michael Butler08ee3f92021-02-03 15:15:43 -080060GeneralResult<std::vector<UnvalidatedConvertOutput<Type>>> unvalidatedConvert(
Michael Butler32acc062020-11-22 19:36:30 -080061 const hidl_vec<Type>& arguments) {
Michael Butler08ee3f92021-02-03 15:15:43 -080062 std::vector<UnvalidatedConvertOutput<Type>> canonical;
Michael Butlera685c3d2020-02-22 22:37:59 -080063 canonical.reserve(arguments.size());
64 for (const auto& argument : arguments) {
Michael Butler32acc062020-11-22 19:36:30 -080065 canonical.push_back(NN_TRY(nn::unvalidatedConvert(argument)));
66 }
67 return canonical;
68}
69
70template <typename Type>
Michael Butler08ee3f92021-02-03 15:15:43 -080071GeneralResult<UnvalidatedConvertOutput<Type>> validatedConvert(const Type& halObject) {
Michael Butler32acc062020-11-22 19:36:30 -080072 auto canonical = NN_TRY(nn::unvalidatedConvert(halObject));
Michael Butler08ee3f92021-02-03 15:15:43 -080073 NN_TRY(hal::V1_0::utils::compliantVersion(canonical));
Michael Butlera685c3d2020-02-22 22:37:59 -080074 return canonical;
75}
76
77} // anonymous namespace
78
Michael Butler32acc062020-11-22 19:36:30 -080079GeneralResult<OperandType> unvalidatedConvert(const hal::V1_0::OperandType& operandType) {
Michael Butlera685c3d2020-02-22 22:37:59 -080080 return static_cast<OperandType>(operandType);
81}
82
Michael Butler32acc062020-11-22 19:36:30 -080083GeneralResult<OperationType> unvalidatedConvert(const hal::V1_0::OperationType& operationType) {
Michael Butlera685c3d2020-02-22 22:37:59 -080084 return static_cast<OperationType>(operationType);
85}
86
Michael Butler32acc062020-11-22 19:36:30 -080087GeneralResult<Operand::LifeTime> unvalidatedConvert(const hal::V1_0::OperandLifeTime& lifetime) {
Michael Butlera685c3d2020-02-22 22:37:59 -080088 return static_cast<Operand::LifeTime>(lifetime);
89}
90
Michael Butler32acc062020-11-22 19:36:30 -080091GeneralResult<DeviceStatus> unvalidatedConvert(const hal::V1_0::DeviceStatus& deviceStatus) {
Michael Butlera685c3d2020-02-22 22:37:59 -080092 return static_cast<DeviceStatus>(deviceStatus);
93}
94
Michael Butler32acc062020-11-22 19:36:30 -080095GeneralResult<Capabilities::PerformanceInfo> unvalidatedConvert(
Michael Butler3670c382020-08-06 23:22:35 -070096 const hal::V1_0::PerformanceInfo& performanceInfo) {
Michael Butlera685c3d2020-02-22 22:37:59 -080097 return Capabilities::PerformanceInfo{
98 .execTime = performanceInfo.execTime,
99 .powerUsage = performanceInfo.powerUsage,
100 };
101}
102
Michael Butler32acc062020-11-22 19:36:30 -0800103GeneralResult<Capabilities> unvalidatedConvert(const hal::V1_0::Capabilities& capabilities) {
104 const auto quantized8Performance =
105 NN_TRY(unvalidatedConvert(capabilities.quantized8Performance));
106 const auto float32Performance = NN_TRY(unvalidatedConvert(capabilities.float32Performance));
Michael Butlera685c3d2020-02-22 22:37:59 -0800107
108 auto table = hal::utils::makeQuantized8PerformanceConsistentWithP(float32Performance,
109 quantized8Performance);
110
111 return Capabilities{
112 .relaxedFloat32toFloat16PerformanceScalar = float32Performance,
113 .relaxedFloat32toFloat16PerformanceTensor = float32Performance,
114 .operandPerformance = std::move(table),
115 };
116}
117
Michael Butler32acc062020-11-22 19:36:30 -0800118GeneralResult<DataLocation> unvalidatedConvert(const hal::V1_0::DataLocation& location) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800119 return DataLocation{
120 .poolIndex = location.poolIndex,
121 .offset = location.offset,
122 .length = location.length,
123 };
124}
125
Michael Butler32acc062020-11-22 19:36:30 -0800126GeneralResult<Operand> unvalidatedConvert(const hal::V1_0::Operand& operand) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800127 return Operand{
Michael Butler32acc062020-11-22 19:36:30 -0800128 .type = NN_TRY(unvalidatedConvert(operand.type)),
Michael Butlera685c3d2020-02-22 22:37:59 -0800129 .dimensions = operand.dimensions,
130 .scale = operand.scale,
131 .zeroPoint = operand.zeroPoint,
Michael Butler32acc062020-11-22 19:36:30 -0800132 .lifetime = NN_TRY(unvalidatedConvert(operand.lifetime)),
133 .location = NN_TRY(unvalidatedConvert(operand.location)),
Michael Butlera685c3d2020-02-22 22:37:59 -0800134 };
135}
136
Michael Butler32acc062020-11-22 19:36:30 -0800137GeneralResult<Operation> unvalidatedConvert(const hal::V1_0::Operation& operation) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800138 return Operation{
Michael Butler32acc062020-11-22 19:36:30 -0800139 .type = NN_TRY(unvalidatedConvert(operation.type)),
Michael Butlera685c3d2020-02-22 22:37:59 -0800140 .inputs = operation.inputs,
141 .outputs = operation.outputs,
142 };
143}
144
Michael Butler32acc062020-11-22 19:36:30 -0800145GeneralResult<Model::OperandValues> unvalidatedConvert(const hidl_vec<uint8_t>& operandValues) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800146 return Model::OperandValues(operandValues.data(), operandValues.size());
147}
148
Michael Butler79a16eb2021-02-07 00:11:13 -0800149GeneralResult<SharedMemory> unvalidatedConvert(const hidl_memory& memory) {
Michael Butlerbbe43d92021-02-08 00:05:07 -0800150 return hal::utils::createSharedMemoryFromHidlMemory(memory);
Michael Butlera685c3d2020-02-22 22:37:59 -0800151}
152
Michael Butler32acc062020-11-22 19:36:30 -0800153GeneralResult<Model> unvalidatedConvert(const hal::V1_0::Model& model) {
154 auto operations = NN_TRY(unvalidatedConvert(model.operations));
Michael Butlera685c3d2020-02-22 22:37:59 -0800155
156 // Verify number of consumers.
157 const auto numberOfConsumers =
Michael Butlerc4d98002021-02-09 15:36:11 -0800158 NN_TRY(hal::utils::countNumberOfConsumers(model.operands.size(), operations));
Michael Butlera685c3d2020-02-22 22:37:59 -0800159 CHECK(model.operands.size() == numberOfConsumers.size());
160 for (size_t i = 0; i < model.operands.size(); ++i) {
161 if (model.operands[i].numberOfConsumers != numberOfConsumers[i]) {
Michael Butler3670c382020-08-06 23:22:35 -0700162 return NN_ERROR(ErrorStatus::GENERAL_FAILURE)
163 << "Invalid numberOfConsumers for operand " << i << ", expected "
164 << numberOfConsumers[i] << " but found " << model.operands[i].numberOfConsumers;
Michael Butlera685c3d2020-02-22 22:37:59 -0800165 }
166 }
167
168 auto main = Model::Subgraph{
Michael Butler32acc062020-11-22 19:36:30 -0800169 .operands = NN_TRY(unvalidatedConvert(model.operands)),
Michael Butlera685c3d2020-02-22 22:37:59 -0800170 .operations = std::move(operations),
171 .inputIndexes = model.inputIndexes,
172 .outputIndexes = model.outputIndexes,
173 };
174
175 return Model{
176 .main = std::move(main),
Michael Butler32acc062020-11-22 19:36:30 -0800177 .operandValues = NN_TRY(unvalidatedConvert(model.operandValues)),
178 .pools = NN_TRY(unvalidatedConvert(model.pools)),
Michael Butlera685c3d2020-02-22 22:37:59 -0800179 };
180}
181
Michael Butler32acc062020-11-22 19:36:30 -0800182GeneralResult<Request::Argument> unvalidatedConvert(const hal::V1_0::RequestArgument& argument) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800183 const auto lifetime = argument.hasNoValue ? Request::Argument::LifeTime::NO_VALUE
184 : Request::Argument::LifeTime::POOL;
185 return Request::Argument{
186 .lifetime = lifetime,
Michael Butler32acc062020-11-22 19:36:30 -0800187 .location = NN_TRY(unvalidatedConvert(argument.location)),
Michael Butlera685c3d2020-02-22 22:37:59 -0800188 .dimensions = argument.dimensions,
189 };
190}
191
Michael Butler32acc062020-11-22 19:36:30 -0800192GeneralResult<Request> unvalidatedConvert(const hal::V1_0::Request& request) {
193 auto memories = NN_TRY(unvalidatedConvert(request.pools));
Michael Butlera685c3d2020-02-22 22:37:59 -0800194 std::vector<Request::MemoryPool> pools;
195 pools.reserve(memories.size());
196 std::move(memories.begin(), memories.end(), std::back_inserter(pools));
197
198 return Request{
Michael Butler32acc062020-11-22 19:36:30 -0800199 .inputs = NN_TRY(unvalidatedConvert(request.inputs)),
200 .outputs = NN_TRY(unvalidatedConvert(request.outputs)),
Michael Butlera685c3d2020-02-22 22:37:59 -0800201 .pools = std::move(pools),
202 };
203}
204
Michael Butler32acc062020-11-22 19:36:30 -0800205GeneralResult<ErrorStatus> unvalidatedConvert(const hal::V1_0::ErrorStatus& status) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800206 switch (status) {
207 case hal::V1_0::ErrorStatus::NONE:
208 case hal::V1_0::ErrorStatus::DEVICE_UNAVAILABLE:
209 case hal::V1_0::ErrorStatus::GENERAL_FAILURE:
210 case hal::V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE:
211 case hal::V1_0::ErrorStatus::INVALID_ARGUMENT:
212 return static_cast<ErrorStatus>(status);
213 }
Michael Butler3670c382020-08-06 23:22:35 -0700214 return NN_ERROR(ErrorStatus::GENERAL_FAILURE)
215 << "Invalid ErrorStatus " << underlyingType(status);
Michael Butlera685c3d2020-02-22 22:37:59 -0800216}
217
Michael Butler32acc062020-11-22 19:36:30 -0800218GeneralResult<DeviceStatus> convert(const hal::V1_0::DeviceStatus& deviceStatus) {
219 return validatedConvert(deviceStatus);
220}
221
222GeneralResult<Capabilities> convert(const hal::V1_0::Capabilities& capabilities) {
223 return validatedConvert(capabilities);
224}
225
226GeneralResult<Model> convert(const hal::V1_0::Model& model) {
227 return validatedConvert(model);
228}
229
230GeneralResult<Request> convert(const hal::V1_0::Request& request) {
231 return validatedConvert(request);
232}
233
234GeneralResult<ErrorStatus> convert(const hal::V1_0::ErrorStatus& status) {
235 return validatedConvert(status);
236}
237
Michael Butlera685c3d2020-02-22 22:37:59 -0800238} // namespace android::nn
239
240namespace android::hardware::neuralnetworks::V1_0::utils {
241namespace {
242
243template <typename Input>
Michael Butler08ee3f92021-02-03 15:15:43 -0800244using UnvalidatedConvertOutput =
Michael Butler32acc062020-11-22 19:36:30 -0800245 std::decay_t<decltype(unvalidatedConvert(std::declval<Input>()).value())>;
Michael Butlera685c3d2020-02-22 22:37:59 -0800246
247template <typename Type>
Michael Butler08ee3f92021-02-03 15:15:43 -0800248nn::GeneralResult<hidl_vec<UnvalidatedConvertOutput<Type>>> unvalidatedConvert(
Michael Butler32acc062020-11-22 19:36:30 -0800249 const std::vector<Type>& arguments) {
Michael Butler08ee3f92021-02-03 15:15:43 -0800250 hidl_vec<UnvalidatedConvertOutput<Type>> halObject(arguments.size());
Michael Butlera685c3d2020-02-22 22:37:59 -0800251 for (size_t i = 0; i < arguments.size(); ++i) {
Michael Butler32acc062020-11-22 19:36:30 -0800252 halObject[i] = NN_TRY(utils::unvalidatedConvert(arguments[i]));
Michael Butlera685c3d2020-02-22 22:37:59 -0800253 }
254 return halObject;
255}
256
Michael Butler32acc062020-11-22 19:36:30 -0800257template <typename Type>
Michael Butler08ee3f92021-02-03 15:15:43 -0800258nn::GeneralResult<UnvalidatedConvertOutput<Type>> validatedConvert(const Type& canonical) {
259 NN_TRY(compliantVersion(canonical));
Michael Butler32acc062020-11-22 19:36:30 -0800260 return utils::unvalidatedConvert(canonical);
261}
262
Michael Butlera685c3d2020-02-22 22:37:59 -0800263} // anonymous namespace
264
Michael Butler32acc062020-11-22 19:36:30 -0800265nn::GeneralResult<OperandType> unvalidatedConvert(const nn::OperandType& operandType) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800266 return static_cast<OperandType>(operandType);
267}
268
Michael Butler32acc062020-11-22 19:36:30 -0800269nn::GeneralResult<OperationType> unvalidatedConvert(const nn::OperationType& operationType) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800270 return static_cast<OperationType>(operationType);
271}
272
Michael Butler32acc062020-11-22 19:36:30 -0800273nn::GeneralResult<OperandLifeTime> unvalidatedConvert(const nn::Operand::LifeTime& lifetime) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800274 if (lifetime == nn::Operand::LifeTime::POINTER) {
Michael Butler3670c382020-08-06 23:22:35 -0700275 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
Michael Butler32acc062020-11-22 19:36:30 -0800276 << "Model cannot be unvalidatedConverted because it contains pointer-based memory";
Michael Butlera685c3d2020-02-22 22:37:59 -0800277 }
278 return static_cast<OperandLifeTime>(lifetime);
279}
280
Michael Butler32acc062020-11-22 19:36:30 -0800281nn::GeneralResult<DeviceStatus> unvalidatedConvert(const nn::DeviceStatus& deviceStatus) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800282 return static_cast<DeviceStatus>(deviceStatus);
283}
284
Michael Butler32acc062020-11-22 19:36:30 -0800285nn::GeneralResult<PerformanceInfo> unvalidatedConvert(
Michael Butler3670c382020-08-06 23:22:35 -0700286 const nn::Capabilities::PerformanceInfo& performanceInfo) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800287 return PerformanceInfo{
288 .execTime = performanceInfo.execTime,
289 .powerUsage = performanceInfo.powerUsage,
290 };
291}
292
Michael Butler32acc062020-11-22 19:36:30 -0800293nn::GeneralResult<Capabilities> unvalidatedConvert(const nn::Capabilities& capabilities) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800294 return Capabilities{
Michael Butler32acc062020-11-22 19:36:30 -0800295 .float32Performance = NN_TRY(unvalidatedConvert(
Michael Butlera685c3d2020-02-22 22:37:59 -0800296 capabilities.operandPerformance.lookup(nn::OperandType::TENSOR_FLOAT32))),
Michael Butler32acc062020-11-22 19:36:30 -0800297 .quantized8Performance = NN_TRY(unvalidatedConvert(
Michael Butlera685c3d2020-02-22 22:37:59 -0800298 capabilities.operandPerformance.lookup(nn::OperandType::TENSOR_QUANT8_ASYMM))),
299 };
300}
301
Michael Butler32acc062020-11-22 19:36:30 -0800302nn::GeneralResult<DataLocation> unvalidatedConvert(const nn::DataLocation& location) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800303 return DataLocation{
304 .poolIndex = location.poolIndex,
305 .offset = location.offset,
306 .length = location.length,
307 };
308}
309
Michael Butler32acc062020-11-22 19:36:30 -0800310nn::GeneralResult<Operand> unvalidatedConvert(const nn::Operand& operand) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800311 return Operand{
Michael Butler32acc062020-11-22 19:36:30 -0800312 .type = NN_TRY(unvalidatedConvert(operand.type)),
Michael Butlera685c3d2020-02-22 22:37:59 -0800313 .dimensions = operand.dimensions,
314 .numberOfConsumers = 0,
315 .scale = operand.scale,
316 .zeroPoint = operand.zeroPoint,
Michael Butler32acc062020-11-22 19:36:30 -0800317 .lifetime = NN_TRY(unvalidatedConvert(operand.lifetime)),
318 .location = NN_TRY(unvalidatedConvert(operand.location)),
Michael Butlera685c3d2020-02-22 22:37:59 -0800319 };
320}
321
Michael Butler32acc062020-11-22 19:36:30 -0800322nn::GeneralResult<Operation> unvalidatedConvert(const nn::Operation& operation) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800323 return Operation{
Michael Butler32acc062020-11-22 19:36:30 -0800324 .type = NN_TRY(unvalidatedConvert(operation.type)),
Michael Butlera685c3d2020-02-22 22:37:59 -0800325 .inputs = operation.inputs,
326 .outputs = operation.outputs,
327 };
328}
329
Michael Butler32acc062020-11-22 19:36:30 -0800330nn::GeneralResult<hidl_vec<uint8_t>> unvalidatedConvert(
331 const nn::Model::OperandValues& operandValues) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800332 return hidl_vec<uint8_t>(operandValues.data(), operandValues.data() + operandValues.size());
333}
334
Michael Butler79a16eb2021-02-07 00:11:13 -0800335nn::GeneralResult<hidl_memory> unvalidatedConvert(const nn::SharedMemory& memory) {
Michael Butlerbbe43d92021-02-08 00:05:07 -0800336 return hal::utils::createHidlMemoryFromSharedMemory(memory);
Michael Butlera685c3d2020-02-22 22:37:59 -0800337}
338
Michael Butler32acc062020-11-22 19:36:30 -0800339nn::GeneralResult<Model> unvalidatedConvert(const nn::Model& model) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800340 if (!hal::utils::hasNoPointerData(model)) {
Michael Butler3670c382020-08-06 23:22:35 -0700341 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
Michael Butler32acc062020-11-22 19:36:30 -0800342 << "Mdoel cannot be unvalidatedConverted because it contains pointer-based memory";
Michael Butlera685c3d2020-02-22 22:37:59 -0800343 }
344
Michael Butler32acc062020-11-22 19:36:30 -0800345 auto operands = NN_TRY(unvalidatedConvert(model.main.operands));
Michael Butlera685c3d2020-02-22 22:37:59 -0800346
347 // Update number of consumers.
348 const auto numberOfConsumers =
Michael Butlerc4d98002021-02-09 15:36:11 -0800349 NN_TRY(hal::utils::countNumberOfConsumers(operands.size(), model.main.operations));
Michael Butlera685c3d2020-02-22 22:37:59 -0800350 CHECK(operands.size() == numberOfConsumers.size());
351 for (size_t i = 0; i < operands.size(); ++i) {
352 operands[i].numberOfConsumers = numberOfConsumers[i];
353 }
354
355 return Model{
356 .operands = std::move(operands),
Michael Butler32acc062020-11-22 19:36:30 -0800357 .operations = NN_TRY(unvalidatedConvert(model.main.operations)),
Michael Butlera685c3d2020-02-22 22:37:59 -0800358 .inputIndexes = model.main.inputIndexes,
359 .outputIndexes = model.main.outputIndexes,
Michael Butler32acc062020-11-22 19:36:30 -0800360 .operandValues = NN_TRY(unvalidatedConvert(model.operandValues)),
361 .pools = NN_TRY(unvalidatedConvert(model.pools)),
Michael Butlera685c3d2020-02-22 22:37:59 -0800362 };
363}
364
Michael Butler32acc062020-11-22 19:36:30 -0800365nn::GeneralResult<RequestArgument> unvalidatedConvert(
366 const nn::Request::Argument& requestArgument) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800367 if (requestArgument.lifetime == nn::Request::Argument::LifeTime::POINTER) {
Michael Butler3670c382020-08-06 23:22:35 -0700368 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
Michael Butler32acc062020-11-22 19:36:30 -0800369 << "Request cannot be unvalidatedConverted because it contains pointer-based memory";
Michael Butlera685c3d2020-02-22 22:37:59 -0800370 }
371 const bool hasNoValue = requestArgument.lifetime == nn::Request::Argument::LifeTime::NO_VALUE;
372 return RequestArgument{
373 .hasNoValue = hasNoValue,
Michael Butler32acc062020-11-22 19:36:30 -0800374 .location = NN_TRY(unvalidatedConvert(requestArgument.location)),
Michael Butlera685c3d2020-02-22 22:37:59 -0800375 .dimensions = requestArgument.dimensions,
376 };
377}
378
Michael Butler32acc062020-11-22 19:36:30 -0800379nn::GeneralResult<hidl_memory> unvalidatedConvert(const nn::Request::MemoryPool& memoryPool) {
Michael Butler79a16eb2021-02-07 00:11:13 -0800380 return unvalidatedConvert(std::get<nn::SharedMemory>(memoryPool));
Michael Butlera685c3d2020-02-22 22:37:59 -0800381}
382
Michael Butler32acc062020-11-22 19:36:30 -0800383nn::GeneralResult<Request> unvalidatedConvert(const nn::Request& request) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800384 if (!hal::utils::hasNoPointerData(request)) {
Michael Butler3670c382020-08-06 23:22:35 -0700385 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
Michael Butler32acc062020-11-22 19:36:30 -0800386 << "Request cannot be unvalidatedConverted because it contains pointer-based memory";
Michael Butlera685c3d2020-02-22 22:37:59 -0800387 }
388
389 return Request{
Michael Butler32acc062020-11-22 19:36:30 -0800390 .inputs = NN_TRY(unvalidatedConvert(request.inputs)),
391 .outputs = NN_TRY(unvalidatedConvert(request.outputs)),
392 .pools = NN_TRY(unvalidatedConvert(request.pools)),
Michael Butlera685c3d2020-02-22 22:37:59 -0800393 };
394}
395
Michael Butler32acc062020-11-22 19:36:30 -0800396nn::GeneralResult<ErrorStatus> unvalidatedConvert(const nn::ErrorStatus& status) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800397 switch (status) {
398 case nn::ErrorStatus::NONE:
399 case nn::ErrorStatus::DEVICE_UNAVAILABLE:
400 case nn::ErrorStatus::GENERAL_FAILURE:
401 case nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE:
402 case nn::ErrorStatus::INVALID_ARGUMENT:
403 return static_cast<ErrorStatus>(status);
404 default:
405 return ErrorStatus::GENERAL_FAILURE;
406 }
407}
408
Michael Butler32acc062020-11-22 19:36:30 -0800409nn::GeneralResult<DeviceStatus> convert(const nn::DeviceStatus& deviceStatus) {
410 return validatedConvert(deviceStatus);
411}
412
413nn::GeneralResult<Capabilities> convert(const nn::Capabilities& capabilities) {
414 return validatedConvert(capabilities);
415}
416
417nn::GeneralResult<Model> convert(const nn::Model& model) {
418 return validatedConvert(model);
419}
420
421nn::GeneralResult<Request> convert(const nn::Request& request) {
422 return validatedConvert(request);
423}
424
425nn::GeneralResult<ErrorStatus> convert(const nn::ErrorStatus& status) {
426 return validatedConvert(status);
427}
428
Michael Butlera685c3d2020-02-22 22:37:59 -0800429} // namespace android::hardware::neuralnetworks::V1_0::utils