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 "Dump.h"
18
19#include <arser/arser.h>
20#include <foder/FileLoader.h>
21#include <fstream>
22
23#include <functional>
24#include <iostream>
25#include <map>
26#include <memory>
27#include <vector>
28#include <string>
29
30#include <signal.h>
31
32void handle_segfault(int signal, siginfo_t *si, void *arg)
33{
34 std::cerr << "ERROR: Failed to load file" << std::endl;
35 exit(255);
36}
37
38int entry(int argc, char **argv)
39{
40 // TODO add option to dump for all sub-graphs
42 "circle-operator allows users to retrieve operator information from a Circle model file"};
43 arser.add_argument("--name").nargs(0).help("Dump operators name in circle file");
44 arser.add_argument("--code").nargs(0).help("Dump operators code in circle file");
45 arser.add_argument("--output_path").help("Save output to file (default output is console)");
46 arser.add_argument("circle").help("Circle file to dump");
47
48 try
49 {
50 arser.parse(argc, argv);
51 }
52 catch (const std::runtime_error &err)
53 {
54 std::cerr << err.what() << std::endl;
55 std::cerr << arser;
56 return 255;
57 }
58
59 cirops::DumpOption option;
60 option.names = arser["--name"];
61 option.codes = arser["--code"];
62
63 std::ofstream oFstream;
64 std::ostream *oStream = &std::cout;
65 if (arser["--output_path"])
66 {
67 auto output_path = arser.get<std::string>("--output_path");
68 oFstream.open(output_path, std::ofstream::out | std::ofstream::trunc);
69 if (oFstream.fail())
70 {
71 std::cerr << "ERROR: Failed to create output to file " << output_path << std::endl;
72 return 255;
73 }
74 oStream = &oFstream;
75 }
76
77 // hook segment fault
78 struct sigaction sa;
79 memset(&sa, 0, sizeof(struct sigaction));
80 sigemptyset(&sa.sa_mask);
81 sa.sa_sigaction = handle_segfault;
82 sa.sa_flags = SA_SIGINFO;
83 sigaction(SIGSEGV, &sa, NULL);
84
85 std::string modelFile = arser.get<std::string>("circle");
86 // Load Circle model from a circle file
87 try
88 {
89 foder::FileLoader fileLoader{modelFile};
90 std::vector<char> modelData = fileLoader.load();
91 const circle::Model *circleModel = circle::GetModel(modelData.data());
92 if (circleModel == nullptr)
93 {
94 std::cerr << "ERROR: Failed to load circle '" << modelFile << "'" << std::endl;
95 return 255;
96 }
98 dump.run(*oStream, circleModel, option);
99 }
100 catch (const std::runtime_error &err)
101 {
102 std::cerr << "ERROR: " << err.what() << std::endl;
103 return 255;
104 }
105
106 if (oFstream.is_open())
107 {
108 oFstream.close();
109 }
110
111 return 0;
112}
void run(std::ostream &os, const circle::Model *model, const DumpOption &option)
Definition Dump.cpp:70
DataBuffer load(void) const
Definition FileLoader.h:39
int entry(int argc, char **argv)
Definition Driver.cpp:29
void handle_segfault(int signal, siginfo_t *si, void *arg)
Definition Driver.cpp:32
void dump(const coco::Op *op, int indent)
Definition Dump.cpp:177
Definition arser.h:39