Guest User

Untitled

a guest
Jun 24th, 2026
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. import os
  2. import glob
  3. import torch
  4. from huggingface_hub import snapshot_download
  5. from safetensors.torch import load_file, save_file
  6.  
  7. def merge_and_cast_qwen3_fp8():
  8. repo_id = "prithivMLmods/Qwen3-VL-4B-Instruct-abliterated-v1"
  9. output_filename = "qwen3vl_4b_abliterated_fp8.safetensors"
  10.  
  11. print(f"Pulling safetensors from {repo_id}...")
  12. # Downloads only the safetensors to bypass heavy unused binaries
  13. model_path = snapshot_download(repo_id=repo_id, allow_patterns=["*.safetensors"])
  14.  
  15. shard_files = sorted(glob.glob(os.path.join(model_path, "*.safetensors")))
  16. print(f"Found {len(shard_files)} shards. Merging and casting to FP8...")
  17.  
  18. merged_state_dict = {}
  19. for file in shard_files:
  20. print(f"Processing {os.path.basename(file)}...")
  21. shard_dict = load_file(file)
  22.  
  23. for key, tensor in shard_dict.items():
  24. # Cast standard floating point weights down to fp8_e4m3fn
  25. if tensor.is_floating_point():
  26. merged_state_dict[key] = tensor.to(torch.float8_e4m3fn)
  27. else:
  28. # Keep any integer/index tensors exactly as they are
  29. merged_state_dict[key] = tensor
  30.  
  31. print(f"Saving merged FP8 weights to {output_filename}...")
  32. save_file(merged_state_dict, output_filename)
  33. print(f"Done. Place '{output_filename}' into ComfyUI/models/text_encoders/")
  34.  
  35. if __name__ == "__main__":
  36. merge_and_cast_qwen3_fp8()
Advertisement
Add Comment
Please, Sign In to add comment