1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* This is simple tests to check that generator is running properly and creates compilable artifact
* This test is not intended to check correctness of generated artifact
*/
#include "mir/Graph.h"
#include "mir/Shape.h"
#include "mir/ops/InputOp.h"
#include "mir/ops/OutputOp.h"
#include "mir/ops/ReluOp.h"
#include "backends/soft_backend/CPPGenerator.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
// This header generated and contains array with test_main.def contents
#include "test_main.generated.h"
using namespace std;
using namespace nnc;
using namespace mir;
// Creates simple graph with input and output
static void fillGraph(Graph &g)
{
Shape input_shape{1, 2, 3};
mir::TensorType input_type{mir::DataType::FLOAT32, input_shape};
Operation *input_op = g.create<ops::InputOp>(input_type);
Operation *relu_op = g.create<ops::ReluOp>(input_op->getOutput(0));
Operation *output_op = g.create<ops::OutputOp>(relu_op->getOutput(0));
input_op->getOutput(0)->setName("in");
relu_op->getOutput(0)->setName("out");
}
static void checkFileExists(const string &path)
{
ifstream f(path);
if (!f.good())
{
cerr << "file " << path << " not created\n";
exit(1);
}
}
static void createMain(const string &path, const string &header_path)
{
ofstream out(path);
if (!out.good())
{
cerr << "Main file " << path << " not created\n";
exit(1);
}
out << "#include \"" << header_path << "\"\n";
out.write(test_main, sizeof(test_main));
}
int main()
{
std::string output_dir = "test_output";
std::string artifact_name = "nnmodel";
Graph g;
fillGraph(g);
nnc::CPPCodeGenerator cpp_code_generator(output_dir, artifact_name);
cpp_code_generator.run(&g);
string base_path = output_dir + "/" + artifact_name;
string code_path = base_path + ".cpp";
string header_path = base_path + ".h";
string main_path = base_path + "_main.cpp";
checkFileExists(code_path);
checkFileExists(header_path);
checkFileExists(base_path + ".params");
createMain(main_path, artifact_name + ".h");
string target_compiler = "g++ -Wall --std=c++11";
string compiler_command =
target_compiler + " -I" + output_dir + " " + main_path + " " + code_path;
// call compiler
int res = system(compiler_command.c_str());
if (res == -1)
{
cerr << "failed to call compiler\n";
return 2;
}
if (res != 0)
{
cerr << "compiler did not succeed with error code " << res << ": " << compiler_command << "\n";
return 3;
}
return 0;
}
|