kostyl-toolkit 0.1.11__py3-none-any.whl → 0.1.12__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.
@@ -4,7 +4,6 @@ from .hyperparams import Lr
4
4
  from .hyperparams import Optimizer
5
5
  from .hyperparams import WeightDecay
6
6
  from .training_settings import CheckpointConfig
7
- from .training_settings import ClearMLTrainingSettings
8
7
  from .training_settings import DataConfig
9
8
  from .training_settings import DDPStrategyConfig
10
9
  from .training_settings import EarlyStoppingConfig
@@ -16,7 +15,6 @@ from .training_settings import TrainingSettings
16
15
 
17
16
  __all__ = [
18
17
  "CheckpointConfig",
19
- "ClearMLTrainingSettings",
20
18
  "DDPStrategyConfig",
21
19
  "DataConfig",
22
20
  "EarlyStoppingConfig",
@@ -1,22 +1,28 @@
1
1
  from pathlib import Path
2
+ from typing import Self
2
3
  from typing import TypeVar
3
4
 
5
+ from caseconverter import pascalcase
6
+ from caseconverter import snakecase
7
+ from clearml import Task
4
8
  from pydantic import BaseModel as PydanticBaseModel
5
9
 
10
+ from kostyl.utils.dict_manipulations import convert_to_flat_dict
11
+ from kostyl.utils.dict_manipulations import flattened_dict_to_nested
6
12
  from kostyl.utils.fs import load_config
7
13
 
8
14
 
9
15
  TConfig = TypeVar("TConfig", bound=PydanticBaseModel)
10
16
 
11
17
 
12
- class ConfigLoadingMixin:
13
- """Pydantic mixin class providing basic configuration loading functionality."""
18
+ class BaseModelWithConfigLoading(PydanticBaseModel):
19
+ """Pydantic class providing basic configuration loading functionality."""
14
20
 
15
21
  @classmethod
16
22
  def from_file(
17
- cls: type[TConfig], # pyright: ignore
23
+ cls: type[Self], # pyright: ignore
18
24
  path: str | Path,
19
- ) -> TConfig:
25
+ ) -> Self:
20
26
  """
21
27
  Create an instance of the class from a configuration file.
22
28
 
@@ -34,9 +40,9 @@ class ConfigLoadingMixin:
34
40
 
35
41
  @classmethod
36
42
  def from_dict(
37
- cls: type[TConfig], # pyright: ignore
43
+ cls: type[Self], # pyright: ignore
38
44
  state_dict: dict,
39
- ) -> TConfig:
45
+ ) -> Self:
40
46
  """
41
47
  Creates an instance from a dictionary.
42
48
 
@@ -54,7 +60,84 @@ class ConfigLoadingMixin:
54
60
  return instance
55
61
 
56
62
 
