Upload inspect_lora_detailed.py with huggingface_hub
Browse files- inspect_lora_detailed.py +82 -0
inspect_lora_detailed.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
from safetensors.torch import load_file
|
3 |
+
import torch # Ensure torch is imported
|
4 |
+
|
5 |
+
def inspect_lora_keys_detailed(lora_path, search_terms=None):
|
6 |
+
"""
|
7 |
+
Loads a LoRA file, prints its first few keys, and searches for specific terms.
|
8 |
+
"""
|
9 |
+
if search_terms is None:
|
10 |
+
search_terms = []
|
11 |
+
try:
|
12 |
+
state_dict = load_file(lora_path)
|
13 |
+
print(f"Successfully loaded LoRA from: {lora_path}")
|
14 |
+
print("\nListing the first 20 keys (or all if fewer):")
|
15 |
+
keys = list(state_dict.keys())
|
16 |
+
for i, key in enumerate(keys):
|
17 |
+
if i >= 20:
|
18 |
+
break
|
19 |
+
print(f" Key: \"{key}\", Shape: {state_dict[key].shape}, Dtype: {state_dict[key].dtype}")
|
20 |
+
|
21 |
+
if search_terms:
|
22 |
+
print(f"\n--- Searching for keys containing any of: {search_terms} ---")
|
23 |
+
overall_found_count = 0
|
24 |
+
for term in search_terms:
|
25 |
+
term_found_this_iteration = 0
|
26 |
+
print(f"\nSearching for keys containing '{term}':")
|
27 |
+
for key in keys:
|
28 |
+
if term in key:
|
29 |
+
print(f" Found: \"{key}\", Shape: {state_dict[key].shape}, Dtype: {state_dict[key].dtype}")
|
30 |
+
overall_found_count += 1
|
31 |
+
term_found_this_iteration += 1
|
32 |
+
if term_found_this_iteration == 0:
|
33 |
+
print(f" No keys found containing '{term}'.")
|
34 |
+
if overall_found_count == 0 and search_terms:
|
35 |
+
print(f"\nNo keys found matching any of the search terms: {search_terms} across the entire file.")
|
36 |
+
|
37 |
+
# Attempt to find and print a common alpha value if present
|
38 |
+
alpha_keys_simple_suffix = [k for k in keys if k.endswith(".alpha")] # e.g. some.module.alpha
|
39 |
+
|
40 |
+
# More complex check for alpha, e.g., if it's like module_name.alpha where module_name is the base for lora_down/up
|
41 |
+
alpha_keys_complex = []
|
42 |
+
# Create a set of base module names from lora keys
|
43 |
+
lora_base_modules = set()
|
44 |
+
for k_lora in keys:
|
45 |
+
if ".lora_down.weight" in k_lora:
|
46 |
+
lora_base_modules.add(k_lora.rsplit(".lora_down.weight", 1)[0])
|
47 |
+
elif ".lora_up.weight" in k_lora:
|
48 |
+
lora_base_modules.add(k_lora.rsplit(".lora_up.weight", 1)[0])
|
49 |
+
elif ".lora_A.weight" in k_lora: # For inspecting original ComfyUI ones too
|
50 |
+
lora_base_modules.add(k_lora.rsplit(".lora_A.weight", 1)[0])
|
51 |
+
elif ".lora_B.weight" in k_lora:
|
52 |
+
lora_base_modules.add(k_lora.rsplit(".lora_B.weight", 1)[0])
|
53 |
+
|
54 |
+
for base_module in lora_base_modules:
|
55 |
+
potential_alpha_key = base_module + ".alpha"
|
56 |
+
if potential_alpha_key in state_dict:
|
57 |
+
alpha_keys_complex.append(potential_alpha_key)
|
58 |
+
|
59 |
+
all_found_alpha_keys = list(set(alpha_keys_simple_suffix + alpha_keys_complex))
|
60 |
+
|
61 |
+
if all_found_alpha_keys:
|
62 |
+
# Just print the first few found for brevity
|
63 |
+
print(f"\nFound {len(all_found_alpha_keys)} alpha key(s). Examples:")
|
64 |
+
for i, alpha_key in enumerate(all_found_alpha_keys):
|
65 |
+
if i >= 5: # Print max 5 examples
|
66 |
+
print(f" ...and {len(all_found_alpha_keys) - 5} more.")
|
67 |
+
break
|
68 |
+
print(f" Key: '{alpha_key}', Value: {state_dict[alpha_key]}")
|
69 |
+
else:
|
70 |
+
print("\nNo obvious '.alpha' keys found with common naming patterns.")
|
71 |
+
|
72 |
+
|
73 |
+
except Exception as e:
|
74 |
+
print(f"Error loading or processing LoRA file: {e}")
|
75 |
+
print("Please ensure you provide a valid .safetensors LoRA file path.")
|
76 |
+
|
77 |
+
if __name__ == "__main__":
|
78 |
+
parser = argparse.ArgumentParser(description="Inspect keys in a LoRA .safetensors file, with search functionality.")
|
79 |
+
parser.add_argument("lora_path", type=str, help="Path to the LoRA .safetensors file")
|
80 |
+
parser.add_argument("--search", type=str, nargs="+", help="Optional: one or more terms to search for in keys (e.g., --search img_emb cross_attn)")
|
81 |
+
args = parser.parse_args()
|
82 |
+
inspect_lora_keys_detailed(args.lora_path, search_terms=args.search)
|