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 "Manager.h" 28 #include "NeuralNetworks.h" 29 30 namespace android { 31 namespace nn { 32 33 class BurstBuilder; 34 class Device; 35 class ExecutionBuilder; 36 class ModelBuilder; 37 38 class CompilationBuilder { 39 public: 40 friend class ExecutionBuilder; // TODO remove this 41 42 // explicitDeviceList is true if the list of devices was provided explicitly 43 // via the ANeuralNetworksModel_createForDevices API (which has certain 44 // special semantics) and false otherwise. 45 CompilationBuilder(const ModelBuilder* model, 46 const std::vector<std::shared_ptr<Device>>& devices, 47 bool explicitDeviceList = false); 48 49 int setPreference(int32_t preference); 50 51 int setCaching(const std::string& cacheDir, const uint8_t* token); 52 // Dups the fds 53 int setCachingFromFds(const int* modelCacheFds, const uint32_t numModelCacheFiles, 54 const int* dataCacheFds, const uint32_t numDataCacheFiles, 55 const uint8_t* token); 56 57 int setPriority(int32_t priority); 58 59 int setTimeoutDuration(uint64_t duration); 60 61 int finish(); 62 63 int getPreferredMemoryAlignmentForInput(uint32_t index, uint32_t* alignment) const; 64 int getPreferredMemoryPaddingForInput(uint32_t index, uint32_t* padding) const; 65 int getPreferredMemoryAlignmentForOutput(uint32_t index, uint32_t* alignment) const; 66 int getPreferredMemoryPaddingForOutput(uint32_t index, uint32_t* padding) const; 67 68 int createExecution(ExecutionBuilder** execution); 69 70 int createBurst(BurstBuilder** burst); 71 getModel()72 const ModelBuilder* getModel() const { return mModel; } 73 74 int forEachStepRoleOfInput(uint32_t index, const StepRoleCallback& callback) const; 75 int forEachStepRoleOfOutput(uint32_t index, const StepRoleCallback& callback) const; 76 createdWithExplicitDeviceList()77 bool createdWithExplicitDeviceList() const { return mExplicitDeviceList; } 78 hasDynamicTemporaries()79 bool hasDynamicTemporaries() const { return mPlan.hasDynamicTemporaries(); } 80 81 // These functions are solely intended for use by unit tests of the 82 // partitioning algorithm. forTest_getExecutionPlan()83 const ExecutionPlan& forTest_getExecutionPlan() const { return mPlan; } 84 int forTest_setPartitioning(uint32_t partitioning); 85 int forTest_failPartitioning( 86 int resultCode); // If not ANEURALNETWORKS_NO_ERROR, then simulate partitioning failure 87 88 private: 89 const ModelBuilder* mModel; 90 91 ExecutionPlan mPlan; 92 93 // Whether the application prefers to go fast or use low power for this execution. 94 int32_t mPreference = ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER; 95 96 // See class DeviceManager. When CompilationBuilder is 97 // instantiated, we capture partitioning from DeviceManager; but 98 // we can override this later. 99 uint32_t mPartitioning; 100 101 // For testing purposes, simulate partitioning failure. 102 int mFailPartitioning = ANEURALNETWORKS_NO_ERROR; 103 104 // Once the compilation has been finished, we should not allow further 105 // modifications to the compilation. 106 bool mFinished = false; 107 108 // The set of devices that the partitioning algorithm operates on when 109 // finish() is called. 110 std::vector<std::shared_ptr<Device>> mDevices; 111 112 // mExplicitDeviceList is true if the list of devices was provided 113 // explicitly via the ANeuralNetworksModel_createForDevices API (which has 114 // certain special semantics) and false otherwise. 115 bool mExplicitDeviceList; 116 117 // Compilation caching information. 118 CacheInfo mCacheInfo; 119 uint8_t mToken[ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN]; 120 bool mIsCacheInfoProvided = false; 121 122 // Compilation priority information. 123 int32_t mPriority = ANEURALNETWORKS_PRIORITY_DEFAULT; 124 125 // Amount of time to complete or abort the execution. 126 std::optional<uint64_t> mTimeoutDuration; 127 }; 128 129 } // namespace nn 130 } // namespace android 131 132 #endif // ANDROID_FRAMEWORKS_ML_NN_RUNTIME_COMPILATION_BUILDER_H 133