1 /*
2  * Copyright (C) 2017 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_FRAMEWORKS_ML_NN_RUNTIME_COMPILATION_BUILDER_H
18 #define ANDROID_FRAMEWORKS_ML_NN_RUNTIME_COMPILATION_BUILDER_H
19 
20 #include <chrono>
21 #include <memory>
22 #include <optional>
23 #include <string>
24 #include <vector>
25 
26 #include "ExecutionPlan.h"
27 #include "NeuralNetworks.h"
28 
29 namespace android {
30 namespace nn {
31 
32 class BurstBuilder;
33 class Device;
34 class ExecutionBuilder;
35 class ModelBuilder;
36 
37 class CompilationBuilder {
38    public:
39     friend class ExecutionBuilder;  // TODO remove this
40 
41     // explicitDeviceList is true if the list of devices was provided explicitly
42     // via the ANeuralNetworksModel_createForDevices API (which has certain
43     // special semantics) and false otherwise.
44     CompilationBuilder(const ModelBuilder* model,
45                        const std::vector<std::shared_ptr<Device>>& devices,
46                        bool explicitDeviceList = false);
47 
48     int setPreference(int32_t preference);
49 
50     int setPartitioning(uint32_t partitioning);
51 
52     int setCaching(const std::string& cacheDir, const uint8_t* token);
53 
54     int setPriority(int32_t priority);
55 
56     int setTimeoutDuration(uint64_t duration);
57 
58     int finish();
59 
60     int createExecution(ExecutionBuilder** execution);
61 
62     int createBurst(BurstBuilder** burst);
63 
getModel()64     const ModelBuilder* getModel() const { return mModel; }
65 
66     int forEachStepRoleOfInput(uint32_t index, const StepRoleCallback& callback) const;
67     int forEachStepRoleOfOutput(uint32_t index, const StepRoleCallback& callback) const;
68 
forTest_getExecutionPlan()69     const ExecutionPlan& forTest_getExecutionPlan() const { return mPlan; }
70 
createdWithExplicitDeviceList()71     bool createdWithExplicitDeviceList() const { return mExplicitDeviceList; }
72 
73    private:
74     const ModelBuilder* mModel;
75 
76     ExecutionPlan mPlan;
77 
78     // Whether the application prefers to go fast or use low power for this execution.
79     int32_t mPreference = ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER;
80 
81     // See class DeviceManager.  When CompilationBuilder is
82     // instantiated, we capture partitioning from DeviceManager; but
83     // we can override this later.
84     uint32_t mPartitioning;
85 
86     // Once the compilation has been finished, we should not allow further
87     // modifications to the compilation.
88     bool mFinished = false;
89 
90     // The set of devices that the partitioning algorithm operates on when
91     // finish() is called.
92     std::vector<std::shared_ptr<Device>> mDevices;
93 
94     // mExplicitDeviceList is true if the list of devices was provided
95     // explicitly via the ANeuralNetworksModel_createForDevices API (which has
96     // certain special semantics) and false otherwise.
97     bool mExplicitDeviceList;
98 
99     // Compilation caching information.
100     std::string mCacheDir;
101     uint8_t mToken[ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN];
102     bool mIsCacheInfoProvided = false;
103 
104     // Compilation priority information.
105     int32_t mPriority = ANEURALNETWORKS_PRIORITY_DEFAULT;
106 
107     // Amount of time to complete or abort the execution.
108     std::optional<uint64_t> mTimeoutDuration;
109 };
110 
111 }  // namespace nn
112 }  // namespace android
113 
114 #endif  // ANDROID_FRAMEWORKS_ML_NN_RUNTIME_COMPILATION_BUILDER_H
115