ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Knob.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 "Knob.h"
18
19#include <pepper/strcast.h>
20
21#include <iostream>
22#include <string>
23#include <map>
24
25// Basic Infrastructure to declare and access Knob values
26namespace
27{
28
29using KnobName = std::string;
30
34struct KnobLoader
35{
36 virtual ~KnobLoader() = default;
37
38 virtual bool load(const KnobName &name, bool default_value) const = 0;
39};
40
50class EnvKnobLoader final : public KnobLoader
51{
52public:
53 EnvKnobLoader() = default;
54
55public:
56 bool load(const KnobName &knob_name, bool default_value) const override
57 {
58 auto envvar = _prefix + knob_name;
59 auto s = std::getenv(envvar.c_str());
60
61 return pepper::safe_strcast<int>(s, default_value ? 1 : 0) != 0;
62 }
63 void knob_set(const KnobName &knob_name, bool value) { _knob[knob_name] = value; }
64 void dialect_set(const exo::Dialect &dialect_name) { _prefix = _label[dialect_name]; }
65 bool knob_get(const KnobName &knob_name) { return load(knob_name, _knob[knob_name]); }
66
67private:
69 std::string _prefix;
70 std::map<KnobName, bool> _knob;
71 std::map<exo::Dialect, KnobName> _label = {{exo::Dialect::TFLITE, "TFL_"},
72 {exo::Dialect::CIRCLE, "CIRCLE_"}};
73};
74
75} // namespace
76
77namespace
78{
79
80EnvKnobLoader &knob_loader(void)
81{
82 // TODO separate "EXOTFLITE_" and "EXOCIRCLE_" when necessary
83 static EnvKnobLoader loader;
84 return loader;
85}
86
87} // namespace
88
89namespace exo
90{
91
92#define KNOB_BOOL(NAME, TFL_DEFAULT, CIRCLE_DEFAULT, DESC) \
93 template <> typename KnobTrait<Knob::NAME>::ValueType get<Knob::NAME>(void) \
94 { \
95 return ::knob_loader().knob_get(#NAME); \
96 }
97#include "Knob.lst"
98#undef KNOB_BOOL
99
101{
102 ::knob_loader().dialect_set(d);
103 switch (d)
104 {
105 case Dialect::TFLITE:
106#define KNOB_BOOL(NAME, TFL_DEFAULT, CIRCLE_DEFAULT, DESC) \
107 ::knob_loader().knob_set(#NAME, TFL_DEFAULT);
108#include "Knob.lst"
109#undef KNOB_BOOL
110 break;
111 case Dialect::CIRCLE:
112#define KNOB_BOOL(NAME, TFL_DEFAULT, CIRCLE_DEFAULT, DESC) \
113 ::knob_loader().knob_set(#NAME, CIRCLE_DEFAULT);
114#include "Knob.lst"
115#undef KNOB_BOOL
116 break;
117 default:
118 std::runtime_error("UnKnown dialect");
119 }
120}
121
122} // namespace exo
Dialect
Definition Knob.h:24
void set(Dialect d)
Definition Knob.cpp:100
int safe_strcast< int >(const char *s, const int &v)
Definition strcast.cpp:24
TensorSignatures load(const char *info_path)
Function to create TensorSignatures defined in info file.