1 /*
2 * Copyright (C) 2020 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 "Burst.h"
18
19 #include <android-base/logging.h>
20 #include <nnapi/IBurst.h>
21 #include <nnapi/IPreparedModel.h>
22 #include <nnapi/Result.h>
23 #include <nnapi/TypeUtils.h>
24 #include <nnapi/Types.h>
25
26 #include <memory>
27 #include <optional>
28 #include <utility>
29
30 namespace android::hardware::neuralnetworks::V1_0::utils {
31
create(nn::SharedPreparedModel preparedModel)32 nn::GeneralResult<std::shared_ptr<const Burst>> Burst::create(
33 nn::SharedPreparedModel preparedModel) {
34 if (preparedModel == nullptr) {
35 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
36 << "V1_0::utils::Burst::create must have non-null preparedModel";
37 }
38
39 return std::make_shared<const Burst>(PrivateConstructorTag{}, std::move(preparedModel));
40 }
41
Burst(PrivateConstructorTag,nn::SharedPreparedModel preparedModel)42 Burst::Burst(PrivateConstructorTag /*tag*/, nn::SharedPreparedModel preparedModel)
43 : kPreparedModel(std::move(preparedModel)) {
44 CHECK(kPreparedModel != nullptr);
45 }
46
cacheMemory(const nn::SharedMemory &) const47 Burst::OptionalCacheHold Burst::cacheMemory(const nn::SharedMemory& /*memory*/) const {
48 return nullptr;
49 }
50
execute(const nn::Request & request,nn::MeasureTiming measure,const nn::OptionalTimePoint & deadline,const nn::OptionalDuration & loopTimeoutDuration) const51 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> Burst::execute(
52 const nn::Request& request, nn::MeasureTiming measure,
53 const nn::OptionalTimePoint& deadline,
54 const nn::OptionalDuration& loopTimeoutDuration) const {
55 return kPreparedModel->execute(request, measure, deadline, loopTimeoutDuration);
56 }
57
createReusableExecution(const nn::Request & request,nn::MeasureTiming measure,const nn::OptionalDuration & loopTimeoutDuration) const58 nn::GeneralResult<nn::SharedExecution> Burst::createReusableExecution(
59 const nn::Request& request, nn::MeasureTiming measure,
60 const nn::OptionalDuration& loopTimeoutDuration) const {
61 return kPreparedModel->createReusableExecution(request, measure, loopTimeoutDuration);
62 }
63
64 } // namespace android::hardware::neuralnetworks::V1_0::utils
65