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 #include <nnapi/hal/HandleError.h>
33 
34 #include <memory>
35 #include <utility>
36 #include <vector>
37 
38 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
39 // lifetimes across processes and for protecting asynchronous calls across HIDL.
40 
41 namespace android::hardware::neuralnetworks::V1_3::utils {
42 
create(std::shared_ptr<const PreparedModel> preparedModel,Request request,hal::utils::RequestRelocation relocation,V1_2::MeasureTiming measure,OptionalTimeoutDuration loopTimeoutDuration)43 nn::GeneralResult<std::shared_ptr<const Execution>> Execution::create(
44         std::shared_ptr<const PreparedModel> preparedModel, Request request,
45         hal::utils::RequestRelocation relocation, V1_2::MeasureTiming measure,
46         OptionalTimeoutDuration loopTimeoutDuration) {
47     if (preparedModel == nullptr) {
48         return NN_ERROR() << "V1_3::utils::Execution::create must have non-null preparedModel";
49     }
50 
51     return std::make_shared<const Execution>(PrivateConstructorTag{}, std::move(preparedModel),
52                                              std::move(request), std::move(relocation), measure,
53                                              std::move(loopTimeoutDuration));
54 }
55 
Execution(PrivateConstructorTag,std::shared_ptr<const PreparedModel> preparedModel,Request request,hal::utils::RequestRelocation relocation,V1_2::MeasureTiming measure,OptionalTimeoutDuration loopTimeoutDuration)56 Execution::Execution(PrivateConstructorTag /*tag*/,
57                      std::shared_ptr<const PreparedModel> preparedModel, Request request,
58                      hal::utils::RequestRelocation relocation, V1_2::MeasureTiming measure,
59                      OptionalTimeoutDuration loopTimeoutDuration)
60     : kPreparedModel(std::move(preparedModel)),
61       kRequest(std::move(request)),
62       kRelocation(std::move(relocation)),
63       kMeasure(measure),
64       kLoopTimeoutDuration(std::move(loopTimeoutDuration)) {}
65 
compute(const nn::OptionalTimePoint & deadline) const66 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> Execution::compute(
67         const nn::OptionalTimePoint& deadline) const {
68     const auto hidlDeadline = NN_TRY(hal::utils::makeExecutionFailure(convert(deadline)));
69     return kPreparedModel->executeInternal(kRequest, kMeasure, hidlDeadline, kLoopTimeoutDuration,
70                                            kRelocation);
71 }
72 
computeFenced(const std::vector<nn::SyncFence> & waitFor,const nn::OptionalTimePoint & deadline,const nn::OptionalDuration & timeoutDurationAfterFence) const73 nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> Execution::computeFenced(
74         const std::vector<nn::SyncFence>& waitFor, const nn::OptionalTimePoint& deadline,
75         const nn::OptionalDuration& timeoutDurationAfterFence) const {
76     const auto hidlWaitFor = NN_TRY(hal::utils::convertSyncFences(waitFor));
77     const auto hidlDeadline = NN_TRY(convert(deadline));
78     const auto hidlTimeoutDurationAfterFence = NN_TRY(convert(timeoutDurationAfterFence));
79     return kPreparedModel->executeFencedInternal(kRequest, hidlWaitFor, kMeasure, hidlDeadline,
80                                                  kLoopTimeoutDuration,
81                                                  hidlTimeoutDurationAfterFence, kRelocation);
82 }
83 
84 }  // namespace android::hardware::neuralnetworks::V1_3::utils
85