classes package
Submodules
classes.channel_profiler module
- class classes.channel_profiler.ChannelProfiler(model, device='cpu')[source]
Bases:
TorchProfiler
ChannelProfiler identifies influential channels of an activation tensor. Influential channel neurons are identified by either maximum value or matrix norm. Contributing neurons in the previous layer are the channels with the greatest maximum values whose sum reaches a specified threshold, or percentage of the maximum value in the influential channel.
- Parameters
model (Module) –
device (str) –
- contrib_adaptive_avg_pool2d(x_in, y_out, infl_neurons, layer=None, threshold=None)[source]
Draws synaptic connections between the given influential neurons in a 2D adaptive average pooling layer and their contributors in a previous layer
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
infl_neurons (torch.Tensor) – tensor containing indices of influential neurons in y_out dimensions: num_influential
layer (List[str], optional) – not used, placeholder for uniformity in arguments
threshold (float, optional) – not used, placeholder for uniformity in arguments
- Return type
Tuple
[coo_matrix
,coo_matrix
,coo_matrix
]- Returns
neuron_counts (sp.coo_matrix)
synapse_counts (sp.coo_matrix)
synapse_weights (sp.coo_matrix)
- contrib_conv2d(x_in, y_out, infl_neurons, layer, threshold=0.1)[source]
Profile a single output neuron from a 2D convolutional layer
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
infl_neurons (torch.Tensor) – tensor containing indices of influential neurons in y_out dimensions: num_influential
layer (List[str]) – list containing keys in self.model.available_modules() dictionary, corresponding to a convolutional module and an activation module
threshold (float, default=0.1) –
- Return type
Tuple
[coo_matrix
,coo_matrix
,coo_matrix
]- Returns
neuron_counts (sp.coo_matrix)
neuron_weights (sp.coo_matrix)
synapse_counts (sp.coo_matrix)
Note
Only implemented for convolution using filters with same height and width and strides equal in both dimensions and padding equal in all dimensions
- contrib_identity(x_in, y_out, infl_neurons, layer=None, threshold=None)[source]
Pass through to keep influential neurons from one layer fixed into the next.
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
infl_neurons (torch.Tensor) – tensor containing indices of influential neurons in y_out dimensions: num_influential
layer (List[str], optional) – not used, placeholder for uniformity in arguments
threshold (float, optional) – not used, placeholder for uniformity in arguments
- Return type
Tuple
[coo_matrix
,coo_matrix
,coo_matrix
]- Returns
neuron_counts
synapse_counts
synapse_weights
- contrib_linear(x_in, y_out, infl_neurons, layer, threshold=0.1)[source]
Profile output neurons from a linear layer
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
infl_neurons (torch.Tensor) – tensor containing indices of influential neurons in y_out dimensions: num_influential
layer (List[str]) – list containing single key in self.model.available_modules() dictionary
threshold (float, default=0.1) –
- Return type
Tuple
[coo_matrix
,coo_matrix
,coo_matrix
]- Returns
neuron_counts (sp.coo_matrix)
synapse_counts (sp.coo_matrix)
synapse_weights (sp.coo_matrix)
- contrib_max2d(x_in, y_out, infl_neurons, layer=None, threshold=None)[source]
Draws synaptic connections between the given influential neurons in a 2D max pooling layer and their contributors in a previous layer
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
infl_neurons (torch.Tensor) – tensor containing indices of influential neurons in y_out dimensions: num_influential
layer (List[str], optional) – not used, placeholder for uniformity in arguments
threshold (float, optional) – not used, placeholder for uniformity in arguments
- Return type
Tuple
[coo_matrix
,coo_matrix
,coo_matrix
]- Returns
neuron_counts (sp.coo_matrix)
synapse_counts (sp.coo_matrix)
synapse_weights (sp.coo_matrix)
- contrib_resnetadd(x_in, y_out, infl_neurons, layer=None, threshold=None)[source]
Draws synaptic connections between the given influential neurons in a ResNet add layer and their contributors in a previous layer
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
infl_neurons (torch.Tensor) – tensor containing indices of influential neurons in y_out dimensions: num_influential
layer (List[str], optional) – not used, placeholder for uniformity in arguments
threshold (float, optional) – not used, placeholder for uniformity in arguments
- Return type
Tuple
[Tuple
[coo_matrix
,coo_matrix
],coo_matrix
,coo_matrix
]- Returns
neuron_counts
synapse_counts
synapse_weights
- Raises
NotImplementedError – Raises error if len(x_in) != 2 or len(y_out) != 1
- create_profile(x, threshold=0.1, layers_to_profile=None, norm=None)[source]
Generate a profile for a single input data x
- Parameters
x (torch.Tensor) – input to model being profiled
layers_to_profile (list or tuple) – list of specific layers to profile or tuple with first,last layers (exclusive of last) to profile and all layers inbetween
threshold (float, default=0.1) – Percentage of contribution to track in a profile.
norm (int, optional) – If given, the order of the norm to be taken to sum the strengths of channel activations. Otherwise, the max value of each channel is used
- Returns
profile contains neuron_counts, neuron_weights, synapse_counts, and synapse_weights across layers in layers_to_profile. Corresponding number of images = 1
- Return type
- influence_generator(activations, norm=None)[source]
- Parameters
activations (dict of tensors) –
norm (int, optional) – If given, the order of the norm to be taken to sum the strengths of channel activations. Otherwise, the max value of each channel is used
- Returns
influential_neurons – A function that will pick out the most influential neurons in a layer up to some threshold
- Return type
function
- single_profile(x_in, y_out, neuron_counts, ldx, threshold)[source]
Profiles a single layer
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
neuron_counts (sp.coo_matrix) – Matrix representing the influential neurons in the layer
ldx (int) – Layer number of the layer to be profiled
threshold (float) – Percentage of contribution to track in a profile
- Return type
Tuple
[Union
[coo_matrix
,Tuple
[coo_matrix
,coo_matrix
]],coo_matrix
,coo_matrix
]- Returns
neuron_counts (sp.coo_matrix or tuple of sp.coo_matrix)
synapse_counts (sp.coo_matrix)
synapse_weights (sp.coo_matrix)
classes.element_profiler module
- class classes.element_profiler.ElementProfiler(model, device='cpu')[source]
Bases:
TorchProfiler
ElementProfiler identifies influential elements of an activation tensor. Influential element neurons are identified by their value or absolute value. Contributing neurons in the previous layer are the elements in the receptive field with the greatest values whose sum reaches a specified threshold, or percentage of the value of the influential element.
- Parameters
model (Module) –
device (str) –
- contrib_adaptive_avg_pool2d(x_in, y_out, infl_neurons, layer, threshold=0.1)[source]
Draws synaptic connections between the given influential neurons in a 2D adaptive average pooling layer and their contributors in a previous layer
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
infl_neurons (torch.Tensor) – tensor containing indices of influential neurons in y_out dimensions: num_influential x 3 (channel, row, col)
layer (List[str]) – list containing single key in self.model.available_modules() dictionary
threshold (float, default=0.1) –
- Return type
Tuple
[coo_matrix
,coo_matrix
,coo_matrix
]- Returns
neuron_counts (sp.coo_matrix)
synapse_counts (sp.coo_matrix)
synapse_weights (sp.coo_matrix)
- contrib_conv2d(x_in, y_out, infl_neurons, layer, threshold=0.1)[source]
Profile a single output neuron from a 2D convolutional layer
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
infl_neurons (torch.Tensor) – tensor containing indices of influential neurons in y_out dimensions: num_influential x 3 (channel, row, col)
layer (List[str]) – list containing keys in self.model.available_modules() dictionary, corresponding to a convolutional module and an activation module
threshold (float, default=0.1) –
- Return type
Tuple
[coo_matrix
,coo_matrix
,coo_matrix
]- Returns
neuron_counts (sp.coo_matrix)
neuron_weights (sp.coo_matrix)
synapse_counts (sp.coo_matrix)
Note
Only implemented for convolution using filters with same height and width and strides equal in both dimensions and padding equal in all dimensions
- contrib_identity(x_in, y_out, infl_neurons, layer=None, threshold=None)[source]
Pass through to keep influential neurons from one layer fixed into the next. Used for normalization layers
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
infl_neurons (torch.Tensor) – tensor containing indices of influential neurons in y_out dimensions: num_influential x 3 (channel, row, col)
layer (List[str], optional) – not used, placeholder for uniformity in arguments
threshold (float, optional) – not used, placeholder for uniformity in arguments
- Return type
Tuple
[coo_matrix
,coo_matrix
,coo_matrix
]- Returns
neuron_counts
synapse_counts
synapse_weights
- contrib_linear(x_in, y_out, infl_neurons, layer, threshold=0.1)[source]
Profile output neurons from a linear layer
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
infl_neurons (torch.Tensor) – tensor containing indices of influential neurons in y_out dimensions: num_influential
layer (List[str]) – list containing single key in self.model.available_modules() dictionary
threshold (float, default=0.1) –
- Return type
Tuple
[coo_matrix
,coo_matrix
,coo_matrix
]- Returns
neuron_counts (sp.coo_matrix)
synapse_counts (sp.coo_matrix)
synapse_weights (sp.coo_matrix)
- contrib_max2d(x_in, y_out, infl_neurons, layer, threshold=None)[source]
Draws synaptic connections between the given influential neurons in a 2D max pooling layer and their contributors in a previous layer
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
infl_neurons (torch.Tensor) – tensor containing indices of influential neurons in y_out dimensions: num_influential x 3 (channel, row, col)
layer (List[str]) – list containing single key in self.model.available_modules() dictionary
threshold (float, optional) – not used, placeholder for uniformity in arguments
- Return type
Tuple
[coo_matrix
,coo_matrix
,coo_matrix
]- Returns
neuron_counts (sp.coo_matrix)
synapse_counts (sp.coo_matrix)
synapse_weights (sp.coo_matrix)
- contrib_resnetadd(x_in, y_out, infl_neurons, layer=None, threshold=None)[source]
Draws synaptic connections between the given influential neurons in a ResNet add layer and their contributors in a previous layer
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
infl_neurons (torch.Tensor) – tensor containing indices of influential neurons in y_out dimensions: num_influential x 3 (channel, row, col)
layer (List[str], optional) – not used, placeholder for uniformity in arguments
threshold (float, optional) – not used, placeholder for uniformity in arguments
- Return type
Tuple
[Tuple
[coo_matrix
,coo_matrix
],coo_matrix
,coo_matrix
]- Returns
neuron_counts
synapse_counts
synapse_weights
- Raises
NotImplementedError – Raises error if len(x_in) != 2 or len(y_out) != 1
- create_profile(x, threshold=0.1, layers_to_profile=None, use_abs=False)[source]
Generate a profile for a single input data x
- Parameters
x (torch.Tensor) – input to model being profiled
layers_to_profile (list or tuple) – list of specific layers to profile or tuple with first,last layers (exclusive of last) to profile and all layers inbetween
threshold (float, default=0.1) – Percentage of contribution to track in a profile.
use_abs (boolean, default=False) – If True, use the absolute value of element activations to determine influence
- Returns
profile contains neuron_counts, neuron_weights, synapse_counts, and synapse_weights across layers in layers_to_profile. Corresponding number of images = 1
- Return type
- influence_generator(activations, use_abs=False)[source]
- Parameters
activations (dict of tensors) –
use_abs (boolean, default=False) – If True, use the absolute value of element activations to determine influence
- Returns
influential_neurons – A function that will pick out the most influential neurons in a layer up to some threshold
- Return type
function
- single_profile(x_in, y_out, neuron_counts, ldx, threshold)[source]
Profiles a single layer
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
neuron_counts (sp.coo_matrix) – Matrix representing the influential neurons in the layer
ldx (int) – Layer number of the layer to be profiled
threshold (float) – Percentage of contribution to track in a profile
- Return type
Tuple
[Union
[coo_matrix
,Tuple
[coo_matrix
,coo_matrix
]],coo_matrix
,coo_matrix
]- Returns
neuron_counts (sp.coo_matrix or tuple of sp.coo_matrix)
synapse_counts (sp.coo_matrix)
synapse_weights (sp.coo_matrix)
classes.profile module
- class classes.profile.Profile(neuron_counts=None, neuron_weights=None, synapse_counts=None, synapse_weights=None, activation_shapes=None, pred_dict=None, num_inputs=0, neuron_type=None)[source]
Bases:
object
Summary
- Parameters
neuron_counts (Optional[Union[Dict[int, spmatrix], Dict[int, Dict[Tuple, float]]]]) –
neuron_weights (Optional[Union[Dict[int, spmatrix], Dict[int, Dict[Tuple, float]]]]) –
synapse_counts (Optional[Union[Dict[int, spmatrix], Dict[int, Dict[Tuple, float]]]]) –
synapse_weights (Optional[Union[Dict[int, spmatrix], Dict[int, Dict[Tuple, float]]]]) –
activation_shapes (Optional[Union[Dict[int, spmatrix], Dict[int, Dict[Tuple, float]]]]) –
pred_dict (Optional[Dict[int, List[int]]]) –
num_inputs (Optional[int]) –
neuron_type (Optional[str]) –
- property activation_shapes: Dict[int, Size]
returns: activation_shapes – Dictionary of the activation tensors shapes, keyed by layer :rtype: Dict of torch.Sizes
- dict_view()[source]
- Return type
- Returns
A copy of the profile with neuron and synapse counts and weights
reformatted as dicts
- property neuron_counts: Union[Dict[int, spmatrix], Dict[int, Dict[Tuple, float]]]
returns: neuron_counts – Dictionary representing profile neurons and their counts, i.e. how many synapses they were influential or contributing to :rtype: Dict of scipy.sparse matrices or Dict of dicts
- property neuron_type: str
returns: neuron_type – The type of neurons used in the profile, i.e. ‘element’, ‘channel’, or ‘mixed’ (aggregate of profiles with mismatched types) :rtype: str
- property neuron_weights: Union[Dict[int, spmatrix], Dict[int, Dict[Tuple, float]]]
returns: neuron_weights – Dictionary representing influential profile neurons and their weights :rtype: Dict of scipy.sparse matrices or Dict of dicts
- property num_inputs: int
returns: num_inputs – The number of input images represented by the profile :rtype: int
Note
Class profiles and other aggregate profiles will have num_inputs > 1
- property num_synapses: int
returns: Total number of synapses across all layers :rtype: int
- property pred_dict: Dict[int, List[int]]
returns: pred_dict – Dictionary giving the layer predecessor hierarchy by layer index :rtype: Dict of list of ints
- property size: int
returns: Total number of neurons identified as influential or contributing (neurons with nonzero neuron counts) :rtype: int
- property synapse_counts: Union[Dict[int, spmatrix], Dict[int, Dict[Tuple, float]]]
returns: synapse_counts – Dictionary representing profile synapses and their counts :rtype: Dict of scipy.sparse matrices or Dict of dicts
Note
For a single image profile (num_inputs=1) all synapses should have a count of 1
- property synapse_weights: Union[Dict[int, spmatrix], Dict[int, Dict[Tuple, float]]]
returns: synapse_weights – Dictionary representing profile synapses and their weights :rtype: Dict of scipy.sparse matrices or Dict of dicts
- property total: int
returns: Total sum of neuron counts across all layers :rtype: int
classes.spatial_profiler module
- class classes.spatial_profiler.SpatialProfiler(model, device='cpu')[source]
Bases:
TorchProfiler
SpatialProfiler identifies influential “spatial neurons”, or tube-like vector slices through all channels of an activation tensor. Influential neurons are identified by the strength of their vector norm. Contributing neurons in the previous layer are the spatials in the receptive field that contribute the most in the direction of the influential spatial vector that they are combined to produce.
- Parameters
model (Module) –
device (str) –
- contrib_adaptive_avg_pool2d(x_in, y_out, infl_neurons, layer, threshold=None)[source]
Draws synaptic connections between the given influential neurons in a 2D adaptive average pooling layer and their contributors in a previous layer
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
infl_neurons (torch.Tensor) – tensor containing indices of influential neurons in y_out dimensions: num_influential
layer (List[str]) – list containing single key in self.model.available_modules() dictionary
threshold (float, optional) – not used, placeholder for uniformity in arguments
- Return type
Tuple
[coo_matrix
,coo_matrix
,coo_matrix
]- Returns
neuron_counts (sp.coo_matrix)
synapse_counts (sp.coo_matrix)
synapse_weights (sp.coo_matrix)
- contrib_conv2d(x_in, y_out, infl_neurons, layer, threshold=0.1)[source]
Draws synaptic connections between the given influential neurons in a 2D convolutional layer and their contributors in a previous layer
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
infl_neurons (torch.Tensor) – tensor containing indices of influential neurons in y_out dimensions: num_influential
layer (List[str]) – list containing keys in self.model.available_modules() dictionary, corresponding to a convolutional module and an activation module
threshold (float, default=0.1) –
- Return type
Tuple
[coo_matrix
,coo_matrix
,coo_matrix
]- Returns
neuron_counts (sp.coo_matrix)
neuron_weights (sp.coo_matrix)
synapse_counts (sp.coo_matrix)
Note
Only implemented for convolution using filters with same height and width and strides equal in both dimensions and padding equal in all dimensions
- contrib_identity(x_in, y_out, infl_neurons, layer=None, threshold=None)[source]
Pass through to keep influential neurons from one layer fixed into the next.
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
infl_neurons (torch.Tensor) – tensor containing indices of influential neurons in y_out dimensions: num_influential
layer (List[str], optional) – not used, placeholder for uniformity in arguments
threshold (float, optional) – not used, placeholder for uniformity in arguments
- Return type
Tuple
[coo_matrix
,coo_matrix
,coo_matrix
]- Returns
neuron_counts
synapse_counts
synapse_weights
- contrib_linear(x_in, y_out, infl_neurons, layer, threshold=0.1)[source]
Draws synaptic connections between the given influential neurons in a linear/ fully connected layer and their contributors in a previous layer
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
infl_neurons (torch.Tensor) – tensor containing indices of influential neurons in y_out dimensions: num_influential
layer (List[str]) – list containing single key in self.model.available_modules() dictionary
threshold (float, default=0.1) –
- Return type
Tuple
[coo_matrix
,coo_matrix
,coo_matrix
]- Returns
neuron_counts (sp.coo_matrix)
synapse_counts (sp.coo_matrix)
synapse_weights (sp.coo_matrix)
- contrib_max2d(x_in, y_out, infl_neurons, layer, threshold=None)[source]
Draws synaptic connections between the given influential neurons in a 2D max pooling layer and their contributors in a previous layer
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
infl_neurons (torch.Tensor) – tensor containing indices of influential neurons in y_out dimensions: num_influential
layer (List[str]) – list containing single key in self.model.available_modules() dictionary
threshold (float, optional) – not used, placeholder for uniformity in arguments
- Return type
Tuple
[coo_matrix
,coo_matrix
,coo_matrix
]- Returns
neuron_counts (sp.coo_matrix)
synapse_counts (sp.coo_matrix)
synapse_weights (sp.coo_matrix)
- contrib_resnetadd(x_in, y_out, infl_neurons, layer=None, threshold=None)[source]
Draws synaptic connections between the given influential neurons in a ResNet add layer and their contributors in a previous layer
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
infl_neurons (torch.Tensor) – tensor containing indices of influential neurons in y_out dimensions: num_influential
layer (List[str], optional) – not used, placeholder for uniformity in arguments
threshold (float, optional) – not used, placeholder for uniformity in arguments
- Return type
Tuple
[Tuple
[coo_matrix
,coo_matrix
],coo_matrix
,coo_matrix
]- Returns
neuron_counts
synapse_counts
synapse_weights
- Raises
NotImplementedError – Raises error if len(x_in) != 2 or len(y_out) != 1
- create_profile(x, infl_threshold=0.97, contrib_threshold=0.1, layers_to_profile=None, use_quantile=True, max_infl=100, norm=2)[source]
Generate a profile for a single input data x
- Parameters
x (torch.Tensor) – input to model being profiled
layers_to_profile (list or tuple) – list of specific layers to profile or tuple with first,last layers (exclusive of last) to profile and all layers inbetween
infl_threshold (float, default=0.97) – Percentage or quantile used to identify influential neurons
contrib_threshold (float, default=0.1) – Percentage of contribution to track in a profile
use_quantile (boolean, default=True) – If True, the infl_threshold argument will be interpreted as a top quantile to draw influential neurons from, Otherwise, the infl_threshold argument will be interpreted as the percentage of the sum of all neuron activations achieved by aggregating the influential neuron activations
max_infl (int, default=100) – An upper bound on the number of influential neurons to be identified
norm (int, default=2) – The order of the norm to be taken when identifying influential neurons to sum the strengths of spatial activations, default is L2-norm
- Returns
profile contains neuron_counts, neuron_weights, synapse_counts, and synapse_weights across layers in layers_to_profile. Corresponding number of images = 1
- Return type
- influence_generator(activations, use_quantile=True, max_infl=100, norm=2)[source]
- Parameters
activations (dict of tensors) –
use_quantile (boolean, default=True) – If True, the function returned by the generator will interpret the threshold argument as a top quantile to draw influential neurons from, Otherwise, the threshold argument will be interpreted as the percentage of the sum of all neuron activations achieved by aggregating the influential neuron activations
max_infl (int, default=100) – An upper bound on the number of influential neurons to be identified
norm (int, default=2) – The order of the norm to be taken to sum the strengths of spatial activations, default is L2-norm
- Returns
influential_neurons – A function that will pick out the most influential neurons in a layer up to some threshold
- Return type
function
- single_profile(x_in, y_out, neuron_counts, ldx, threshold=0.1)[source]
Profiles a single layer
- Parameters
x_in (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
y_out (Dict[int, torch.Tensor]) – dict where key is layer, value is tensor dimensions: batchsize,channels,height,width
neuron_counts (sp.coo_matrix) – Matrix representing the influential neurons in the layer
ldx (int) – Layer number of the layer to be profiled
threshold (float, default=0.1) – Percentage of contribution to track in a profile
- Return type
Tuple
[Union
[coo_matrix
,Tuple
[coo_matrix
,coo_matrix
]],coo_matrix
,coo_matrix
]- Returns
neuron_counts (sp.coo_matrix or tuple of sp.coo_matrix)
synapse_counts (sp.coo_matrix)
synapse_weights (sp.coo_matrix)
classes.svd_influential module
- class classes.svd_influential.SVDProfiler(model, device=device(type='cpu'), compute_svd=True)[source]
Bases:
TorchProfiler
Torch Profiler wraps a PyTorch model into a TorchHook model which can register activations as it evaluates data. Using the activations, inputs to the model may be profiled.
The function call to generate an influenial SVD profile is slightly different than that for SpatialProfiler/ChannelProfiler. Here is how to create a profile:
import deep_data_profiler as ddp # define the profiler influential_profiler = ddp.SVDProfiler(model) # profile a tensor x profile = influential_profiler.create_influential(x) # view neuron weights dictionary print(profile.neuron_weights) # view the neuron weights for a specific layer print(profile.neuron_weights[22].todense()) ...
- Parameters
model (Sequential) –
device (device) –
compute_svd (bool) –
- implemented_classes
Set of classes in PyTorch which we can find influential for
- Type
list
- model
Model to be profiled
- Type
torch.nn.Sequential()
- create_influential(x, layers_to_find=None, threshold=0.1, activations=None, aggregation='sum')[source]
Generate an influential profile for a single input data x.
- Parameters
x (torch.Tensor) – input to model being profiled
layers_to_find (list, optional) – Optional list of layers to find influential SVD neurons for.
threshold (float, optional, default=0.1) – Percentage of contribution to track in a profile.
- Returns
profile contains neuron_counts, neuron_weights across layers. Corresponding number of images = 1
- Return type
- create_projections(x, layers_to_find=None)[source]
Generate SVD projections for a single input data x.
- Parameters
x (torch.Tensor) – input to model being profiled
layers_to_find (list, optional) – Optional list of layers to find influential SVD neurons for.
- Returns
projections – SVD projections keyed by layer
- Return type
dict
- create_svd(layers_to_find=None)[source]
Create a dictionary of the Singular Value Decomposition of a layer’s weights.
- Parameters
layers_to_find (list, optional) – Optional list of layers to find influential SVD neurons for.
- Returns
svd_dict – A dictionary of SVD of weights, keyed by their position from the beginning of the model, according to the order the layers of the model are listed.
- Return type
dict
- static influential_svd_neurons(agg, threshold=0.1, norm=1, device=device(type='cpu'))[source]
Returns a dictionary of relative contributions keyed by influential SVD neurons for layer up to some threshold
- Parameters
agg (torch.Tensor) – The SVD projections tensor, with some aggregation applied. Expected to be 1-D.
(int (ord) – order of norm. See https://pytorch.org/docs/stable/linalg.html#torch.linalg.norm
float – order of norm. See https://pytorch.org/docs/stable/linalg.html#torch.linalg.norm
+/-inf – order of norm. See https://pytorch.org/docs/stable/linalg.html#torch.linalg.norm
'fro' – order of norm. See https://pytorch.org/docs/stable/linalg.html#torch.linalg.norm
'nuc' – order of norm. See https://pytorch.org/docs/stable/linalg.html#torch.linalg.norm
optional) – order of norm. See https://pytorch.org/docs/stable/linalg.html#torch.linalg.norm
threshold (float) –
- Return type
Tuple
[coo_matrix
,coo_matrix
]- Returns
influential_neurons (sp.coo_matrix) – Matrix representing the influential neurons within the threshold
influential_weights (sp.coo_matrix) – Matrix assigning weights to each influential neuron according to its contribution to the threshold
classes.torch_profiler module
- class classes.torch_profiler.TorchProfiler(model, device='cpu')[source]
Bases:
ABC
A TorchProfiler wraps a PyTorch model into a TorchHook model which can register activations as it evaluates data. Subclasses of TorchProfiler profile model inputs by tracing influential neurons through the layers of the network during classification. Each subclass implementation defines neurons, influence, and contribution differently.
- Parameters
model (Module) –
device (str) –
- activation_classes
List of activation classes in PyTorch
- Type
list
- dropout_classes
List of dropout classes in PyTorch
- Type
list
- implemented_classes
List of classes in PyTorch which have contrib functions implemented
- Type
list
- contrib_functions
List of contrib functions ordered by their corresponding classes in implemented_classes (does not include “contrib_resnetadd”)
- Type
list
- model
Model to be profiled
- Type
torch.nn.Module
- build_dicts(x, infl_threshold, contrib_threshold, layers_to_profile=None, **kwargs)[source]
Generates neuron/synapse counts/weights for a given input.
- Parameters
x (torch.Tensor) – input to model being profiled
layers_to_profile (list or tuple) – list of specific layers to profile or tuple with first,last layers (exclusive of last) to profile and all layers in between
infl_threshold (float) – Parameter for influence
contrib_threshold (float) – Parameter for contribution
- Return type
Tuple
[dict
,dict
,dict
,dict
]- Returns
neuron_counts
neuron_weights
synapse_counts
synapse_weights
activation_shapes
- create_layers()[source]
Create a dictionary of layers to profile.
- Returns
layerdict – A dictionary of layers keyed by their position according to the order Pytorch uses to list the layers of the model.
- Return type
OrderedDict
Note
Because a weighted module is often followed by an activation and/or normalization modules those modules are combined into a single layer to be profiled. As a consequence the number of layers generated will typically be less than the number of available modules in the model.