easydel.utils.graph_utils#
- easydel.utils.graph_utils.get_module_from_path(model: Module, path: Tuple[str, ...]) Optional[Module][source]#
Retrieves a module from a model given its path.
- Parameters
model – The root module to traverse.
path – tp.Tuple of strings/integers representing the path to the module.
- Returns
The module at the specified path, or None if path is empty.
Example
>>> module = get_module_from_path(model, ("encoder", "layer1", "attention"))
- easydel.utils.graph_utils.iter_module_search(model: Module, instance: Optional[Type[T]] = None) Iterator[Tuple[Tuple[str, ...], T]][source]#
Iterates through a model and yields paths and modules of a specific type.
- Parameters
model – The root module to search through.
instance – The type of module to search for.
- Yields
tp.Tuple containing – - Path to the module as a tuple of strings/integers - The module instance matching the specified type
Example
>>> for path, module in iter_module_search(model, nn.Linear): ... print(f"Found Linear layer at {path}")
- easydel.utils.graph_utils.set_module_from_path(model: Module, path: Tuple[str, ...], new_value: Any) None[source]#
Sets a module at a specific path in the model.
- Parameters
model – The root module to modify.
path – tp.Tuple of strings/integers representing the path to the module.
new_value – The new value/module to set at the specified path.
- Raises
AttributeError – If the path is invalid.
IndexError – If trying to access an invalid index.
Example
>>> new_layer = nn.Linear(features=64) >>> set_module_from_path(model, ("encoder", "layer1"), new_layer)