blob: 8d00e5926a4a85de22863a478312f93bb921735a [file] [log] [blame]
Lev Proleev6b6dfcd2020-11-11 18:28:50 +00001/*
2 * Copyright (C) 2021 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 "Utils.h"
18
19#include <nnapi/Result.h>
20
21namespace aidl::android::hardware::neuralnetworks::utils {
Lev Proleevc185e882020-12-15 19:25:32 +000022namespace {
Lev Proleev6b6dfcd2020-11-11 18:28:50 +000023
24using ::android::nn::GeneralResult;
25
Lev Proleevc185e882020-12-15 19:25:32 +000026template <typename Type>
27nn::GeneralResult<std::vector<Type>> cloneVec(const std::vector<Type>& arguments) {
28 std::vector<Type> clonedObjects;
29 clonedObjects.reserve(arguments.size());
30 for (const auto& argument : arguments) {
31 clonedObjects.push_back(NN_TRY(clone(argument)));
32 }
33 return clonedObjects;
34}
35
36template <typename Type>
37GeneralResult<std::vector<Type>> clone(const std::vector<Type>& arguments) {
38 return cloneVec(arguments);
39}
40
41} // namespace
42
43GeneralResult<Memory> clone(const Memory& memory) {
44 common::NativeHandle nativeHandle;
45 nativeHandle.ints = memory.handle.ints;
46 nativeHandle.fds.reserve(memory.handle.fds.size());
47 for (const auto& fd : memory.handle.fds) {
48 const int newFd = dup(fd.get());
49 if (newFd < 0) {
50 return NN_ERROR() << "Couldn't dup a file descriptor";
51 }
52 nativeHandle.fds.emplace_back(newFd);
53 }
54 return Memory{
55 .handle = std::move(nativeHandle),
56 .size = memory.size,
57 .name = memory.name,
58 };
59}
60
61GeneralResult<RequestMemoryPool> clone(const RequestMemoryPool& requestPool) {
62 using Tag = RequestMemoryPool::Tag;
63 switch (requestPool.getTag()) {
64 case Tag::pool:
65 return RequestMemoryPool::make<Tag::pool>(NN_TRY(clone(requestPool.get<Tag::pool>())));
66 case Tag::token:
67 return RequestMemoryPool::make<Tag::token>(requestPool.get<Tag::token>());
68 }
69 // Using explicit type conversion because std::variant inside the RequestMemoryPool confuses the
70 // compiler.
71 return (NN_ERROR() << "Unrecognized request pool tag: " << requestPool.getTag())
72 .
73 operator GeneralResult<RequestMemoryPool>();
74}
75
76GeneralResult<Request> clone(const Request& request) {
77 return Request{
78 .inputs = request.inputs,
79 .outputs = request.outputs,
80 .pools = NN_TRY(clone(request.pools)),
81 };
82}
83
84GeneralResult<Model> clone(const Model& model) {
85 return Model{
Lev Proleev6b6dfcd2020-11-11 18:28:50 +000086 .main = model.main,
87 .referenced = model.referenced,
88 .operandValues = model.operandValues,
Lev Proleevc185e882020-12-15 19:25:32 +000089 .pools = NN_TRY(clone(model.pools)),
Lev Proleev6b6dfcd2020-11-11 18:28:50 +000090 .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16,
91 .extensionNameToPrefix = model.extensionNameToPrefix,
92 };
Lev Proleev6b6dfcd2020-11-11 18:28:50 +000093}
94
95} // namespace aidl::android::hardware::neuralnetworks::utils