-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from VainF/v1.2
V1.2.2
- Loading branch information
Showing
17 changed files
with
325 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from transformers import AutoTokenizer, BertModel | ||
import torch | ||
from transformers.models.bert.modeling_bert import BertSelfAttention | ||
import torch_pruning as tp | ||
|
||
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") | ||
model = BertModel.from_pretrained("bert-base-uncased") | ||
#print(model) | ||
hf_inputs = tokenizer("Hello, my dog is cute", return_tensors="pt") | ||
example_inputs = {'input_ids': hf_inputs['input_ids'], 'token_type_ids': hf_inputs['token_type_ids'], 'attention_mask': hf_inputs['attention_mask']} | ||
|
||
#outputs = model(**example_inputs) | ||
#last_hidden_states = outputs.last_hidden_state | ||
|
||
imp = tp.importance.MagnitudeImportance(p=2, group_reduction="mean") | ||
base_macs, base_params = tp.utils.count_ops_and_params(model, example_inputs) | ||
channel_groups = {} | ||
|
||
# All heads should be pruned simultaneously, so we group channels by head. | ||
for m in model.modules(): | ||
if isinstance(m, BertSelfAttention): | ||
channel_groups[m.query] = m.num_attention_heads | ||
channel_groups[m.key] = m.num_attention_heads | ||
channel_groups[m.value] = m.num_attention_heads | ||
|
||
pruner = tp.pruner.MagnitudePruner( | ||
model, | ||
example_inputs, | ||
global_pruning=False, # If False, a uniform sparsity will be assigned to different layers. | ||
importance=imp, # importance criterion for parameter selection | ||
iterative_steps=1, # the number of iterations to achieve target sparsity | ||
ch_sparsity=0.5, | ||
channel_groups=channel_groups, | ||
output_transform=lambda out: out.pooler_output.sum(), | ||
ignored_layers=[model.pooler], | ||
) | ||
|
||
for g in pruner.step(interactive=True): | ||
print(g) | ||
g.prune() | ||
|
||
# Modify the attention head size and all head size aftering pruning | ||
for m in model.modules(): | ||
if isinstance(m, BertSelfAttention): | ||
m.attention_head_size = m.query.out_features // m.num_attention_heads | ||
m.all_head_size = m.query.out_features | ||
|
||
print(model) | ||
test_output = model(**example_inputs) | ||
pruned_macs, pruned_params = tp.utils.count_ops_and_params(model, example_inputs) | ||
print("Base MACs: %f M, Pruned MACs: %f M"%(base_macs/1e6, pruned_macs/1e6)) | ||
print("Base Params: %f M, Pruned Params: %f M"%(base_params/1e6, pruned_params/1e6)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
|
||
setuptools.setup( | ||
name="torch-pruning", | ||
version="v1.2.1", | ||
version="v1.2.2", | ||
author="Gongfan Fang", | ||
author_email="[email protected]", | ||
description="Towards Any Structural Pruning", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import sys, os | ||
|
||
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))) | ||
|
||
import torch | ||
import torch_pruning as tp | ||
import torch.nn as nn | ||
|
||
class Net(nn.Module): | ||
def __init__(self, in_dim): | ||
super().__init__() | ||
self.block1 = nn.Sequential( | ||
nn.Conv2d(in_dim, in_dim, 1), | ||
nn.BatchNorm2d(in_dim), | ||
nn.GELU(), | ||
nn.Conv2d(in_dim, in_dim, 1), | ||
nn.BatchNorm2d(in_dim) | ||
) | ||
self.parallel_path = nn.Sequential( | ||
nn.Conv2d(in_dim, in_dim, 1), | ||
nn.BatchNorm2d(in_dim), | ||
nn.GELU(), | ||
nn.Conv2d(in_dim, in_dim, 1), | ||
nn.BatchNorm2d(in_dim) | ||
) | ||
|
||
self.conv1 = nn.Conv2d(in_dim, in_dim, 1) | ||
self.conv2 = nn.Conv2d(in_dim, in_dim, 1) | ||
|
||
def forward(self, x): | ||
x1 = self.block1(x) | ||
x2 = self.parallel_path(x) | ||
x = torch.cat([x1, x2], dim=2) | ||
x = self.conv1(x) | ||
x1, x2 = torch.split(x, [x1.shape[2], x2.shape[2]], dim=2) | ||
x = self.conv2(x1) | ||
return x | ||
|
||
def test_pruner(): | ||
model = Net(512) | ||
print(model) | ||
# Global metrics | ||
example_inputs = torch.randn(1, 512, 7, 7) | ||
imp = tp.importance.MagnitudeImportance(p=2) | ||
ignored_layers = [] | ||
|
||
# DO NOT prune the final classifier! | ||
for m in model.modules(): | ||
if isinstance(m, torch.nn.Linear) and m.out_features == 1000: | ||
ignored_layers.append(m) | ||
|
||
iterative_steps = 1 | ||
pruner = tp.pruner.MagnitudePruner( | ||
model, | ||
example_inputs, | ||
importance=imp, | ||
iterative_steps=iterative_steps, | ||
ch_sparsity=0.5, # remove 50% channels, ResNet18 = {64, 128, 256, 512} => ResNet18_Half = {32, 64, 128, 256} | ||
ignored_layers=ignored_layers, | ||
) | ||
|
||
base_macs, base_nparams = tp.utils.count_ops_and_params(model, example_inputs) | ||
for i in range(iterative_steps): | ||
pruner.step() | ||
print(model) | ||
macs, nparams = tp.utils.count_ops_and_params(model, example_inputs) | ||
|
||
print(model(example_inputs).shape) | ||
print( | ||
" Iter %d/%d, Params: %.2f => %.2f" | ||
% (i+1, iterative_steps, base_nparams, nparams) | ||
) | ||
print( | ||
" Iter %d/%d, MACs: %.2f => %.2f" | ||
% (i+1, iterative_steps, base_macs, macs) | ||
) | ||
# finetune your model here | ||
# finetune(model) | ||
# ... | ||
|
||
if __name__=='__main__': | ||
test_pruner() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.