ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Driver.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 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 "FMEqualizer.h"
18#include "EqualizePatternRead.h"
19
20#include <arser/arser.h>
21#include <foder/FileLoader.h>
22#include <luci/CircleExporter.h>
24#include <luci/ImporterEx.h>
26
27#include <iostream>
28#include <string>
29
30using namespace fme_apply;
31
32int entry(int argc, char **argv)
33{
34 arser::Arser arser("fm-apply applies channel-wise scale/shift for FM equalization");
35
37
38 arser.add_argument("--input").required().help("Input circle model");
39
40 arser.add_argument("--fme_patterns")
41 .required()
42 .help("Json file that describes feture map equalization info");
43
44 arser.add_argument("--output").required().help("Output circle model");
45
46 try
47 {
48 arser.parse(argc, argv);
49 }
50 catch (const std::runtime_error &err)
51 {
52 std::cerr << err.what() << std::endl;
53 std::cout << arser;
54 return EXIT_FAILURE;
55 }
56
57 if (arser.get<bool>("--verbose"))
58 {
59 // The third parameter of setenv means REPLACE.
60 // If REPLACE is zero, it does not overwrite an existing value.
61 setenv("LUCI_LOG", "100", 0);
62 }
63
64 const std::string input_path = arser.get<std::string>("--input");
65 const std::string fme_patterns_path = arser.get<std::string>("--fme_patterns");
66 const std::string output_path = arser.get<std::string>("--output");
67
68 luci::ImporterEx importerex;
69 auto module = importerex.importVerifyModule(input_path);
70 assert(module != nullptr); // FIX_ME_UNLESS
71
72 auto patterns = fme_apply::read(fme_patterns_path);
73
74 for (size_t idx = 0; idx < module->size(); ++idx)
75 {
76 auto graph = module->graph(idx);
77
78 FMEqualizer equalizer;
79 equalizer.equalize(graph, patterns);
80
81 if (!luci::validate(graph))
82 {
83 std::cerr << "ERROR: Equalized graph is invalid" << std::endl;
84 return EXIT_FAILURE;
85 }
86 }
87
88 // Export to output Circle file
89 luci::CircleExporter exporter;
90
91 luci::CircleFileExpContract contract(module.get(), output_path);
92
93 if (!exporter.invoke(&contract))
94 {
95 std::cerr << "ERROR: Failed to export '" << output_path << "'" << std::endl;
96 return EXIT_FAILURE;
97 }
98
99 return EXIT_SUCCESS;
100}
static void add_verbose(Arser &arser)
Definition arser.h:765
void equalize(loco::Graph *g, std::vector< EqualizePattern > &p)
bool invoke(Contract *) const
int entry(int argc, char **argv)
Dump IR for given arguments.
Definition Driver.cpp:32
Definition arser.h:39
std::vector< EqualizePattern > read(const std::string &filename)
bool validate(luci::PartitionTable &partition)