Skip to content

[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
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

danzimm
Copy link
Contributor

@danzimm danzimm commented May 1, 2025

In the original PR introducing these knobs @peterbell10 raised a good point that each knob had a side effect so that there was no real way to reset them.

I thought I fixed this with the scope() API, but it turns out I didn't-- instead I actually just broke the environment propagation. Specifically I was using putenv and unsetenv to propagate values, but those don't update os.environ or the result of os.getenv.

I've added a test to verify that os.environ gets properly updated now, and addressed a bug in scope() which wasn't restoring the environment.

I also understand that env propagation might not be desirable to everyone (since it's a side effect), so I've added a flag to disable the feature.

@danzimm danzimm requested review from Jokeren and peterbell10 May 1, 2025 16:12
@danzimm danzimm requested a review from ptillet as a code owner May 1, 2025 16:12
os.unsetenv(self.key)
else:
os.putenv(self.key, value)
setenv(self.key, value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if propagate_env is False this doesn't set the value at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's right, that was intentional. In the least this is needed for testing, but I figured some users might want to opt out of the environment updates altogether (and just use the config classes as essentially a dataclass). Do you think that's ok? Or should we try to get rid of that flag (meaning I'd have to rethink the tests)?

Copy link
Contributor

@peterbell10 peterbell10 May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see there is __set__, set and setenv which all do subtly different things. Could __set__ just be:

    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
            setenv(self.key, repr(value) if value is not None else value)

and get rid of set altogether

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good idea I think so-- it might need to look slightly different because e.g. env_class would need a custom repr

Comment on lines +97 to +98
if env_val := toenv(value):
setenv(self.key, env_val[0])
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 env_class)

@@ -226,20 +245,29 @@ def test_nvidia_tool(fresh_knobs, tmp_path, monkeypatch):
triton_root = Path(fresh_knobs.__file__).parent
default_ptxas = triton_root / "backends/nvidia/bin/ptxas"

assert default_ptxas.exists()
Copy link
Contributor Author

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).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea this looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants