File size: 1,173 Bytes
ee594f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import re

with open('Train_CMTLNet_Step1.py', 'r') as f:
    content = f.read()

# Remove tqdm import
content = re.sub(r'from tqdm import tqdm.*?\n', '', content)

# Remove Checkpoint prints
content = re.sub(r' *print\("\[Checkpoint \d/6\].*?\n', '', content)

# Replace tqdm loop with regular loop and add print statement
content = content.replace(
    'pbar = tqdm(range(len(train_loader_1p19q)), desc=f"Epoch {epoch}/{config.epochs_s1}", dynamic_ncols=True)\n        \n        for batch_idx in pbar:',
    'for batch_idx in range(len(train_loader_1p19q)):'
)

content = content.replace('pbar.set_postfix({"RAM": f"{mem_gb:.2f} GB"})', '')

# Insert the batch print
target = 'scaler.update()\n            # --------------------------------------------------'
replacement = '''scaler.update()
            # --------------------------------------------------
            
            if batch_idx % 10 == 0:
                print(f"   [Epoch {epoch}] Batch {batch_idx}/{len(train_loader_1p19q)} | Loss: {total_loss.item():.4f} | RAM: {mem_gb:.2f} GB")'''

content = content.replace(target, replacement)

with open('Train_CMTLNet_Step1.py', 'w') as f:
    f.write(content)