trainloop 0.10.0__tar.gz → 0.12.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.
- {trainloop-0.10.0 → trainloop-0.12.0}/PKG-INFO +1 -1
- {trainloop-0.10.0 → trainloop-0.12.0}/pyproject.toml +1 -1
- {trainloop-0.10.0 → trainloop-0.12.0}/pyproject.toml.orig +1 -1
- {trainloop-0.10.0 → trainloop-0.12.0}/src/trainloop/__init__.py +2 -0
- {trainloop-0.10.0 → trainloop-0.12.0}/src/trainloop/hooks.py +75 -8
- {trainloop-0.10.0 → trainloop-0.12.0}/src/trainloop/trainer.py +3 -1
- {trainloop-0.10.0 → trainloop-0.12.0}/README.md +0 -0
- {trainloop-0.10.0 → trainloop-0.12.0}/src/trainloop/py.typed +0 -0
- {trainloop-0.10.0 → trainloop-0.12.0}/src/trainloop/utils.py +0 -0
|
@@ -4,6 +4,7 @@ from .hooks import (
|
|
|
4
4
|
CUDAMaxMemoryHook,
|
|
5
5
|
EMAHook,
|
|
6
6
|
ImageFileLoggerHook,
|
|
7
|
+
JSONLHook,
|
|
7
8
|
ProgressHook,
|
|
8
9
|
StatsHook,
|
|
9
10
|
TensorBoardHook,
|
|
@@ -19,6 +20,7 @@ __all__ = [
|
|
|
19
20
|
"CheckpointingHook",
|
|
20
21
|
"EMAHook",
|
|
21
22
|
"ImageFileLoggerHook",
|
|
23
|
+
"JSONLHook",
|
|
22
24
|
"LossNoneWarning",
|
|
23
25
|
"ProgressHook",
|
|
24
26
|
"StatsHook",
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import math
|
|
1
3
|
import os
|
|
2
4
|
import shutil
|
|
3
5
|
import signal
|
|
@@ -590,15 +592,20 @@ class EMAHook(BaseHook):
|
|
|
590
592
|
Args:
|
|
591
593
|
decay: EMA decay rate.
|
|
592
594
|
use_buffers: Whether to include model buffers in the EMA.
|
|
595
|
+
name: Name used for the EMA state in the trainer state dict.
|
|
593
596
|
"""
|
|
594
597
|
|
|
595
|
-
def __init__(
|
|
598
|
+
def __init__(
|
|
599
|
+
self, decay: float = 0.999, use_buffers: bool = False, name: str = "ema"
|
|
600
|
+
):
|
|
596
601
|
self.decay = decay
|
|
597
602
|
self.use_buffers = use_buffers
|
|
603
|
+
self.name = name
|
|
598
604
|
|
|
599
605
|
def on_before_train(self, trainer: BaseTrainer):
|
|
600
|
-
trainer.logger.info("=> Creating EMA model ...")
|
|
601
|
-
#
|
|
606
|
+
trainer.logger.info(f"=> Creating EMA model {self.name!r} ...")
|
|
607
|
+
# NOTE: AveragedModel does not seem to support FSDP. It will crash here.
|
|
608
|
+
# NOTE: AveragedModel's deepcopy clears _compiled_call_impl; compiled trainer.model does not imply compiled ema_model.
|
|
602
609
|
self.ema_model = AveragedModel(
|
|
603
610
|
trainer.model,
|
|
604
611
|
multi_avg_fn=get_ema_multi_avg_fn(self.decay),
|
|
@@ -610,10 +617,8 @@ class EMAHook(BaseHook):
|
|
|
610
617
|
self.ema_model.update_parameters(trainer.model)
|
|
611
618
|
|
|
612
619
|
def on_load_state_dict(self, trainer: BaseTrainer, state_dict: dict):
|
|
613
|
-
trainer.logger.info("=> Loading EMA model state ...")
|
|
614
|
-
incompatible_keys = set_model_state_dict(
|
|
615
|
-
self.ema_model, state_dict["ema_model"]
|
|
616
|
-
)
|
|
620
|
+
trainer.logger.info(f"=> Loading EMA model {self.name!r} state ...")
|
|
621
|
+
incompatible_keys = set_model_state_dict(self.ema_model, state_dict[self.name])
|
|
617
622
|
# This currently doesn't do anything because strict=True is implicit.
|
|
618
623
|
log_state_dict_incompatible_keys(
|
|
619
624
|
trainer.logger,
|
|
@@ -622,8 +627,10 @@ class EMAHook(BaseHook):
|
|
|
622
627
|
)
|
|
623
628
|
|
|
624
629
|
def on_state_dict(self, trainer: BaseTrainer, state_dict: dict):
|
|
630
|
+
if self.name in state_dict:
|
|
631
|
+
raise ValueError(f"State dict key {self.name!r} already exists")
|
|
625
632
|
# Note: sadly, we need to keep the AveragedModel wrapper, to save its n_averaged buffer
|
|
626
|
-
state_dict[
|
|
633
|
+
state_dict[self.name] = get_model_state_dict(self.ema_model)
|
|
627
634
|
|
|
628
635
|
|
|
629
636
|
class WandbHook(BaseHook):
|
|
@@ -855,6 +862,66 @@ class TensorBoardHook(BaseHook):
|
|
|
855
862
|
return img.convert("RGB")
|
|
856
863
|
|
|
857
864
|
|
|
865
|
+
class JSONLHook(BaseHook):
|
|
866
|
+
"""Log nested scalar records to a JSONL file (rank 0 only)."""
|
|
867
|
+
|
|
868
|
+
def __init__(self, filename: str = "metrics.jsonl"):
|
|
869
|
+
self.filename = filename
|
|
870
|
+
self.file = None
|
|
871
|
+
|
|
872
|
+
def on_before_train(self, trainer: BaseTrainer):
|
|
873
|
+
if _dist_rank() == 0:
|
|
874
|
+
path = trainer.workspace / self.filename
|
|
875
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
876
|
+
self.file = path.open("a", encoding="utf-8")
|
|
877
|
+
|
|
878
|
+
def on_after_train(self, trainer: BaseTrainer):
|
|
879
|
+
if self.file is None:
|
|
880
|
+
return
|
|
881
|
+
|
|
882
|
+
self.file.close()
|
|
883
|
+
self.file = None
|
|
884
|
+
|
|
885
|
+
def on_log_scalars(
|
|
886
|
+
self, trainer: BaseTrainer, records: dict, dry_run: bool = False
|
|
887
|
+
):
|
|
888
|
+
if self.file is None:
|
|
889
|
+
return
|
|
890
|
+
|
|
891
|
+
data, non_finite = self._split_non_finite(records)
|
|
892
|
+
event = {
|
|
893
|
+
"step": trainer.step,
|
|
894
|
+
"wall_time": time.time(),
|
|
895
|
+
"data": data,
|
|
896
|
+
}
|
|
897
|
+
if non_finite:
|
|
898
|
+
event["non_finite"] = non_finite
|
|
899
|
+
if dry_run:
|
|
900
|
+
trainer.logger.debug(f"Dry run log. Would log: {event}")
|
|
901
|
+
return
|
|
902
|
+
|
|
903
|
+
self.file.write(
|
|
904
|
+
json.dumps(event, separators=(",", ":"), allow_nan=False) + "\n"
|
|
905
|
+
)
|
|
906
|
+
self.file.flush()
|
|
907
|
+
|
|
908
|
+
@classmethod
|
|
909
|
+
def _split_non_finite(cls, value):
|
|
910
|
+
if isinstance(value, dict):
|
|
911
|
+
data = {}
|
|
912
|
+
non_finite = {}
|
|
913
|
+
for k, v in value.items():
|
|
914
|
+
data[k], non_finite_value = cls._split_non_finite(v)
|
|
915
|
+
if non_finite_value:
|
|
916
|
+
non_finite[k] = non_finite_value
|
|
917
|
+
return data, non_finite
|
|
918
|
+
if isinstance(value, float) and not math.isfinite(value):
|
|
919
|
+
return None, (
|
|
920
|
+
"NaN" if math.isnan(value) else "Infinity" if value > 0 else "-Infinity"
|
|
921
|
+
)
|
|
922
|
+
return value, None
|
|
923
|
+
|
|
924
|
+
|
|
858
925
|
class ImageFileLoggerHook(BaseHook):
|
|
859
926
|
"""Persist logged images to ``workspace/visualizations`` on rank 0.
|
|
860
927
|
|
|
@@ -359,7 +359,9 @@ class BaseTrainer:
|
|
|
359
359
|
nested paths may otherwise map to the same backend metric name.
|
|
360
360
|
|
|
361
361
|
Args:
|
|
362
|
-
records: Nested dict
|
|
362
|
+
records: Nested dict with string keys and Python ``int`` or ``float``
|
|
363
|
+
metric values. Convert numeric NumPy scalars and scalar PyTorch
|
|
364
|
+
tensors with ``.item()`` before logging.
|
|
363
365
|
dry_run: If True, hooks should avoid side effects and only report intent.
|
|
364
366
|
"""
|
|
365
367
|
self.logger.debug("log_scalars()")
|
|
File without changes
|
|
File without changes
|
|
File without changes
|