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 "Conversions.h"
20 #include "PreparedModel.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/types.h>
26 #include <android/hardware/neuralnetworks/1.3/IPreparedModel.h>
27 #include <android/hardware/neuralnetworks/1.3/types.h>
28 #include <nnapi/IExecution.h>
29 #include <nnapi/Result.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_3::utils {
41
create(std::shared_ptr<const PreparedModel> preparedModel,Request request,hal::utils::RequestRelocation relocation,V1_2::MeasureTiming measure,OptionalTimeoutDuration loopTimeoutDuration)42 nn::GeneralResult<std::shared_ptr<const Execution>> Execution::create(
43 std::shared_ptr<const PreparedModel> preparedModel, Request request,
44 hal::utils::RequestRelocation relocation, V1_2::MeasureTiming measure,
45 OptionalTimeoutDuration loopTimeoutDuration) {
46 if (preparedModel == nullptr) {
47 return NN_ERROR() << "V1_3::utils::Execution::create must have non-null preparedModel";
48 }
49
50 return std::make_shared<const Execution>(PrivateConstructorTag{}, std::move(preparedModel),
51 std::move(request), std::move(relocation), measure,
52 std::move(loopTimeoutDuration));
53 }
54
Execution(PrivateConstructorTag,std::shared_ptr<const PreparedModel> preparedModel,Request request,hal::utils::RequestRelocation relocation,V1_2::MeasureTiming measure,OptionalTimeoutDuration loopTimeoutDuration)55 Execution::Execution(PrivateConstructorTag /*tag*/,
56 std::shared_ptr<const PreparedModel> preparedModel, Request request,
57 hal::utils::RequestRelocation relocation, V1_2::MeasureTiming measure,
58 OptionalTimeoutDuration loopTimeoutDuration)
59 : kPreparedModel(std::move(preparedModel)),
60 kRequest(std::move(request)),
61 kRelocation(std::move(relocation)),
62 kMeasure(measure),
63 kLoopTimeoutDuration(std::move(loopTimeoutDuration)) {}
64
compute(const nn::OptionalTimePoint & deadline) const65 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> Execution::compute(
66 const nn::OptionalTimePoint& deadline) const {
67 const auto hidlDeadline = NN_TRY(convert(deadline));
68 return kPreparedModel->executeInternal(kRequest, kMeasure, hidlDeadline, kLoopTimeoutDuration,
69 kRelocation);
70 }
71
computeFenced(const std::vector<nn::SyncFence> & waitFor,const nn::OptionalTimePoint & deadline,const nn::OptionalDuration & timeoutDurationAfterFence) const72 nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> Execution::computeFenced(
73 const std::vector<nn::SyncFence>& waitFor, const nn::OptionalTimePoint& deadline,
74 const nn::OptionalDuration& timeoutDurationAfterFence) const {
75 const auto hidlWaitFor = NN_TRY(convertSyncFences(waitFor));
76 const auto hidlDeadline = NN_TRY(convert(deadline));
77 const auto hidlTimeoutDurationAfterFence = NN_TRY(convert(timeoutDurationAfterFence));
78 return kPreparedModel->executeFencedInternal(kRequest, hidlWaitFor, kMeasure, hidlDeadline,
79 kLoopTimeoutDuration,
80 hidlTimeoutDurationAfterFence, kRelocation);
81 }
82
83 } // namespace android::hardware::neuralnetworks::V1_3::utils
84