Skip to content
Merged
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
2 changes: 2 additions & 0 deletions dgf/src/learning/ten_lines/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ py_test(
":common",
# absl/testing:absltest dep,
# absl/testing:parameterized dep,
"//dgf/src/util:log",
# jax dep,
# numpy dep,
],
)
Expand Down
18 changes: 18 additions & 0 deletions dgf/src/learning/ten_lines/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,21 @@ def check_number_of_seeds(
f" than the batch size ({batch_size}). Increase the number of"
f" validation seed {key}s or decrease the batch size."
)


def log_jax_backend(verbose: int = 2) -> None:
"""Logs the active JAX backend and issues a warning if running on CPU.

Args:
verbose: The verbosity level. If >= 2, logs an info message with the
backend.
"""
backend = jax.default_backend()
if verbose >= 2:
log.info("Using %s JAX backend", backend)

if backend.lower() == "cpu":
log.warning(
"Using CPU JAX backend. Training will be slow. Consider using a GPU"
" or TPU."
)
20 changes: 20 additions & 0 deletions dgf/src/learning/ten_lines/common_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from absl.testing import absltest
from absl.testing import parameterized
from dgf.src.learning.ten_lines import common
from dgf.src.util import log
import jax
import numpy as np


Expand Down Expand Up @@ -125,6 +127,24 @@ def test_check_number_of_seeds_insufficient_validation_fails(self):
batch_size=10, num_training=15, num_validation=5, key="edge"
)

def test_log_jax_backend(self):
with log.capture_logs(log_info=True, log_warning=True) as captured:
common.log_jax_backend(verbose=2)
self.assertTrue(
any(
"Using" in msg.text and "JAX backend" in msg.text
for msg in captured
)
)
if jax.default_backend().lower() == "cpu":
self.assertTrue(
any(
msg.severity == log.Severity.WARNING
and "Using CPU JAX backend" in msg.text
for msg in captured
)
)


if __name__ == "__main__":
absltest.main()
3 changes: 1 addition & 2 deletions dgf/src/learning/ten_lines/link_prediction_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,7 @@ def train_link_model(
if diagnostic_dir is not None:
fs.makedirs(diagnostic_dir)

if verbose >= 2:
log.info("Using %s JAX backend", jax.default_backend())
common.log_jax_backend(verbose)

if target_edgeset is None:
if len(schema.edge_sets) == 1:
Expand Down
3 changes: 1 addition & 2 deletions dgf/src/learning/ten_lines/node_prediction_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,7 @@ def train_node_model(
if diagnostic_dir is not None:
fs.makedirs(diagnostic_dir)

if verbose >= 2:
log.info("Using %s JAX backend", jax.default_backend())
common.log_jax_backend(verbose)

if target_nodeset is None:
if len(schema.node_sets) == 1:
Expand Down