ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Driver.cpp
Go to the documentation of this file.
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 <enco/Frontend.h>
18#include <enco/Backend.h>
19
20#include <cmdline/View.h>
21
22#include <string>
23#include <vector>
24
25#include <functional>
26
27#include "Dump.h"
28
29namespace cmdline
30{
31
32// TODO Extract this helper class
33class Vector : public cmdline::View
34{
35public:
36 uint32_t size(void) const { return _args.size(); }
37
38public:
39 const char *at(uint32_t nth) const { return _args.at(nth).c_str(); }
40
41public:
42 Vector &append(const std::string &arg)
43 {
44 _args.emplace_back(arg);
45 return (*this);
46 }
47
48private:
49 std::vector<std::string> _args;
50};
51
52} // namespace cmdline
53
54namespace
55{
56
57class Zone
58{
59public:
60 Zone() = default;
61
62public:
63 const cmdline::View *args(void) const { return &_args; }
64
65public:
66 void append(const std::string &arg) { _args.append(arg); }
67
68private:
69 cmdline::Vector _args;
70};
71
72} // namespace
73
74#include <dlfcn.h>
75
76namespace
77{
78
79class FrontendFactory
80{
81public:
82 FrontendFactory(const std::string &path)
83 {
84 _handle = dlopen(path.c_str(), RTLD_LAZY);
85 assert(_handle != nullptr);
86 }
87
88public:
89 // Copy is not allowed to avoid double close
90 FrontendFactory(const FrontendFactory &) = delete;
91 FrontendFactory(FrontendFactory &&) = delete;
92
93public:
94 ~FrontendFactory() { dlclose(_handle); }
95
96private:
97 using Entry = std::unique_ptr<enco::Frontend> (*)(const cmdline::View &);
98
99private:
100 Entry entry(void) const
101 {
102 auto entry = reinterpret_cast<Entry>(dlsym(_handle, "make_frontend"));
103 assert(entry != nullptr);
104 return entry;
105 }
106
107public:
108 std::unique_ptr<enco::Frontend> make(const cmdline::View *args) const
109 {
110 auto fn = entry();
111 return fn(*args);
112 }
113
114private:
115 void *_handle;
116};
117
118} // namespace
119
120namespace
121{
122
123class FrontendZone : public Zone
124{
125public:
126 FrontendZone(const std::string &path) : _factory{path}
127 {
128 // DO NOTHING
129 }
130
131public:
132 const FrontendFactory *factory(void) const { return &_factory; }
133
134private:
135 FrontendFactory _factory;
136};
137
138} // namespace
139
140#include <memory>
141#include <map>
142
143#include <iostream>
144#include <stdexcept>
145
155int entry(int argc, char **argv)
156{
157 // Usage:
158 // [Command] --frontend [Frontend .so path] --frontend-arg ...
159 std::unique_ptr<FrontendZone> frontend_zone;
160
161 // Simple argument parser (based on map)
162 std::map<std::string, std::function<void(const std::string &arg)>> argparse;
163
164 argparse["--frontend"] = [&](const std::string &path) {
165 frontend_zone = std::make_unique<FrontendZone>(path);
166 };
167
168 argparse["--frontend-arg"] = [&](const std::string &arg) { frontend_zone->append(arg); };
169
170 if (argc < 2)
171 {
172 std::cerr << "Usage:" << std::endl;
173 std::cerr << "[Command] --frontend [.so path]" << std::endl;
174 std::cerr << " --frontend-arg [argument] ..." << std::endl;
175 return 255;
176 }
177
178 for (int n = 1; n < argc; n += 2)
179 {
180 const std::string tag{argv[n]};
181 const std::string arg{argv[n + 1]};
182
183 auto it = argparse.find(tag);
184
185 if (it == argparse.end())
186 {
187 std::cerr << "Option '" << tag << "' is not supported" << std::endl;
188 return 255;
189 }
190
191 it->second(arg);
192 }
193
194 assert(frontend_zone != nullptr);
195
196 auto frontend = frontend_zone->factory()->make(frontend_zone->args());
197
198 auto bundle = frontend->load();
199
200 // dump
201 dump(bundle.module());
202
203 // todo : dump data
204
205 return 0;
206}
Vector & append(const std::string &arg)
Definition Driver.cpp:42
uint32_t size(void) const
Definition Driver.cpp:36
const char * at(uint32_t nth) const
Definition Driver.cpp:39
int entry(const int argc, char **argv)
Definition Driver.cpp:53
int entry(int argc, char **argv)
Definition Driver.cpp:29
void dump(const coco::Op *op, int indent)
Definition Dump.cpp:177
Definition View.h:23
args
Definition infer.py:21