Guest User

Untitled

a guest
Apr 22nd, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.35 KB | None | 0 0
  1. import torch
  2. import os
  3. import sys
  4. import safetensors.torch
  5. from safetensors.torch import load_file, save_file
  6.  
  7. BLOCKID26=["BASE","IN00","IN01","IN02","IN03","IN04","IN05","IN06","IN07","IN08","IN09","IN10","IN11","M00","OUT00","OUT01","OUT02","OUT03","OUT04","OUT05","OUT06","OUT07","OUT08","OUT09","OUT10","OUT11"]
  8. BLOCKID17=["BASE","IN01","IN02","IN04","IN05","IN07","IN08","M00","OUT03","OUT04","OUT05","OUT06","OUT07","OUT08","OUT09","OUT10","OUT11"]
  9. BLOCKID12=["BASE","IN04","IN05","IN07","IN08","M00","OUT00","OUT01","OUT02","OUT03","OUT04","OUT05"]
  10. BLOCKID20=["BASE","IN00","IN01","IN02","IN03","IN04","IN05","IN06","IN07","IN08","M00","OUT00","OUT01","OUT02","OUT03","OUT04","OUT05","OUT06","OUT07","OUT08"]
  11. BLOCKNUMS = [12,17,20,26]
  12. BLOCKIDS=[BLOCKID12,BLOCKID17,BLOCKID20,BLOCKID26]
  13.  
  14. BLOCKIDX=["encoder",
  15. "lora_unet_input_blocks_0_",
  16. "lora_unet_input_blocks_1_",
  17. "lora_unet_input_blocks_2_",
  18. "lora_unet_input_blocks_3_",
  19. "lora_unet_input_blocks_4_",
  20. "lora_unet_input_blocks_5_",
  21. "lora_unet_input_blocks_6_",
  22. "lora_unet_input_blocks_7_",
  23. "lora_unet_input_blocks_8_",
  24. "lora_unet_input_blocks_9_",
  25. "lora_unet_input_blocks_10_",
  26. "lora_unet_input_blocks_11_",
  27. "lora_unet_middle_block_",
  28. "lora_unet_output_blocks_0_",
  29. "lora_unet_output_blocks_1_",
  30. "lora_unet_output_blocks_2_",
  31. "lora_unet_output_blocks_3_",
  32. "lora_unet_output_blocks_4_",
  33. "lora_unet_output_blocks_5_",
  34. "lora_unet_output_blocks_6_",
  35. "lora_unet_output_blocks_7_",
  36. "lora_unet_output_blocks_8_",
  37. "lora_unet_output_blocks_9_",
  38. "lora_unet_output_blocks_10_",
  39. "lora_unet_output_blocks_11_",
  40. ]
  41.  
  42. def to26(ratios):
  43.     if len(ratios) == 26: return ratios
  44.     ids = BLOCKIDS[BLOCKNUMS.index(len(ratios))]
  45.     output = [0]*26
  46.     for i, id in enumerate(ids):
  47.         output[BLOCKID26.index(id)] = ratios[i]
  48.     return output
  49.  
  50. device = "cuda"
  51. filename = "d:/ai/models/lora/jrpg_characters_dora-000012.safetensors"
  52. outfilename = "C:/ai/automatic/models/Lora/EnvyStarlighJRPGCharacters.safetensors"
  53. weights = [0,0,0,0,0,0,1,1,1,0,0,0]
  54.  
  55. def load_lora(file_name):
  56.     sd = load_file(file_name,device=device)
  57.  
  58.     for key in list(sd.keys()):
  59.         #print(f"{key}: {type(sd[key])}")
  60.         if type(sd[key]) == torch.Tensor:
  61.             sd[key] = sd[key].to(device = device)
  62.             if "resblocks" in key:
  63.                 isv2 = True
  64.  
  65.     return sd
  66.  
  67.  
  68. def load_metadata_from_safetensors(safetensors_file: str) -> dict:
  69.     """
  70.    This method locks the file. see https://github.com/huggingface/safetensors/issues/164
  71.    If the file isn't .safetensors or doesn't have metadata, return empty dict.
  72.    """
  73.     if os.path.splitext(safetensors_file)[1] != ".safetensors":
  74.         return {}
  75.  
  76.     with safetensors.safe_open(safetensors_file, framework="pt", device="cpu") as f:
  77.         metadata = f.metadata()
  78.     if metadata is None:
  79.         metadata = {}
  80.     return metadata
  81.  
  82. def get_idx(key):
  83.     for i, k in enumerate(BLOCKIDX):
  84.         if k in key:
  85.             return i
  86.     return None
  87.  
  88. lora = load_lora(filename)
  89. metadata = load_metadata_from_safetensors(filename)
  90. weights = to26(weights)
  91.  
  92. for key in list(lora.keys()):
  93.     idx = get_idx(key)
  94.     if idx != None:
  95.         print(f"{key}: {idx}")
  96.     else:
  97.         print(f"{key}: no index")
  98.         continue
  99.    
  100.     if 'lora_up' in key or 'lora_down' in key:
  101.         lora[key] *= weights[idx]
  102.  
  103.  
  104.  
  105. save_file(lora, outfilename)
Advertisement
Add Comment
Please, Sign In to add comment