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
53 changes: 53 additions & 0 deletions recml/core/training/batch_callbacks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,59 @@ class _CustomSummary(keras_utils.EpochSummaryCallback):
custom = _CustomSummary(log_dir=self._tmpdir, steps_per_epoch=1)
self.assertEqual(batch_callbacks.without_batch_hooks([custom]), [custom])

def test_zero_sync_list_skips_pythonify_logs_for_train_throughput_callback(
self,
):
throughput_cb = keras_utils.TrainThroughputCallback(
global_batch_size=8,
padded_seq_len=1024,
valid_seq_len_key="labels/count_seq_len",
device_count=4,
)
trainer_callbacks = [
throughput_cb,
*batch_callbacks.without_batch_hooks(_train_callbacks(self._tmpdir)),
]
with mock.patch.object(batch_callbacks.logging, "info") as info_log:
batch_callbacks.ZeroSyncCallbackList(
trainer_callbacks,
add_history=True,
add_progbar=True,
verbose=1,
)
cb_list = batch_callbacks.ZeroSyncCallbackList(
[throughput_cb],
add_history=True,
add_progbar=True,
model=mock.Mock(),
verbose=1,
)
info_log.assert_not_called()

epoch_logs = {"labels/count_seq_len": 512.0}
with mock.patch.object(keras_utils.time, "time", side_effect=[0.0, 2.0]):
cb_list.on_train_begin()
cb_list.on_epoch_begin(0)
with mock.patch.object(
python_utils, "pythonify_logs", wraps=python_utils.pythonify_logs
) as pythonify:
cb_list.on_train_batch_end(0, logs={"loss": 1.0})
cb_list.on_train_batch_end(4, logs={"loss": 1.0})
pythonify.assert_not_called()
cb_list.on_epoch_end(0, logs=epoch_logs)

# 5 steps / 2.0 s / 4 devices = 0.625 steps/s/device -> 5 examples/s/device
# -> 2.56k valid tokens/s/device and 50% packing efficiency.
self.assertAlmostEqual(
throughput_cb.latest_metrics[
"throughput/k_valid_tokens_per_sec_per_device"
],
2.56,
)
self.assertAlmostEqual(
throughput_cb.latest_metrics["throughput/packing_efficiency_pct"], 50.0
)


if __name__ == "__main__":
absltest.main()
13 changes: 8 additions & 5 deletions recml/core/utils/keras_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2150,15 +2150,18 @@ class _TokenThroughputCallback(MetricsCallback):
startup or upon recovery after a preemption) therefore includes JIT
compilation time and reads lower than steady-state windows.

The callback never reads `logs` in its batch hooks and sets `async_safe`.
Thus Keras can keep dispatching batch callbacks asynchronously. Without
`async_safe`, one callback with a batch hook makes Keras convert all the
step logs to Python floats on the main thread on every step, which blocks
the accelerator.
The callback never reads `logs` in its batch hooks and sets `async_safe` and
`requires_batch_logs = False`. Thus Keras can keep dispatching batch callbacks
asynchronously, and `KerasTrainer(skip_batch_logs=True)` continues to skip
per-step host conversions of `logs`. Without `async_safe`, one callback with a
batch hook makes Keras convert all the step logs to Python floats on the main
thread on every step, which blocks the accelerator.

Subclasses select the Keras hooks that open and close the window.
"""

requires_batch_logs: bool = False

def __init__(
self,
*,
Expand Down
9 changes: 9 additions & 0 deletions recml/core/utils/keras_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2855,6 +2855,15 @@ def test_is_async_safe(self):

self.assertTrue(callback.async_safe)

def test_does_not_require_batch_logs(self):
# Otherwise `ZeroSyncCallbackList` (`skip_batch_logs=True`) converts the
# step logs to Python floats on every step.
train_cb = _throughput_callback(keras_utils.TrainThroughputCallback)
eval_cb = _throughput_callback(keras_utils.EvalThroughputCallback)

self.assertFalse(train_cb.requires_batch_logs)
self.assertFalse(eval_cb.requires_batch_logs)

def test_step_count_comes_from_keras_end_step(self):
# One batch end with end step 99 means that 100 steps completed. This is
# what happens when `steps_per_execution` is more than one.
Expand Down
Loading