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