MCPcopy Create free account
hub / github.com/ModelTC/LightX2V / LoRAPatternMatcher

Class LoRAPatternMatcher

lightx2v/utils/lora_loader.py:66–178  ·  view source on GitHub ↗

Detects and matches LoRA format patterns in state dicts.

Source from the content-addressed store, hash-verified

64
65
66class LoRAPatternMatcher:
67 """Detects and matches LoRA format patterns in state dicts."""
68
69 def __init__(self):
70 """Initialize the pattern matcher with all supported formats."""
71 self.patterns: Dict[LoRAFormat, LoRAPatternDefinition] = {
72 LoRAFormat.STANDARD: LoRAPatternDefinition(
73 LoRAFormat.STANDARD,
74 up_suffix=".lora_up.weight",
75 down_suffix=".lora_down.weight",
76 mid_suffix=".lora_mid.weight",
77 ),
78 LoRAFormat.DIFFUSERS: LoRAPatternDefinition(
79 LoRAFormat.DIFFUSERS,
80 up_suffix="_lora.up.weight",
81 down_suffix="_lora.down.weight",
82 ),
83 LoRAFormat.DIFFUSERS_V2: LoRAPatternDefinition(
84 LoRAFormat.DIFFUSERS_V2,
85 up_suffix=".lora_B.weight",
86 down_suffix=".lora_A.weight",
87 ),
88 LoRAFormat.DIFFUSERS_V3: LoRAPatternDefinition(
89 LoRAFormat.DIFFUSERS_V3,
90 up_suffix=".lora.up.weight",
91 down_suffix=".lora.down.weight",
92 ),
93 LoRAFormat.MOCHI: LoRAPatternDefinition(
94 LoRAFormat.MOCHI,
95 up_suffix=".lora_B",
96 down_suffix=".lora_A",
97 has_weight_suffix=False,
98 ),
99 LoRAFormat.TRANSFORMERS: LoRAPatternDefinition(
100 LoRAFormat.TRANSFORMERS,
101 up_suffix=".lora_linear_layer.up.weight",
102 down_suffix=".lora_linear_layer.down.weight",
103 ),
104 LoRAFormat.QWEN: LoRAPatternDefinition(
105 LoRAFormat.QWEN,
106 up_suffix=".lora_B.default.weight",
107 down_suffix=".lora_A.default.weight",
108 ),
109 }
110
111 def detect_format(self, key: str, lora_weights: Dict) -> Optional[Tuple[LoRAFormat, str]]:
112 """
113 Detect the LoRA format of a given key.
114
115 Args:
116 key: The weight key to check
117 lora_weights: The full LoRA weights dictionary
118
119 Returns:
120 Tuple of (LoRAFormat, detected_suffix) if format detected, None otherwise
121 """
122 for format_type, pattern in self.patterns.items():
123 if key.endswith(pattern.up_suffix):

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected