dlkit-zihao 0.2.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.
@@ -0,0 +1,224 @@
1
+ Metadata-Version: 2.4
2
+ Name: dlkit-zihao
3
+ Version: 0.2.0
4
+ Summary: Deep Learning experiment assistant toolkit: config parsing, device setup, logging, checkpointing, CSV tracking, and more.
5
+ Author-email: Zihao Chen <chenzihao@example.com>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/chenzihao/dlkit
8
+ Project-URL: Repository, https://github.com/chenzihao/dlkit
9
+ Keywords: deep-learning,pytorch,experiment,pipeline,nas,training
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Intended Audience :: Science/Research
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.8
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
21
+ Requires-Python: >=3.8
22
+ Description-Content-Type: text/markdown
23
+ Requires-Dist: torch>=1.8.0
24
+ Requires-Dist: numpy>=1.20.0
25
+ Requires-Dist: pandas>=1.3.0
26
+ Requires-Dist: pyyaml>=5.4.0
27
+ Provides-Extra: dev
28
+ Requires-Dist: pytest>=7.0; extra == "dev"
29
+ Requires-Dist: build>=0.8.0; extra == "dev"
30
+ Requires-Dist: twine>=4.0.0; extra == "dev"
31
+
32
+ # dlkit
33
+
34
+ > Deep Learning experiment assistant toolkit — 深度学习实验辅助工具包
35
+
36
+ `dlkit` 是一个用于深度学习实验的辅助工具包,提供从配置解析、设备分配、实验目录管理、日志记录到训练状态保存/加载的全流程辅助功能。
37
+
38
+ ## 安装
39
+
40
+ ```bash
41
+ pip install dlkit-zihao
42
+ ```
43
+
44
+ 或从源码安装:
45
+
46
+ ```bash
47
+ cd dlkit
48
+ pip install -e .
49
+ ```
50
+
51
+ ## 功能模块
52
+
53
+ | 模块 | 主要类/函数 | 说明 |
54
+ |------|------------|------|
55
+ | `dlkit.pipeline` | `StandardTrainingPipeline` | 标准化实验流水线:配置解析、设备分配、种子设定、目录管理、日志、代码备份、CSV 文件名生成 |
56
+ | `dlkit.argparse_utils` | `GroupArgparse` | 多层级分组命令行参数解析器,支持 YAML/字典深度合并 |
57
+ | `dlkit.meters` | `ScalarMeter`, `TimeMeter` | 标量累加器与计时器 |
58
+ | `dlkit.tracker` | `CSVExpTracker` | CSV 实验记录管理:多服务器聚合、查重、统计 |
59
+ | `dlkit.summary` | `Summary` | JSON 格式标量指标记录 |
60
+ | `dlkit.utils` | `set_seed`, `count_parameters_in_MB`, `get_gpus_memory_info`, `cur_time_str`, `get_caller_filename` | 通用工具函数 |
61
+
62
+ ## 快速开始
63
+
64
+ ### 1. 使用 StandardTrainingPipeline
65
+
66
+ ```python
67
+ from dlkit import StandardTrainingPipeline
68
+
69
+ config = {
70
+ 'train': {
71
+ 'lr': 0.01,
72
+ 'batch_size': 32,
73
+ }
74
+ }
75
+
76
+ # 保留参数可直接作为关键字参数传入,获得 IDE 自动补全
77
+ pipeline = StandardTrainingPipeline(
78
+ base_config=config,
79
+ gpu=0,
80
+ seed=42,
81
+ platform='6001',
82
+ workspace='experiments', # 工作区根目录
83
+ project_name='my_project', # 项目目录名
84
+ csv_dir='./csv', # CSV 文件保存目录
85
+ csv_tag='search', # CSV 文件名标签
86
+ )
87
+ args, config = pipeline.init(run_name='my_experiment')
88
+
89
+ pipeline.print('Training started', lr=args.train.lr)
90
+ pipeline.print(Epoch=1, Loss=0.45, Acc='92%')
91
+
92
+ # 自动生成 CSV 文件路径: './csv/train_search_6001.csv'
93
+ csv_path = pipeline.gen_csv_path(tag='search')
94
+
95
+ # 实验结束后可重命名目录
96
+ # pipeline.rename_run_name('Acc_92.5')
97
+ ```
98
+
99
+ ### 2. 使用 GroupArgparse
100
+
101
+ ```python
102
+ from dlkit import GroupArgparse
103
+
104
+ config = {
105
+ 'model': {'type': 'resnet', 'layers': 50},
106
+ 'train': {'lr': 0.01, 'epochs': 100},
107
+ }
108
+
109
+ gparser = GroupArgparse(base_config=config)
110
+ gparser.set_cur_group('train')
111
+ gparser.add_argument('--lr', type=float, default=0.01)
112
+ gparser.add_argument('--epochs', type=int, default=100)
113
+
114
+ args = gparser.parse_args(export_dataclass=True)
115
+ print(args.train.lr) # 支持点操作符访问
116
+ ```
117
+
118
+ ### 3. 使用 CSVExpTracker
119
+
120
+ ```python
121
+ from dlkit import StandardTrainingPipeline, CSVExpTracker
122
+
123
+ # 方式一:从 args 自动推导 CSV 路径(推荐)
124
+ pipeline = StandardTrainingPipeline(
125
+ platform='6001',
126
+ csv_dir='./csv',
127
+ csv_tag='search',
128
+ )
129
+ args, config = pipeline.init(run_name='my_experiment')
130
+
131
+ # 不传 root,自动从 args 读取 csv_dir / csv_tag / platform
132
+ # 生成路径: './csv/train_search_6001.csv'
133
+ tracker = CSVExpTracker(
134
+ args=args,
135
+ x_columns=['lr', 'batch_size'],
136
+ y_columns=['acc', 'loss'],
137
+ avoid_duplication=True,
138
+ platforms=['6001', '6002'],
139
+ )
140
+
141
+ # 方式二:直接指定 root
142
+ # tracker = CSVExpTracker(root='./csv/results.csv', ...)
143
+
144
+ # 查重
145
+ if not tracker.is_sampled(lr=0.01, batch_size=32, run_id=0):
146
+ # ... 训练模型 ...
147
+ tracker.save_csv({'lr': 0.01, 'batch_size': 32, 'acc': 0.925, 'loss': 0.05, 'run_id': 0})
148
+
149
+ # 统计聚合
150
+ tracker.count()
151
+ ```
152
+
153
+ ### 4. 使用 Meters
154
+
155
+ ```python
156
+ from dlkit import ScalarMeter, TimeMeter
157
+
158
+ loss_meter = ScalarMeter(reduction='mean')
159
+ timer = TimeMeter()
160
+
161
+ for batch in dataloader:
162
+ timer.update()
163
+ loss = train_batch(batch)
164
+ loss_meter.update(loss, num=batch_size)
165
+
166
+ print(f'Avg Loss: {loss_meter.avg}, Time: {timer.cost} min')
167
+ ```
168
+
169
+ ### 5. 使用 get_caller_filename
170
+
171
+ ```python
172
+ from dlkit import get_caller_filename
173
+
174
+ # 在 train.py 中调用
175
+ print(get_caller_filename()) # 'train'
176
+ print(get_caller_filename(stem=False)) # 'train.py'
177
+ ```
178
+
179
+ ## 相对原版 assistant.py 的修复
180
+
181
+ 1. **`TimeMeter`** — 修复了 `start_time` 未初始化导致 `total` 属性崩溃的问题
182
+ 2. **`set_seed`** — 使用 `manual_seed_all` 覆盖所有 GPU(原版仅 `manual_seed`)
183
+ 3. **`CSVExpTracker.save_csv`** — 修复了文档声称更新 `self.df` 但实际未实现的问题
184
+ 4. **`Summary.save_record`** — 修复了 `record_name=None` 时的逻辑不清晰问题
185
+ 5. **`StandardTrainingPipeline`** — 修复了 `enable_directory=False` 时 `self.print` 未定义的问题
186
+ 6. **清理** — 移除了废弃的 `_export_dataclass_schema1`、未使用的 `glob` import
187
+ 7. **移除 checkpoint** — 移除了 `save_checkpoint` / `load_checkpoint`(原版存在死代码 bug,且该功能可由 PyTorch 原生 `torch.save` / `torch.load` 替代)
188
+ 8. **新增 `get_caller_filename`** — 自动获取主调脚本文件名
189
+ 9. **新增 `gen_csv_path`** — 自动生成 `[主调文件名]_[tag]_[platform].csv` 格式的 CSV 文件路径
190
+ 10. **新增 `platform` 保留参数** — 用于标识当前运行所在的平台
191
+ 11. **重命名目录参数** — `exp_root` → `workspace`(工作区根目录),`task_name` → `project_name`(项目名),含义更直观
192
+ 12. **`CSVExpTracker` 支持 `args` 推导路径** — `root` 改为可选,未指定时自动从 `args` 中读取 `csv_dir`、`csv_tag`、`platform` 推导路径,默认保存到 `./csv/` 目录
193
+ 13. **新增 `csv_dir`、`csv_tag` 保留参数** — 用于 `CSVExpTracker` 自动推导 CSV 路径
194
+
195
+ ## 项目结构
196
+
197
+ ```
198
+ dlkit/
199
+ ├── pyproject.toml
200
+ ├── README.md
201
+ └── src/
202
+ └── dlkit/
203
+ ├── __init__.py
204
+ ├── py.typed
205
+ ├── utils.py # 通用工具函数 (含 get_caller_filename)
206
+ ├── meters.py # ScalarMeter, TimeMeter
207
+ ├── tracker.py # CSVExpTracker
208
+ ├── summary.py # Summary
209
+ ├── argparse_utils.py # GroupArgparse
210
+ └── pipeline.py # StandardTrainingPipeline (含 gen_csv_path)
211
+ ```
212
+
213
+ ## 发布到 PyPI
214
+
215
+ ```bash
216
+ cd dlkit
217
+ pip install build twine
218
+ python -m build
219
+ twine upload dist/*
220
+ ```
221
+
222
+ ## License
223
+
224
+ MIT
@@ -0,0 +1,193 @@
1
+ # dlkit
2
+
3
+ > Deep Learning experiment assistant toolkit — 深度学习实验辅助工具包
4
+
5
+ `dlkit` 是一个用于深度学习实验的辅助工具包,提供从配置解析、设备分配、实验目录管理、日志记录到训练状态保存/加载的全流程辅助功能。
6
+
7
+ ## 安装
8
+
9
+ ```bash
10
+ pip install dlkit-zihao
11
+ ```
12
+
13
+ 或从源码安装:
14
+
15
+ ```bash
16
+ cd dlkit
17
+ pip install -e .
18
+ ```
19
+
20
+ ## 功能模块
21
+
22
+ | 模块 | 主要类/函数 | 说明 |
23
+ |------|------------|------|
24
+ | `dlkit.pipeline` | `StandardTrainingPipeline` | 标准化实验流水线:配置解析、设备分配、种子设定、目录管理、日志、代码备份、CSV 文件名生成 |
25
+ | `dlkit.argparse_utils` | `GroupArgparse` | 多层级分组命令行参数解析器,支持 YAML/字典深度合并 |
26
+ | `dlkit.meters` | `ScalarMeter`, `TimeMeter` | 标量累加器与计时器 |
27
+ | `dlkit.tracker` | `CSVExpTracker` | CSV 实验记录管理:多服务器聚合、查重、统计 |
28
+ | `dlkit.summary` | `Summary` | JSON 格式标量指标记录 |
29
+ | `dlkit.utils` | `set_seed`, `count_parameters_in_MB`, `get_gpus_memory_info`, `cur_time_str`, `get_caller_filename` | 通用工具函数 |
30
+
31
+ ## 快速开始
32
+
33
+ ### 1. 使用 StandardTrainingPipeline
34
+
35
+ ```python
36
+ from dlkit import StandardTrainingPipeline
37
+
38
+ config = {
39
+ 'train': {
40
+ 'lr': 0.01,
41
+ 'batch_size': 32,
42
+ }
43
+ }
44
+
45
+ # 保留参数可直接作为关键字参数传入,获得 IDE 自动补全
46
+ pipeline = StandardTrainingPipeline(
47
+ base_config=config,
48
+ gpu=0,
49
+ seed=42,
50
+ platform='6001',
51
+ workspace='experiments', # 工作区根目录
52
+ project_name='my_project', # 项目目录名
53
+ csv_dir='./csv', # CSV 文件保存目录
54
+ csv_tag='search', # CSV 文件名标签
55
+ )
56
+ args, config = pipeline.init(run_name='my_experiment')
57
+
58
+ pipeline.print('Training started', lr=args.train.lr)
59
+ pipeline.print(Epoch=1, Loss=0.45, Acc='92%')
60
+
61
+ # 自动生成 CSV 文件路径: './csv/train_search_6001.csv'
62
+ csv_path = pipeline.gen_csv_path(tag='search')
63
+
64
+ # 实验结束后可重命名目录
65
+ # pipeline.rename_run_name('Acc_92.5')
66
+ ```
67
+
68
+ ### 2. 使用 GroupArgparse
69
+
70
+ ```python
71
+ from dlkit import GroupArgparse
72
+
73
+ config = {
74
+ 'model': {'type': 'resnet', 'layers': 50},
75
+ 'train': {'lr': 0.01, 'epochs': 100},
76
+ }
77
+
78
+ gparser = GroupArgparse(base_config=config)
79
+ gparser.set_cur_group('train')
80
+ gparser.add_argument('--lr', type=float, default=0.01)
81
+ gparser.add_argument('--epochs', type=int, default=100)
82
+
83
+ args = gparser.parse_args(export_dataclass=True)
84
+ print(args.train.lr) # 支持点操作符访问
85
+ ```
86
+
87
+ ### 3. 使用 CSVExpTracker
88
+
89
+ ```python
90
+ from dlkit import StandardTrainingPipeline, CSVExpTracker
91
+
92
+ # 方式一:从 args 自动推导 CSV 路径(推荐)
93
+ pipeline = StandardTrainingPipeline(
94
+ platform='6001',
95
+ csv_dir='./csv',
96
+ csv_tag='search',
97
+ )
98
+ args, config = pipeline.init(run_name='my_experiment')
99
+
100
+ # 不传 root,自动从 args 读取 csv_dir / csv_tag / platform
101
+ # 生成路径: './csv/train_search_6001.csv'
102
+ tracker = CSVExpTracker(
103
+ args=args,
104
+ x_columns=['lr', 'batch_size'],
105
+ y_columns=['acc', 'loss'],
106
+ avoid_duplication=True,
107
+ platforms=['6001', '6002'],
108
+ )
109
+
110
+ # 方式二:直接指定 root
111
+ # tracker = CSVExpTracker(root='./csv/results.csv', ...)
112
+
113
+ # 查重
114
+ if not tracker.is_sampled(lr=0.01, batch_size=32, run_id=0):
115
+ # ... 训练模型 ...
116
+ tracker.save_csv({'lr': 0.01, 'batch_size': 32, 'acc': 0.925, 'loss': 0.05, 'run_id': 0})
117
+
118
+ # 统计聚合
119
+ tracker.count()
120
+ ```
121
+
122
+ ### 4. 使用 Meters
123
+
124
+ ```python
125
+ from dlkit import ScalarMeter, TimeMeter
126
+
127
+ loss_meter = ScalarMeter(reduction='mean')
128
+ timer = TimeMeter()
129
+
130
+ for batch in dataloader:
131
+ timer.update()
132
+ loss = train_batch(batch)
133
+ loss_meter.update(loss, num=batch_size)
134
+
135
+ print(f'Avg Loss: {loss_meter.avg}, Time: {timer.cost} min')
136
+ ```
137
+
138
+ ### 5. 使用 get_caller_filename
139
+
140
+ ```python
141
+ from dlkit import get_caller_filename
142
+
143
+ # 在 train.py 中调用
144
+ print(get_caller_filename()) # 'train'
145
+ print(get_caller_filename(stem=False)) # 'train.py'
146
+ ```
147
+
148
+ ## 相对原版 assistant.py 的修复
149
+
150
+ 1. **`TimeMeter`** — 修复了 `start_time` 未初始化导致 `total` 属性崩溃的问题
151
+ 2. **`set_seed`** — 使用 `manual_seed_all` 覆盖所有 GPU(原版仅 `manual_seed`)
152
+ 3. **`CSVExpTracker.save_csv`** — 修复了文档声称更新 `self.df` 但实际未实现的问题
153
+ 4. **`Summary.save_record`** — 修复了 `record_name=None` 时的逻辑不清晰问题
154
+ 5. **`StandardTrainingPipeline`** — 修复了 `enable_directory=False` 时 `self.print` 未定义的问题
155
+ 6. **清理** — 移除了废弃的 `_export_dataclass_schema1`、未使用的 `glob` import
156
+ 7. **移除 checkpoint** — 移除了 `save_checkpoint` / `load_checkpoint`(原版存在死代码 bug,且该功能可由 PyTorch 原生 `torch.save` / `torch.load` 替代)
157
+ 8. **新增 `get_caller_filename`** — 自动获取主调脚本文件名
158
+ 9. **新增 `gen_csv_path`** — 自动生成 `[主调文件名]_[tag]_[platform].csv` 格式的 CSV 文件路径
159
+ 10. **新增 `platform` 保留参数** — 用于标识当前运行所在的平台
160
+ 11. **重命名目录参数** — `exp_root` → `workspace`(工作区根目录),`task_name` → `project_name`(项目名),含义更直观
161
+ 12. **`CSVExpTracker` 支持 `args` 推导路径** — `root` 改为可选,未指定时自动从 `args` 中读取 `csv_dir`、`csv_tag`、`platform` 推导路径,默认保存到 `./csv/` 目录
162
+ 13. **新增 `csv_dir`、`csv_tag` 保留参数** — 用于 `CSVExpTracker` 自动推导 CSV 路径
163
+
164
+ ## 项目结构
165
+
166
+ ```
167
+ dlkit/
168
+ ├── pyproject.toml
169
+ ├── README.md
170
+ └── src/
171
+ └── dlkit/
172
+ ├── __init__.py
173
+ ├── py.typed
174
+ ├── utils.py # 通用工具函数 (含 get_caller_filename)
175
+ ├── meters.py # ScalarMeter, TimeMeter
176
+ ├── tracker.py # CSVExpTracker
177
+ ├── summary.py # Summary
178
+ ├── argparse_utils.py # GroupArgparse
179
+ └── pipeline.py # StandardTrainingPipeline (含 gen_csv_path)
180
+ ```
181
+
182
+ ## 发布到 PyPI
183
+
184
+ ```bash
185
+ cd dlkit
186
+ pip install build twine
187
+ python -m build
188
+ twine upload dist/*
189
+ ```
190
+
191
+ ## License
192
+
193
+ MIT
@@ -0,0 +1,51 @@
1
+ [build-system]
2
+ requires = ["setuptools>=64.0", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "dlkit-zihao"
7
+ version = "0.2.0"
8
+ description = "Deep Learning experiment assistant toolkit: config parsing, device setup, logging, checkpointing, CSV tracking, and more."
9
+ readme = "README.md"
10
+ requires-python = ">=3.8"
11
+ license = { text = "MIT" }
12
+ authors = [
13
+ { name = "Zihao Chen", email = "chenzihao@example.com" }
14
+ ]
15
+ keywords = ["deep-learning", "pytorch", "experiment", "pipeline", "nas", "training"]
16
+ classifiers = [
17
+ "Development Status :: 4 - Beta",
18
+ "Intended Audience :: Developers",
19
+ "Intended Audience :: Science/Research",
20
+ "License :: OSI Approved :: MIT License",
21
+ "Programming Language :: Python :: 3",
22
+ "Programming Language :: Python :: 3.8",
23
+ "Programming Language :: Python :: 3.9",
24
+ "Programming Language :: Python :: 3.10",
25
+ "Programming Language :: Python :: 3.11",
26
+ "Programming Language :: Python :: 3.12",
27
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
28
+ ]
29
+ dependencies = [
30
+ "torch>=1.8.0",
31
+ "numpy>=1.20.0",
32
+ "pandas>=1.3.0",
33
+ "pyyaml>=5.4.0",
34
+ ]
35
+
36
+ [project.optional-dependencies]
37
+ dev = [
38
+ "pytest>=7.0",
39
+ "build>=0.8.0",
40
+ "twine>=4.0.0",
41
+ ]
42
+
43
+ [project.urls]
44
+ Homepage = "https://github.com/chenzihao/dlkit"
45
+ Repository = "https://github.com/chenzihao/dlkit"
46
+
47
+ [tool.setuptools.packages.find]
48
+ where = ["src"]
49
+
50
+ [tool.setuptools.package-data]
51
+ dlkit = ["py.typed"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,60 @@
1
+ """
2
+ dlkit
3
+ =====
4
+
5
+ Deep Learning experiment assistant toolkit.
6
+
7
+ 一个用于深度学习实验的辅助工具包,提供:
8
+
9
+ - **配置解析**: ``GroupArgparse`` — 多层级分组命令行参数解析器
10
+ - **实验流水线**: ``StandardTrainingPipeline`` — 设备分配、种子设定、目录管理、日志、代码备份、CSV 文件名生成
11
+ - **计量器**: ``ScalarMeter``、``TimeMeter`` — 标量累加与计时
12
+ - **实验追踪**: ``CSVExpTracker`` — CSV 存储、多服务器聚合、查重、统计
13
+ - **标量记录**: ``Summary`` — JSON 格式标量指标记录
14
+ - **工具函数**: ``set_seed``、``count_parameters_in_MB``、``get_gpus_memory_info``、``get_caller_filename`` 等
15
+
16
+ 快速开始::
17
+
18
+ from dlkit import StandardTrainingPipeline
19
+
20
+ pipeline = StandardTrainingPipeline(base_config={'gpu': 0, 'seed': 42, 'platform': '6001'})
21
+ args, config = pipeline.init(run_name='my_exp')
22
+ csv_path = pipeline.gen_csv_path(tag='search')
23
+ pipeline.print('CSV path:', csv_path)
24
+ """
25
+
26
+ from .argparse_utils import GroupArgparse
27
+ from .meters import ScalarMeter, TimeMeter
28
+ from .pipeline import StandardTrainingPipeline, print_log
29
+ from .summary import Summary
30
+ from .tracker import CSVExpTracker
31
+ from .utils import (
32
+ count_parameters_in_MB,
33
+ cur_time_str,
34
+ get_caller_filename,
35
+ get_gpus_memory_info,
36
+ set_seed,
37
+ )
38
+
39
+ __version__ = "0.2.0"
40
+
41
+ __all__ = [
42
+ # pipeline
43
+ "StandardTrainingPipeline",
44
+ "print_log",
45
+ # argparse
46
+ "GroupArgparse",
47
+ # meters
48
+ "ScalarMeter",
49
+ "TimeMeter",
50
+ # tracker
51
+ "CSVExpTracker",
52
+ # summary
53
+ "Summary",
54
+ # utils
55
+ "cur_time_str",
56
+ "get_caller_filename",
57
+ "get_gpus_memory_info",
58
+ "set_seed",
59
+ "count_parameters_in_MB",
60
+ ]