|
|
import torch
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TARGET_PRECISION = 'fp32'
|
|
|
|
|
|
|
|
|
fp32_checkpoint_path = r"E:\comfyui\ComfyUI-aki-v1.3\models\SDMatte\1\SDMatte_plus.pth"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output_filename = fp32_checkpoint_path.replace('.pth', f'_{TARGET_PRECISION}_inference.pth')
|
|
|
|
|
|
if not os.path.exists(fp32_checkpoint_path):
|
|
|
print(f"[错误] 文件不存在: {fp32_checkpoint_path}")
|
|
|
else:
|
|
|
try:
|
|
|
print(f"--- 开始处理训练检查点: {fp32_checkpoint_path} ---")
|
|
|
full_checkpoint = torch.load(fp32_checkpoint_path, map_location="cpu", weights_only=False)
|
|
|
|
|
|
if 'model' in full_checkpoint:
|
|
|
state_dict = full_checkpoint['model']
|
|
|
print("成功提取到 'model' 键中的权重字典。")
|
|
|
else:
|
|
|
print("[警告] 未在顶层找到 'model' 键,将尝试转换整个文件。")
|
|
|
state_dict = full_checkpoint
|
|
|
|
|
|
print(f"目标输出精度: {TARGET_PRECISION}")
|
|
|
|
|
|
|
|
|
|
|
|
if TARGET_PRECISION != 'fp32':
|
|
|
print(f"开始将权重转换为 {TARGET_PRECISION} ...")
|
|
|
target_dtype = torch.float16 if TARGET_PRECISION == 'fp16' else torch.bfloat16
|
|
|
|
|
|
for key in state_dict:
|
|
|
if isinstance(state_dict[key], torch.Tensor) and state_dict[key].is_floating_point():
|
|
|
state_dict[key] = state_dict[key].to(target_dtype)
|
|
|
else:
|
|
|
print("保留原始 FP32 精度,仅剥离训练数据。")
|
|
|
|
|
|
|
|
|
print(f"正在保存纯推理模型到: {output_filename} ...")
|
|
|
torch.save(state_dict, output_filename)
|
|
|
|
|
|
original_size_gb = os.path.getsize(fp32_checkpoint_path) / (1024**3)
|
|
|
final_size_gb = os.path.getsize(output_filename) / (1024**3)
|
|
|
|
|
|
print("\n--- 转换成功 ---")
|
|
|
print(f"原始训练检查点大小: {original_size_gb:.2f} GB")
|
|
|
print(f"生成的纯推理模型大小 ({TARGET_PRECISION.upper()}): {final_size_gb:.2f} GB")
|
|
|
print("说明: 新文件只包含用于推理的核心模型权重,已移除训练相关的优化器状态。")
|
|
|
|
|
|
except Exception as e:
|
|
|
print(f"\n[错误] 处理过程中发生错误: {e}") |