Instant resume without re-downloading completed MoE shards
Browse files- step3_streaming_surgery_colab.py +40 -16
step3_streaming_surgery_colab.py
CHANGED
|
@@ -1231,21 +1231,44 @@ def run_streaming_surgery(
|
|
| 1231 |
read_dir, write_dir = buffer_mgr.get_read_write_dirs(parity)
|
| 1232 |
|
| 1233 |
if st_name in selection_report["completed_stages"]:
|
| 1234 |
-
|
| 1235 |
-
|
| 1236 |
-
|
| 1237 |
-
|
| 1238 |
-
|
| 1239 |
-
|
| 1240 |
-
|
| 1241 |
-
|
| 1242 |
-
|
| 1243 |
-
|
| 1244 |
-
|
| 1245 |
-
|
| 1246 |
-
|
| 1247 |
-
|
| 1248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1249 |
|
| 1250 |
t0 = time.time()
|
| 1251 |
print(
|
|
@@ -1735,7 +1758,8 @@ def run_streaming_surgery(
|
|
| 1735 |
# -------------------------------------------------------------------
|
| 1736 |
# CẬP NHẬT CHECKPOINT, ĐẨY LÊN HUGGING FACE & XÓA SHARD ĐÃ DÙNG XONG
|
| 1737 |
# -------------------------------------------------------------------
|
| 1738 |
-
selection_report["completed_stages"]
|
|
|
|
| 1739 |
with open(report_path, "w", encoding="utf-8") as f:
|
| 1740 |
json.dump(selection_report, f, indent=2, ensure_ascii=False)
|
| 1741 |
|
|
|
|
| 1231 |
read_dir, write_dir = buffer_mgr.get_read_write_dirs(parity)
|
| 1232 |
|
| 1233 |
if st_name in selection_report["completed_stages"]:
|
| 1234 |
+
print(f"[Skip] Giai đoạn [{st_name}] đã hoàn thành trong Checkpoint trước.")
|
| 1235 |
+
if layer_type in ("embedding", "dense", "hash_moe", "moe"):
|
| 1236 |
+
parity += 1
|
| 1237 |
+
shard_mgr.delete_finished_shards(stage["shards_safe_to_delete"])
|
| 1238 |
+
continue
|
| 1239 |
+
|
| 1240 |
+
# Nếu nối lại (Resume) trên một máy ảo Colab mới tinh (ổ cứng `/content/activation_buffer` trống),
|
| 1241 |
+
# tự động khởi tạo bộ đệm `read_dir` từ tầng `embed` (1.85 GB, mất ~10 giây) để bắt đầu ngay từ tầng hiện tại
|
| 1242 |
+
# mà KHÔNG cần tải lại các tầng MoE 13.5 GB đã cắt xong!
|
| 1243 |
+
if layer_type in ("dense", "hash_moe", "moe", "mtp_moe") and not os.path.isfile(
|
| 1244 |
+
os.path.join(read_dir, "chunk_0000.pt")
|
| 1245 |
+
):
|
| 1246 |
+
print(
|
| 1247 |
+
f"[*] Phát hiện Resume từ [{st_name}] trên phiên Colab mới (chưa có activation_buffer). "
|
| 1248 |
+
f"Đang khởi tạo nhanh bộ đệm tín hiệu đầu vào (~10s) để nhảy thẳng tới [{st_name}]...",
|
| 1249 |
+
flush=True,
|
| 1250 |
+
)
|
| 1251 |
+
emb_stage = stage_plans[0]
|
| 1252 |
+
emb_tensors_init = shard_mgr.load_specific_tensors(emb_stage["tensors_map"])
|
| 1253 |
+
emb_key_init = next(
|
| 1254 |
+
k
|
| 1255 |
+
for k in emb_tensors_init.keys()
|
| 1256 |
+
if k == "embed.weight" or "embed_tokens.weight" in k
|
| 1257 |
+
)
|
| 1258 |
+
emb_w_init = emb_tensors_init[emb_key_init].to(device, dtype=torch.bfloat16)
|
| 1259 |
+
for c_idx in range(num_chunks):
|
| 1260 |
+
s_i = c_idx * batch_size
|
| 1261 |
+
e_i = min(num_samples, (c_idx + 1) * batch_size)
|
| 1262 |
+
ids_batch = input_ids_all[s_i:e_i].to(device, dtype=torch.long)
|
| 1263 |
+
ids_batch = torch.clamp(ids_batch, 0, emb_w_init.shape[0] - 1)
|
| 1264 |
+
hs_batch = F.embedding(ids_batch, emb_w_init)
|
| 1265 |
+
if hc_mult > 1:
|
| 1266 |
+
hs_batch = hs_batch.unsqueeze(2).repeat(1, 1, hc_mult, 1)
|
| 1267 |
+
buffer_mgr.save_chunk(read_dir, c_idx, hs_batch)
|
| 1268 |
+
del emb_tensors_init, emb_w_init
|
| 1269 |
+
shard_mgr.delete_finished_shards(emb_stage["shards_safe_to_delete"])
|
| 1270 |
+
if str(device).startswith("cuda") and torch.cuda.is_available():
|
| 1271 |
+
torch.cuda.empty_cache()
|
| 1272 |
|
| 1273 |
t0 = time.time()
|
| 1274 |
print(
|
|
|
|
| 1758 |
# -------------------------------------------------------------------
|
| 1759 |
# CẬP NHẬT CHECKPOINT, ĐẨY LÊN HUGGING FACE & XÓA SHARD ĐÃ DÙNG XONG
|
| 1760 |
# -------------------------------------------------------------------
|
| 1761 |
+
if st_name not in selection_report["completed_stages"]:
|
| 1762 |
+
selection_report["completed_stages"].append(st_name)
|
| 1763 |
with open(report_path, "w", encoding="utf-8") as f:
|
| 1764 |
json.dump(selection_report, f, indent=2, ensure_ascii=False)
|
| 1765 |
|