kostyl-toolkit 0.1.3__py3-none-any.whl → 0.1.6__py3-none-any.whl
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.
- kostyl/ml_core/{configs/config_base.py → clearml/config_mixin.py} +6 -69
- kostyl/ml_core/clearml/dataset_utils.py +61 -0
- kostyl/ml_core/configs/__init__.py +1 -1
- kostyl/ml_core/configs/base.py +54 -0
- kostyl/ml_core/configs/hyperparams.py +2 -3
- kostyl/ml_core/configs/training_params.py +3 -3
- kostyl/ml_core/lightning/extenstions/pretrained_model.py +3 -3
- kostyl/utils/fs.py +19 -0
- {kostyl_toolkit-0.1.3.dist-info → kostyl_toolkit-0.1.6.dist-info}/METADATA +1 -1
- {kostyl_toolkit-0.1.3.dist-info → kostyl_toolkit-0.1.6.dist-info}/RECORD +11 -8
- {kostyl_toolkit-0.1.3.dist-info → kostyl_toolkit-0.1.6.dist-info}/WHEEL +1 -1
|
@@ -2,80 +2,17 @@ from pathlib import Path
|
|
|
2
2
|
from typing import TypeVar
|
|
3
3
|
|
|
4
4
|
import clearml
|
|
5
|
-
import yaml
|
|
6
5
|
from caseconverter import pascalcase
|
|
7
6
|
from caseconverter import snakecase
|
|
8
7
|
from pydantic import BaseModel as PydanticBaseModel
|
|
9
8
|
|
|
10
|
-
from kostyl.
|
|
11
|
-
from kostyl.utils import
|
|
9
|
+
from kostyl.ml_core.configs.base import ConfigLoadingMixin
|
|
10
|
+
from kostyl.utils.dict_manipulations import convert_to_flat_dict
|
|
11
|
+
from kostyl.utils.dict_manipulations import flattened_dict_to_nested
|
|
12
|
+
from kostyl.utils.fs import load_config
|
|
12
13
|
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
"""Load a configuration from file."""
|
|
16
|
-
if isinstance(path, str):
|
|
17
|
-
path = Path(path)
|
|
18
|
-
|
|
19
|
-
if not path.is_file():
|
|
20
|
-
raise ValueError(f"Config file {path} does not exist or is not a file.")
|
|
21
|
-
|
|
22
|
-
match path.suffix:
|
|
23
|
-
case ".yaml" | ".yml":
|
|
24
|
-
config = yaml.safe_load(path.open("r"))
|
|
25
|
-
case _:
|
|
26
|
-
raise ValueError(f"Unsupported config file format: {path.suffix}")
|
|
27
|
-
return config
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
TConfig = TypeVar("TConfig", bound=PydanticBaseModel)
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
class ConfigLoadingMixin:
|
|
34
|
-
"""Pydantic mixin class providing basic configuration loading functionality."""
|
|
35
|
-
|
|
36
|
-
@classmethod
|
|
37
|
-
def from_file(
|
|
38
|
-
cls: type[TConfig], # pyright: ignore
|
|
39
|
-
path: str | Path,
|
|
40
|
-
) -> TConfig:
|
|
41
|
-
"""
|
|
42
|
-
Create an instance of the class from a configuration file.
|
|
43
|
-
|
|
44
|
-
Args:
|
|
45
|
-
cls_: The class type to instantiate.
|
|
46
|
-
path (str | Path): Path to the configuration file.
|
|
47
|
-
|
|
48
|
-
Returns:
|
|
49
|
-
An instance of the class created from the configuration file.
|
|
50
|
-
|
|
51
|
-
"""
|
|
52
|
-
config = load_config(path)
|
|
53
|
-
instance = cls.model_validate(config)
|
|
54
|
-
return instance
|
|
55
|
-
|
|
56
|
-
@classmethod
|
|
57
|
-
def from_dict(
|
|
58
|
-
cls: type[TConfig], # pyright: ignore
|
|
59
|
-
state_dict: dict,
|
|
60
|
-
) -> TConfig:
|
|
61
|
-
"""
|
|
62
|
-
Creates an instance from a dictionary.
|
|
63
|
-
|
|
64
|
-
Args:
|
|
65
|
-
cls_: The class type to instantiate.
|
|
66
|
-
state_dict (dict): A dictionary representing the state of the
|
|
67
|
-
class that must be validated and used for initialization.
|
|
68
|
-
|
|
69
|
-
Returns:
|
|
70
|
-
An initialized instance of the class based on the
|
|
71
|
-
provided state dictionary.
|
|
72
|
-
|
|
73
|
-
"""
|
|
74
|
-
instance = cls.model_validate(state_dict)
|
|
75
|
-
return instance
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
TModel = TypeVar("TModel", bound="ClearMLBaseModel")
|
|
15
|
+
TModel = TypeVar("TModel", bound="_ClearMLBaseModel")
|
|
79
16
|
|
|
80
17
|
|
|
81
18
|
class ClearMLConfigMixin(ConfigLoadingMixin):
|
|
@@ -155,7 +92,7 @@ class ClearMLConfigMixin(ConfigLoadingMixin):
|
|
|
155
92
|
return model
|
|
156
93
|
|
|
157
94
|
|
|
158
|
-
class
|
|
95
|
+
class _ClearMLBaseModel(PydanticBaseModel, ClearMLConfigMixin):
|
|
159
96
|
"""A Pydantic model class with ClearML configuration loading and syncing functionality."""
|
|
160
97
|
|
|
161
98
|
pass
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
2
|
+
from concurrent.futures import as_completed
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from clearml import Dataset as ClearMLDataset
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def collect_clearml_datasets(
|
|
9
|
+
datasets_mapping: dict[str, str],
|
|
10
|
+
) -> dict[str, ClearMLDataset]:
|
|
11
|
+
"""
|
|
12
|
+
Collect ClearML datasets by dataset ID.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
datasets_mapping: Mapping where keys are human-readable names and values
|
|
16
|
+
are ClearML dataset IDs.
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
A mapping of dataset names to fetched `ClearMLDataset` instances.
|
|
20
|
+
|
|
21
|
+
"""
|
|
22
|
+
datasets_list = {}
|
|
23
|
+
for name, dataset_id in datasets_mapping.items():
|
|
24
|
+
clearml_dataset = ClearMLDataset.get(dataset_id, alias=name)
|
|
25
|
+
datasets_list[name] = clearml_dataset
|
|
26
|
+
return datasets_list
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def download_clearml_datasets(datasets_mapping: dict[str, ClearMLDataset]) -> None:
|
|
30
|
+
"""
|
|
31
|
+
Download all ClearML datasets in parallel.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
datasets_mapping: Mapping of dataset names to initialized
|
|
35
|
+
`ClearMLDataset` instances whose contents must be downloaded
|
|
36
|
+
locally.
|
|
37
|
+
|
|
38
|
+
"""
|
|
39
|
+
with ThreadPoolExecutor() as executor:
|
|
40
|
+
futures = [
|
|
41
|
+
executor.submit(ds.get_local_copy) for ds in datasets_mapping.values()
|
|
42
|
+
]
|
|
43
|
+
for future in as_completed(futures):
|
|
44
|
+
future.result()
|
|
45
|
+
return
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def get_datasets_paths(datasets_mapping: dict[str, ClearMLDataset]) -> dict[str, Path]:
|
|
49
|
+
"""
|
|
50
|
+
Return local filesystem paths for ClearML datasets.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
datasets_mapping: Mapping of dataset names to initialized
|
|
54
|
+
`ClearMLDataset` instances.
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
Mapping of dataset names to local `Path` objects pointing to the
|
|
58
|
+
downloaded dataset copies.
|
|
59
|
+
|
|
60
|
+
"""
|
|
61
|
+
return {name: Path(ds.get_local_copy()) for name, ds in datasets_mapping.items()}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import TypeVar
|
|
3
|
+
|
|
4
|
+
from pydantic import BaseModel as PydanticBaseModel
|
|
5
|
+
|
|
6
|
+
from kostyl.utils.fs import load_config
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
TConfig = TypeVar("TConfig", bound=PydanticBaseModel)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ConfigLoadingMixin:
|
|
13
|
+
"""Pydantic mixin class providing basic configuration loading functionality."""
|
|
14
|
+
|
|
15
|
+
@classmethod
|
|
16
|
+
def from_file(
|
|
17
|
+
cls: type[TConfig], # pyright: ignore
|
|
18
|
+
path: str | Path,
|
|
19
|
+
) -> TConfig:
|
|
20
|
+
"""
|
|
21
|
+
Create an instance of the class from a configuration file.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
cls_: The class type to instantiate.
|
|
25
|
+
path (str | Path): Path to the configuration file.
|
|
26
|
+
|
|
27
|
+
Returns:
|
|
28
|
+
An instance of the class created from the configuration file.
|
|
29
|
+
|
|
30
|
+
"""
|
|
31
|
+
config = load_config(path)
|
|
32
|
+
instance = cls.model_validate(config)
|
|
33
|
+
return instance
|
|
34
|
+
|
|
35
|
+
@classmethod
|
|
36
|
+
def from_dict(
|
|
37
|
+
cls: type[TConfig], # pyright: ignore
|
|
38
|
+
state_dict: dict,
|
|
39
|
+
) -> TConfig:
|
|
40
|
+
"""
|
|
41
|
+
Creates an instance from a dictionary.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
cls_: The class type to instantiate.
|
|
45
|
+
state_dict (dict): A dictionary representing the state of the
|
|
46
|
+
class that must be validated and used for initialization.
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
An initialized instance of the class based on the
|
|
50
|
+
provided state dictionary.
|
|
51
|
+
|
|
52
|
+
"""
|
|
53
|
+
instance = cls.model_validate(state_dict)
|
|
54
|
+
return instance
|
|
@@ -2,10 +2,9 @@ from pydantic import BaseModel
|
|
|
2
2
|
from pydantic import Field
|
|
3
3
|
from pydantic import model_validator
|
|
4
4
|
|
|
5
|
+
from kostyl.ml_core.clearml.config_mixin import ClearMLConfigMixin
|
|
5
6
|
from kostyl.utils.logging import setup_logger
|
|
6
7
|
|
|
7
|
-
from .config_base import ClearMLBaseModel
|
|
8
|
-
|
|
9
8
|
|
|
10
9
|
logger = setup_logger(fmt="only_message")
|
|
11
10
|
|
|
@@ -75,7 +74,7 @@ class WeightDecay(BaseModel):
|
|
|
75
74
|
return self
|
|
76
75
|
|
|
77
76
|
|
|
78
|
-
class HyperparamsConfig(
|
|
77
|
+
class HyperparamsConfig(BaseModel, ClearMLConfigMixin):
|
|
79
78
|
"""Model training hyperparameters configuration."""
|
|
80
79
|
|
|
81
80
|
grad_clip_val: float | None = Field(default=None, gt=0, validate_default=False)
|
|
@@ -3,10 +3,10 @@ from typing import Literal
|
|
|
3
3
|
from pydantic import BaseModel
|
|
4
4
|
from pydantic import Field
|
|
5
5
|
|
|
6
|
+
from kostyl.ml_core.clearml.config_mixin import ClearMLConfigMixin
|
|
6
7
|
from kostyl.utils.logging import setup_logger
|
|
7
8
|
|
|
8
|
-
from .
|
|
9
|
-
from .config_base import ConfigLoadingMixin
|
|
9
|
+
from .base import ConfigLoadingMixin
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
logger = setup_logger(fmt="only_message")
|
|
@@ -101,8 +101,8 @@ class TrainingParams(BaseModel, ConfigLoadingMixin):
|
|
|
101
101
|
|
|
102
102
|
|
|
103
103
|
class ClearMLTrainingParameters(
|
|
104
|
+
ClearMLConfigMixin,
|
|
104
105
|
TrainingParams,
|
|
105
|
-
ClearMLBaseModel,
|
|
106
106
|
):
|
|
107
107
|
"""Training parameters configuration with ClearML features support (config syncing, model identifiers tracking and etc)."""
|
|
108
108
|
|
|
@@ -27,7 +27,7 @@ class LightningCheckpointLoaderMixin(PreTrainedModel):
|
|
|
27
27
|
checkpoint_path: str | Path,
|
|
28
28
|
config_key: str = "config",
|
|
29
29
|
weights_prefix: str = "model.",
|
|
30
|
-
|
|
30
|
+
should_log_incompatible_keys: bool = True,
|
|
31
31
|
) -> TModelInstance:
|
|
32
32
|
"""
|
|
33
33
|
Load a model from a Lightning checkpoint file.
|
|
@@ -47,7 +47,7 @@ class LightningCheckpointLoaderMixin(PreTrainedModel):
|
|
|
47
47
|
Defaults to "config".
|
|
48
48
|
weights_prefix (str, optional): Prefix to strip from state dict keys. Defaults to "model.".
|
|
49
49
|
If not empty and doesn't end with ".", a "." is appended.
|
|
50
|
-
|
|
50
|
+
should_log_incompatible_keys (bool, optional): Whether to log incompatible keys. Defaults to True.
|
|
51
51
|
|
|
52
52
|
Returns:
|
|
53
53
|
TModelInstance: The loaded model instance.
|
|
@@ -110,6 +110,6 @@ class LightningCheckpointLoaderMixin(PreTrainedModel):
|
|
|
110
110
|
)
|
|
111
111
|
incompatible_keys["missing_keys"] = missing_keys
|
|
112
112
|
incompatible_keys["unexpected_keys"] = unexpected_keys
|
|
113
|
-
if
|
|
113
|
+
if should_log_incompatible_keys:
|
|
114
114
|
log_incompatible_keys(incompatible_keys=incompatible_keys, logger=logger)
|
|
115
115
|
return model
|
kostyl/utils/fs.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
import yaml
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def load_config(path: Path | str) -> dict:
|
|
7
|
+
"""Load a configuration from file."""
|
|
8
|
+
if isinstance(path, str):
|
|
9
|
+
path = Path(path)
|
|
10
|
+
|
|
11
|
+
if not path.is_file():
|
|
12
|
+
raise ValueError(f"Config file {path} does not exist or is not a file.")
|
|
13
|
+
|
|
14
|
+
match path.suffix:
|
|
15
|
+
case ".yaml" | ".yml":
|
|
16
|
+
config = yaml.safe_load(path.open("r"))
|
|
17
|
+
case _:
|
|
18
|
+
raise ValueError(f"Unsupported config file format: {path.suffix}")
|
|
19
|
+
return config
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
kostyl/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
2
|
kostyl/ml_core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
3
|
kostyl/ml_core/clearml/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
kostyl/ml_core/clearml/config_mixin.py,sha256=a3YO8kocbiJMSRKcnEIYZlHe_-jvozIh0t_CFcBd1MU,3610
|
|
5
|
+
kostyl/ml_core/clearml/dataset_utils.py,sha256=mkuXqLXjuxR2UUeVwV_NdToILOObOse88ItdfIyyx04,1812
|
|
4
6
|
kostyl/ml_core/clearml/logging_utils.py,sha256=GBjIIZbH_itd5sj7XpvxjkyZwxxGOpEcQ3BiWaJTyq8,1210
|
|
5
7
|
kostyl/ml_core/clearml/pulling_utils.py,sha256=Yf70ux8dS0_ENdvfbNQkXOrDxwd4ed2GnRCmOR2ppEk,3252
|
|
6
|
-
kostyl/ml_core/configs/__init__.py,sha256=
|
|
7
|
-
kostyl/ml_core/configs/
|
|
8
|
-
kostyl/ml_core/configs/hyperparams.py,sha256=
|
|
9
|
-
kostyl/ml_core/configs/training_params.py,sha256=
|
|
8
|
+
kostyl/ml_core/configs/__init__.py,sha256=AcLsl_aGnUuhEtNxfZWYbrKMfJrCAf47laPNeOKPfT4,889
|
|
9
|
+
kostyl/ml_core/configs/base.py,sha256=4trY_fO_VnguNrwT0BgSSoxmnG8BS_z9mEsgX6DmaNU,1467
|
|
10
|
+
kostyl/ml_core/configs/hyperparams.py,sha256=u-7FIM-cD3nz9Sycuvg7r0Vdiu4pduafiCoAxq8JK0s,3011
|
|
11
|
+
kostyl/ml_core/configs/training_params.py,sha256=vYZUcOPNQNGLoMD9_-RCO9t72D8z7AwUU5JumkncYBk,2618
|
|
10
12
|
kostyl/ml_core/dist_utils.py,sha256=G8atjzkRbXZZiZh9rdEYBmeXqX26rJdDDovft2n6xiU,3201
|
|
11
13
|
kostyl/ml_core/lightning/__init__.py,sha256=-F3JAyq8KU1d-nACWryGu8d1CbvWbQ1rXFdeRwfE2X8,175
|
|
12
14
|
kostyl/ml_core/lightning/callbacks/__init__.py,sha256=Vd-rozY4T9Prr3IMqbliXxj6sC6y9XsovHQqRwzc2HI,297
|
|
@@ -15,7 +17,7 @@ kostyl/ml_core/lightning/callbacks/early_stopping.py,sha256=nEj3OkMNJkpQzR6pt0Z0
|
|
|
15
17
|
kostyl/ml_core/lightning/callbacks/registry_uploading.py,sha256=1aqT38FVOMQo4JphXcyjyK3ZY6A6HF1JBOsKqYNXar8,4706
|
|
16
18
|
kostyl/ml_core/lightning/extenstions/__init__.py,sha256=OY6QGv1agYgqqKf1xJBrxgp_i8FunVfPzYezfaRrGXU,182
|
|
17
19
|
kostyl/ml_core/lightning/extenstions/custom_module.py,sha256=8Z9iT7kdfLkzBQ8nLaCIxolXytiQqFVRLDSaNACjfK8,6146
|
|
18
|
-
kostyl/ml_core/lightning/extenstions/pretrained_model.py,sha256=
|
|
20
|
+
kostyl/ml_core/lightning/extenstions/pretrained_model.py,sha256=67ihzuO9J5AHpuE4f4qzL8XGJnA6c4LYaDyhXAGYnaw,4671
|
|
19
21
|
kostyl/ml_core/lightning/loggers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
20
22
|
kostyl/ml_core/lightning/loggers/tb_logger.py,sha256=Zh9n-lLu-bXMld-FIUO3lJfCyDf0IQFhS3JVShDJmvg,937
|
|
21
23
|
kostyl/ml_core/lightning/steps_estimation.py,sha256=fTZ0IrUEZV3H6VYlx4GYn56oco56mMiB7FO9F0Z7qc4,1511
|
|
@@ -27,7 +29,8 @@ kostyl/ml_core/schedulers/composite.py,sha256=ee4xlMDMMtjKPkbTF2ue9GTr9DuGCGjZWf
|
|
|
27
29
|
kostyl/ml_core/schedulers/cosine.py,sha256=jufULVHn_L_ZZEc3ZTG3QCY_pc0jlAMH5Aw496T31jo,8203
|
|
28
30
|
kostyl/utils/__init__.py,sha256=hkpmB6c5pr4Ti5BshOROebb7cvjDZfNCw83qZ_FFKMM,240
|
|
29
31
|
kostyl/utils/dict_manipulations.py,sha256=e3vBicID74nYP8lHkVTQc4-IQwoJimrbFELy5uSF6Gk,1073
|
|
32
|
+
kostyl/utils/fs.py,sha256=gAQNIU4R_2DhwjgzOS8BOMe0gZymtY1eZwmdgOdDgqo,510
|
|
30
33
|
kostyl/utils/logging.py,sha256=3MvfDPArZhwakHu5nMlp_LpOsWg0E0SP26y41clsBtA,5232
|
|
31
|
-
kostyl_toolkit-0.1.
|
|
32
|
-
kostyl_toolkit-0.1.
|
|
33
|
-
kostyl_toolkit-0.1.
|
|
34
|
+
kostyl_toolkit-0.1.6.dist-info/WHEEL,sha256=AaqHSNJgTyoT6I9ETCXrbV_7cVSjA_q07lkDGeNjGdQ,79
|
|
35
|
+
kostyl_toolkit-0.1.6.dist-info/METADATA,sha256=S3AJNbTWSplf75-tGR-nGqNFubXyxJb4BLVYNvvE5Rk,4268
|
|
36
|
+
kostyl_toolkit-0.1.6.dist-info/RECORD,,
|