57
- class KostylBaseModel(PydanticBaseModel, ConfigLoadingMixin):
63
+ class BaseModelWithClearmlSyncing(BaseModelWithConfigLoading):
64
+ """Pydantic class providing ClearML configuration loading and syncing functionality."""
65
+
66
+ @classmethod
67
+ def connect_as_file(
68
+ cls: type[Self], # pyright: ignore
69
+ task: Task,
70
+ path: str | Path,
71
+ alias: str | None = None,
72
+ ) -> Self:
73
+ """
74
+ Connects the configuration file to a ClearML task and creates an instance of the class from it.
75
+
76
+ This method connects the specified configuration file to the given ClearML task for version control and monitoring,
77
+ then loads and validates the configuration to the class.
78
+
79
+ Args:
80
+ cls: The class type to instantiate.
81
+ task: The ClearML Task object to connect the configuration to.
82
+ path: Path to the configuration file (supports YAML format).
83
+ alias: Optional alias for the configuration in ClearML. Defaults to PascalCase of the class name if None.
84
+
85
+ Returns:
86
+ An instance of the class created from the connected configuration file.
87
+
88
+ """
89
+ if isinstance(path, Path):
90
+ str_path = str(path)
91
+ else:
92
+ str_path = path
93
+
94
+ name = alias if alias is not None else pascalcase(cls.__name__)
95
+ connected_path = task.connect_configuration(str_path, name=pascalcase(name))
96
+
97
+ if not isinstance(connected_path, str):
98
+ connected_path_str = str(connected_path)
99
+ else:
100
+ connected_path_str = connected_path
101
+
102
+ model = cls.from_file(path=connected_path_str)
103
+ return model
104
+
105
+ @classmethod
106
+ def connect_as_dict(
107
+ cls: type[Self], # pyright: ignore
108
+ task: Task,
109
+ path: str | Path,
110
+ alias: str | None = None,
111
+ ) -> Self:
112
+ """
113
+ Connects configuration from a file as a dictionary to a ClearML task and creates an instance of the class.
114
+
115
+ This class method loads configuration from a file as a dictionary, flattens and sync them with ClearML
116
+ task parameters. Then it creates an instance of the class using the synced dictionary.
117
+
118
+ Args:
119
+ cls: The class type of the model to be created (must be a TRetuningModel subclass).
120
+ task: The ClearML task to connect the configuration to.
121
+ path: Path to the configuration file to load parameters from.
122
+ alias: Optional alias name for the configuration. If None, uses snake_case of class name.
123
+
124
+ Returns:
125
+ An instance of the specified class created from the loaded configuration.
126
+
127
+ """
128
+ name = alias if alias is not None else snakecase(cls.__name__)
129
+
130
+ config = load_config(path)
131
+
132
+ flattened_config = convert_to_flat_dict(config)
133
+ task.connect(flattened_config, name=pascalcase(name))
134
+ config = flattened_dict_to_nested(flattened_config)
135
+
136
+ model = cls.from_dict(state_dict=config)
137
+ return model
138
+
139
+
140
+ class KostylBaseModel(BaseModelWithClearmlSyncing):
58
141
  """A Pydantic model class with basic configuration loading functionality."""
59
142
 
60
143
  pass
@@ -2,9 +2,10 @@ from pydantic import BaseModel
2
2
  from pydantic import Field
3
3
  from pydantic import model_validator
4
4
 
5
- from kostyl.ml.clearml.config_mixin import ClearMLConfigMixin
6
5
  from kostyl.utils.logging import setup_logger
7
6
 
7
+ from .base_model import KostylBaseModel
8
+
8
9
 
9
10
  logger = setup_logger(fmt="only_message")
10
11
 
@@ -74,7 +75,7 @@ class WeightDecay(BaseModel):
74
75
  return self
75
76
 
76
77
 
77
- class HyperparamsConfig(BaseModel, ClearMLConfigMixin):
78
+ class HyperparamsConfig(KostylBaseModel):
78
79
  """Model training hyperparameters configuration."""
79
80
 
80
81
  grad_clip_val: float | None = Field(default=None, gt=0, validate_default=False)
@@ -3,7 +3,6 @@ from typing import Literal
3
3
  from pydantic import BaseModel
4
4
  from pydantic import Field
5
5
 
6
- from kostyl.ml.clearml.config_mixin import ClearMLConfigMixin
7
6
  from kostyl.utils.logging import setup_logger
8
7
 
9
8
  from .base_model import KostylBaseModel
@@ -103,13 +102,3 @@ class TrainingSettings(KostylBaseModel):
103
102
  early_stopping: EarlyStoppingConfig | None = None
104
103
  checkpoint: CheckpointConfig
105
104
  data: DataConfig
106
-
107
-
108
- class ClearMLTrainingSettings(
109
- TrainingSettings,
110
- ClearMLConfigMixin,
111
- ):
112
- """Training parameters configuration with ClearML features support (config syncing, model identifiers tracking and etc)."""
113
-
114
- model_id: str
115
- tokenizer_id: str
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: kostyl-toolkit
3
- Version: 0.1.11
3
+ Version: 0.1.12
4
4
  Summary: Kickass Orchestration System for Training, Yielding & Logging
