@@ -28,31 +28,60 @@ class Config:
28
28
This configs must be installed with install_config_module to be used
29
29
30
30
Precedence Order:
31
+ env_name_force: If set, this environment variable overrides everything
31
32
user_override: If a user sets a value (i.e. foo.bar=True), that
32
- has the highest precendance and is always respected
33
+ has precedence over everything after this.
34
+ env_name_default: If set, this environment variable will override everything
35
+ after this.
33
36
justknob: If this pytorch installation supports justknobs, that will
34
37
override defaults, but will not override the user_override precendence.
35
38
default: This value is the lowest precendance, and will be used if nothing is
36
39
set.
37
40
41
+ Environment Variables:
42
+ These are interpreted to be either "0" or "1" to represent true and false.
43
+
38
44
Arguments:
39
45
justknob: the name of the feature / JK. In OSS this is unused.
40
46
default: is the value to default this knob to in OSS.
47
+ env_name_force: The environment variable to read that is a FORCE
48
+ environment variable. I.e. it overrides everything
49
+ env_name_default: The environment variable to read that changes the
50
+ default behaviour. I.e. user overrides take preference.
41
51
"""
42
52
43
53
default : Any = True
44
54
justknob : Optional [str ] = None
55
+ env_name_default : Optional [str ] = None
56
+ env_name_force : Optional [str ] = None
45
57
46
- def __init__ (self , default : Any = True , justknob : Optional [str ] = None ):
58
+ def __init__ (
59
+ self ,
60
+ default : Any = True ,
61
+ justknob : Optional [str ] = None ,
62
+ env_name_default : Optional [str ] = None ,
63
+ env_name_force : Optional [str ] = None ,
64
+ ):
47
65
# python 3.9 does not support kw_only on the dataclass :(.
48
66
self .default = default
49
67
self .justknob = justknob
68
+ self .env_name_default = env_name_default
69
+ self .env_name_force = env_name_force
50
70
51
71
52
72
# Types saved/loaded in configs
53
73
CONFIG_TYPES = (int , float , bool , type (None ), str , list , set , tuple , dict )
54
74
55
75
76
+ def _read_env_variable (name : str ) -> Optional [bool ]:
77
+ value = os .environ .get (name )
78
+ if value == "1" :
79
+ return True
80
+ if value == "0" :
81
+ return False
82
+ return None
83
+
84
+
56
85
def install_config_module (module : ModuleType ) -> None :
57
86
"""
58
87
Converts a module-level config into a `ConfigModule()`.
@@ -87,6 +116,7 @@ def visit(
87
116
delattr (module , key )
88
117
elif isinstance (value , Config ):
89
118
config [name ] = _ConfigEntry (value )
119
+
90
120
if dest is module :
91
121
delattr (module , key )
92
122
elif isinstance (value , type ):
@@ -167,10 +197,19 @@ class _ConfigEntry:
167
197
user_override : Any = _UNSET_SENTINEL
168
198
# The justknob to check for this config
169
199
justknob : Optional [str ] = None
200
+ # environment variables are read at install time
201
+ env_value_force : Any = _UNSET_SENTINEL
202
+ env_value_default : Any = _UNSET_SENTINEL
170
203
171
204
def __init__ (self , config : Config ):
172
205
self .default = config .default
173
206
self .justknob = config .justknob
207
+ if config .env_name_default is not None :
208
+ if (env_value := _read_env_variable (config .env_name_default )) is not None :
209
+ self .env_value_default = env_value
210
+ if config .env_name_force is not None :
211
+ if (env_value := _read_env_variable (config .env_name_force )) is not None :
212
+ self .env_value_force = env_value
174
213
175
214
176
215
class ConfigModule (ModuleType ):
@@ -202,9 +241,16 @@ def __setattr__(self, name: str, value: object) -> None:
202
241
def __getattr__ (self , name : str ) -> Any :
203
242
try :
204
243
config = self ._config [name ]
244
+
245
+ if config .env_value_force is not _UNSET_SENTINEL :
246
+ return config .env_value_force
247
+
205
248
if config .user_override is not _UNSET_SENTINEL :
206
249
return config .user_override
207
250
251
+ if config .env_value_default is not _UNSET_SENTINEL :
252
+ return config .env_value_default
253
+
208
254
if config .justknob is not None :
209
255
# JK only supports bools and ints
210
256
return justknobs_check (name = config .justknob , default = config .default )
0 commit comments