rrrrou commited on
Commit
de9412a
·
verified ·
1 Parent(s): 54e87f1

Delete HPL-Cluster/duplicate.py

Browse files
Files changed (1) hide show
  1. HPL-Cluster/duplicate.py +0 -401
HPL-Cluster/duplicate.py DELETED
@@ -1,401 +0,0 @@
1
- import os
2
- import shutil
3
- from pathlib import Path
4
- import re
5
-
6
- def copy_last_epoch_models(source_dir, target_dir):
7
- """
8
- 复制整个目录结构,但只保留每个子文件夹中最后一个epoch的.pkl文件
9
-
10
- 新增功能:如果目标子文件夹中已经有.pkl文件,跳过该文件夹的复制
11
-
12
- Args:
13
- source_dir: 源目录路径
14
- target_dir: 目标目录路径
15
- """
16
- # 转换路径为Path对象
17
- source_path = Path(source_dir)
18
- target_path = Path(target_dir)
19
-
20
- # 确保源目录存在
21
- if not source_path.exists():
22
- print(f"错误:源目录不存在 - {source_dir}")
23
- return
24
-
25
- # 创建目标目录(如果不存在)
26
- target_path.mkdir(parents=True, exist_ok=True)
27
-
28
- # 计数器
29
- total_folders = 0
30
- total_folders_skipped = 0
31
- total_files_copied = 0
32
-
33
- print(f"开始从 {source_dir} 复制到 {target_dir}")
34
- print("=" * 60)
35
-
36
- # 遍历源目录
37
- for root, dirs, files in os.walk(source_dir):
38
- root_path = Path(root)
39
-
40
- # 获取相对路径(相对于源目录)
41
- relative_path = root_path.relative_to(source_path)
42
- target_subdir = target_path / relative_path
43
-
44
- # **新增功能:检查目标子文件夹是否已有.pkl文件**
45
- if target_subdir.exists():
46
- existing_pkl = list(target_subdir.glob("*.pkl"))
47
- if existing_pkl:
48
- print(f"\n跳过目录: {relative_path}")
49
- print(f" 原因: 目标文件夹中已有 {len(existing_pkl)} 个.pkl文件")
50
- total_folders_skipped += 1
51
- continue
52
-
53
- # 创建对应的目标子目录
54
- if not target_subdir.exists():
55
- target_subdir.mkdir(parents=True, exist_ok=True)
56
-
57
- # 如果没有文件,继续下一个目录
58
- if not files:
59
- continue
60
-
61
- # 只收集.pkl文件
62
- pkl_files = [f for f in files if f.lower().endswith('.pkl')]
63
-
64
- if not pkl_files:
65
- continue
66
-
67
- print(f"\n处理目录: {relative_path}")
68
- print(f"找到 {len(pkl_files)} 个.pkl文件")
69
-
70
- # 从文件名中提取epoch数字并排序
71
- def extract_epoch_number(filename):
72
- """从文件名中提取epoch数字"""
73
- # 匹配 ep1, ep2, ep10 等模式(不区分大小写)
74
- match = re.search(r'ep(\d+)', filename, re.IGNORECASE)
75
- if match:
76
- return int(match.group(1))
77
-
78
- # 匹配 epoch1, epoch2, epoch10 等模式
79
- match = re.search(r'epoch[_-]?(\d+)', filename, re.IGNORECASE)
80
- if match:
81
- return int(match.group(1))
82
-
83
- # 如果都没有匹配到,返回0
84
- return 0
85
-
86
- # 创建(epoch_number, filename)的列表
87
- epoch_files = []
88
- for file in pkl_files:
89
- epoch_num = extract_epoch_number(file)
90
- epoch_files.append((epoch_num, file))
91
-
92
- # 按epoch数字排序
93
- epoch_files.sort(key=lambda x: x[0])
94
-
95
- # 显示所有找到的文件和它们的epoch数字
96
- for epoch_num, file in epoch_files:
97
- print(f" - {file} (epoch: {epoch_num})")
98
-
99
- # 获取最后一个(最大epoch)的文件
100
- if epoch_files:
101
- last_epoch, last_file = epoch_files[-1]
102
-
103
- # 源文件路径
104
- source_file = root_path / last_file
105
- # 目标文件路径
106
- target_file = target_subdir / last_file
107
-
108
- # 复制文件
109
- shutil.copy2(source_file, target_file)
110
-
111
- print(f"✓ 已复制最后一个epoch文件: {last_file} (epoch: {last_epoch})")
112
-
113
- total_files_copied += 1
114
-
115
- total_folders += 1
116
-
117
- print(f"\n" + "=" * 60)
118
- print("复制完成!")
119
- print(f"总处理文件夹数: {total_folders}")
120
- print(f"跳过文件夹数(目标已有文件): {total_folders_skipped}")
121
- print(f"总复制文件数: {total_files_copied}")
122
- print(f"源目录: {source_dir}")
123
- print(f"目标目录: {target_dir}")
124
-
125
- # 增强版本:处理更多命名模式,同样包含跳过功能
126
- def copy_last_epoch_models_enhanced(source_dir, target_dir):
127
- """
128
- 增强版本:处理更多命名模式,如ep1, epoch1, epoch_1等
129
-
130
- 新增功能:如果目标子文件夹中已经有.pkl文件,跳过该文件夹的复制
131
- """
132
- import os
133
- import shutil
134
- from pathlib import Path
135
- import re
136
-
137
- source_path = Path(source_dir)
138
- target_path = Path(target_dir)
139
-
140
- if not source_path.exists():
141
- print(f"错误:源目录不存在 - {source_dir}")
142
- return
143
-
144
- target_path.mkdir(parents=True, exist_ok=True)
145
-
146
- total_folders = 0
147
- total_folders_skipped = 0
148
- total_files_copied = 0
149
-
150
- print(f"开始从 {source_dir} 复制到 {target_dir}")
151
- print("=" * 60)
152
-
153
- # 定义更多可能的epoch模式
154
- epoch_patterns = [
155
- r'ep(\d+)', # ep1, ep10
156
- r'epoch[_-]?(\d+)', # epoch1, epoch_1, epoch-1
157
- r'e(\d+)', # e1, e10
158
- r'_(\d+)\.pkl$', # _1.pkl, _10.pkl
159
- r'step[_-]?(\d+)', # step1, step_1
160
- r'checkpoint[_-]?(\d+)', # checkpoint1, checkpoint_1
161
- r'ckpt[_-]?(\d+)', # ckpt1, ckpt_1
162
- ]
163
-
164
- for root, dirs, files in os.walk(source_dir):
165
- root_path = Path(root)
166
- relative_path = root_path.relative_to(source_path)
167
- target_subdir = target_path / relative_path
168
-
169
- # **新增功能:检查目标子文件夹是否已有.pkl文件**
170
- if target_subdir.exists():
171
- existing_pkl = list(target_subdir.glob("*.pkl"))
172
- if existing_pkl:
173
- print(f"\n跳过目录: {relative_path}")
174
- print(f" 原因: 目标文件夹中已有 {len(existing_pkl)} 个.pkl文件:")
175
- for i, existing_file in enumerate(existing_pkl[:5]): # 最多显示5个
176
- print(f" - {existing_file.name}")
177
- if len(existing_pkl) > 5:
178
- print(f" ... 等共 {len(existing_pkl)} 个文件")
179
- total_folders_skipped += 1
180
- continue
181
-
182
- if not target_subdir.exists():
183
- target_subdir.mkdir(parents=True, exist_ok=True)
184
-
185
- # 只收集.pkl文件
186
- pkl_files = [f for f in files if f.lower().endswith('.pkl')]
187
-
188
- if not pkl_files:
189
- continue
190
-
191
- print(f"\n处理目录: {relative_path}")
192
- print(f"找到 {len(pkl_files)} 个.pkl文件")
193
-
194
- # 从文件名中提取epoch数字
195
- def extract_epoch_number_enhanced(filename):
196
- """增强版epoch数字提取"""
197
- filename_lower = filename.lower()
198
-
199
- # 尝试所有模式
200
- for pattern in epoch_patterns:
201
- match = re.search(pattern, filename_lower)
202
- if match:
203
- return int(match.group(1))
204
-
205
- # 如果以上模式都不匹配,尝试提取所有数字
206
- numbers = re.findall(r'\d+', filename_lower)
207
- if numbers:
208
- # 返回最大的数字(假设是epoch)
209
- return int(max(numbers, key=lambda x: (len(x), x)))
210
-
211
- return 0
212
-
213
- # 创建(epoch_number, filename)的列表
214
- epoch_files = []
215
- for file in pkl_files:
216
- epoch_num = extract_epoch_number_enhanced(file)
217
- epoch_files.append((epoch_num, file))
218
-
219
- # 按epoch数字排序
220
- epoch_files.sort(key=lambda x: x[0])
221
-
222
- # 显示所有找到的文件和它们的epoch数字
223
- if len(epoch_files) <= 10: # 如果文件不多,显示所有
224
- for epoch_num, file in epoch_files:
225
- print(f" - {file} (epoch: {epoch_num})")
226
- else: # 如果文件很多,只显示前5个和后5个
227
- for i, (epoch_num, file) in enumerate(epoch_files):
228
- if i < 5 or i >= len(epoch_files) - 5:
229
- print(f" - {file} (epoch: {epoch_num})")
230
- elif i == 5:
231
- print(f" ... 省略中间 {len(epoch_files)-10} 个文件 ...")
232
-
233
- # 获取最后一个(最大epoch)的文件
234
- if epoch_files:
235
- last_epoch, last_file = epoch_files[-1]
236
-
237
- # 检查是否有相同epoch的文件(可能有多个文件)
238
- max_epoch = last_epoch
239
- last_epoch_files = [f for e, f in epoch_files if e == max_epoch]
240
-
241
- if len(last_epoch_files) > 1:
242
- print(f"⚠ 注意:有 {len(last_epoch_files)} 个epoch为 {max_epoch} 的文件:")
243
- for f in last_epoch_files:
244
- print(f" - {f}")
245
- print(f" 将复制最后一个文件: {last_file}")
246
-
247
- # 源文件路径
248
- source_file = root_path / last_file
249
- # 目标文件路径
250
- target_file = target_subdir / last_file
251
-
252
- # 复制文件
253
- shutil.copy2(source_file, target_file)
254
-
255
- print(f"✓ 已复制: {last_file} (epoch: {last_epoch})")
256
-
257
- total_files_copied += 1
258
-
259
- total_folders += 1
260
-
261
- print(f"\n" + "=" * 60)
262
- print("复制完成!")
263
- print(f"总处理文件夹数: {total_folders}")
264
- print(f"跳过文件夹数(目标已有文件): {total_folders_skipped}")
265
- print(f"总复制文件数: {total_files_copied}")
266
- print(f"源目录: {source_dir}")
267
- print(f"目标目录: {target_dir}")
268
-
269
- # 更稳妥的版本:如果目标文件夹存在且��空,询问用户是否覆盖
270
- def copy_last_epoch_models_safe(source_dir, target_dir, force=False):
271
- """
272
- 更稳妥的版本:如果目标文件夹存在且非空,询问用户是否覆盖
273
-
274
- Args:
275
- source_dir: 源目录路径
276
- target_dir: 目标目录路径
277
- force: 如果为True,则自动跳过已有文件的文件夹;如果为False,则询问用户
278
- """
279
- import os
280
- import shutil
281
- from pathlib import Path
282
- import re
283
-
284
- source_path = Path(source_dir)
285
- target_path = Path(target_dir)
286
-
287
- if not source_path.exists():
288
- print(f"错误:源目录不存在 - {source_dir}")
289
- return
290
-
291
- target_path.mkdir(parents=True, exist_ok=True)
292
-
293
- total_folders = 0
294
- total_folders_skipped = 0
295
- total_folders_processed = 0
296
- total_files_copied = 0
297
-
298
- print(f"开始从 {source_dir} 复制到 {target_dir}")
299
- print("=" * 60)
300
-
301
- for root, dirs, files in os.walk(source_dir):
302
- root_path = Path(root)
303
- relative_path = root_path.relative_to(source_path)
304
- target_subdir = target_path / relative_path
305
-
306
- # 检查目标文件夹状态
307
- if target_subdir.exists() and any(target_subdir.iterdir()):
308
- existing_pkl = list(target_subdir.glob("*.pkl"))
309
-
310
- if not force:
311
- print(f"\n目标文件夹已存在且有文件: {relative_path}")
312
- print(f" 包含 {len(existing_pkl)} 个.pkl文件")
313
- response = input(" 是否跳过此文件夹? (y=跳过, n=处理, a=全部跳过, o=全部处理): ").lower()
314
-
315
- if response == 'a': # 全部跳过
316
- force = True
317
- print(" 已选择: 后续所有已存在的文件夹都将跳过")
318
- total_folders_skipped += 1
319
- continue
320
- elif response == 'o': # 全部处理
321
- force = False
322
- print(" 已选择: 后续所有已存在的文件夹都将被处理")
323
- elif response == 'y': # 跳过当前
324
- total_folders_skipped += 1
325
- continue
326
- # response == 'n' 继续处理
327
- elif force: # force=True时自动跳过
328
- print(f"\n跳过目录: {relative_path} (目标文件夹已有文件)")
329
- total_folders_skipped += 1
330
- continue
331
-
332
- # 创建目标子目录(如果不存在)
333
- if not target_subdir.exists():
334
- target_subdir.mkdir(parents=True, exist_ok=True)
335
-
336
- # 处理.pkl文件...
337
- pkl_files = [f for f in files if f.lower().endswith('.pkl')]
338
-
339
- if not pkl_files:
340
- continue
341
-
342
- print(f"\n处理目录: {relative_path}")
343
- print(f"找到 {len(pkl_files)} 个.pkl文件")
344
-
345
- # 从文件名中提取epoch数字
346
- def extract_epoch_number(filename):
347
- match = re.search(r'ep(\d+)', filename, re.IGNORECASE)
348
- if match:
349
- return int(match.group(1))
350
- match = re.search(r'epoch[_-]?(\d+)', filename, re.IGNORECASE)
351
- if match:
352
- return int(match.group(1))
353
- return 0
354
-
355
- # 创建(epoch_number, filename)的列表并排序
356
- epoch_files = [(extract_epoch_number(f), f) for f in pkl_files]
357
- epoch_files.sort(key=lambda x: x[0])
358
-
359
- # 显示文件信息
360
- for epoch_num, file in epoch_files:
361
- print(f" - {file} (epoch: {epoch_num})")
362
-
363
- # 复制最后一个文件
364
- if epoch_files:
365
- last_epoch, last_file = epoch_files[-1]
366
- source_file = root_path / last_file
367
- target_file = target_subdir / last_file
368
-
369
- shutil.copy2(source_file, target_file)
370
- print(f"✓ 已复制: {last_file} (epoch: {last_epoch})")
371
-
372
- total_files_copied += 1
373
- total_folders_processed += 1
374
-
375
- total_folders += 1
376
-
377
- print(f"\n" + "=" * 60)
378
- print("复制完成!")
379
- print(f"总处理文件夹数: {total_folders}")
380
- print(f"实际处理文件夹数: {total_folders_processed}")
381
- print(f"跳过文件夹数: {total_folders_skipped}")
382
- print(f"总复制文件数: {total_files_copied}")
383
-
384
- # 使用方法:
385
- if __name__ == "__main__":
386
- source_directory = "/NAS/zhouyiling/model/main_task/HPL-Cluster"
387
- target_directory = "/NAS/zhouyiling/hpl_hitph_server/model_files/HPL-Cluster"
388
-
389
- print("开始复制.pkl模型文件...")
390
- print(f"源目录: {source_directory}")
391
- print(f"目标目录: {target_directory}")
392
- print("-" * 50)
393
-
394
- # 方法1:使用基本版本(自动跳过已有文件的文件夹)
395
- copy_last_epoch_models(source_directory, target_directory)
396
-
397
- # 方法2:使用增强版本(处理更多命名模式,也自动跳过)
398
- # copy_last_epoch_models_enhanced(source_directory, target_directory)
399
-
400
- # 方法3:使用安全版本(会询问用户如何处理已存在的文件夹)
401
- # copy_last_epoch_models_safe(source_directory, target_directory, force=False)