Spaces:
Running
Running
Create utils.py
Browse files
utils.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import boto3
|
2 |
+
from boto3.s3.transfer import TransferConfig
|
3 |
+
from tqdm import tqdm
|
4 |
+
import os
|
5 |
+
|
6 |
+
def upload_file_to_s3(file_path, bucket_name, s3_prefix):
|
7 |
+
|
8 |
+
|
9 |
+
class ProgressPercentage(object):
|
10 |
+
def __init__(self, filename):
|
11 |
+
self._filename = filename
|
12 |
+
self._size = float(os.path.getsize(filename))
|
13 |
+
self._seen_so_far = 0
|
14 |
+
self._pbar = tqdm(total=self._size, unit='B', unit_scale=True, desc=f"Uploading {os.path.basename(filename)}")
|
15 |
+
|
16 |
+
def __call__(self, bytes_amount):
|
17 |
+
self._seen_so_far += bytes_amount
|
18 |
+
self._pbar.update(bytes_amount)
|
19 |
+
|
20 |
+
s3_client = boto3.client('s3')
|
21 |
+
file_name = os.path.basename(file_path)
|
22 |
+
s3_path = f"{s3_prefix}/{file_name}"
|
23 |
+
|
24 |
+
# Configure multipart upload
|
25 |
+
config = TransferConfig(
|
26 |
+
multipart_threshold=1024 * 25, # 25MB
|
27 |
+
max_concurrency=10,
|
28 |
+
multipart_chunksize=1024 * 25, # 25MB
|
29 |
+
use_threads=True
|
30 |
+
)
|
31 |
+
|
32 |
+
try:
|
33 |
+
s3_client.upload_file(
|
34 |
+
file_path,
|
35 |
+
bucket_name,
|
36 |
+
s3_path,
|
37 |
+
Config=config,
|
38 |
+
Callback=ProgressPercentage(file_path)
|
39 |
+
)
|
40 |
+
return f"s3://{bucket_name}/{s3_path}"
|
41 |
+
except Exception as e:
|
42 |
+
print(f"Failed to upload {file_path} to S3: {str(e)}")
|
43 |
+
return None
|
44 |
+
|
45 |
+
max_lr = 1e-3
|
46 |
+
warmup_steps = 10
|
47 |
+
max_steps = 25000
|
48 |
+
import math
|
49 |
+
def get_lr_lambda(current_step, warmup_steps, max_steps, max_lr):
|
50 |
+
"""
|
51 |
+
Learning rate scheduler with:
|
52 |
+
1. Linear warmup
|
53 |
+
2. Cosine decay
|
54 |
+
3. Minimum learning rate of 10% of max_lr
|
55 |
+
"""
|
56 |
+
min_lr = max_lr * 0.1 # Minimum learning rate (10% of max_lr)
|
57 |
+
|
58 |
+
if current_step < warmup_steps:
|
59 |
+
# Linear warmup
|
60 |
+
return max_lr * (current_step + 1) / warmup_steps
|
61 |
+
elif current_step > max_steps:
|
62 |
+
# After max_steps, return minimum learning rate
|
63 |
+
return min_lr
|
64 |
+
else:
|
65 |
+
# Cosine decay between warmup_steps and max_steps
|
66 |
+
decay_ratio = (current_step - warmup_steps) / (max_steps - warmup_steps)
|
67 |
+
coeff = 0.5 * (1.0 + math.cos(math.pi * decay_ratio))
|
68 |
+
return min_lr + coeff * (max_lr - min_lr)
|
69 |
+
|
70 |
+
|
71 |
+
def plot_lr_schedule():
|
72 |
+
"""
|
73 |
+
Helper function to visualize the learning rate schedule
|
74 |
+
"""
|
75 |
+
import matplotlib.pyplot as plt
|
76 |
+
steps = list(range(0, max_steps + 100))
|
77 |
+
lrs = [get_lr_lambda(step, warmup_steps, max_steps, max_lr) for step in steps]
|
78 |
+
|
79 |
+
plt.figure(figsize=(10, 5))
|
80 |
+
plt.plot(steps, lrs)
|
81 |
+
plt.title('Learning Rate Schedule')
|
82 |
+
plt.xlabel('Steps')
|
83 |
+
plt.ylabel('Learning Rate')
|
84 |
+
plt.grid(True)
|
85 |
+
plt.show()
|
86 |
+
|
87 |
+
if __name__ == "__main__":
|
88 |
+
plot_lr_schedule()
|