""" Copyright 2020 Tianshu AI Platform. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ============================================================= """ import torch_pruning as tp import abc import torch import torch.nn as nn import random import numpy as np _PRUNABLE_MODULES= tp.DependencyGraph.PRUNABLE_MODULES class BaseStrategy(abc.ABC): @abc.abstractmethod def select(self, layer_to_prune): pass def __call__(self, model, rate=0.1, example_inputs=None): if example_inputs is None: example_inputs = torch.randn( 1,3,256,256 ) DG = tp.DependencyGraph() DG.build_dependency(model, example_inputs=example_inputs) prunable_layers = [] total_params = 0 num_accumulative_conv_params = [ 0, ] for m in model.modules(): if isinstance(m, _PRUNABLE_MODULES ) : nparam = tp.utils.count_prunable_params( m ) total_params += nparam if isinstance(m, (nn.modules.conv._ConvNd, nn.Linear)): prunable_layers.append( m ) num_accumulative_conv_params.append( num_accumulative_conv_params[-1]+nparam ) prunable_layers.pop(-1) # remove the last layer num_accumulative_conv_params.pop(-1) # remove the last layer num_conv_params = num_accumulative_conv_params[-1] num_accumulative_conv_params = [ ( num_accumulative_conv_params[i], num_accumulative_conv_params[i+1] ) for i in range(len(num_accumulative_conv_params)-1) ] def map_param_idx_to_conv_layer(i): for l, accu in zip( prunable_layers, num_accumulative_conv_params ): if accu[0]<=i and i