CompilationBuilder.cpp revision c2f1c1198c84f5a75fc2305935155f33b8ff5db2
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), mPartitioning(DeviceManager::get()->getPartitioning()) {
32    VLOG(COMPILATION) << "CompilationBuilder::CompilationBuilder";
33}
34
35int CompilationBuilder::finish() {
36    // Get the list of HAL devices.
37    return finish(DeviceManager::get()->getDrivers());
38}
39
40int CompilationBuilder::finish(const std::vector<std::shared_ptr<Device>>& devices) {
41    if (mFinished) {
42        LOG(ERROR) << "ANeuralNetworksCompilation_finish called more than once";
43        return ANEURALNETWORKS_BAD_STATE;
44    }
45    // TODO validate the rest
46
47    mFinished = true;
48
49    if (mPartitioning) {
50        int n = mModel->partitionTheWork(devices, mPreference, &mPlan);
51        if (!DeviceManager::partitioningAllowsFallback(mPartitioning) &&
52            (n != ANEURALNETWORKS_NO_ERROR)) {
53            return n;
54        }
55    }
56
57    return ANEURALNETWORKS_NO_ERROR;
58}
59
60int CompilationBuilder::setPreference(int32_t preference) {
61    if (mFinished) {
62        LOG(ERROR) <<
63                "ANeuralNetworksCompilation_setPreference can't modify after compilation finished";
64        return ANEURALNETWORKS_BAD_STATE;
65    }
66    if (preference >= kNumberOfPreferences) {
67        LOG(ERROR) << "ANeuralNetworksCompilation_setPreference invalid preference " << preference;
68        return ANEURALNETWORKS_BAD_DATA;
69    }
70
71    mPreference = preference;
72    return ANEURALNETWORKS_NO_ERROR;
73}
74
75int CompilationBuilder::setPartitioning(uint32_t partitioning) {
76    if (mFinished) {
77        LOG(ERROR) <<
78                "ANeuralNetworksCompilation_setPartitioning can't modify after compilation finished";
79        return ANEURALNETWORKS_BAD_STATE;
80    }
81
82    mPartitioning = partitioning;
83    return ANEURALNETWORKS_NO_ERROR;
84}
85
86int CompilationBuilder::createExecution(ExecutionBuilder **execution) {
87    if (!mFinished) {
88        LOG(ERROR) << "ANeuralNetworksExecution_create passed an unfinished compilation";
89        *execution = nullptr;
90        return ANEURALNETWORKS_BAD_STATE;
91    }
92    *execution = new ExecutionBuilder(this);
93    return (*execution ? ANEURALNETWORKS_NO_ERROR : ANEURALNETWORKS_OUT_OF_MEMORY);
94}
95
96}  // namespace nn
97}  // namespace android
98