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
24// Basic Infrastructure to declare and access Knob values
25//
26// TODO Reuse this infrastructure as a library
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
42// Template-programming helpers
43template <typename T> T knob_load(const KnobLoader &, const KnobName &, const T &);
44
45template <>
46bool knob_load(const KnobLoader &l, const KnobName &knob_name, const bool &default_value)
47{
48 return l.load(knob_name, default_value);
49}
50
60class EnvKnobLoader final : public KnobLoader
61{
62public:
63 EnvKnobLoader(const std::string &prefix) : _prefix{prefix}
64 {
65 // DO NOTHING
66 }
67
68public:
69 bool load(const KnobName &knob_name, bool default_value) const override
70 {
71 auto envvar = _prefix + knob_name;
72 auto s = std::getenv(envvar.c_str());
73
74 return pepper::safe_strcast<int>(s, default_value ? 1 : 0) != 0;
75 }
76
77private:
79 std::string _prefix;
80};
81
82} // namespace
83
84namespace
85{
86
99const KnobLoader &knob_loader(void)
100{
101 static EnvKnobLoader loader{"MOCO_"};
102 return loader;
103}
104
105} // namespace
106
107namespace moco
108{
109namespace tf
110{
111
112#define KNOB_BOOL(NAME, DEFAULT, DESC) \
113 template <> typename KnobTrait<Knob::NAME>::ValueType get<Knob::NAME>(void) \
114 { \
115 static typename KnobTrait<Knob::NAME>::ValueType value = \
116 ::knob_load<typename KnobTrait<Knob::NAME>::ValueType>(::knob_loader(), #NAME, DEFAULT); \
117 return value; \
118 }
119#include "Knob.lst"
120#undef KNOB_BOOL
121
122} // namespace tf
123} // namespace moco
Definition Log.h:23
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.