Skip to content

Commit 714f643

Browse files
Revert "Add multi env variable support to configs (pytorch#145288)"
This reverts commit a8b7cb6. Reverted pytorch#145288 on behalf of https://github.com/huydhn due to Sorry for reverting your change but it is failing lint from a landrace with some recent PEP585 changes ([comment](pytorch#145288 (comment)))
1 parent 6a2b4db commit 714f643

File tree

3 files changed

+17
-47
lines changed

3 files changed

+17
-47
lines changed

test/test_utils_config_module.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ def test_env_name_semantics(self):
100100
config.e_env_force = False
101101
self.assertTrue(config.e_env_force)
102102

103-
def test_multi_env(self):
104-
self.assertTrue(config2.e_env_default_multi)
105-
self.assertTrue(config2.e_env_force_multi)
106-
107103
def test_save_config(self):
108104
p = config.save_config()
109105
self.assertDictEqual(
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import sys
22

3-
from torch.utils._config_module import Config, install_config_module
3+
from torch.utils._config_module import install_config_module
44

55

66
e_aliasing_bool = False
77

8-
e_env_default_multi: bool = Config(
9-
env_name_default=["ENV_TRUE", "ENV_FALSE"], default=False
10-
)
11-
e_env_force_multi: bool = Config(env_name_force=["ENV_FAKE", "ENV_TRUE"], default=False)
12-
138
install_config_module(sys.modules[__name__])

torch/utils/_config_module.py

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,10 @@ class _Config(Generic[T]):
5353
alias: If set, the directly use the value of the alias.
5454
env_name_force: If set, this environment variable has precedence over
5555
everything after this.
56-
If multiple env variables are given, the precendence order is from
57-
left to right.
5856
user_override: If a user sets a value (i.e. foo.bar=True), that
5957
has precedence over everything after this.
6058
env_name_default: If set, this environment variable will override everything
6159
after this.
62-
If multiple env variables are given, the precendence order is from
63-
left to right.
6460
justknob: If this pytorch installation supports justknobs, that will
6561
override defaults, but will not override the user_override precendence.
6662
default: This value is the lowest precendance, and will be used if nothing is
@@ -73,35 +69,33 @@ class _Config(Generic[T]):
7369
justknob: the name of the feature / JK. In OSS this is unused.
7470
default: is the value to default this knob to in OSS.
7571
alias: The alias config to read instead.
76-
env_name_force: The environment variable, or list of, to read that is a FORCE
72+
env_name_force: The environment variable to read that is a FORCE
7773
environment variable. I.e. it overrides everything except for alias.
78-
env_name_default: The environment variable, or list of, to read that changes the
74+
env_name_default: The environment variable to read that changes the
7975
default behaviour. I.e. user overrides take preference.
8076
"""
8177

8278
default: Union[T, object]
8379
justknob: Optional[str] = None
84-
env_name_default: Optional[List[str]] = None
85-
env_name_force: Optional[List[str]] = None
80+
env_name_default: Optional[str] = None
81+
env_name_force: Optional[str] = None
8682
value_type: Optional[type] = None
8783
alias: Optional[str] = None
8884

8985
def __init__(
9086
self,
9187
default: Union[T, object] = _UNSET_SENTINEL,
9288
justknob: Optional[str] = None,
93-
env_name_default: Optional[Union[str, List[str]]] = None,
94-
env_name_force: Optional[Union[str, List[str]]] = None,
89+
env_name_default: Optional[str] = None,
90+
env_name_force: Optional[str] = None,
9591
value_type: Optional[type] = None,
9692
alias: Optional[str] = None,
9793
):
9894
# python 3.9 does not support kw_only on the dataclass :(.
9995
self.default = default
10096
self.justknob = justknob
101-
self.env_name_default = _Config.string_or_list_of_string_to_list(
102-
env_name_default
103-
)
104-
self.env_name_force = _Config.string_or_list_of_string_to_list(env_name_force)
97+
self.env_name_default = env_name_default
98+
self.env_name_force = env_name_force
10599
self.value_type = value_type
106100
self.alias = alias
107101
if self.justknob is not None:
@@ -116,17 +110,6 @@ def __init__(
116110
and env_name_force is None
117111
), "if alias is set, default, justknob or env var cannot be set"
118112

119-
@staticmethod
120-
def string_or_list_of_string_to_list(
121-
val: Optional[Union[str, List[str]]]
122-
) -> Optional[List[str]]:
123-
if val is None:
124-
return None
125-
if isinstance(val, str):
126-
return [val]
127-
assert isinstance(val, list)
128-
return val
129-
130113

131114
# In runtime, we unbox the Config[T] to a T, but typechecker cannot see this,
132115
# so in order to allow for this dynamic behavior to work correctly with
@@ -137,8 +120,8 @@ def string_or_list_of_string_to_list(
137120
def Config(
138121
default: Union[T, object] = _UNSET_SENTINEL,
139122
justknob: Optional[str] = None,
140-
env_name_default: Optional[Union[str, List[str]]] = None,
141-
env_name_force: Optional[Union[str, List[str]]] = None,
123+
env_name_default: Optional[str] = None,
124+
env_name_force: Optional[str] = None,
142125
value_type: Optional[type] = None,
143126
alias: Optional[str] = None,
144127
) -> T:
@@ -149,8 +132,8 @@ def Config(
149132
def Config(
150133
default: Union[T, object] = _UNSET_SENTINEL,
151134
justknob: Optional[str] = None,
152-
env_name_default: Optional[Union[str, List[str]]] = None,
153-
env_name_force: Optional[Union[str, List[str]]] = None,
135+
env_name_default: Optional[str] = None,
136+
env_name_force: Optional[str] = None,
154137
value_type: Optional[type] = None,
155138
alias: Optional[str] = None,
156139
) -> _Config[T]:
@@ -317,15 +300,11 @@ def __init__(self, config: _Config):
317300
self.justknob = config.justknob
318301
self.alias = config.alias
319302
if config.env_name_default is not None:
320-
for val in config.env_name_default:
321-
if (env_value := _read_env_variable(val)) is not None:
322-
self.env_value_default = env_value
323-
break
303+
if (env_value := _read_env_variable(config.env_name_default)) is not None:
304+
self.env_value_default = env_value
324305
if config.env_name_force is not None:
325-
for val in config.env_name_force:
326-
if (env_value := _read_env_variable(val)) is not None:
327-
self.env_value_force = env_value
328-
break
306+
if (env_value := _read_env_variable(config.env_name_force)) is not None:
307+
self.env_value_force = env_value
329308

330309

331310
class ConfigModule(ModuleType):

0 commit comments

Comments
 (0)