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 "Execution.h"
18
19 #include "Callbacks.h"
20 #include "Conversions.h"
21 #include "Utils.h"
22
23 #include <android/hardware/neuralnetworks/1.0/types.h>
24 #include <android/hardware/neuralnetworks/1.1/types.h>
25 #include <android/hardware/neuralnetworks/1.2/IPreparedModel.h>
26 #include <android/hardware/neuralnetworks/1.2/types.h>
27 #include <nnapi/IExecution.h>
28 #include <nnapi/Result.h>
29 #include <nnapi/TypeUtils.h>
30 #include <nnapi/Types.h>
31 #include <nnapi/hal/CommonUtils.h>
32
33 #include <memory>
34 #include <utility>
35 #include <vector>
36
37 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
38 // lifetimes across processes and for protecting asynchronous calls across HIDL.
39
40 namespace android::hardware::neuralnetworks::V1_2::utils {
41
create(std::shared_ptr<const PreparedModel> preparedModel,V1_0::Request request,hal::utils::RequestRelocation relocation,V1_2::MeasureTiming measure)42 nn::GeneralResult<std::shared_ptr<const Execution>> Execution::create(
43 std::shared_ptr<const PreparedModel> preparedModel, V1_0::Request request,
44 hal::utils::RequestRelocation relocation, V1_2::MeasureTiming measure) {
45 if (preparedModel == nullptr) {
46 return NN_ERROR() << "V1_2::utils::Execution::create must have non-null preparedModel";
47 }
48
49 return std::make_shared<const Execution>(PrivateConstructorTag{}, std::move(preparedModel),
50 std::move(request), std::move(relocation), measure);
51 }
52
Execution(PrivateConstructorTag,std::shared_ptr<const PreparedModel> preparedModel,V1_0::Request request,hal::utils::RequestRelocation relocation,V1_2::MeasureTiming measure)53 Execution::Execution(PrivateConstructorTag /*tag*/,
54 std::shared_ptr<const PreparedModel> preparedModel, V1_0::Request request,
55 hal::utils::RequestRelocation relocation, V1_2::MeasureTiming measure)
56 : kPreparedModel(std::move(preparedModel)),
57 kRequest(std::move(request)),
58 kRelocation(std::move(relocation)),
59 kMeasure(measure) {}
60
compute(const nn::OptionalTimePoint &) const61 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> Execution::compute(
62 const nn::OptionalTimePoint& /*deadline*/) const {
63 return kPreparedModel->executeInternal(kRequest, kMeasure, kRelocation);
64 }
65
computeFenced(const std::vector<nn::SyncFence> &,const nn::OptionalTimePoint &,const nn::OptionalDuration &) const66 nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> Execution::computeFenced(
67 const std::vector<nn::SyncFence>& /*waitFor*/, const nn::OptionalTimePoint& /*deadline*/,
68 const nn::OptionalDuration& /*timeoutDurationAfterFence*/) const {
69 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
70 << "IExecution::computeFenced is not supported on 1.2 HAL service";
71 }
72
73 } // namespace android::hardware::neuralnetworks::V1_2::utils
74