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 #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_2::utils {
42
create(std::shared_ptr<const PreparedModel> preparedModel,V1_0::Request request,hal::utils::RequestRelocation relocation,V1_2::MeasureTiming measure)43 nn::GeneralResult<std::shared_ptr<const Execution>> Execution::create(
44 std::shared_ptr<const PreparedModel> preparedModel, V1_0::Request request,
45 hal::utils::RequestRelocation relocation, V1_2::MeasureTiming measure) {
46 if (preparedModel == nullptr) {
47 return NN_ERROR() << "V1_2::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 }
53
Execution(PrivateConstructorTag,std::shared_ptr<const PreparedModel> preparedModel,V1_0::Request request,hal::utils::RequestRelocation relocation,V1_2::MeasureTiming measure)54 Execution::Execution(PrivateConstructorTag /*tag*/,
55 std::shared_ptr<const PreparedModel> preparedModel, V1_0::Request request,
56 hal::utils::RequestRelocation relocation, V1_2::MeasureTiming measure)
57 : kPreparedModel(std::move(preparedModel)),
58 kRequest(std::move(request)),
59 kRelocation(std::move(relocation)),
60 kMeasure(measure) {}
61
compute(const nn::OptionalTimePoint &) const62 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> Execution::compute(
63 const nn::OptionalTimePoint& /*deadline*/) const {
64 return kPreparedModel->executeInternal(kRequest, kMeasure, kRelocation);
65 }
66
computeFenced(const std::vector<nn::SyncFence> &,const nn::OptionalTimePoint &,const nn::OptionalDuration &) const67 nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> Execution::computeFenced(
68 const std::vector<nn::SyncFence>& /*waitFor*/, const nn::OptionalTimePoint& /*deadline*/,
69 const nn::OptionalDuration& /*timeoutDurationAfterFence*/) const {
70 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
71 << "IExecution::computeFenced is not supported on 1.2 HAL service";
72 }
73
74 } // namespace android::hardware::neuralnetworks::V1_2::utils
75