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 { |
| 22 | |
| 23 | using ::android::nn::GeneralResult; |
| 24 | |
| 25 | GeneralResult<Model> copyModel(const Model& model) { |
| 26 | Model newModel{ |
| 27 | .main = model.main, |
| 28 | .referenced = model.referenced, |
| 29 | .operandValues = model.operandValues, |
| 30 | .pools = {}, |
| 31 | .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16, |
| 32 | .extensionNameToPrefix = model.extensionNameToPrefix, |
| 33 | }; |
| 34 | newModel.pools.reserve(model.pools.size()); |
| 35 | for (const auto& pool : model.pools) { |
| 36 | common::NativeHandle nativeHandle; |
| 37 | nativeHandle.ints = pool.handle.ints; |
| 38 | nativeHandle.fds.reserve(pool.handle.fds.size()); |
| 39 | for (const auto& fd : pool.handle.fds) { |
| 40 | const int newFd = dup(fd.get()); |
| 41 | if (newFd == -1) { |
| 42 | return NN_ERROR() << "Couldn't dup a file descriptor."; |
| 43 | } |
| 44 | nativeHandle.fds.emplace_back(newFd); |
| 45 | } |
| 46 | Memory memory = { |
| 47 | .handle = std::move(nativeHandle), |
| 48 | .size = pool.size, |
| 49 | .name = pool.name, |
| 50 | }; |
| 51 | newModel.pools.push_back(std::move(memory)); |
| 52 | } |
| 53 | return newModel; |
| 54 | } |
| 55 | |
| 56 | } // namespace aidl::android::hardware::neuralnetworks::utils |