5
5
  Requires-Dist: case-converter>=1.2.0
6
6
  Requires-Dist: loguru>=0.7.3
@@ -1,14 +1,13 @@
1
1
  kostyl/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  kostyl/ml/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
3
  kostyl/ml/clearml/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- kostyl/ml/clearml/config_mixin.py,sha256=Wkz92BLL9y9aDk8qPtla6BrtZegQe7aw40G-oNkBIGs,3363
5
4
  kostyl/ml/clearml/dataset_utils.py,sha256=eij_sr2KDhm8GxEbVbK8aBjPsuVvLl9-PIGGaKVgXLA,1729
6
5
  kostyl/ml/clearml/logging_utils.py,sha256=GBjIIZbH_itd5sj7XpvxjkyZwxxGOpEcQ3BiWaJTyq8,1210
7
6
  kostyl/ml/clearml/pulling_utils.py,sha256=FOk9rtwu9PZ4tcIexiK0LvF6MfuuCwpZxtg2oDUWlTw,3526
8
- kostyl/ml/configs/__init__.py,sha256=pBuEhcumGBNncprqz-t_IGll2XtMmogTn9S6ZPHXCTg,997
9
- kostyl/ml/configs/base_model.py,sha256=nOuiBIQn5pYDRRq-F4HbfWi5cn8pfaT2kM02VGoo4TE,1622
10
- kostyl/ml/configs/hyperparams.py,sha256=UqMk2y1LISK7QMPbh3LyICjJ1DnUQhsYh1NDMR6YnR8,3006
11
- kostyl/ml/configs/training_settings.py,sha256=JznYSXpTRraFEH7dvk4gt2Ym-XpPGlN-ALzklD7kzCw,2848
7
+ kostyl/ml/configs/__init__.py,sha256=IetcivbqYGutowLqxdKp7QR4tkXKBr4m8t4Zkk9jHZU,911
8
+ kostyl/ml/configs/base_model.py,sha256=Eofn14J9RsjpVx_J4rp6C19pDDCANU4hr3JtX-d0FpQ,4820
9
+ kostyl/ml/configs/hyperparams.py,sha256=CaVNEvpW4LvlHhLsbe2FockIGI1mJufCqjH298nYgKE,2971
10
+ kostyl/ml/configs/training_settings.py,sha256=CZBwVVmkpCUtXCnfC1RFGQGUemht5xbOI43rgg7cNeg,2535
12
11
  kostyl/ml/dist_utils.py,sha256=G8atjzkRbXZZiZh9rdEYBmeXqX26rJdDDovft2n6xiU,3201
13
12
  kostyl/ml/lightning/__init__.py,sha256=-F3JAyq8KU1d-nACWryGu8d1CbvWbQ1rXFdeRwfE2X8,175
14
13
  kostyl/ml/lightning/callbacks/__init__.py,sha256=Vd-rozY4T9Prr3IMqbliXxj6sC6y9XsovHQqRwzc2HI,297
@@ -31,6 +30,6 @@ kostyl/utils/__init__.py,sha256=hkpmB6c5pr4Ti5BshOROebb7cvjDZfNCw83qZ_FFKMM,240
31
30
  kostyl/utils/dict_manipulations.py,sha256=e3vBicID74nYP8lHkVTQc4-IQwoJimrbFELy5uSF6Gk,1073
32
31
  kostyl/utils/fs.py,sha256=gAQNIU4R_2DhwjgzOS8BOMe0gZymtY1eZwmdgOdDgqo,510
33
32
  kostyl/utils/logging.py,sha256=3MvfDPArZhwakHu5nMlp_LpOsWg0E0SP26y41clsBtA,5232
