forked from kaldi-asr/kaldi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.h
More file actions
109 lines (86 loc) · 3.82 KB
/
config.h
File metadata and controls
109 lines (86 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// tensor/config.h
// Copyright 2019 Johns Hopkins University (author: Daniel Povey)
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#ifndef KALDI_TENSOR_CONFIG_H_
#define KALDI_TENSOR_CONFIG_H_ 1
#include <string>
#include "util/text-utils.h"
namespace kaldi {
namespace tensor {
/**
This Config class is used when we want to store configuration information
inside Variables (e.g., to set per-parameter learning rates).
We'll eventually need mechanisms to read and write this.
*/
class Config {
public:
/**
This template will be defined only for types
`T = {std::string, bool, int32, float }`.
@param [in] key The name of the config parameter we are setting,
e.g. "learning-rate". Must satisfy IsValidName(key),
i.e. starts with `[a-zA-Z]`, and contains only characters
`[a-zA-Z0-9_-]`.
@param [in] value The value to be set, of type string, bool, int32 or
float. Any previous value (of whatever type) set for this
key will be overwritten.
*/
template<typename T> void SetValue(const std::string &key,
const T &value);
/**
This template will be defined only for types
`T = {std::string, bool, int32, float }`.
@param [in] key The name of the config parameter we are querying,
e.g. "learning-rate". Must satisfy IsValidName(key),
i.e. starts with `[a-zA-Z]`, and contains only characters
`[a-zA-Z0-9_-]`.
@param [out] value The value to be set, of type string, bool, int32 or
float. If the key was not present in the map, we return
false and don't set `value`. If the key was present and
the value was of a compatible type, we set `value`.
If they key was present but the value was not of a
compatible type, we die with an error.
As for type compatibility: all types are compatible
with themselves, and the only automatic conversion we
do (so far) is from int to float. We may add more
conversions later if needed.
@return Returns true if the key was in the map and of a compatible
type, false if the key was not in the map. (Dies
if the key was present but with an incompatible type).
*/
template<typename T> bool GetValue(const std::string &key,
T *value);
private:
enum ValueType { kStringValue, kBoolValue, kIntValue, kFloatValue };
struct ConfigElement {
ValueType value_type;
std::string str;
union {
int32 i;
float f;
bool b;
} u;
};
// If we later end up storing many configuration value, we could change this
// to unordered_map, but in most cases it will only be one or two so that
// would be overkill.
std::map<std::string, ConfigElement> map_;
};
} // namespace tensor
} // namespace kaldi
// Include implementation of inline functions.
#include "tensor/config-inl.h"
#endif // KALDI_TENSOR_CONFIG_H_