2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <NeuralNetworks.h>
21 #include "wrapper/ANeuralNetworksModel.h"
22 #include "wrapper/ANeuralNetworksCompilation.h"
23 #include "util/logging.h"
26 // NNAPI Implementation
28 int ANeuralNetworksCompilation_create(ANeuralNetworksModel *model,
29 ANeuralNetworksCompilation **compilation)
31 if ((model == nullptr) || (compilation == nullptr))
33 VERBOSE(NNAPI::Compilation) << "create: Incorrect null pointer parameter(s)" << std::endl;
34 return ANEURALNETWORKS_UNEXPECTED_NULL;
37 if (!model->isFinished())
39 VERBOSE(NNAPI::Compilation) << "create: Model define is not finished" << std::endl;
40 return ANEURALNETWORKS_BAD_STATE;
43 *compilation = new (std::nothrow) ANeuralNetworksCompilation(model);
44 if (*compilation == nullptr)
46 VERBOSE(NNAPI::Compilation) << "create: ail to create compilation object" << std::endl;
47 return ANEURALNETWORKS_OUT_OF_MEMORY;
50 return ANEURALNETWORKS_NO_ERROR;
53 int ANeuralNetworksCompilation_finish(ANeuralNetworksCompilation *compilation)
55 if (compilation == nullptr)
57 VERBOSE(NNAPI::Compilation) << "finish: Incorrect null pointer parameter" << std::endl;
58 return ANEURALNETWORKS_UNEXPECTED_NULL;
61 if (compilation->state() != ::onert::compiler::State::CREATED)
63 VERBOSE(NNAPI::Compilation) << "finish: Already finished" << std::endl;
64 return ANEURALNETWORKS_BAD_STATE;
67 if (!compilation->finish())
69 VERBOSE(NNAPI::Compilation) << "finish: Fail to compile" << std::endl;
70 return ANEURALNETWORKS_BAD_STATE;
73 return ANEURALNETWORKS_NO_ERROR;
76 void ANeuralNetworksCompilation_free(ANeuralNetworksCompilation *compilation)
81 int ANeuralNetworksCompilation_setPreference(ANeuralNetworksCompilation *compilation,
84 if (compilation == nullptr)
86 VERBOSE(NNAPI::Compilation) << "setPreference: Incorrect null pointer parameter" << std::endl;
87 return ANEURALNETWORKS_UNEXPECTED_NULL;
90 if (compilation->state() != ::onert::compiler::State::CREATED)
92 VERBOSE(NNAPI::Compilation) << "setPreference: Already finished" << std::endl;
93 return ANEURALNETWORKS_BAD_STATE;
96 const PreferenceCode FIRST_PREFERENCE_CODE = ANEURALNETWORKS_PREFER_LOW_POWER;
97 const PreferenceCode LAST_PREFERENCE_CODE = ANEURALNETWORKS_PREFER_SUSTAINED_SPEED;
98 if ((preference < FIRST_PREFERENCE_CODE) || (preference > LAST_PREFERENCE_CODE))
100 VERBOSE(NNAPI::Compilation) << "setPreference: Incorrect preference code" << std::endl;
101 return ANEURALNETWORKS_BAD_DATA;
104 // NYI: nothing to set
105 return ANEURALNETWORKS_NO_ERROR;