34
- kostyl_toolkit-0.1.11.dist-info/WHEEL,sha256=3id4o64OvRm9dUknh3mMJNcfoTRK08ua5cU6DFyVy-4,79
35
- kostyl_toolkit-0.1.11.dist-info/METADATA,sha256=ImcjKFPWoQznLjTGCKjcvmf_Nn2F6NXNWzfQNPXPFDU,4269
36
- kostyl_toolkit-0.1.11.dist-info/RECORD,,
33
+ kostyl_toolkit-0.1.12.dist-info/WHEEL,sha256=3id4o64OvRm9dUknh3mMJNcfoTRK08ua5cU6DFyVy-4,79
34
+ kostyl_toolkit-0.1.12.dist-info/METADATA,sha256=JLx_ddPY88eK3cAhBTU5oYQ5Eo3IhJ9W8xCQ_zBnVEw,4269
35
+ kostyl_toolkit-0.1.12.dist-info/RECORD,,
@@ -1,91 +0,0 @@
1
- from pathlib import Path
2
- from typing import TypeVar
3
-
4
- import clearml
5
- from caseconverter import pascalcase
6
- from caseconverter import snakecase
7
-
8
- from kostyl.ml.configs.base_model import KostylBaseModel
9
- from kostyl.utils.dict_manipulations import convert_to_flat_dict
10
- from kostyl.utils.dict_manipulations import flattened_dict_to_nested
11
- from kostyl.utils.fs import load_config
12
-
13
-
14
- TModel = TypeVar("TModel", bound="KostylBaseModel")
15
-
16
-
17
- class ClearMLConfigMixin:
18
- """Pydantic mixin class providing ClearML configuration loading and syncing functionality."""
19
-
20
- @classmethod
21
- def connect_as_file(
22
- cls: type[TModel], # pyright: ignore
23
- task: clearml.Task,
24
- path: str | Path,
25
- alias: str | None = None,
26
- ) -> TModel:
27
- """
28
- Connects the configuration file to a ClearML task and creates an instance of the class from it.
29
-
30
- This method connects the specified configuration file to the given ClearML task for version control and monitoring,
31
- then loads and validates the configuration to the class.
32
-
33
- Args:
34
- cls: The class type to instantiate.
35
- task: The ClearML Task object to connect the configuration to.
36
- path: Path to the configuration file (supports YAML format).
37
- alias: Optional alias for the configuration in ClearML. Defaults to PascalCase of the class name if None.
38
-
39
- Returns:
40
- An instance of the class created from the connected configuration file.
41
-
42
- """
43
- if isinstance(path, Path):
44
- str_path = str(path)
45
- else:
46
- str_path = path
47
-
48
- name = alias if alias is not None else pascalcase(cls.__name__)
49
- connected_path = task.connect_configuration(str_path, name=pascalcase(name))
50
-
51
- if not isinstance(connected_path, str):
52
- connected_path_str = str(connected_path)
53
- else:
54
- connected_path_str = connected_path
55
-
56
- model = cls.from_file(path=connected_path_str)
57
- return model
58
-
59
- @classmethod
60
- def connect_as_dict(
61
- cls: type[TModel], # pyright: ignore
62
- task: clearml.Task,
63
- path: str | Path,
64
- alias: str | None = None,
65
- ) -> TModel:
66
- """
67
- Connects configuration from a file as a dictionary to a ClearML task and creates an instance of the class.
68
-
69
- This class method loads configuration from a file as a dictionary, flattens and sync them with ClearML
70
- task parameters. Then it creates an instance of the class using the synced dictionary.
71
-
72
- Args:
73
- cls: The class type of the model to be created (must be a TRetuningModel subclass).
74
- task: The ClearML task to connect the configuration to.
75
- path: Path to the configuration file to load parameters from.
76
- alias: Optional alias name for the configuration. If None, uses snake_case of class name.
77
-
78
- Returns:
79
- An instance of the specified class created from the loaded configuration.
80
-
81
- """
82
- name = alias if alias is not None else snakecase(cls.__name__)
83
-
84
- config = load_config(path)
85
-
86
- flattened_config = convert_to_flat_dict(config)
87
- task.connect(flattened_config, name=pascalcase(name))
88
- config = flattened_dict_to_nested(flattened_config)
89
-
90
- model = cls.from_dict(state_dict=config)
91
- return model