trainloop 0.4.0__tar.gz → 0.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: trainloop
3
- Version: 0.4.0
3
+ Version: 0.5.0
4
4
  Summary: Minimal PyTorch training loop with hooks and checkpointing.
5
5
  Author: Karim Abou Zeid
6
6
  Author-email: Karim Abou Zeid <contact@ka.codes>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "trainloop"
3
- version = "0.4.0"
3
+ version = "0.5.0"
4
4
  description = "Minimal PyTorch training loop with hooks and checkpointing."
5
5
  readme = "README.md"
6
6
  authors = [
@@ -346,7 +346,8 @@ class CheckpointingHook(BaseHook):
346
346
  Args:
347
347
  interval: Save every ``interval`` steps.
348
348
  keep_previous: Keep the last N checkpoints in addition to the latest.
349
- keep_interval: Keep checkpoints every ``keep_interval`` steps.
349
+ keep_interval: Save and keep checkpoints every ``keep_interval`` steps.
350
+ keep_steps: Save and keep checkpoints at these explicit step numbers.
350
351
  path: Directory (relative to workspace unless absolute) for checkpoints.
351
352
  load: Path to load at startup or ``\"latest\"`` to auto-resume.
352
353
  exit_signals: Signals that trigger a checkpoint then exit.
@@ -358,7 +359,9 @@ class CheckpointingHook(BaseHook):
358
359
  self,
359
360
  interval: int,
360
361
  keep_previous: int = 0, # keep N previous checkpoints
361
- keep_interval: int = 0, # keep checkpoints of every N-th step
362
+ keep_interval: int = 0, # save and keep checkpoints of every N-th step
363
+ keep_steps: Sequence[int]
364
+ | None = None, # save and keep checkpoints at these steps
362
365
  path: Path | str = "checkpoints",
363
366
  load: Path | str | Literal["latest"] | None = "latest",
364
367
  exit_signals: list[signal.Signals] | signal.Signals = None,
@@ -370,6 +373,7 @@ class CheckpointingHook(BaseHook):
370
373
  self.interval = interval
371
374
  self.keep_previous = keep_previous
372
375
  self.keep_interval = keep_interval
376
+ self.keep_steps = set(keep_steps or [])
373
377
  self.path = Path(path)
374
378
  self.load_path = Path(load) if load is not None else None
375
379
 
@@ -435,18 +439,23 @@ class CheckpointingHook(BaseHook):
435
439
  save_and_exit = exit_signal != -1
436
440
 
437
441
  # NOTE: Check if last step here (not in on_after_train) to avoid saving twice
438
- if (
442
+ should_keep = trainer.step in self.keep_steps or (
443
+ self.keep_interval > 0 and trainer.step % self.keep_interval == 0
444
+ )
445
+ should_save = (
439
446
  trainer.step % self.interval == 0
440
447
  or trainer.step == trainer.max_steps
448
+ or should_keep
441
449
  or save_and_exit
442
- ):
450
+ )
451
+ if should_save:
443
452
  if save_and_exit:
444
453
  trainer.logger.info(
445
454
  f"=> Caught signal {exit_signal}. Saving checkpoint before exit ..."
446
455
  )
447
456
  self._save_checkpoint(
448
457
  trainer,
449
- keep=self.keep_interval > 0 and trainer.step % self.keep_interval == 0,
458
+ keep=should_keep,
450
459
  )
451
460
  if save_and_exit:
452
461
  _dist_barrier()
@@ -276,7 +276,9 @@ class BaseTrainer:
276
276
  < self.max_non_finite_grad_retries
277
277
  ):
278
278
  non_finite_grad_retry_count += 1
279
- self.step_info["non_finite_grad_retry_count"] = non_finite_grad_retry_count
279
+ self.step_info["non_finite_grad_retry_count"] = (
280
+ non_finite_grad_retry_count
281
+ )
280
282
  self.logger.warning(
281
283
  f"Gradient is non-finite. Retrying step {self.step} (retry {non_finite_grad_retry_count}"
282
284
  + (
File without changes