MCPcopy Index your code
hub / github.com/huggingface/diffusers / _find_submodule_by_name

Function _find_submodule_by_name

src/diffusers/hooks/context_parallel.py:363–382  ·  view source on GitHub ↗
(model: torch.nn.Module, name: str)

Source from the content-addressed store, hash-verified

361
362
363def _find_submodule_by_name(model: torch.nn.Module, name: str) -> torch.nn.Module | list[torch.nn.Module]:
364 if name == "":
365 return model
366 first_atom, remaining_name = name.split(".", 1) if "." in name else (name, "")
367 if first_atom == "*":
368 if not isinstance(model, torch.nn.ModuleList):
369 raise ValueError("Wildcard '*' can only be used with ModuleList")
370 submodules = []
371 for submodule in model:
372 subsubmodules = _find_submodule_by_name(submodule, remaining_name)
373 if not isinstance(subsubmodules, list):
374 subsubmodules = [subsubmodules]
375 submodules.extend(subsubmodules)
376 return submodules
377 else:
378 if hasattr(model, first_atom):
379 submodule = getattr(model, first_atom)
380 return _find_submodule_by_name(submodule, remaining_name)
381 else:
382 raise ValueError(f"'{first_atom}' is not a submodule of '{model.__class__.__name__}'")

Callers 1

_get_submodule_by_nameFunction · 0.85

Calls 1

splitMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…