ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Driver.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 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 "OMInterpreter.h"
18
19#include <stdexcept>
20#include <cstdlib>
21#include <fstream>
22#include <vector>
23#include <string>
24#include <iostream>
25
26namespace
27{
28
29using DataBuffer = std::vector<char>;
30
31void readDataFromFile(const std::string &filename, char *data, size_t data_size)
32{
33 std::ifstream fs(filename, std::ifstream::binary);
34 if (fs.fail())
35 throw std::runtime_error("Cannot open file \"" + filename + "\".\n");
36 if (fs.read(data, data_size).fail())
37 throw std::runtime_error("Failed to read data from file \"" + filename + "\".\n");
38}
39
40void writeDataToFile(const std::string &filename, const char *data, size_t data_size)
41{
42 std::ofstream fs(filename, std::ofstream::binary);
43 if (fs.fail())
44 throw std::runtime_error("Cannot open file \"" + filename + "\".\n");
45 if (fs.write(data, data_size).fail())
46 {
47 throw std::runtime_error("Failed to write data to file \"" + filename + "\".\n");
48 }
49}
50
51} // namespace
52
53/*
54 * @brief EvalDriver main
55 *
56 * Driver for testing luci-inerpreter
57 *
58 */
59int entry(int argc, char **argv)
60{
61 if (argc != 5)
62 {
63 std::cerr
64 << "Usage: " << argv[0]
65 << " <path/to/circle/model> <num_inputs> <path/to/input/prefix> <path/to/output/file>\n";
66 return EXIT_FAILURE;
67 }
68
69 const char *filename = argv[1];
70 const int32_t num_inputs = atoi(argv[2]);
71 const char *input_prefix = argv[3];
72 const char *output_file = argv[4];
73
74 std::ifstream file(filename, std::ios::binary | std::ios::in);
75 if (!file.good())
76 {
77 std::string errmsg = "Failed to open file";
78 throw std::runtime_error(errmsg.c_str());
79 }
80
81 file.seekg(0, std::ios::end);
82 auto fileSize = file.tellg();
83 file.seekg(0, std::ios::beg);
84
85 // reserve capacity
86 DataBuffer model_data(fileSize);
87
88 // read the data
89 file.read(model_data.data(), fileSize);
90 if (file.fail())
91 {
92 std::string errmsg = "Failed to read file";
93 throw std::runtime_error(errmsg.c_str());
94 }
95
96 // Create interpreter.
99 interpreter.importModel(model_data.data(), config);
100
101 // Set input.
102 // Data for n'th input is read from ${input_prefix}n
103 // (ex: Add.circle.input0, Add.circle.input1 ..)
104 int num_inference = 1;
105 for (int j = 0; j < num_inference; ++j)
106 {
107 interpreter.reset();
108 interpreter.allocateInputs();
109 for (int32_t i = 0; i < num_inputs; i++)
110 {
111 auto input_data = reinterpret_cast<char *>(interpreter.getInputDataAt(i));
112 readDataFromFile(std::string(input_prefix) + std::to_string(i), input_data,
113 interpreter.getInputSizeAt(i) * sizeof(float));
114 }
115
116 // Do inference.
117 interpreter.run(config);
118 }
119
120 // Get output.
121 int num_outputs = 1;
122 for (int i = 0; i < num_outputs; i++)
123 {
124 auto data = interpreter.getOutputDataAt(i);
125
126 // Output data is written in ${output_file}
127 // (ex: Add.circle.output0)
128 writeDataToFile(std::string(output_file) + std::to_string(i), reinterpret_cast<char *>(data),
129 interpreter.getOutputSizeAt(i) * sizeof(float));
130 }
131 interpreter.reset();
132 return EXIT_SUCCESS;
133}
134
135int entry(int argc, char **argv);
136
137#ifdef NDEBUG
138int main(int argc, char **argv)
139{
140 try
141 {
142 return entry(argc, argv);
143 }
144 catch (const std::exception &e)
145 {
146 std::cerr << "ERROR: " << e.what() << std::endl;
147 }
148
149 return 255;
150}
151#else // NDEBUG
152int main(int argc, char **argv)
153{
154 // NOTE main does not catch internal exceptions for debug build to make it easy to
155 // check the stacktrace with a debugger
156 return entry(argc, argv);
157}
158#endif // !NDEBUG
int main(void)
int entry(int argc, char **argv)
Definition Driver.cpp:29
void writeDataToFile(const std::string &file_path, const std::string &data)
write data to file_path
void readDataFromFile(const std::string &filename, std::vector< char > &data, size_t data_size)
Definition Utils.cpp:65
std::vector< char > DataBuffer