Comparator Utils#
utils
#
| FUNCTION | DESCRIPTION |
|---|---|
get_layer_names |
Get names of all layers in a model. |
filter_layers_by_type |
Filter layers in a model by their type. |
match_layers_by_name |
Match layers with the same names. |
match_layers_by_position |
Match layers by their position in the model. |
match_layers_by_depth |
Match layers by their relative depth in the network. |
match_layers_by_type |
Match all layers of a specific type between two models. |
Functions#
get_layer_names(model: torch.nn.Module, include_container_modules: bool = False) -> list[str]
#
Get names of all layers in a model.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
Model to inspect.
TYPE:
|
include_container_modules
|
If True, include container modules (Sequential, ModuleList, etc.). If False, only include leaf modules (Conv2d, Linear, etc.).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of layer names. |
Examples:
>>> import torch.nn as nn
>>> model = nn.Sequential(nn.Linear(10, 20), nn.ReLU(), nn.Linear(20, 5))
>>> get_layer_names(model)
['0', '1', '2']
Custom model:
>>> class Net(nn.Module):
... def __init__(self):
... super().__init__()
... self.conv1 = nn.Conv2d(3, 64, 3)
... self.fc = nn.Linear(64, 10)
...
... def forward(self, x):
... return self.fc(self.conv1(x).flatten(1))
>>> model = Net()
>>> get_layer_names(model)
['conv1', 'fc']
Source code in spectre/comparator/utils.py
filter_layers_by_type(model: torch.nn.Module, layer_types: list[type] | type) -> dict[str, torch.nn.Module]
#
Filter layers in a model by their type.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
Model to filter.
TYPE:
|
layer_types
|
Layer type(s) to filter for (e.g., nn.Conv2d, nn.Linear).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Module]
|
Dictionary mapping layer names to modules of specified type(s). |
Examples:
>>> import torch.nn as nn
>>> model = nn.Sequential(nn.Conv2d(3, 64, 3), nn.ReLU(), nn.Linear(64, 10))
>>> filter_layers_by_type(model, nn.Linear)
{'2': Linear(in_features=64, out_features=10, bias=True)}
Multiple types:
>>> filter_layers_by_type(model, [nn.Conv2d, nn.Linear])
{'0': Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1)),
'2': Linear(in_features=64, out_features=10, bias=True)}
Source code in spectre/comparator/utils.py
match_layers_by_name(layers1: list[str], layers2: list[str]) -> list[tuple[str, str]]
#
Match layers with the same names.
| PARAMETER | DESCRIPTION |
|---|---|
layers1
|
Layer names from first model.
TYPE:
|
layers2
|
Layer names from second model.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[str, str]]
|
List of (layer1_name, layer2_name) pairs for matching names. |
Examples:
>>> layers1 = ["conv1", "relu", "conv2"]
>>> layers2 = ["conv1", "bn", "conv2", "relu"]
>>> match_layers_by_name(layers1, layers2)
[('conv1', 'conv1'), ('relu', 'relu'), ('conv2', 'conv2')]
Source code in spectre/comparator/utils.py
match_layers_by_position(layers1: list[str], layers2: list[str]) -> list[tuple[str, str]]
#
Match layers by their position in the model.
| PARAMETER | DESCRIPTION |
|---|---|
layers1
|
Layer names from first model.
TYPE:
|
layers2
|
Layer names from second model.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[str, str]]
|
List of (layer1_name, layer2_name) pairs matched by position. Length is min(len(layers1), len(layers2)). |
Examples:
>>> layers1 = ["layer1", "layer2", "layer3"]
>>> layers2 = ["conv1", "conv2"]
>>> match_layers_by_position(layers1, layers2)
[('layer1', 'conv1'), ('layer2', 'conv2')]
Source code in spectre/comparator/utils.py
match_layers_by_depth(model1: torch.nn.Module, model2: torch.nn.Module) -> list[tuple[str, str]]
#
Match layers by their relative depth in the network.
This function computes the depth of each layer (number of parent modules) and matches layers at similar relative positions in the network hierarchy.
| PARAMETER | DESCRIPTION |
|---|---|
model1
|
First model.
TYPE:
|
model2
|
Second model.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[str, str]]
|
List of (layer1_name, layer2_name) pairs matched by depth. |
Examples:
>>> import torch.nn as nn
>>> model1 = nn.Sequential(
... nn.Sequential(nn.Linear(10, 20), nn.ReLU()),
... nn.Linear(20, 5),
... )
>>> model2 = nn.Sequential(nn.Linear(10, 15), nn.ReLU(), nn.Linear(15, 5))
>>> match_layers_by_depth(model1, model2)
[('0.0', '0'), ('0.1', '1'), ('1', '2')]
Source code in spectre/comparator/utils.py
match_layers_by_type(model1: torch.nn.Module, model2: torch.nn.Module, layer_type: type | list[type] = torch.nn.Linear) -> list[tuple[str, str]]
#
Match all layers of a specific type between two models.
| PARAMETER | DESCRIPTION |
|---|---|
model1
|
First model.
TYPE:
|
model2
|
Second model.
TYPE:
|
layer_type
|
Layer type(s) to match (e.g., torch.nn.Conv2d, torch.nn.Linear).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[str, str]]
|
List of (layer1_name, layer2_name) pairs for matching layer types. Matched by position within layers of that type. |
Examples:
>>> import torch.nn as nn
>>> model1 = nn.Sequential(nn.Conv2d(3, 64, 3), nn.ReLU(), nn.Linear(64, 10))
>>> model2 = nn.Sequential(nn.Conv2d(3, 32, 3), nn.ReLU(), nn.Linear(32, 10))
>>> match_layers_by_type(model1, model2, nn.Linear)
[('2', '2')]
Multiple types: