MCPcopy Create free account
hub / github.com/Francis-Rings/FlashPortrait / create_modules

Method create_modules

wan/utils/lora_utils.py:191–247  ·  view source on GitHub ↗
(
            is_unet: bool,
            root_module: torch.nn.Module,
            target_replace_modules: List[torch.nn.Module],
        )

Source from the content-addressed store, hash-verified

189
190 # create module instances
191 def create_modules(
192 is_unet: bool,
193 root_module: torch.nn.Module,
194 target_replace_modules: List[torch.nn.Module],
195 ) -> List[LoRAModule]:
196 prefix = (
197 self.LORA_PREFIX_TRANSFORMER
198 if is_unet
199 else self.LORA_PREFIX_TEXT_ENCODER
200 )
201 loras = []
202 skipped = []
203 for name, module in root_module.named_modules():
204 if module.__class__.__name__ in target_replace_modules:
205 for child_name, child_module in module.named_modules():
206 is_linear = child_module.__class__.__name__ == "Linear" or child_module.__class__.__name__ == "LoRACompatibleLinear"
207 is_conv2d = child_module.__class__.__name__ == "Conv2d" or child_module.__class__.__name__ == "LoRACompatibleConv"
208 is_conv2d_1x1 = is_conv2d and child_module.kernel_size == (1, 1)
209
210 if skip_name is not None and skip_name in child_name:
211 continue
212
213 if target_name is not None:
214 target_name_in = False
215 if isinstance(target_name, str):
216 target_name_in = target_name in child_name
217 elif isinstance(target_name, list):
218 target_name_in = any([_target_name in child_name for _target_name in target_name])
219 if not target_name_in:
220 continue
221
222 if is_linear or is_conv2d:
223 lora_name = prefix + "." + name + "." + child_name
224 lora_name = lora_name.replace(".", "_")
225
226 dim = None
227 alpha = None
228
229 if is_linear or is_conv2d_1x1:
230 dim = self.lora_dim
231 alpha = self.alpha
232
233 if dim is None or dim == 0:
234 if is_linear or is_conv2d_1x1:
235 skipped.append(lora_name)
236 continue
237
238 lora = module_class(
239 lora_name,
240 child_module,
241 self.multiplier,
242 dim,
243 alpha,
244 dropout=dropout,
245 )
246 loras.append(lora)
247 return loras, skipped
248

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected