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 <aidl/android/hardware/neuralnetworks/Request.h>
24 #include <nnapi/IExecution.h>
25 #include <nnapi/Result.h>
26 #include <nnapi/Types.h>
27 #include <nnapi/hal/CommonUtils.h>
28 #include <nnapi/hal/HandleError.h>
29
30 #include <memory>
31 #include <utility>
32 #include <vector>
33
34 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
35 // lifetimes across processes and for protecting asynchronous calls across HIDL.
36
37 namespace aidl::android::hardware::neuralnetworks::utils {
38
create(std::shared_ptr<const PreparedModel> preparedModel,Request request,hal::utils::RequestRelocation relocation,bool measure,int64_t loopTimeoutDuration)39 nn::GeneralResult<std::shared_ptr<const Execution>> Execution::create(
40 std::shared_ptr<const PreparedModel> preparedModel, Request request,
41 hal::utils::RequestRelocation relocation, bool measure, int64_t loopTimeoutDuration) {
42 if (preparedModel == nullptr) {
43 return NN_ERROR() << "aidl::utils::Execution::create must have non-null preparedModel";
44 }
45
46 return std::make_shared<const Execution>(PrivateConstructorTag{}, std::move(preparedModel),
47 std::move(request), std::move(relocation), measure,
48 loopTimeoutDuration);
49 }
50
Execution(PrivateConstructorTag,std::shared_ptr<const PreparedModel> preparedModel,Request request,hal::utils::RequestRelocation relocation,bool measure,int64_t loopTimeoutDuration)51 Execution::Execution(PrivateConstructorTag /*tag*/,
52 std::shared_ptr<const PreparedModel> preparedModel, Request request,
53 hal::utils::RequestRelocation relocation, bool measure,
54 int64_t loopTimeoutDuration)
55 : kPreparedModel(std::move(preparedModel)),
56 kRequest(std::move(request)),
57 kRelocation(std::move(relocation)),
58 kMeasure(measure),
59 kLoopTimeoutDuration(loopTimeoutDuration) {}
60
compute(const nn::OptionalTimePoint & deadline) const61 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> Execution::compute(
62 const nn::OptionalTimePoint& deadline) const {
63 const auto aidlDeadline = NN_TRY(hal::utils::makeExecutionFailure(convert(deadline)));
64 return kPreparedModel->executeInternal(kRequest, kMeasure, aidlDeadline, kLoopTimeoutDuration,
65 kRelocation);
66 }
67
computeFenced(const std::vector<nn::SyncFence> & waitFor,const nn::OptionalTimePoint & deadline,const nn::OptionalDuration & timeoutDurationAfterFence) const68 nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> Execution::computeFenced(
69 const std::vector<nn::SyncFence>& waitFor, const nn::OptionalTimePoint& deadline,
70 const nn::OptionalDuration& timeoutDurationAfterFence) const {
71 const auto aidlWaitFor = NN_TRY(convert(waitFor));
72 const auto aidlDeadline = NN_TRY(convert(deadline));
73 const auto aidlTimeoutDurationAfterFence = NN_TRY(convert(timeoutDurationAfterFence));
74 return kPreparedModel->executeFencedInternal(kRequest, aidlWaitFor, kMeasure, aidlDeadline,
75 kLoopTimeoutDuration,
76 aidlTimeoutDurationAfterFence, kRelocation);
77 }
78
79 } // namespace aidl::android::hardware::neuralnetworks::utils
80