trainur 0.0.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.
trainur-0.0.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nam Kha Nguyen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
trainur-0.0.0/PKG-INFO ADDED
@@ -0,0 +1,36 @@
1
+ Metadata-Version: 2.4
2
+ Name: trainur
3
+ Version: 0.0.0
4
+ Summary: A simple Pytorch trainer
5
+ Author-email: Nam Kha Nguyen <namkha1032@gmail.com>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2026 Nam Kha Nguyen
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/namkha1032/trainur
29
+ Project-URL: Repository, https://github.com/namkha1032/trainur
30
+ Requires-Python: >=3.10
31
+ Description-Content-Type: text/markdown
32
+ License-File: LICENSE
33
+ Dynamic: license-file
34
+
35
+ # trainur
36
+ A simple Pytorch trainer
@@ -0,0 +1,2 @@
1
+ # trainur
2
+ A simple Pytorch trainer
@@ -0,0 +1,21 @@
1
+ [project]
2
+ name = "trainur"
3
+ version = "0.0.0"
4
+ description = "A simple Pytorch trainer"
5
+ readme = "README.md"
6
+ authors = [
7
+ { name = "Nam Kha Nguyen", email = "namkha1032@gmail.com" }
8
+ ]
9
+ license = { file = "LICENSE" }
10
+ requires-python = ">=3.10"
11
+
12
+ [project.urls]
13
+ Homepage = "https://github.com/namkha1032/trainur"
14
+ Repository = "https://github.com/namkha1032/trainur"
15
+
16
+ [build-system]
17
+ requires = ["setuptools>=61.0", "wheel"]
18
+ build-backend = "setuptools.build_meta"
19
+
20
+ [tool.setuptools]
21
+ packages = ["trainur"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,21 @@
1
+ from trainur import Trainur
2
+ from logab import log_wrap
3
+ from fire import Fire
4
+ from dataclasses import dataclass
5
+
6
+ class CustomTrainer(Trainur):
7
+ train_var:any=1
8
+ def train(self):
9
+ pass
10
+
11
+ def inference(self):
12
+ pass
13
+
14
+ class CustomTester(CustomTrainer):
15
+ test_var:any="some str"
16
+ def run_test(self):
17
+ pass
18
+
19
+ if __name__ == "__main__":
20
+ with log_wrap():
21
+ Fire(CustomTester)
@@ -0,0 +1 @@
1
+ from .trainer_utils import Trainur
@@ -0,0 +1,88 @@
1
+ import torch
2
+ from torch.utils.data import DataLoader
3
+ import sys
4
+ from dataclasses import dataclass, asdict, fields
5
+ import random
6
+ import numpy as np
7
+ from logab import log_init
8
+
9
+ @dataclass
10
+ class Trainur:
11
+ device: any ="cuda:0"
12
+ dtype: int = 32
13
+ epoch: int = 1
14
+ batch_size: int = 2
15
+ effective_batch_size: int = 32
16
+ accumulated_steps: int = None
17
+ num_workers: int = 4
18
+ prefetch_factor: int = 8
19
+
20
+ def __post_init__(self):
21
+ self.accumulated_steps = self.effective_batch_size // self.batch_size
22
+ attr_old = asdict(self)
23
+
24
+ result_dict = dict()
25
+
26
+ for cls in reversed(self.__class__.__mro__):
27
+ if hasattr(cls, '__annotations__'):
28
+ result_dict[cls.__name__] = dict()
29
+ for attr in cls.__annotations__:
30
+ if attr in attr_old:
31
+ result_dict[cls.__name__][attr] = attr_old[attr]
32
+ attr_old.pop(attr, None)
33
+
34
+ for cls_name, attr_list in result_dict.items():
35
+ print(f"{cls_name}:")
36
+ for attr_name, attr_value in attr_list.items():
37
+ print(f"\t{attr_name}: {attr_value}")
38
+ print(f"{'-'*50}\n")
39
+
40
+ self.dtype = torch.float32 if self.dtype == 32 else torch.bfloat16
41
+ self.logger = log_init()
42
+
43
+ def __init_subclass__(cls, **kwargs):
44
+ super().__init_subclass__(**kwargs)
45
+ dataclass(cls)
46
+ pass
47
+
48
+ def fix_seed(self, seed=42):
49
+ random.seed(seed)
50
+ np.random.seed(seed)
51
+ torch.manual_seed(seed)
52
+ torch.cuda.manual_seed(seed)
53
+ torch.cuda.manual_seed_all(seed)
54
+
55
+ def count_trainable(self, model):
56
+ total = 0
57
+ trainable = 0
58
+ for p in model.parameters():
59
+ total += p.numel()
60
+ if p.requires_grad:
61
+ trainable += p.numel()
62
+ result = f"Trainable {trainable} / {total} ({trainable/total*100:.4f})%"
63
+ return result
64
+
65
+ def transfer_tensor(self, tensor):
66
+ return {key: value.to(device=self.device, dtype=self.dtype if value.dtype == torch.float32 else value.dtype) if isinstance(value, torch.Tensor) else value for key, value in tensor.items()}
67
+
68
+ def create_dataloader(self, dataset, drop_last=True, is_shuffle=True):
69
+ if 'debugpy' in sys.modules:
70
+ num_workers = 0
71
+ prefetch_factor = None
72
+ persistent_workers = False
73
+ else:
74
+ num_workers = self.num_workers
75
+ prefetch_factor = self.prefetch_factor
76
+ persistent_workers = True if num_workers > 0 else False
77
+ dataloader = DataLoader(
78
+ dataset,
79
+ shuffle=is_shuffle,
80
+ batch_size=self.batch_size,
81
+ num_workers=num_workers,
82
+ prefetch_factor=prefetch_factor,
83
+ persistent_workers=persistent_workers,
84
+ pin_memory=True,
85
+ drop_last=drop_last,
86
+ )
87
+ return dataloader
88
+
@@ -0,0 +1,36 @@
1
+ Metadata-Version: 2.4
2
+ Name: trainur
3
+ Version: 0.0.0
4
+ Summary: A simple Pytorch trainer
5
+ Author-email: Nam Kha Nguyen <namkha1032@gmail.com>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2026 Nam Kha Nguyen
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/namkha1032/trainur
29
+ Project-URL: Repository, https://github.com/namkha1032/trainur
30
+ Requires-Python: >=3.10
31
+ Description-Content-Type: text/markdown
32
+ License-File: LICENSE
33
+ Dynamic: license-file
34
+
35
+ # trainur
36
+ A simple Pytorch trainer
@@ -0,0 +1,10 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ tests/test.py
5
+ trainur/__init__.py
6
+ trainur/trainer_utils.py
7
+ trainur.egg-info/PKG-INFO
8
+ trainur.egg-info/SOURCES.txt
9
+ trainur.egg-info/dependency_links.txt
10
+ trainur.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ trainur