ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
onnx2circle.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 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 "exo/LoggingContext.h"
18#include "exo/CircleExporter.h"
19
20#include "mir2loco.h"
21#include "ONNXImporterImpl.h"
22
24
26#include "hermes/EnvConfig.h"
27
28#include <cassert>
29#include <memory>
30#include <iostream>
31#include <stdexcept>
32#include <string>
33
34//
35// Logging Support
36//
37namespace
38{
39
40struct Logger final : public hermes::Source
41{
42 Logger(hermes::Context *ctx) { activate(ctx->sources(), ctx->bus()); }
43 ~Logger() { deactivate(); }
44};
45
46struct LoggingContext
47{
48 static hermes::Context *get(void)
49 {
51
52 static hermes::Context *ctx = nullptr;
53
54 if (ctx == nullptr)
55 {
56 ctx = new hermes::Context;
57 ctx->sinks()->append(std::make_unique<hermes::ConsoleReporter>());
58 ctx->config(std::make_unique<EnvConfig>("ONNX2CIRCLE_Log"));
59 }
60
61 return ctx;
62 }
63};
64
65void print_help()
66{
67 std::cerr << "Usage: onnx2circle <path/to/onnx> <path/to/circle/model> " << std::endl;
68}
69
70} // namespace
71
72#define LOGGER(name) \
73 ::Logger name { ::LoggingContext::get() }
74
75#define INFO(name) HERMES_INFO(name)
76
77int main(int argc, char **argv)
78{
80
81 // This line allows users to control all the exo-circle loggers via ONNX2CIRCLE_Log_Backend
82 exo::LoggingContext::get()->config(std::make_unique<EnvConfig>("ONNX2CIRCLE_Log_Backend"));
83
84 LOGGER(l);
85
86 // TODO We need better args parsing in future
87 if (!(argc == 3))
88 {
89 print_help();
90 return 255;
91 }
92
93 std::string onnx_path{argv[1]}; // .pb file
94 std::string circle_path{argv[2]};
95
96 std::cout << "Import from '" << onnx_path << "'" << std::endl;
97 auto mir_g = mir_onnx::loadModel(onnx_path);
98 auto loco_g = mir2loco::Transformer().transform(mir_g.get());
99 std::cout << "Import from '" << onnx_path << "' - Done" << std::endl;
100
101 INFO(l) << "Import Graph" << std::endl;
102 INFO(l) << locop::fmt<locop::Formatter::LinearV1>(loco_g) << std::endl;
103
104 std::cout << "Export into '" << circle_path << "'" << std::endl;
105 exo::CircleExporter(loco_g.get()).dumpToFile(circle_path.c_str());
106 std::cout << "Export into '" << circle_path << "' - Done" << std::endl;
107
108 return 0;
109}
int main(void)
void dumpToFile(const char *path) const
write to a file
Logging controller.
Definition Context.h:40
Source::Registry * sources(void)
Definition Context.h:55
Sink::Registry * sinks(void)
Definition Context.h:64
MessageBus * bus(void)
Definition Context.h:48
const Config * config(void) const
Get the global configuration.
Definition Context.cpp:24
Message Source.
Definition Source.h:35
void deactivate(void)
Definition Source.cpp:49
void activate(Registry *, MessageBus *)
Definition Source.cpp:37
std::unique_ptr< loco::Graph > transform(mir::Graph *mir_graph)
Definition mir2loco.cpp:710
KnobTrait< K >::ValueType get(void)
std::unique_ptr< mir::Graph > loadModel(const std::string &filename)
#define LOGGER(name)
#define INFO(name)
static hermes::Context * get(void)
virtual void append(std::unique_ptr< Sink > &&)=0