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
9 changes: 7 additions & 2 deletions vit_jax/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
from vit_jax import utils


def _should_checkpoint(config, step, total_steps):
"""Returns whether a checkpoint should be stored at the current step."""
return (step == total_steps or
bool(config.checkpoint_every and step % config.checkpoint_every == 0))


def make_update_fn(*, apply_fn, accum_steps, tx):
"""Returns update step for data parallel training."""

Expand Down Expand Up @@ -239,8 +245,7 @@ def init_model():
img_sec_core_test=img_sec_core_test))

# Store checkpoint.
if ((config.checkpoint_every and step % config.eval_every == 0) or
step == total_steps):
if _should_checkpoint(config, step, total_steps):
checkpoint_path = flax_checkpoints.save_checkpoint(
workdir, (flax.jax_utils.unreplicate(params_repl),
flax.jax_utils.unreplicate(opt_state_repl), step), step)
Expand Down
15 changes: 15 additions & 0 deletions vit_jax/train_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@

class TrainTest(parameterized.TestCase):

@parameterized.named_parameters(
('configured_interval', 6, 10, 3, True),
('between_intervals', 4, 10, 3, False),
('disabled', 4, 10, 0, False),
('final_step_when_disabled', 10, 10, 0, True),
)
def test_should_checkpoint(self, step, total_steps, checkpoint_every,
expected):
config = ml_collections.ConfigDict({
'checkpoint_every': checkpoint_every,
'eval_every': 2,
})
self.assertEqual(
train._should_checkpoint(config, step, total_steps), expected)

@parameterized.named_parameters(
('tfds', 'tfds'),
('directory', 'directory'),
Expand Down