Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pathwaysutils/_initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def is_pathways_backend_used() -> bool:
is not necessarily which backend is ultimately selected or there may be more
than one platform specified and another may have higher priority.
"""
return jax.config.jax_platforms and "proxy" in jax.config.jax_platforms
return bool(
jax.config.jax_platforms and "proxy" in jax.config.jax_platforms
)


def _is_persistence_enabled() -> bool:
Expand Down
14 changes: 13 additions & 1 deletion pathwaysutils/proxy_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,19 @@ def make_client():
options = ifrt_proxy.ClientConnectionOptions()
timeout_secs = os.environ.get("PATHWAYS_PROXY_CONNECTION_TIMEOUT_SECS")
if timeout_secs:
options.connection_timeout_in_seconds = int(timeout_secs)
try:
timeout_val = int(timeout_secs)
except ValueError as e:
raise ValueError(
"PATHWAYS_PROXY_CONNECTION_TIMEOUT_SECS must be an integer, got:"
f" {timeout_secs}"
) from e
if timeout_val <= 0:
raise ValueError(
"PATHWAYS_PROXY_CONNECTION_TIMEOUT_SECS must be positive, got:"
f" {timeout_secs}"
)
options.connection_timeout_in_seconds = timeout_val
return ifrt_proxy.get_client(
jax.config.read("jax_backend_target"),
options,
Expand Down
6 changes: 4 additions & 2 deletions pathwaysutils/test/initialize_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,17 @@ def test_initialize_more_than_once(self, initialization_count):
)

@parameterized.named_parameters(
("none", None),
("empty", ""),
("cpu", "cpu"),
("tpu", "tpu"),
("gpu", "gpu"),
("cpu,tpu,gpu", "cpu,tpu,gpu"),
)
def test_not_is_pathways_backend_used(self, platform: str):
def test_not_is_pathways_backend_used(self, platform: str | None):
jax.config.update("jax_platforms", platform)
self.assertFalse(_initialize.is_pathways_backend_used())
result = _initialize.is_pathways_backend_used()
self.assertIs(result, False)

@parameterized.named_parameters(
("proxy", "proxy"),
Expand Down
12 changes: 12 additions & 0 deletions pathwaysutils/test/proxy_backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ def test_proxy_backend_registration_without_timeout(self):
options = args[1]
self.assertNotEqual(options.connection_timeout_in_seconds, 42)

def test_proxy_backend_registration_with_invalid_timeout(self):
self.enter_context(
mock.patch.dict(
os.environ, {"PATHWAYS_PROXY_CONNECTION_TIMEOUT_SECS": "-5"}
)
)
proxy_backend.register_backend_factory()
with self.assertRaisesRegex(
RuntimeError, "PATHWAYS_PROXY_CONNECTION_TIMEOUT_SECS must be positive"
):
backend.backends()


if __name__ == "__main__":
absltest.main()
Loading