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