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 #ifndef ANDROID_PACKAGES_MODULES_NEURALNETWORKS_DRIVER_SAMPLE_CANONICAL_PREPARED_MODEL_H
18 #define ANDROID_PACKAGES_MODULES_NEURALNETWORKS_DRIVER_SAMPLE_CANONICAL_PREPARED_MODEL_H
19 
20 #include <BufferTracker.h>
21 #include <CpuExecutor.h>
22 #include <nnapi/IExecution.h>
23 #include <nnapi/IPreparedModel.h>
24 #include <nnapi/Result.h>
25 #include <nnapi/Types.h>
26 
27 #include <memory>
28 #include <tuple>
29 #include <utility>
30 #include <vector>
31 
32 namespace android::nn::sample {
33 
34 class PreparedModel final : public IPreparedModel,
35                             public std::enable_shared_from_this<PreparedModel> {
36    public:
37     PreparedModel(Model model, ExecutionPreference preference, Priority priority,
38                   const IOperationResolver* operationResolver,
39                   std::shared_ptr<BufferTracker> bufferTracker,
40                   std::vector<RunTimePoolInfo> poolInfos);
41 
42     ExecutionResult<std::pair<std::vector<OutputShape>, Timing>> execute(
43             const Request& request, MeasureTiming measure, const OptionalTimePoint& deadline,
44             const OptionalDuration& loopTimeoutDuration, const std::vector<TokenValuePair>& hints,
45             const std::vector<ExtensionNameAndPrefix>& extensionNameToPrefix) const override;
46 
47     GeneralResult<std::pair<SyncFence, ExecuteFencedInfoCallback>> executeFenced(
48             const Request& request, const std::vector<SyncFence>& waitFor, MeasureTiming measure,
49             const OptionalTimePoint& deadline, const OptionalDuration& loopTimeoutDuration,
50             const OptionalDuration& timeoutDurationAfterFence,
51             const std::vector<TokenValuePair>& hints,
52             const std::vector<ExtensionNameAndPrefix>& extensionNameToPrefix) const override;
53 
54     GeneralResult<nn::SharedExecution> createReusableExecution(
55             const Request& request, MeasureTiming measure,
56             const OptionalDuration& loopTimeoutDuration, const std::vector<TokenValuePair>& hints,
57             const std::vector<ExtensionNameAndPrefix>& extensionNameToPrefix) const override;
58 
59     GeneralResult<SharedBurst> configureExecutionBurst() const override;
60 
61     std::any getUnderlyingResource() const override;
62 
63    private:
64     const Model kModel;
65     [[maybe_unused]] const ExecutionPreference kExecutionPreference;
66     [[maybe_unused]] const Priority kExecutionPriority;
67     const IOperationResolver& kOperationResolver;
68     const std::shared_ptr<BufferTracker> kBufferTracker;
69     const std::vector<RunTimePoolInfo> kPoolInfos;
70 };
71 
72 }  // namespace android::nn::sample
73 
74 #endif  // ANDROID_PACKAGES_MODULES_NEURALNETWORKS_DRIVER_SAMPLE_CANONICAL_PREPARED_MODEL_H
75