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#define LOG_TAG "CompilationBuilder" 18 19#include "CompilationBuilder.h" 20 21#include "ExecutionBuilder.h" 22#include "ExecutionPlan.h" 23#include "Manager.h" 24#include "ModelBuilder.h" 25#include "Utils.h" 26 27namespace android { 28namespace nn { 29 30CompilationBuilder::CompilationBuilder(const ModelBuilder* model) : 31 mModel(model) { 32 VLOG(COMPILATION) << "CompilationBuilder::CompilationBuilder"; 33} 34 35int CompilationBuilder::finish() { 36 if (mFinished) { 37 LOG(ERROR) << "ANeuralNetworksCompilation_finish called more than once"; 38 return ANEURALNETWORKS_BAD_STATE; 39 } 40 // TODO validate the rest 41 42 mFinished = true; 43 44 if (uint32_t p = DeviceManager::get()->getPartitioning()) { 45 // Get the list of HAL devices. 46 const std::vector<std::shared_ptr<Device>>& devices = DeviceManager::get()->getDrivers(); 47 48 int n = mModel->partitionTheWork(devices, mPreference, &mPlan); 49 if (!DeviceManager::partitioningAllowsFallback(p) && 50 (n != ANEURALNETWORKS_NO_ERROR)) { 51 return n; 52 } 53 } 54 55 return ANEURALNETWORKS_NO_ERROR; 56} 57 58int CompilationBuilder::setPreference(int32_t preference) { 59 if (mFinished) { 60 LOG(ERROR) << 61 "ANeuralNetworksCompilation_setPreference can't modify after compilation finished"; 62 return ANEURALNETWORKS_BAD_STATE; 63 } 64 if (preference >= kNumberOfPreferences) { 65 LOG(ERROR) << "ANeuralNetworksCompilation_setPreference invalid preference " << preference; 66 return ANEURALNETWORKS_BAD_DATA; 67 } 68 69 mPreference = preference; 70 return ANEURALNETWORKS_NO_ERROR; 71} 72 73int CompilationBuilder::createExecution(ExecutionBuilder **execution) { 74 if (!mFinished) { 75 LOG(ERROR) << "ANeuralNetworksExecution_create passed an unfinished compilation"; 76 *execution = nullptr; 77 return ANEURALNETWORKS_BAD_STATE; 78 } 79 *execution = new ExecutionBuilder(this); 80 return (*execution ? ANEURALNETWORKS_NO_ERROR : ANEURALNETWORKS_OUT_OF_MEMORY); 81} 82 83} // namespace nn 84} // namespace android 85