-
Notifications
You must be signed in to change notification settings - Fork 566
feat: make JAX optional with env variable control #9615
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
Closed
rajkthakur
wants to merge
3
commits into
pytorch:master
from
rajkthakur:rajkthakur/jax_import_behind_env_variable
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import os | ||
from absl.testing import absltest | ||
from unittest.mock import patch | ||
from torch_xla._internal.jax_workarounds import maybe_get_jax | ||
|
||
|
||
class TestJaxEnvVar(absltest.TestCase): | ||
|
||
def setUp(self): | ||
# Clean up environment | ||
if 'TORCH_XLA_ENABLE_JAX' in os.environ: | ||
del os.environ['TORCH_XLA_ENABLE_JAX'] | ||
|
||
def test_jax_enabled_attempts_import(self): | ||
os.environ['TORCH_XLA_ENABLE_JAX'] = '1' | ||
with patch( | ||
'torch_xla._internal.jax_workarounds.logging.warning') as mock_warn: | ||
result = maybe_get_jax() | ||
self.assertIsNone(result) | ||
mock_warn.assert_called_once() | ||
self.assertIn('JAX explicitly enabled but not installed', | ||
mock_warn.call_args[0][0]) | ||
|
||
def test_jax_unset_warns_with_guidance(self): | ||
"""Test that unset environment variable warns with guidance""" | ||
with patch( | ||
'torch_xla._internal.jax_workarounds.logging.warning') as mock_warn: | ||
result = maybe_get_jax() | ||
self.assertIsNone(result) | ||
mock_warn.assert_called_once() | ||
warning_msg = mock_warn.call_args[0][0] | ||
self.assertIn('You are trying to use a feature that requires JAX', | ||
warning_msg) | ||
self.assertIn('TORCH_XLA_ENABLE_JAX=1', warning_msg) | ||
self.assertIn('TORCH_XLA_ENABLE_JAX=0', warning_msg) | ||
|
||
def test_jax_disabled_values_silent(self): | ||
for val in ['0']: | ||
with self.subTest(val=val): | ||
os.environ['TORCH_XLA_ENABLE_JAX'] = val | ||
with patch( | ||
'torch_xla._internal.jax_workarounds.logging.warning') as mock_warn: | ||
result = maybe_get_jax() | ||
self.assertIsNone(result) | ||
mock_warn.assert_not_called() | ||
|
||
def test_jax_enabled_values(self): | ||
"""Test various values that enable JAX""" | ||
for val in ['1']: | ||
with self.subTest(val=val): | ||
os.environ['TORCH_XLA_ENABLE_JAX'] = val | ||
with patch( | ||
'torch_xla._internal.jax_workarounds.logging.warning') as mock_warn: | ||
result = maybe_get_jax() | ||
self.assertIsNone(result) | ||
mock_warn.assert_called_once() | ||
self.assertIn('JAX explicitly enabled but not installed', | ||
mock_warn.call_args[0][0]) | ||
|
||
|
||
if __name__ == '__main__': | ||
absltest.main() |
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
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.
Let's do something simpler.
Instead of the env variable, just remove this logging.
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.
We can probably fix the test if that's a concern? Or do you see some other issue?
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.
just the tests. Although I don't need the logging in this function, so it can be simplified if so.
i.e. it's reasonable to push the logging to the callsites of
maybe_get_jax
.