Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 21 | namespace aidl::android::hardware::neuralnetworks::utils { |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 22 | namespace { |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 23 | |
| 24 | using ::android::nn::GeneralResult; |
| 25 | |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 26 | template <typename Type> |
| 27 | nn::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 | |
| 36 | template <typename Type> |
| 37 | GeneralResult<std::vector<Type>> clone(const std::vector<Type>& arguments) { |
| 38 | return cloneVec(arguments); |
| 39 | } |
| 40 | |
| 41 | } // namespace |
| 42 | |
| 43 | GeneralResult<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 | |
| 61 | GeneralResult<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 | |
| 76 | GeneralResult<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 | |
| 84 | GeneralResult<Model> clone(const Model& model) { |
| 85 | return Model{ |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 86 | .main = model.main, |
| 87 | .referenced = model.referenced, |
| 88 | .operandValues = model.operandValues, |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 89 | .pools = NN_TRY(clone(model.pools)), |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 90 | .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16, |
| 91 | .extensionNameToPrefix = model.extensionNameToPrefix, |
| 92 | }; |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | } // namespace aidl::android::hardware::neuralnetworks::utils |