-
Notifications
You must be signed in to change notification settings - Fork 2k
[knobs] Fix environment propagation & scope() API #6664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danzimm
wants to merge
5
commits into
triton-lang:main
Choose a base branch
from
danzimm:danzimm/fixknobsscope
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ba07b01
[knobs] Fix propagating env vars to os.environ
d4c1b7f
[knobs] Fix scope() not resetting env
danzimm 910fcbe
Unset previously unset env when specifying fresh_config
danzimm 45815b1
Merge remote-tracking branch 'origin/main' into danzimm/fixknobsscope
danzimm 3b52133
Remove set(), implement toenv() + use in __set__, fix tests
danzimm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,12 +22,41 @@ class Env: | |
|
||
env = Env() | ||
|
||
propagate_env: bool = True | ||
|
||
|
||
def getenv(key: str) -> Optional[str]: | ||
res = os.getenv(key) | ||
return res.strip() if res is not None else res | ||
|
||
|
||
def setenv(key: str, value: Optional[str]) -> None: | ||
if not propagate_env: | ||
return | ||
|
||
if value is not None: | ||
os.environ[key] = value | ||
elif key in os.environ: | ||
del os.environ[key] | ||
|
||
|
||
def toenv(val: Any) -> Union[None, tuple[Optional[str]]]: | ||
if val is None: | ||
return (None, ) | ||
|
||
t = type(val) | ||
if t is bool: | ||
return ("1" if val else "0", ) | ||
|
||
if t is str: | ||
return (val, ) | ||
|
||
if t is int: | ||
return (str(val), ) | ||
|
||
return None | ||
|
||
|
||
# There's an asymmetry here so that e.g. env_nvidia_tool can be specified with a | ||
# a string but return an NvidiaTool. | ||
SetType = TypeVar("SetType") | ||
|
@@ -52,16 +81,21 @@ def __get__(self, obj: Optional[object], objclass: Optional[Type[object]]) -> Ge | |
else: | ||
return self.get() | ||
|
||
@property | ||
def env_val(self) -> str | None: | ||
return getenv(self.key) | ||
|
||
def get(self) -> GetType: | ||
env = getenv(self.key) | ||
env = self.env_val | ||
return self.transform(self.default() if env is None else self.from_env(env)) | ||
|
||
def __set__(self, obj: object, value: Union[SetType, Env]) -> None: | ||
if isinstance(value, Env): | ||
obj.__dict__.pop(self.name, None) | ||
else: | ||
obj.__dict__[self.name] = value | ||
self.set(value) | ||
if env_val := toenv(value): | ||
setenv(self.key, env_val[0]) | ||
Comment on lines
+97
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Almost exactly the same impl you gave @peterbell10 , but needed a function to convert the value (e.g. "1" instead of "True" and to ignore when e.g. value is a class for |
||
|
||
def __delete__(self, obj: object) -> None: | ||
obj.__dict__.pop(self.name, None) | ||
|
@@ -71,21 +105,12 @@ def transform(self, val: SetType) -> GetType: | |
# if GetType != SetType. | ||
return cast(GetType, val) | ||
|
||
def set(self, val: SetType) -> None: | ||
pass | ||
|
||
def from_env(self, val: str) -> SetType: | ||
raise NotImplementedError() | ||
|
||
|
||
class env_str(env_base[str, str]): | ||
|
||
def set(self, value: Optional[str]) -> None: | ||
if value is None: | ||
os.unsetenv(self.key) | ||
else: | ||
os.putenv(self.key, value) | ||
|
||
def from_env(self, val: str) -> str: | ||
return val | ||
|
||
|
@@ -253,8 +278,7 @@ def knobs(self) -> dict[str, Any]: | |
|
||
def copy(self: knobs_type) -> knobs_type: | ||
res = type(self)() | ||
for k, v in self.__dict__.items(): | ||
res.__dict__[k] = v | ||
res.__dict__.update(self.__dict__) | ||
return res | ||
|
||
def reset(self: knobs_type) -> knobs_type: | ||
|
@@ -265,12 +289,19 @@ def reset(self: knobs_type) -> knobs_type: | |
@contextmanager | ||
def scope(self) -> Generator[None, None, None]: | ||
try: | ||
initial_env = {knob.key: knob.env_val for knob in self.knob_descriptors.values()} | ||
orig = dict(self.__dict__) | ||
yield | ||
finally: | ||
self.__dict__.clear() | ||
self.__dict__.update(orig) | ||
|
||
for k, v in initial_env.items(): | ||
if v is not None: | ||
os.environ[k] = v | ||
elif k in os.environ: | ||
del os.environ[k] | ||
|
||
|
||
class build_knobs(base_knobs): | ||
"""Configuration controlling how the native compiler is invoked""" | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CliveUnger I think nixing this assert should address your concern, since everything else is just validating Paths/strings (follow up from here for any other readers).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea this looks good!