Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / runtime / onert / frontend / nnapi / compilation.cc
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
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 #include <NeuralNetworks.h>
18
19 #include <new>
20
21 #include "wrapper/ANeuralNetworksModel.h"
22 #include "wrapper/ANeuralNetworksCompilation.h"
23 #include "util/logging.h"
24
25 //
26 // NNAPI Implementation
27 //
28 int ANeuralNetworksCompilation_create(ANeuralNetworksModel *model,
29                                       ANeuralNetworksCompilation **compilation)
30 {
31   if ((model == nullptr) || (compilation == nullptr))
32   {
33     VERBOSE(NNAPI::Compilation) << "create: Incorrect null pointer parameter(s)" << std::endl;
34     return ANEURALNETWORKS_UNEXPECTED_NULL;
35   }
36
37   if (!model->isFinished())
38   {
39     VERBOSE(NNAPI::Compilation) << "create: Model define is not finished" << std::endl;
40     return ANEURALNETWORKS_BAD_STATE;
41   }
42
43   *compilation = new (std::nothrow) ANeuralNetworksCompilation(model);
44   if (*compilation == nullptr)
45   {
46     VERBOSE(NNAPI::Compilation) << "create: ail to create compilation object" << std::endl;
47     return ANEURALNETWORKS_OUT_OF_MEMORY;
48   }
49
50   return ANEURALNETWORKS_NO_ERROR;
51 }
52
53 int ANeuralNetworksCompilation_finish(ANeuralNetworksCompilation *compilation)
54 {
55   if (compilation == nullptr)
56   {
57     VERBOSE(NNAPI::Compilation) << "finish: Incorrect null pointer parameter" << std::endl;
58     return ANEURALNETWORKS_UNEXPECTED_NULL;
59   }
60
61   if (compilation->state() != ::onert::compiler::State::CREATED)
62   {
63     VERBOSE(NNAPI::Compilation) << "finish: Already finished" << std::endl;
64     return ANEURALNETWORKS_BAD_STATE;
65   }
66
67   if (!compilation->finish())
68   {
69     VERBOSE(NNAPI::Compilation) << "finish: Fail to compile" << std::endl;
70     return ANEURALNETWORKS_BAD_STATE;
71   }
72
73   return ANEURALNETWORKS_NO_ERROR;
74 }
75
76 void ANeuralNetworksCompilation_free(ANeuralNetworksCompilation *compilation)
77 {
78   delete compilation;
79 }
80
81 int ANeuralNetworksCompilation_setPreference(ANeuralNetworksCompilation *compilation,
82                                              int32_t preference)
83 {
84   if (compilation == nullptr)
85   {
86     VERBOSE(NNAPI::Compilation) << "setPreference: Incorrect null pointer parameter" << std::endl;
87     return ANEURALNETWORKS_UNEXPECTED_NULL;
88   }
89
90   if (compilation->state() != ::onert::compiler::State::CREATED)
91   {
92     VERBOSE(NNAPI::Compilation) << "setPreference: Already finished" << std::endl;
93     return ANEURALNETWORKS_BAD_STATE;
94   }
95
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))
99   {
100     VERBOSE(NNAPI::Compilation) << "setPreference: Incorrect preference code" << std::endl;
101     return ANEURALNETWORKS_BAD_DATA;
102   }
103
104   // NYI: nothing to set
105   return ANEURALNETWORKS_NO_ERROR;
106 }