MCPcopy Index your code
hub / github.com/YesianRohn/TextSSR / get_peft_kwargs

Function get_peft_kwargs

diffusers/src/diffusers/utils/peft_utils.py:150–192  ·  view source on GitHub ↗
(rank_dict, network_alpha_dict, peft_state_dict, is_unet=True)

Source from the content-addressed store, hash-verified

148
149
150def get_peft_kwargs(rank_dict, network_alpha_dict, peft_state_dict, is_unet=True):
151 rank_pattern = {}
152 alpha_pattern = {}
153 r = lora_alpha = list(rank_dict.values())[0]
154
155 if len(set(rank_dict.values())) > 1:
156 # get the rank occuring the most number of times
157 r = collections.Counter(rank_dict.values()).most_common()[0][0]
158
159 # for modules with rank different from the most occuring rank, add it to the `rank_pattern`
160 rank_pattern = dict(filter(lambda x: x[1] != r, rank_dict.items()))
161 rank_pattern = {k.split(".lora_B.")[0]: v for k, v in rank_pattern.items()}
162
163 if network_alpha_dict is not None and len(network_alpha_dict) > 0:
164 if len(set(network_alpha_dict.values())) > 1:
165 # get the alpha occuring the most number of times
166 lora_alpha = collections.Counter(network_alpha_dict.values()).most_common()[0][0]
167
168 # for modules with alpha different from the most occuring alpha, add it to the `alpha_pattern`
169 alpha_pattern = dict(filter(lambda x: x[1] != lora_alpha, network_alpha_dict.items()))
170 if is_unet:
171 alpha_pattern = {
172 ".".join(k.split(".lora_A.")[0].split(".")).replace(".alpha", ""): v
173 for k, v in alpha_pattern.items()
174 }
175 else:
176 alpha_pattern = {".".join(k.split(".down.")[0].split(".")[:-1]): v for k, v in alpha_pattern.items()}
177 else:
178 lora_alpha = set(network_alpha_dict.values()).pop()
179
180 # layer names without the Diffusers specific
181 target_modules = list({name.split(".lora")[0] for name in peft_state_dict.keys()})
182 use_dora = any("lora_magnitude_vector" in k for k in peft_state_dict)
183
184 lora_config_kwargs = {
185 "r": r,
186 "lora_alpha": lora_alpha,
187 "rank_pattern": rank_pattern,
188 "alpha_pattern": alpha_pattern,
189 "target_modules": target_modules,
190 "use_dora": use_dora,
191 }
192 return lora_config_kwargs
193
194
195def get_adapter_name(model):

Calls 1

popMethod · 0.45

Tested by

no test coverage detected