-
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.
- Loading branch information
Showing
3 changed files
with
64 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from transformers import ViTImageProcessor, ViTForImageClassification | ||
from transformers.models.vit.modeling_vit import ViTSelfAttention | ||
import torch_pruning as tp | ||
from PIL import Image | ||
import requests | ||
|
||
url = 'http://images.cocodataset.org/val2017/000000039769.jpg' | ||
image = Image.open(requests.get(url, stream=True).raw) | ||
|
||
processor = ViTImageProcessor.from_pretrained('google/vit-base-patch16-224') | ||
model = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224') | ||
example_inputs = processor(images=image, return_tensors="pt")["pixel_values"] | ||
#outputs = model(example_inputs) | ||
#logits = outputs.logits | ||
# model predicts one of the 1000 ImageNet classes | ||
#predicted_class_idx = logits.argmax(-1).item() | ||
#print("Predicted class:", model.config.id2label[predicted_class_idx]) | ||
|
||
print(model) | ||
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, ViTSelfAttention): | ||
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.logits.sum(), | ||
ignored_layers=[model.classifier], | ||
) | ||
|
||
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, ViTSelfAttention): | ||
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: %d G, Pruned MACs: %d G"%(base_macs/1e9, pruned_macs/1e9)) | ||
print("Base Params: %d M, Pruned Params: %d 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Example for HuggingFace ViT | ||
|
||
## Pruning | ||
```bash | ||
python prune_vit.py | ||
``` |
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