File size: 15,959 Bytes
3e04ca0 | 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | import torch
import torch.distributed.tensor
from safetensors.torch import save_file
import os
from collections import OrderedDict
import gc
def merge_fsdp_to_safetensors(rank0_path, rank1_path, output_path, target_dtype=None):
"""
FSDP๋ก ๋ถํ ๋ ๋ ๊ฐ์ .pt ํ์ผ์ ํ๋์ .safetensors ํ์ผ๋ก ๋ณํฉ
Args:
rank0_path (str): rank 0 .pt ํ์ผ ๊ฒฝ๋ก
rank1_path (str): rank 1 .pt ํ์ผ ๊ฒฝ๋ก
output_path (str): ์ถ๋ ฅํ .safetensors ํ์ผ ๊ฒฝ๋ก
target_dtype (torch.dtype, optional): ํ๊ฒ dtype (์: torch.float16, torch.bfloat16)
"""
print("Loading rank 0 checkpoint...")
rank0_dict = torch.load(rank0_path, map_location='cpu', weights_only=False)
print("Loading rank 1 checkpoint...")
rank1_dict = torch.load(rank1_path, map_location='cpu', weights_only=False)
# DTensor๋ฅผ ์ผ๋ฐ ํ
์๋ก ๋ณํํ๋ ํจ์
def convert_dtensor_to_tensor(state_dict):
converted_dict = OrderedDict()
dtype_info = {}
for key, value in state_dict.items():
if hasattr(value, '_local_tensor'):
# DTensor์ธ ๊ฒฝ์ฐ ๋ก์ปฌ ํ
์ ์ถ์ถ
tensor = value._local_tensor
converted_dict[key] = tensor
dtype_info[key] = tensor.dtype
print(f"Converted DTensor to tensor: {key} (dtype: {tensor.dtype})")
elif isinstance(value, torch.Tensor):
converted_dict[key] = value
dtype_info[key] = value.dtype
else:
# ๋ค๋ฅธ ํ์
์ ๊ทธ๋๋ก ์ ์ง
converted_dict[key] = value
dtype_info[key] = type(value).__name__
return converted_dict, dtype_info
print("Converting DTensors to regular tensors...")
rank0_dict, rank0_dtypes = convert_dtensor_to_tensor(rank0_dict)
rank1_dict, rank1_dtypes = convert_dtensor_to_tensor(rank1_dict)
# dtype ์ ๋ณด ์ถ๋ ฅ
print("\n๐ Original dtype information:")
all_dtypes_r0 = set(dtype_info for dtype_info in rank0_dtypes.values() if isinstance(dtype_info, torch.dtype))
all_dtypes_r1 = set(dtype_info for dtype_info in rank1_dtypes.values() if isinstance(dtype_info, torch.dtype))
all_dtypes = all_dtypes_r0 | all_dtypes_r1
print(f" Rank 0 dtypes found: {all_dtypes_r0}")
print(f" Rank 1 dtypes found: {all_dtypes_r1}")
print(f" All dtypes: {all_dtypes}")
if target_dtype:
print(f" Target dtype specified: {target_dtype}")
else:
print(" No target dtype specified - keeping original dtypes")
# ๋ณํฉ๋ ์ํ ์ฌ์
merged_state_dict = OrderedDict()
# rank 0์ ๋ชจ๋ ํค๋ค์ ๋จผ์ ์ฒ๋ฆฌ
all_keys = set(rank0_dict.keys()) | set(rank1_dict.keys())
print(f"Total unique keys found: {len(all_keys)}")
for key in sorted(all_keys):
rank0_tensor = rank0_dict.get(key)
rank1_tensor = rank1_dict.get(key)
if rank0_tensor is not None and rank1_tensor is not None:
# ๋ rank์ ๋ชจ๋ ์กด์ฌํ๋ ๊ฒฝ์ฐ - ์ฐจ์ ํ์ธ ํ ์ฐ๊ฒฐ
print(f"Merging key: {key}")
print(f" Rank 0 shape: {rank0_tensor.shape}, dtype: {rank0_tensor.dtype}")
print(f" Rank 1 shape: {rank1_tensor.shape}, dtype: {rank1_tensor.dtype}")
# dtype ๋ณํ (ํ์ํ ๊ฒฝ์ฐ)
if target_dtype and rank0_tensor.dtype != target_dtype:
rank0_tensor = rank0_tensor.to(target_dtype)
print(f" Converted rank 0 to {target_dtype}")
if target_dtype and rank1_tensor.dtype != target_dtype:
rank1_tensor = rank1_tensor.to(target_dtype)
print(f" Converted rank 1 to {target_dtype}")
# ์ฒซ ๋ฒ์งธ ์ฐจ์์ผ๋ก ์ฐ๊ฒฐ (์ผ๋ฐ์ ์ธ FSDP ์ค๋ฉ ๋ฐฉ์)
merged_tensor = torch.cat([rank0_tensor, rank1_tensor], dim=0)
merged_state_dict[key] = merged_tensor
print(f" Merged shape: {merged_tensor.shape}, dtype: {merged_tensor.dtype}")
elif rank0_tensor is not None:
# rank 0์๋ง ์กด์ฌ
tensor = rank0_tensor
if target_dtype and isinstance(tensor, torch.Tensor) and tensor.dtype != target_dtype:
tensor = tensor.to(target_dtype)
print(f"Converting {key} from rank 0: {rank0_tensor.dtype} -> {target_dtype}")
print(f"Adding from rank 0: {key} (shape: {tensor.shape if isinstance(tensor, torch.Tensor) else 'N/A'}, dtype: {tensor.dtype if isinstance(tensor, torch.Tensor) else type(tensor).__name__})")
merged_state_dict[key] = tensor
elif rank1_tensor is not None:
# rank 1์๋ง ์กด์ฌ
tensor = rank1_tensor
if target_dtype and isinstance(tensor, torch.Tensor) and tensor.dtype != target_dtype:
tensor = tensor.to(target_dtype)
print(f"Converting {key} from rank 1: {rank1_tensor.dtype} -> {target_dtype}")
print(f"Adding from rank 1: {key} (shape: {tensor.shape if isinstance(tensor, torch.Tensor) else 'N/A'}, dtype: {tensor.dtype if isinstance(tensor, torch.Tensor) else type(tensor).__name__})")
merged_state_dict[key] = tensor
print(f"\nTotal merged parameters: {len(merged_state_dict)}")
# ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ
del rank0_dict, rank1_dict
gc.collect()
# safetensors๋ก ์ ์ฅ
print(f"Saving merged model to {output_path}...")
# ์ต์ข
dtype ์ ๋ณด ์ถ๋ ฅ
final_dtypes = {}
for key, tensor in merged_state_dict.items():
if isinstance(tensor, torch.Tensor):
final_dtypes[tensor.dtype] = final_dtypes.get(tensor.dtype, 0) + 1
print(f"๐ Final merged model dtype distribution:")
for dtype, count in final_dtypes.items():
print(f" {dtype}: {count} tensors")
save_file(merged_state_dict, output_path)
print("โ
Successfully saved merged model!")
return merged_state_dict
def merge_with_custom_concatenation(rank0_path, rank1_path, output_path, concat_rules=None):
"""
์ฌ์ฉ์ ์ ์ ์ฐ๊ฒฐ ๊ท์น์ผ๋ก ๋ณํฉ
Args:
concat_rules (dict): ํค๋ณ ์ฐ๊ฒฐ ์ฐจ์ ์ง์ {'key_pattern': dim}
"""
if concat_rules is None:
# ๊ธฐ๋ณธ ๊ท์น
concat_rules = {
'weight': 0, # ๊ฐ์ค์น๋ ์ฒซ ๋ฒ์งธ ์ฐจ์์ผ๋ก ์ฐ๊ฒฐ
'bias': 0, # ํธํฅ๋ ์ฒซ ๋ฒ์งธ ์ฐจ์์ผ๋ก ์ฐ๊ฒฐ
}
print("Loading checkpoints...")
rank0_dict = torch.load(rank0_path, map_location='cpu', weights_only=False)
rank1_dict = torch.load(rank1_path, map_location='cpu', weights_only=False)
merged_state_dict = OrderedDict()
all_keys = set(rank0_dict.keys()) | set(rank1_dict.keys())
for key in sorted(all_keys):
rank0_tensor = rank0_dict.get(key)
rank1_tensor = rank1_dict.get(key)
if rank0_tensor is not None and rank1_tensor is not None:
# ์ฐ๊ฒฐ ์ฐจ์ ๊ฒฐ์
concat_dim = 0 # ๊ธฐ๋ณธ๊ฐ
for pattern, dim in concat_rules.items():
if pattern in key:
concat_dim = dim
break
print(f"Merging {key} along dimension {concat_dim}")
merged_tensor = torch.cat([rank0_tensor, rank1_tensor], dim=concat_dim)
merged_state_dict[key] = merged_tensor
elif rank0_tensor is not None:
merged_state_dict[key] = rank0_tensor
elif rank1_tensor is not None:
merged_state_dict[key] = rank1_tensor
# ์ ๋ฆฌ ๋ฐ ์ ์ฅ
del rank0_dict, rank1_dict
gc.collect()
print(f"Saving to {output_path}...")
save_file(merged_state_dict, output_path)
print("โ
Merge completed!")
def comprehensive_verification(rank0_path, rank1_path, merged_path):
"""๋ณํฉ์ด ์ฌ๋ฐ๋ฅด๊ฒ ๋์๋์ง ์ข
ํฉ์ ์ผ๋ก ๊ฒ์ฆ"""
import torch.distributed.tensor
from safetensors import safe_open
print("๐ Starting comprehensive verification...")
# 1. ์๋ณธ ํ์ผ๋ค ๋ก๋
print("\n๐ Loading original files...")
rank0_dict = torch.load(rank0_path, map_location='cpu', weights_only=False)
rank1_dict = torch.load(rank1_path, map_location='cpu', weights_only=False)
# DTensor๋ฅผ ์ผ๋ฐ ํ
์๋ก ๋ณํ
def convert_dtensor_to_tensor(state_dict):
converted_dict = {}
for key, value in state_dict.items():
if hasattr(value, '_local_tensor'):
converted_dict[key] = value._local_tensor
elif isinstance(value, torch.Tensor):
converted_dict[key] = value
else:
converted_dict[key] = value
return converted_dict
rank0_dict = convert_dtensor_to_tensor(rank0_dict)
rank1_dict = convert_dtensor_to_tensor(rank1_dict)
# 2. ์๋ณธ ํ์ผ๋ค ๋ถ์
rank0_keys = set(rank0_dict.keys())
rank1_keys = set(rank1_dict.keys())
all_original_keys = rank0_keys | rank1_keys
shared_keys = rank0_keys & rank1_keys
rank0_only = rank0_keys - rank1_keys
rank1_only = rank1_keys - rank0_keys
print(f"๐ Original files analysis:")
print(f" Rank 0 keys: {len(rank0_keys)}")
print(f" Rank 1 keys: {len(rank1_keys)}")
print(f" Shared keys: {len(shared_keys)}")
print(f" Rank 0 only: {len(rank0_only)}")
print(f" Rank 1 only: {len(rank1_only)}")
print(f" Total unique keys: {len(all_original_keys)}")
# 3. ์๋ณธ ํ๋ผ๋ฏธํฐ ์ ๊ณ์ฐ
original_params = 0
original_shapes = {}
for key in all_original_keys:
if key in shared_keys:
# ๊ณต์ ํค๋ ๋ ํ
์๋ฅผ ์ฐ๊ฒฐํ ํฌ๊ธฐ๋ก ๊ณ์ฐ
r0_tensor = rank0_dict[key]
r1_tensor = rank1_dict[key]
combined_shape = list(r0_tensor.shape)
combined_shape[0] += r1_tensor.shape[0] # ์ฒซ ๋ฒ์งธ ์ฐจ์์ผ๋ก ์ฐ๊ฒฐ ๊ฐ์
original_shapes[key] = tuple(combined_shape)
original_params += r0_tensor.numel() + r1_tensor.numel()
elif key in rank0_only:
original_shapes[key] = rank0_dict[key].shape
original_params += rank0_dict[key].numel()
elif key in rank1_only:
original_shapes[key] = rank1_dict[key].shape
original_params += rank1_dict[key].numel()
print(f" Original total parameters: {original_params:,}")
# 4. ๋ณํฉ๋ ํ์ผ ๋ถ์
print(f"\n๐ Loading merged file: {merged_path}")
merged_params = 0
merged_keys = set()
merged_shapes = {}
with safe_open(merged_path, framework="pt", device="cpu") as f:
merged_keys = set(f.keys())
for key in f.keys():
tensor = f.get_tensor(key)
merged_shapes[key] = tensor.shape
merged_params += tensor.numel()
print(f"๐ Merged file analysis:")
print(f" Merged keys: {len(merged_keys)}")
print(f" Merged parameters: {merged_params:,}")
# 5. ๋น๊ต ๋ฐ ๊ฒ์ฆ
print(f"\nโ
Verification Results:")
# ํค ๊ฐ์ ๋น๊ต
keys_match = len(merged_keys) == len(all_original_keys)
print(f" Keys count match: {keys_match} ({len(merged_keys)} vs {len(all_original_keys)})")
# ํ๋ผ๋ฏธํฐ ์ ๋น๊ต
params_match = merged_params == original_params
print(f" Parameter count match: {params_match} ({merged_params:,} vs {original_params:,})")
# ํค ์ด๋ฆ ๋น๊ต
missing_keys = all_original_keys - merged_keys
extra_keys = merged_keys - all_original_keys
if missing_keys:
print(f" โ Missing keys: {missing_keys}")
if extra_keys:
print(f" โ Extra keys: {extra_keys}")
# ๊ฐ๋ณ ํ
์ ํฌ๊ธฐ ๋น๊ต
shape_mismatches = []
for key in merged_keys & all_original_keys:
if merged_shapes[key] != original_shapes[key]:
shape_mismatches.append((key, merged_shapes[key], original_shapes[key]))
if shape_mismatches:
print(f" โ Shape mismatches:")
for key, merged_shape, original_shape in shape_mismatches[:5]: # ์ฒ์ 5๊ฐ๋ง ํ์
print(f" {key}: {merged_shape} vs {original_shape}")
if len(shape_mismatches) > 5:
print(f" ... and {len(shape_mismatches) - 5} more")
# 6. ์ธ๋ถ ๋ถ์ (์ ํ์ )
print(f"\n๐ Detailed Analysis:")
print(f" Shared keys that should be concatenated:")
for key in sorted(list(shared_keys))[:10]: # ์ฒ์ 10๊ฐ๋ง ํ์
r0_shape = rank0_dict[key].shape
r1_shape = rank1_dict[key].shape
expected_shape = list(r0_shape)
expected_shape[0] += r1_shape[0]
actual_shape = merged_shapes.get(key, "MISSING")
status = "โ
" if tuple(expected_shape) == actual_shape else "โ"
print(f" {status} {key}: {r0_shape} + {r1_shape} -> {actual_shape}")
if len(shared_keys) > 10:
print(f" ... and {len(shared_keys) - 10} more shared keys")
# 7. ์ต์ข
๊ฒฐ๊ณผ
overall_success = keys_match and params_match and not missing_keys and not extra_keys and not shape_mismatches
print(f"\n{'='*50}")
if overall_success:
print("๐ MERGE VERIFICATION SUCCESSFUL!")
print(" All parameters have been correctly merged.")
else:
print("โ ๏ธ MERGE VERIFICATION FOUND ISSUES!")
print(" Please review the mismatches above.")
print(f"{'='*50}")
# ์ ๋ฆฌ
del rank0_dict, rank1_dict
gc.collect()
return overall_success
# ์ฌ์ฉ ์์
if __name__ == "__main__":
# ํ์ผ ๊ฒฝ๋ก ์ค์
rank0_file = "model_rank_0.pt" # ์ค์ ํ์ผ๋ช
์ผ๋ก ๋ณ๊ฒฝ
rank1_file = "model_rank_1.pt" # ์ค์ ํ์ผ๋ช
์ผ๋ก ๋ณ๊ฒฝ
output_file = "merged_model.safetensors"
# dtype ์ต์
์ค์
target_dtype = torch.bfloat16 # bf16์ผ๋ก ๋ณํ
# ๊ธฐ๋ณธ ๋ณํฉ
print("Starting merge process...")
merged_dict = merge_fsdp_to_safetensors(rank0_file, rank1_file, output_file, target_dtype)
# ์ข
ํฉ์ ์ธ ๊ฒ์ฆ
print("\nStarting comprehensive verification...")
verification_passed = comprehensive_verification(rank0_file, rank1_file, output_file)
if verification_passed:
print(f"\n๐ Successfully merged and verified FSDP model to {output_file}")
else:
print(f"\nโ ๏ธ Merge completed but verification found issues. Please review the output above.")
# ์ถ๊ฐ: ๊ฐ๋จํ ๋ก๋ ํ
์คํธ
print(f"\n๐ Testing if merged model can be loaded...")
try:
from safetensors import safe_open
with safe_open(output_file, framework="pt", device="cpu") as f:
sample_keys = list(f.keys())[:3]
for key in sample_keys:
tensor = f.get_tensor(key)
print(f" โ
Successfully loaded {key}: {tensor.shape}, dtype: {tensor.dtype}")
print(" โ
Merged model loads correctly!")
except Exception as e:
print(f" โ Error loading merged model: {e}")
print(f"\n๐ก Tip: To change dtype, modify 'target_dtype' in the script:")
print(f" - torch.float16 for fp16 (smaller file, less precision)")
print(f" - torch.bfloat16 for bf16 (good balance)")
print(f" - torch.float32 for fp32 (larger file, best precision)")
print(f" - None to keep original dtypes")
|