Note
Click here to download the full example code
(beta) Accelerating BERT with semi-structured (2:4) sparsity¶
Author: Jesse Cai
Overview¶
Like other forms of sparsity, semi-structured sparsity is a model optimization technique that seeks to reduce the memory overhead and latency of a neural network at the expense of some model accuracy. It is also known as fine-grained structured sparsity or 2:4 structured sparsity.
Semi-structured sparsity derives its name from its unique sparsity pattern, where n out of every 2n elements are pruned. We most often see n=2, hence 2:4 sparsity Semi-structured sparsity is particularly interesting because it can be efficiently accelerated on GPUs and doesn’t degrade model accuracy as much as other sparsity patterns.
With the introduction of semi-structured sparsity support, it is possible to prune and accelerate a semi-structured sparse model without leaving PyTorch. We will explain this process in this tutorial.
By the end of this tutorial, we will have sparsified a BERT question-answering model to be 2:4 sparse, fine-tuning it to recover nearly all F1 loss (86.92 dense vs 86.48 sparse). Finally, we will accelerate this 2:4 sparse model for inference, yielding a 1.3x speedup.
Requirements¶
PyTorch >= 2.1.
A NVIDIA GPU with semi-structured sparsity support (Compute Capability 8.0+).
This tutorial is designed for beginners to semi-structured sparsity and
sparsity in general. For users with existing 2:4 sparse models,
accelerating nn.Linear
layers for inference with
to_sparse_semi_structured
is quite straightforward. Here is an example:
import torch
from torch.sparse import to_sparse_semi_structured, SparseSemiStructuredTensor
from torch.utils.benchmark import Timer
SparseSemiStructuredTensor._FORCE_CUTLASS = True
# mask Linear weight to be 2:4 sparse
mask = torch.Tensor([0, 0, 1, 1]).tile((3072, 2560)).cuda().bool()
linear = torch.nn.Linear(10240, 3072).half().cuda().eval()
linear.weight = torch.nn.Parameter(mask * linear.weight)
x = torch.rand(3072, 10240).half().cuda()
with torch.inference_mode():
dense_output = linear(x)
dense_t = Timer(stmt="linear(x)",
globals={"linear": linear,
"x": x}).blocked_autorange().median * 1e3
# accelerate via SparseSemiStructuredTensor
linear.weight = torch.nn.Parameter(to_sparse_semi_structured(linear.weight))
sparse_output = linear(x)
sparse_t = Timer(stmt="linear(x)",
globals={"linear": linear,
"x": x}).blocked_autorange().median * 1e3
# sparse and dense matmul are numerically equivalent
# On an A100 80GB, we see: `Dense: 0.870ms Sparse: 0.630ms | Speedup: 1.382x`
assert torch.allclose(sparse_output, dense_output, atol=1e-3)
print(f"Dense: {dense_t:.3f}ms Sparse: {sparse_t:.3f}ms | Speedup: {(dense_t / sparse_t):.3f}x")
Dense: 2.920ms Sparse: 1.661ms | Speedup: 1.758x
What problem does semi-structured sparsity solve?¶
The general motivation behind sparsity is simple: if there are zeros in your network, you can optimize efficiency by not storing or computing those parameters. However, the specifics of sparsity are tricky. Zeroing out parameters doesn’t affect the latency / memory overhead of our model out of the box.
This is because the dense tensor still contains the pruned (zero) elements, which the dense matrix multiplication kernel will still operate on this elements. In order to realize performance gains, we need to swap out dense kernels for sparse kernels, which skip calculation involving pruned elements.
To do this, these kernels work on sparse matrices, which do not store the pruned elements and store the specified elements in a compressed format.
For semi-structured sparsity, we store exactly half of the original parameters along with some compressed metadata about how the elements were arranged.
There are many different sparse layouts, each with their own benefits and drawbacks. The 2:4 semi-structured sparse layout is particularly interesting for two reasons:
Unlike previous sparse formats, semi-structured sparsity was designed to be efficiently accelerated on GPUs. In 2020, NVIDIA introduced hardware support for semi-structured sparsity with their Ampere architecture, and have also released fast sparse kernels via CUTLASS cuSPARSELt.
At the same time, semi-structured sparsity tends to have a milder impact on model accuracy compared to other sparse formats, especially when accounting for more advanced pruning / fine-tuning methods. NVIDIA has shown in their white paper that a simple paradigm of magnitude pruning once to be 2:4 sparse and then retraining the model yields nearly identical model accuracies.
Semi-structured exists in a sweet spot, providing a 2x (theoretical) speedup at a much lower sparsity level (50%), while still being granular enough to preserve model accuracy.
Network |
Data Set |
Metric |
Dense FP16 |
Sparse FP16 |
---|---|---|---|---|
ResNet-50 |
ImageNet |
Top-1 |
76.1 |
76.2 |
ResNeXt-101_32x8d |
ImageNet |
Top-1 |
79.3 |
79.3 |
Xception |
ImageNet |
Top-1 |
79.2 |
79.2 |
SSD-RN50 |
COCO2017 |
bbAP |
24.8 |
24.8 |
MaskRCNN-RN50 |
COCO2017 |
bbAP |
37.9 |
37.9 |
FairSeq Transformer |
EN-DE WMT14 |
BLEU |
28.2 |
28.5 |
BERT-Large |
SQuAD v1.1 |
F1 |
91.9 |
91.9 |
Semi-structured sparsity has an additional advantage from a workflow perspective. Because the sparsity level is fixed at 50%, it is easier to decompose the problem of sparsifying a model into two distinct subproblems:
Accuracy - How can we find a set of 2:4 sparse weights that minimize the accuracy degradation of our model?
Performance - How can we accelerate our 2:4 sparse weights for inference and reduced memory overhead?
The natural handoff point between these two problems are zeroed-out dense tensors. Our inference solution is designed to compress and accelerate tensors in this format. We anticipate many users coming up with custom masking solution, as this is an active area of research.
Now that we’ve learned a little more about semi-structured sparsity, let’s apply it to a BERT model trained on a question answering task, SQuAD.
Intro & Setup¶
Let’s start by importing all the packages we need.
# If you are running this in Google Colab, run:
# .. code-block: python
#
# !pip install datasets transformers evaluate accelerate pandas
#
import os
os.environ["WANDB_DISABLED"] = "true"
import collections
import datasets
import evaluate
import numpy as np
import torch
import torch.utils.benchmark as benchmark
from torch import nn
from torch.sparse import to_sparse_semi_structured, SparseSemiStructuredTensor
from torch.ao.pruning import WeightNormSparsifier
import transformers
# force CUTLASS use if ``cuSPARSELt`` is not available
SparseSemiStructuredTensor._FORCE_CUTLASS = True
torch.manual_seed(100)
<torch._C.Generator object at 0x7f837bdfb510>
We’ll also need to define some helper functions that are specific to the dataset / task at hand. These were adapted from this Hugging Face course as a reference.
def preprocess_validation_function(examples, tokenizer):
inputs = tokenizer(
[q.strip() for q in examples["question"]],
examples["context"],
max_length=384,
truncation="only_second",
return_overflowing_tokens=True,
return_offsets_mapping=True,
padding="max_length",
)
sample_map = inputs.pop("overflow_to_sample_mapping")
example_ids = []
for i in range(len(inputs["input_ids"])):
sample_idx = sample_map[i]
example_ids.append(examples["id"][sample_idx])
sequence_ids = inputs.sequence_ids(i)
offset = inputs["offset_mapping"][i]
inputs["offset_mapping"][i] = [
o if sequence_ids[k] == 1 else None for k, o in enumerate(offset)
]
inputs["example_id"] = example_ids
return inputs
def preprocess_train_function(examples, tokenizer):
inputs = tokenizer(
[q.strip() for q in examples["question"]],
examples["context"],
max_length=384,
truncation="only_second",
return_offsets_mapping=True,
padding="max_length",
)
offset_mapping = inputs["offset_mapping"]
answers = examples["answers"]
start_positions = []
end_positions = []
for i, (offset, answer) in enumerate(zip(offset_mapping, answers)):
start_char = answer["answer_start"][0]
end_char = start_char + len(answer["text"][0])
sequence_ids = inputs.sequence_ids(i)
# Find the start and end of the context
idx = 0
while sequence_ids[idx] != 1:
idx += 1
context_start = idx
while sequence_ids[idx] == 1:
idx += 1
context_end = idx - 1
# If the answer is not fully inside the context, label it (0, 0)
if offset[context_start][0] > end_char or offset[context_end][1] < start_char:
start_positions.append(0)
end_positions.append(0)
else:
# Otherwise it's the start and end token positions
idx = context_start
while idx <= context_end and offset[idx][0] <= start_char:
idx += 1
start_positions.append(idx - 1)
idx = context_end
while idx >= context_start and offset[idx][1] >= end_char:
idx -= 1
end_positions.append(idx + 1)
inputs["start_positions"] = start_positions
inputs["end_positions"] = end_positions
return inputs
def compute_metrics(start_logits, end_logits, features, examples):
n_best = 20
max_answer_length = 30
metric = evaluate.load("squad")
example_to_features = collections.defaultdict(list)
for idx, feature in enumerate(features):
example_to_features[feature["example_id"]].append(idx)
predicted_answers = []
# for example in ``tqdm`` (examples):
for example in examples:
example_id = example["id"]
context = example["context"]
answers = []
# Loop through all features associated with that example
for feature_index in example_to_features[example_id]:
start_logit = start_logits[feature_index]
end_logit = end_logits[feature_index]
offsets = features[feature_index]["offset_mapping"]
start_indexes = np.argsort(start_logit)[-1 : -n_best - 1 : -1].tolist()
end_indexes = np.argsort(end_logit)[-1 : -n_best - 1 : -1].tolist()
for start_index in start_indexes:
for end_index in end_indexes:
# Skip answers that are not fully in the context
if offsets[start_index] is None or offsets[end_index] is None:
continue
# Skip answers with a length that is either < 0
# or > max_answer_length
if (
end_index < start_index
or end_index - start_index + 1 > max_answer_length
):
continue
answer = {
"text": context[
offsets[start_index][0] : offsets[end_index][1]
],
"logit_score": start_logit[start_index] + end_logit[end_index],
}
answers.append(answer)
# Select the answer with the best score
if len(answers) > 0:
best_answer = max(answers, key=lambda x: x["logit_score"])
predicted_answers.append(
{"id": example_id, "prediction_text": best_answer["text"]}
)
else:
predicted_answers.append({"id": example_id, "prediction_text": ""})
theoretical_answers = [
{"id": ex["id"], "answers": ex["answers"]} for ex in examples
]
return metric.compute(predictions=predicted_answers, references=theoretical_answers)
Now that those are defined, we just need one additional helper function, which will help us benchmark our model.
def measure_execution_time(model, batch_sizes, dataset):
dataset_for_model = dataset.remove_columns(["example_id", "offset_mapping"])
dataset_for_model.set_format("torch")
batch_size_to_time_sec = {}
for batch_size in batch_sizes:
batch = {
k: dataset_for_model[k][:batch_size].cuda()
for k in dataset_for_model.column_names
}
with torch.no_grad():
baseline_predictions = model(**batch)
timer = benchmark.Timer(
stmt="model(**batch)", globals={"model": model, "batch": batch}
)
p50 = timer.blocked_autorange().median * 1000
batch_size_to_time_sec[batch_size] = p50
model_c = torch.compile(model, fullgraph=True)
timer = benchmark.Timer(
stmt="model(**batch)", globals={"model": model_c, "batch": batch}
)
p50 = timer.blocked_autorange().median * 1000
batch_size_to_time_sec[f"{batch_size}_compile"] = p50
new_predictions = model_c(**batch)
return batch_size_to_time_sec
We will get started by loading our model and tokenizer, and then setting up our dataset.
# load model
model_name = "bert-base-cased"
tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
model = transformers.AutoModelForQuestionAnswering.from_pretrained(model_name)
print(f"Loading tokenizer: {model_name}")
print(f"Loading model: {model_name}")
# set up train and val dataset
squad_dataset = datasets.load_dataset("squad")
tokenized_squad_dataset = {}
tokenized_squad_dataset["train"] = squad_dataset["train"].map(
lambda x: preprocess_train_function(x, tokenizer), batched=True
)
tokenized_squad_dataset["validation"] = squad_dataset["validation"].map(
lambda x: preprocess_validation_function(x, tokenizer),
batched=True,
remove_columns=squad_dataset["train"].column_names,
)
data_collator = transformers.DataCollatorWithPadding(tokenizer=tokenizer)
Downloading tokenizer_config.json: 0%| | 0.00/49.0 [00:00<?, ?B/s]
Downloading tokenizer_config.json: 100%|##########| 49.0/49.0 [00:00<00:00, 502kB/s]
Downloading config.json: 0%| | 0.00/570 [00:00<?, ?B/s]
Downloading config.json: 100%|##########| 570/570 [00:00<00:00, 7.57MB/s]
Downloading vocab.txt: 0%| | 0.00/213k [00:00<?, ?B/s]
Downloading vocab.txt: 100%|##########| 213k/213k [00:00<00:00, 7.93MB/s]
Downloading tokenizer.json: 0%| | 0.00/436k [00:00<?, ?B/s]
Downloading tokenizer.json: 100%|##########| 436k/436k [00:00<00:00, 3.62MB/s]
Downloading tokenizer.json: 100%|##########| 436k/436k [00:00<00:00, 3.60MB/s]
Downloading model.safetensors: 0%| | 0.00/436M [00:00<?, ?B/s]
Downloading model.safetensors: 14%|#4 | 62.9M/436M [00:00<00:00, 564MB/s]
Downloading model.safetensors: 29%|##8 | 126M/436M [00:00<00:00, 566MB/s]
Downloading model.safetensors: 43%|####3 | 189M/436M [00:00<00:00, 577MB/s]
Downloading model.safetensors: 58%|#####7 | 252M/436M [00:00<00:00, 581MB/s]
Downloading model.safetensors: 72%|#######2 | 315M/436M [00:00<00:00, 575MB/s]
Downloading model.safetensors: 87%|########6 | 377M/436M [00:00<00:00, 563MB/s]
Downloading model.safetensors: 100%|##########| 436M/436M [00:00<00:00, 570MB/s]
Loading tokenizer: bert-base-cased
Loading model: bert-base-cased
Downloading readme: 0%| | 0.00/7.62k [00:00<?, ?B/s]
Downloading readme: 100%|##########| 7.62k/7.62k [00:00<00:00, 51.7MB/s]
Downloading data files: 0%| | 0/2 [00:00<?, ?it/s]
Downloading data: 0%| | 0.00/14.5M [00:00<?, ?B/s]
Downloading data: 87%|########7 | 12.6M/14.5M [00:00<00:00, 101MB/s]
Downloading data: 100%|##########| 14.5M/14.5M [00:00<00:00, 115MB/s]
Downloading data files: 50%|##### | 1/2 [00:00<00:00, 7.85it/s]
Downloading data: 0%| | 0.00/1.82M [00:00<?, ?B/s]
Downloading data: 100%|##########| 1.82M/1.82M [00:00<00:00, 44.8MB/s]
Downloading data files: 100%|##########| 2/2 [00:00<00:00, 11.75it/s]
Extracting data files: 0%| | 0/2 [00:00<?, ?it/s]
Extracting data files: 100%|##########| 2/2 [00:00<00:00, 2324.36it/s]
Generating train split: 0%| | 0/87599 [00:00<?, ? examples/s]
Generating train split: 100%|##########| 87599/87599 [00:00<00:00, 860918.65 examples/s]
Generating train split: 100%|##########| 87599/87599 [00:00<00:00, 854851.39 examples/s]
Generating validation split: 0%| | 0/10570 [00:00<?, ? examples/s]
Generating validation split: 100%|##########| 10570/10570 [00:00<00:00, 731242.88 examples/s]
Map: 0%| | 0/87599 [00:00<?, ? examples/s]
Map: 1%|1 | 1000/87599 [00:00<00:57, 1513.08 examples/s]
Map: 2%|2 | 2000/87599 [00:01<00:50, 1682.83 examples/s]
Map: 3%|3 | 3000/87599 [00:01<00:47, 1776.40 examples/s]
Map: 5%|4 | 4000/87599 [00:02<00:45, 1819.64 examples/s]
Map: 6%|5 | 5000/87599 [00:02<00:44, 1848.78 examples/s]
Map: 7%|6 | 6000/87599 [00:03<00:44, 1852.96 examples/s]
Map: 8%|7 | 7000/87599 [00:03<00:43, 1861.29 examples/s]
Map: 9%|9 | 8000/87599 [00:04<00:42, 1878.60 examples/s]
Map: 10%|# | 9000/87599 [00:04<00:42, 1860.85 examples/s]
Map: 11%|#1 | 10000/87599 [00:05<00:41, 1876.62 examples/s]
Map: 13%|#2 | 11000/87599 [00:05<00:40, 1889.31 examples/s]
Map: 14%|#3 | 12000/87599 [00:06<00:39, 1894.93 examples/s]
Map: 15%|#4 | 13000/87599 [00:07<00:39, 1895.58 examples/s]
Map: 16%|#5 | 14000/87599 [00:07<00:38, 1896.82 examples/s]
Map: 17%|#7 | 15000/87599 [00:08<00:38, 1899.94 examples/s]
Map: 18%|#8 | 16000/87599 [00:08<00:37, 1912.76 examples/s]
Map: 19%|#9 | 17000/87599 [00:09<00:36, 1908.13 examples/s]
Map: 21%|## | 18000/87599 [00:09<00:36, 1914.70 examples/s]
Map: 22%|##1 | 19000/87599 [00:10<00:35, 1911.01 examples/s]
Map: 23%|##2 | 20000/87599 [00:10<00:35, 1912.79 examples/s]
Map: 24%|##3 | 21000/87599 [00:11<00:34, 1905.39 examples/s]
Map: 25%|##5 | 22000/87599 [00:11<00:34, 1910.35 examples/s]
Map: 26%|##6 | 23000/87599 [00:12<00:33, 1909.42 examples/s]
Map: 27%|##7 | 24000/87599 [00:12<00:33, 1883.52 examples/s]
Map: 29%|##8 | 25000/87599 [00:13<00:33, 1896.02 examples/s]
Map: 30%|##9 | 26000/87599 [00:13<00:32, 1886.21 examples/s]
Map: 31%|### | 27000/87599 [00:14<00:32, 1846.94 examples/s]
Map: 32%|###1 | 28000/87599 [00:15<00:48, 1218.79 examples/s]
Map: 33%|###3 | 29000/87599 [00:16<00:43, 1347.76 examples/s]
Map: 34%|###4 | 30000/87599 [00:17<00:39, 1449.41 examples/s]
Map: 35%|###5 | 31000/87599 [00:17<00:37, 1529.33 examples/s]
Map: 37%|###6 | 32000/87599 [00:18<00:34, 1590.84 examples/s]
Map: 38%|###7 | 33000/87599 [00:18<00:33, 1642.04 examples/s]
Map: 39%|###8 | 34000/87599 [00:19<00:31, 1687.54 examples/s]
Map: 40%|###9 | 35000/87599 [00:19<00:30, 1709.77 examples/s]
Map: 41%|####1 | 36000/87599 [00:20<00:29, 1732.88 examples/s]
Map: 42%|####2 | 37000/87599 [00:20<00:29, 1743.97 examples/s]
Map: 43%|####3 | 38000/87599 [00:21<00:28, 1749.54 examples/s]
Map: 45%|####4 | 39000/87599 [00:22<00:27, 1760.95 examples/s]
Map: 46%|####5 | 40000/87599 [00:22<00:27, 1750.48 examples/s]
Map: 47%|####6 | 41000/87599 [00:23<00:26, 1763.92 examples/s]
Map: 48%|####7 | 42000/87599 [00:23<00:25, 1772.44 examples/s]
Map: 49%|####9 | 43000/87599 [00:24<00:25, 1783.86 examples/s]
Map: 50%|##### | 44000/87599 [00:24<00:24, 1776.98 examples/s]
Map: 51%|#####1 | 45000/87599 [00:25<00:24, 1760.72 examples/s]
Map: 53%|#####2 | 46000/87599 [00:26<00:23, 1738.68 examples/s]
Map: 54%|#####3 | 47000/87599 [00:26<00:23, 1744.84 examples/s]
Map: 55%|#####4 | 48000/87599 [00:27<00:22, 1768.00 examples/s]
Map: 56%|#####5 | 49000/87599 [00:27<00:21, 1771.78 examples/s]
Map: 57%|#####7 | 50000/87599 [00:28<00:21, 1772.61 examples/s]
Map: 58%|#####8 | 51000/87599 [00:28<00:20, 1772.78 examples/s]
Map: 59%|#####9 | 52000/87599 [00:29<00:20, 1768.69 examples/s]
Map: 61%|###### | 53000/87599 [00:30<00:19, 1777.41 examples/s]
Map: 62%|######1 | 54000/87599 [00:30<00:18, 1773.99 examples/s]
Map: 63%|######2 | 55000/87599 [00:31<00:18, 1774.41 examples/s]
Map: 64%|######3 | 56000/87599 [00:31<00:17, 1775.86 examples/s]
Map: 65%|######5 | 57000/87599 [00:32<00:17, 1765.80 examples/s]
Map: 66%|######6 | 58000/87599 [00:32<00:16, 1766.01 examples/s]
Map: 67%|######7 | 59000/87599 [00:33<00:16, 1766.76 examples/s]
Map: 68%|######8 | 60000/87599 [00:33<00:15, 1771.69 examples/s]
Map: 70%|######9 | 61000/87599 [00:34<00:15, 1772.82 examples/s]
Map: 71%|####### | 62000/87599 [00:35<00:14, 1768.00 examples/s]
Map: 72%|#######1 | 63000/87599 [00:35<00:13, 1765.52 examples/s]
Map: 73%|#######3 | 64000/87599 [00:36<00:13, 1764.36 examples/s]
Map: 74%|#######4 | 65000/87599 [00:36<00:12, 1754.77 examples/s]
Map: 75%|#######5 | 66000/87599 [00:37<00:12, 1754.49 examples/s]
Map: 76%|#######6 | 67000/87599 [00:37<00:11, 1761.17 examples/s]
Map: 78%|#######7 | 68000/87599 [00:38<00:11, 1754.33 examples/s]
Map: 79%|#######8 | 69000/87599 [00:39<00:10, 1746.51 examples/s]
Map: 80%|#######9 | 70000/87599 [00:40<00:14, 1192.67 examples/s]
Map: 81%|########1 | 71000/87599 [00:41<00:12, 1317.51 examples/s]
Map: 82%|########2 | 72000/87599 [00:41<00:10, 1430.24 examples/s]
Map: 83%|########3 | 73000/87599 [00:42<00:09, 1520.44 examples/s]
Map: 84%|########4 | 74000/87599 [00:42<00:08, 1586.52 examples/s]
Map: 86%|########5 | 75000/87599 [00:43<00:07, 1624.85 examples/s]
Map: 87%|########6 | 76000/87599 [00:43<00:07, 1656.57 examples/s]
Map: 88%|########7 | 77000/87599 [00:44<00:06, 1694.09 examples/s]
Map: 89%|########9 | 78000/87599 [00:45<00:05, 1715.40 examples/s]
Map: 90%|######### | 79000/87599 [00:45<00:05, 1715.55 examples/s]
Map: 91%|#########1| 80000/87599 [00:46<00:04, 1732.16 examples/s]
Map: 92%|#########2| 81000/87599 [00:46<00:03, 1739.36 examples/s]
Map: 94%|#########3| 82000/87599 [00:47<00:03, 1750.32 examples/s]
Map: 95%|#########4| 83000/87599 [00:47<00:02, 1745.89 examples/s]
Map: 96%|#########5| 84000/87599 [00:48<00:02, 1749.25 examples/s]
Map: 97%|#########7| 85000/87599 [00:49<00:01, 1750.84 examples/s]
Map: 98%|#########8| 86000/87599 [00:49<00:00, 1760.03 examples/s]
Map: 99%|#########9| 87000/87599 [00:50<00:00, 1754.65 examples/s]
Map: 100%|##########| 87599/87599 [00:50<00:00, 1733.95 examples/s]
Map: 100%|##########| 87599/87599 [00:50<00:00, 1730.56 examples/s]
Map: 0%| | 0/10570 [00:00<?, ? examples/s]
Map: 9%|9 | 1000/10570 [00:00<00:04, 2349.09 examples/s]
Map: 19%|#8 | 2000/10570 [00:00<00:03, 2349.49 examples/s]
Map: 28%|##8 | 3000/10570 [00:01<00:03, 2287.92 examples/s]
Map: 38%|###7 | 4000/10570 [00:01<00:02, 2230.22 examples/s]
Map: 47%|####7 | 5000/10570 [00:02<00:02, 2068.12 examples/s]
Map: 57%|#####6 | 6000/10570 [00:02<00:02, 2057.85 examples/s]
Map: 66%|######6 | 7000/10570 [00:03<00:01, 2075.05 examples/s]
Map: 76%|#######5 | 8000/10570 [00:03<00:01, 2091.72 examples/s]
Map: 85%|########5 | 9000/10570 [00:04<00:00, 2104.91 examples/s]
Map: 95%|#########4| 10000/10570 [00:04<00:00, 2134.15 examples/s]
Map: 100%|##########| 10570/10570 [00:04<00:00, 2124.02 examples/s]
Map: 100%|##########| 10570/10570 [00:04<00:00, 2132.28 examples/s]
Establishing a baseline¶
Next, we’ll train a quick baseline of our model on SQuAD. This task asks our model to identify spans, or segments of text, in a given context (Wikipedia articles) that answer a given question. Running the following code gives me an F1 score of 86.9. This is quite close to the reported NVIDIA score and the difference is likely due to BERT-base vs. BERT-large or fine-tuning hyperparameters.
training_args = transformers.TrainingArguments(
"trainer",
num_train_epochs=1,
lr_scheduler_type="constant",
per_device_train_batch_size=32,
per_device_eval_batch_size=256,
logging_steps=50,
# Limit max steps for tutorial runners. Delete the below line to see the reported accuracy numbers.
max_steps=500,
report_to=None,
)
trainer = transformers.Trainer(
model,
training_args,
train_dataset=tokenized_squad_dataset["train"],
eval_dataset=tokenized_squad_dataset["validation"],
data_collator=data_collator,
tokenizer=tokenizer,
)
trainer.train()
# batch sizes to compare for eval
batch_sizes = [4, 16, 64, 256]
# 2:4 sparsity require fp16, so we cast here for a fair comparison
with torch.autocast("cuda"):
with torch.no_grad():
predictions = trainer.predict(tokenized_squad_dataset["validation"])
start_logits, end_logits = predictions.predictions
fp16_baseline = compute_metrics(
start_logits,
end_logits,
tokenized_squad_dataset["validation"],
squad_dataset["validation"],
)
fp16_time = measure_execution_time(
model,
batch_sizes,
tokenized_squad_dataset["validation"],
)
print("fp16", fp16_baseline)
print("cuda_fp16 time", fp16_time)
import pandas as pd
df = pd.DataFrame(trainer.state.log_history)
df.plot.line(x='step', y='loss', title="Loss vs. # steps", ylabel="loss")
Detected kernel version 4.14.336, which is below the recommended minimum of 5.5.0; this can cause the process to hang. It is recommended to upgrade the kernel to the minimum version or higher.
0%| | 0/500 [00:00<?, ?it/s]
0%| | 1/500 [00:00<05:08, 1.62it/s]
0%| | 2/500 [00:01<04:58, 1.67it/s]
1%| | 3/500 [00:01<04:55, 1.68it/s]
1%| | 4/500 [00:02<04:54, 1.68it/s]
1%|1 | 5/500 [00:02<04:52, 1.69it/s]
1%|1 | 6/500 [00:03<04:51, 1.69it/s]
1%|1 | 7/500 [00:04<04:50, 1.70it/s]
2%|1 | 8/500 [00:04<04:49, 1.70it/s]
2%|1 | 9/500 [00:05<04:49, 1.70it/s]
2%|2 | 10/500 [00:05<04:48, 1.70it/s]
2%|2 | 11/500 [00:06<04:47, 1.70it/s]
2%|2 | 12/500 [00:07<04:47, 1.70it/s]
3%|2 | 13/500 [00:07<04:46, 1.70it/s]
3%|2 | 14/500 [00:08<04:46, 1.70it/s]
3%|3 | 15/500 [00:08<04:45, 1.70it/s]
3%|3 | 16/500 [00:09<04:45, 1.70it/s]
3%|3 | 17/500 [00:10<04:44, 1.70it/s]
4%|3 | 18/500 [00:10<04:43, 1.70it/s]
4%|3 | 19/500 [00:11<04:43, 1.70it/s]
4%|4 | 20/500 [00:11<04:42, 1.70it/s]
4%|4 | 21/500 [00:12<04:42, 1.70it/s]
4%|4 | 22/500 [00:12<04:42, 1.69it/s]
5%|4 | 23/500 [00:13<04:41, 1.69it/s]
5%|4 | 24/500 [00:14<04:40, 1.69it/s]
5%|5 | 25/500 [00:14<04:40, 1.69it/s]
5%|5 | 26/500 [00:15<04:39, 1.69it/s]
5%|5 | 27/500 [00:15<04:39, 1.69it/s]
6%|5 | 28/500 [00:16<04:38, 1.69it/s]
6%|5 | 29/500 [00:17<04:38, 1.69it/s]
6%|6 | 30/500 [00:17<04:37, 1.69it/s]
6%|6 | 31/500 [00:18<04:37, 1.69it/s]
6%|6 | 32/500 [00:18<04:36, 1.69it/s]
7%|6 | 33/500 [00:19<04:35, 1.69it/s]
7%|6 | 34/500 [00:20<04:35, 1.69it/s]
7%|7 | 35/500 [00:20<04:34, 1.69it/s]
7%|7 | 36/500 [00:21<04:34, 1.69it/s]
7%|7 | 37/500 [00:21<04:33, 1.69it/s]
8%|7 | 38/500 [00:22<04:33, 1.69it/s]
8%|7 | 39/500 [00:23<04:32, 1.69it/s]
8%|8 | 40/500 [00:23<04:32, 1.69it/s]
8%|8 | 41/500 [00:24<04:31, 1.69it/s]
8%|8 | 42/500 [00:24<04:30, 1.69it/s]
9%|8 | 43/500 [00:25<04:30, 1.69it/s]
9%|8 | 44/500 [00:25<04:29, 1.69it/s]
9%|9 | 45/500 [00:26<04:29, 1.69it/s]
9%|9 | 46/500 [00:27<04:28, 1.69it/s]
9%|9 | 47/500 [00:27<04:27, 1.69it/s]
10%|9 | 48/500 [00:28<04:27, 1.69it/s]
10%|9 | 49/500 [00:28<04:26, 1.69it/s]
10%|# | 50/500 [00:29<04:26, 1.69it/s]
{'loss': 3.5771, 'learning_rate': 5e-05, 'epoch': 0.02}
10%|# | 50/500 [00:29<04:26, 1.69it/s]
10%|# | 51/500 [00:30<04:25, 1.69it/s]
10%|# | 52/500 [00:30<04:25, 1.69it/s]
11%|# | 53/500 [00:31<04:24, 1.69it/s]
11%|# | 54/500 [00:31<04:24, 1.69it/s]
11%|#1 | 55/500 [00:32<04:23, 1.69it/s]
11%|#1 | 56/500 [00:33<04:22, 1.69it/s]
11%|#1 | 57/500 [00:33<04:22, 1.69it/s]
12%|#1 | 58/500 [00:34<04:21, 1.69it/s]
12%|#1 | 59/500 [00:34<04:21, 1.69it/s]
12%|#2 | 60/500 [00:35<04:20, 1.69it/s]
12%|#2 | 61/500 [00:36<04:20, 1.69it/s]
12%|#2 | 62/500 [00:36<04:19, 1.69it/s]
13%|#2 | 63/500 [00:37<04:18, 1.69it/s]
13%|#2 | 64/500 [00:37<04:18, 1.69it/s]
13%|#3 | 65/500 [00:38<04:17, 1.69it/s]
13%|#3 | 66/500 [00:39<04:17, 1.69it/s]
13%|#3 | 67/500 [00:39<04:16, 1.69it/s]
14%|#3 | 68/500 [00:40<04:16, 1.69it/s]
14%|#3 | 69/500 [00:40<04:15, 1.69it/s]
14%|#4 | 70/500 [00:41<04:14, 1.69it/s]
14%|#4 | 71/500 [00:41<04:14, 1.69it/s]
14%|#4 | 72/500 [00:42<04:13, 1.69it/s]
15%|#4 | 73/500 [00:43<04:13, 1.69it/s]
15%|#4 | 74/500 [00:43<04:12, 1.69it/s]
15%|#5 | 75/500 [00:44<04:11, 1.69it/s]
15%|#5 | 76/500 [00:44<04:11, 1.69it/s]
15%|#5 | 77/500 [00:45<04:10, 1.69it/s]
16%|#5 | 78/500 [00:46<04:10, 1.69it/s]
16%|#5 | 79/500 [00:46<04:09, 1.69it/s]
16%|#6 | 80/500 [00:47<04:09, 1.69it/s]
16%|#6 | 81/500 [00:47<04:08, 1.69it/s]
16%|#6 | 82/500 [00:48<04:07, 1.69it/s]
17%|#6 | 83/500 [00:49<04:07, 1.69it/s]
17%|#6 | 84/500 [00:49<04:06, 1.69it/s]
17%|#7 | 85/500 [00:50<04:06, 1.69it/s]
17%|#7 | 86/500 [00:50<04:05, 1.69it/s]
17%|#7 | 87/500 [00:51<04:05, 1.69it/s]
18%|#7 | 88/500 [00:52<04:04, 1.69it/s]
18%|#7 | 89/500 [00:52<04:03, 1.68it/s]
18%|#8 | 90/500 [00:53<04:03, 1.68it/s]
18%|#8 | 91/500 [00:53<04:02, 1.68it/s]
18%|#8 | 92/500 [00:54<04:02, 1.68it/s]
19%|#8 | 93/500 [00:55<04:01, 1.68it/s]
19%|#8 | 94/500 [00:55<04:01, 1.68it/s]
19%|#9 | 95/500 [00:56<04:00, 1.68it/s]
19%|#9 | 96/500 [00:56<04:00, 1.68it/s]
19%|#9 | 97/500 [00:57<03:59, 1.68it/s]
20%|#9 | 98/500 [00:58<03:58, 1.68it/s]
20%|#9 | 99/500 [00:58<03:58, 1.68it/s]
20%|## | 100/500 [00:59<03:57, 1.68it/s]
{'loss': 2.0981, 'learning_rate': 5e-05, 'epoch': 0.04}
20%|## | 100/500 [00:59<03:57, 1.68it/s]
20%|## | 101/500 [00:59<03:57, 1.68it/s]
20%|## | 102/500 [01:00<03:56, 1.68it/s]
21%|## | 103/500 [01:00<03:56, 1.68it/s]
21%|## | 104/500 [01:01<03:55, 1.68it/s]
21%|##1 | 105/500 [01:02<03:54, 1.68it/s]
21%|##1 | 106/500 [01:02<03:54, 1.68it/s]
21%|##1 | 107/500 [01:03<03:53, 1.68it/s]
22%|##1 | 108/500 [01:03<03:53, 1.68it/s]
22%|##1 | 109/500 [01:04<03:52, 1.68it/s]
22%|##2 | 110/500 [01:05<03:51, 1.68it/s]
22%|##2 | 111/500 [01:05<03:51, 1.68it/s]
22%|##2 | 112/500 [01:06<03:50, 1.68it/s]
23%|##2 | 113/500 [01:06<03:50, 1.68it/s]
23%|##2 | 114/500 [01:07<03:49, 1.68it/s]
23%|##3 | 115/500 [01:08<03:49, 1.68it/s]
23%|##3 | 116/500 [01:08<03:48, 1.68it/s]
23%|##3 | 117/500 [01:09<03:47, 1.68it/s]
24%|##3 | 118/500 [01:09<03:47, 1.68it/s]
24%|##3 | 119/500 [01:10<03:46, 1.68it/s]
24%|##4 | 120/500 [01:11<03:46, 1.68it/s]
24%|##4 | 121/500 [01:11<03:45, 1.68it/s]
24%|##4 | 122/500 [01:12<03:45, 1.68it/s]
25%|##4 | 123/500 [01:12<03:44, 1.68it/s]
25%|##4 | 124/500 [01:13<03:43, 1.68it/s]
25%|##5 | 125/500 [01:14<03:43, 1.68it/s]
25%|##5 | 126/500 [01:14<03:42, 1.68it/s]
25%|##5 | 127/500 [01:15<03:42, 1.68it/s]
26%|##5 | 128/500 [01:15<03:41, 1.68it/s]
26%|##5 | 129/500 [01:16<03:40, 1.68it/s]
26%|##6 | 130/500 [01:17<03:40, 1.68it/s]
26%|##6 | 131/500 [01:17<03:39, 1.68it/s]
26%|##6 | 132/500 [01:18<03:39, 1.68it/s]
27%|##6 | 133/500 [01:18<03:38, 1.68it/s]
27%|##6 | 134/500 [01:19<03:38, 1.68it/s]
27%|##7 | 135/500 [01:20<03:37, 1.68it/s]
27%|##7 | 136/500 [01:20<03:36, 1.68it/s]
27%|##7 | 137/500 [01:21<03:36, 1.68it/s]
28%|##7 | 138/500 [01:21<03:35, 1.68it/s]
28%|##7 | 139/500 [01:22<03:35, 1.68it/s]
28%|##8 | 140/500 [01:23<03:34, 1.68it/s]
28%|##8 | 141/500 [01:23<03:33, 1.68it/s]
28%|##8 | 142/500 [01:24<03:33, 1.68it/s]
29%|##8 | 143/500 [01:24<03:32, 1.68it/s]
29%|##8 | 144/500 [01:25<03:32, 1.68it/s]
29%|##9 | 145/500 [01:25<03:31, 1.68it/s]
29%|##9 | 146/500 [01:26<03:30, 1.68it/s]
29%|##9 | 147/500 [01:27<03:30, 1.68it/s]
30%|##9 | 148/500 [01:27<03:29, 1.68it/s]
30%|##9 | 149/500 [01:28<03:29, 1.68it/s]
30%|### | 150/500 [01:28<03:28, 1.68it/s]
{'loss': 1.8057, 'learning_rate': 5e-05, 'epoch': 0.05}
30%|### | 150/500 [01:28<03:28, 1.68it/s]
30%|### | 151/500 [01:29<03:28, 1.68it/s]
30%|### | 152/500 [01:30<03:27, 1.68it/s]
31%|### | 153/500 [01:30<03:26, 1.68it/s]
31%|### | 154/500 [01:31<03:26, 1.68it/s]
31%|###1 | 155/500 [01:31<03:25, 1.68it/s]
31%|###1 | 156/500 [01:32<03:25, 1.68it/s]
31%|###1 | 157/500 [01:33<03:24, 1.68it/s]
32%|###1 | 158/500 [01:33<03:23, 1.68it/s]
32%|###1 | 159/500 [01:34<03:23, 1.68it/s]
32%|###2 | 160/500 [01:34<03:22, 1.68it/s]
32%|###2 | 161/500 [01:35<03:22, 1.68it/s]
32%|###2 | 162/500 [01:36<03:21, 1.68it/s]
33%|###2 | 163/500 [01:36<03:20, 1.68it/s]
33%|###2 | 164/500 [01:37<03:20, 1.68it/s]
33%|###3 | 165/500 [01:37<03:19, 1.68it/s]
33%|###3 | 166/500 [01:38<03:19, 1.68it/s]
33%|###3 | 167/500 [01:39<03:18, 1.68it/s]
34%|###3 | 168/500 [01:39<03:17, 1.68it/s]
34%|###3 | 169/500 [01:40<03:17, 1.68it/s]
34%|###4 | 170/500 [01:40<03:16, 1.68it/s]
34%|###4 | 171/500 [01:41<03:16, 1.68it/s]
34%|###4 | 172/500 [01:42<03:15, 1.68it/s]
35%|###4 | 173/500 [01:42<03:14, 1.68it/s]
35%|###4 | 174/500 [01:43<03:14, 1.68it/s]
35%|###5 | 175/500 [01:43<03:13, 1.68it/s]
35%|###5 | 176/500 [01:44<03:13, 1.68it/s]
35%|###5 | 177/500 [01:45<03:12, 1.68it/s]
36%|###5 | 178/500 [01:45<03:12, 1.68it/s]
36%|###5 | 179/500 [01:46<03:11, 1.68it/s]
36%|###6 | 180/500 [01:46<03:10, 1.68it/s]
36%|###6 | 181/500 [01:47<03:10, 1.68it/s]
36%|###6 | 182/500 [01:48<03:09, 1.68it/s]
37%|###6 | 183/500 [01:48<03:09, 1.68it/s]
37%|###6 | 184/500 [01:49<03:08, 1.68it/s]
37%|###7 | 185/500 [01:49<03:07, 1.68it/s]
37%|###7 | 186/500 [01:50<03:07, 1.68it/s]
37%|###7 | 187/500 [01:51<03:06, 1.68it/s]
38%|###7 | 188/500 [01:51<03:06, 1.68it/s]
38%|###7 | 189/500 [01:52<03:05, 1.68it/s]
38%|###8 | 190/500 [01:52<03:05, 1.67it/s]
38%|###8 | 191/500 [01:53<03:04, 1.67it/s]
38%|###8 | 192/500 [01:54<03:03, 1.68it/s]
39%|###8 | 193/500 [01:54<03:03, 1.68it/s]
39%|###8 | 194/500 [01:55<03:02, 1.68it/s]
39%|###9 | 195/500 [01:55<03:02, 1.67it/s]
39%|###9 | 196/500 [01:56<03:01, 1.67it/s]
39%|###9 | 197/500 [01:57<03:00, 1.67it/s]
40%|###9 | 198/500 [01:57<03:00, 1.67it/s]
40%|###9 | 199/500 [01:58<02:59, 1.67it/s]
40%|#### | 200/500 [01:58<02:59, 1.67it/s]
{'loss': 1.6519, 'learning_rate': 5e-05, 'epoch': 0.07}
40%|#### | 200/500 [01:58<02:59, 1.67it/s]
40%|#### | 201/500 [01:59<02:58, 1.67it/s]
40%|#### | 202/500 [01:59<02:58, 1.67it/s]
41%|#### | 203/500 [02:00<02:57, 1.67it/s]
41%|#### | 204/500 [02:01<02:57, 1.67it/s]
41%|####1 | 205/500 [02:01<02:56, 1.67it/s]
41%|####1 | 206/500 [02:02<02:55, 1.67it/s]
41%|####1 | 207/500 [02:02<02:55, 1.67it/s]
42%|####1 | 208/500 [02:03<02:54, 1.67it/s]
42%|####1 | 209/500 [02:04<02:53, 1.67it/s]
42%|####2 | 210/500 [02:04<02:53, 1.67it/s]
42%|####2 | 211/500 [02:05<02:52, 1.67it/s]
42%|####2 | 212/500 [02:05<02:52, 1.67it/s]
43%|####2 | 213/500 [02:06<02:51, 1.67it/s]
43%|####2 | 214/500 [02:07<02:50, 1.67it/s]
43%|####3 | 215/500 [02:07<02:50, 1.67it/s]
43%|####3 | 216/500 [02:08<02:49, 1.67it/s]
43%|####3 | 217/500 [02:08<02:49, 1.67it/s]
44%|####3 | 218/500 [02:09<02:48, 1.67it/s]
44%|####3 | 219/500 [02:10<02:48, 1.67it/s]
44%|####4 | 220/500 [02:10<02:47, 1.67it/s]
44%|####4 | 221/500 [02:11<02:46, 1.67it/s]
44%|####4 | 222/500 [02:11<02:46, 1.67it/s]
45%|####4 | 223/500 [02:12<02:45, 1.67it/s]
45%|####4 | 224/500 [02:13<02:44, 1.67it/s]
45%|####5 | 225/500 [02:13<02:44, 1.67it/s]
45%|####5 | 226/500 [02:14<02:43, 1.67it/s]
45%|####5 | 227/500 [02:14<02:43, 1.67it/s]
46%|####5 | 228/500 [02:15<02:42, 1.67it/s]
46%|####5 | 229/500 [02:16<02:42, 1.67it/s]
46%|####6 | 230/500 [02:16<02:41, 1.67it/s]
46%|####6 | 231/500 [02:17<02:40, 1.67it/s]
46%|####6 | 232/500 [02:17<02:40, 1.67it/s]
47%|####6 | 233/500 [02:18<02:39, 1.67it/s]
47%|####6 | 234/500 [02:19<02:39, 1.67it/s]
47%|####6 | 235/500 [02:19<02:38, 1.67it/s]
47%|####7 | 236/500 [02:20<02:37, 1.67it/s]
47%|####7 | 237/500 [02:20<02:37, 1.67it/s]
48%|####7 | 238/500 [02:21<02:36, 1.67it/s]
48%|####7 | 239/500 [02:22<02:36, 1.67it/s]
48%|####8 | 240/500 [02:22<02:35, 1.67it/s]
48%|####8 | 241/500 [02:23<02:34, 1.67it/s]
48%|####8 | 242/500 [02:23<02:34, 1.67it/s]
49%|####8 | 243/500 [02:24<02:33, 1.67it/s]
49%|####8 | 244/500 [02:25<02:33, 1.67it/s]
49%|####9 | 245/500 [02:25<02:32, 1.67it/s]
49%|####9 | 246/500 [02:26<02:31, 1.67it/s]
49%|####9 | 247/500 [02:26<02:31, 1.67it/s]
50%|####9 | 248/500 [02:27<02:30, 1.67it/s]
50%|####9 | 249/500 [02:28<02:30, 1.67it/s]
50%|##### | 250/500 [02:28<02:29, 1.67it/s]
{'loss': 1.5305, 'learning_rate': 5e-05, 'epoch': 0.09}
50%|##### | 250/500 [02:28<02:29, 1.67it/s]
50%|##### | 251/500 [02:29<02:29, 1.67it/s]
50%|##### | 252/500 [02:29<02:28, 1.67it/s]
51%|##### | 253/500 [02:30<02:27, 1.67it/s]
51%|##### | 254/500 [02:31<02:27, 1.67it/s]
51%|#####1 | 255/500 [02:31<02:26, 1.67it/s]
51%|#####1 | 256/500 [02:32<02:25, 1.67it/s]
51%|#####1 | 257/500 [02:32<02:25, 1.67it/s]
52%|#####1 | 258/500 [02:33<02:24, 1.67it/s]
52%|#####1 | 259/500 [02:34<02:24, 1.67it/s]
52%|#####2 | 260/500 [02:34<02:23, 1.67it/s]
52%|#####2 | 261/500 [02:35<02:22, 1.67it/s]
52%|#####2 | 262/500 [02:35<02:22, 1.67it/s]
53%|#####2 | 263/500 [02:36<02:21, 1.67it/s]
53%|#####2 | 264/500 [02:37<02:21, 1.67it/s]
53%|#####3 | 265/500 [02:37<02:20, 1.67it/s]
53%|#####3 | 266/500 [02:38<02:20, 1.67it/s]
53%|#####3 | 267/500 [02:38<02:19, 1.67it/s]
54%|#####3 | 268/500 [02:39<02:18, 1.67it/s]
54%|#####3 | 269/500 [02:40<02:18, 1.67it/s]
54%|#####4 | 270/500 [02:40<02:17, 1.67it/s]
54%|#####4 | 271/500 [02:41<02:17, 1.67it/s]
54%|#####4 | 272/500 [02:41<02:16, 1.67it/s]
55%|#####4 | 273/500 [02:42<02:15, 1.67it/s]
55%|#####4 | 274/500 [02:43<02:15, 1.67it/s]
55%|#####5 | 275/500 [02:43<02:14, 1.67it/s]
55%|#####5 | 276/500 [02:44<02:14, 1.67it/s]
55%|#####5 | 277/500 [02:44<02:13, 1.67it/s]
56%|#####5 | 278/500 [02:45<02:12, 1.67it/s]
56%|#####5 | 279/500 [02:46<02:12, 1.67it/s]
56%|#####6 | 280/500 [02:46<02:11, 1.67it/s]
56%|#####6 | 281/500 [02:47<02:11, 1.67it/s]
56%|#####6 | 282/500 [02:47<02:10, 1.67it/s]
57%|#####6 | 283/500 [02:48<02:09, 1.67it/s]
57%|#####6 | 284/500 [02:49<02:09, 1.67it/s]
57%|#####6 | 285/500 [02:49<02:08, 1.67it/s]
57%|#####7 | 286/500 [02:50<02:08, 1.67it/s]
57%|#####7 | 287/500 [02:50<02:07, 1.67it/s]
58%|#####7 | 288/500 [02:51<02:06, 1.67it/s]
58%|#####7 | 289/500 [02:52<02:06, 1.67it/s]
58%|#####8 | 290/500 [02:52<02:05, 1.67it/s]
58%|#####8 | 291/500 [02:53<02:05, 1.67it/s]
58%|#####8 | 292/500 [02:53<02:04, 1.67it/s]
59%|#####8 | 293/500 [02:54<02:03, 1.67it/s]
59%|#####8 | 294/500 [02:55<02:03, 1.67it/s]
59%|#####8 | 295/500 [02:55<02:02, 1.67it/s]
59%|#####9 | 296/500 [02:56<02:02, 1.67it/s]
59%|#####9 | 297/500 [02:56<02:01, 1.67it/s]
60%|#####9 | 298/500 [02:57<02:00, 1.67it/s]
60%|#####9 | 299/500 [02:58<02:00, 1.67it/s]
60%|###### | 300/500 [02:58<01:59, 1.67it/s]
{'loss': 1.5315, 'learning_rate': 5e-05, 'epoch': 0.11}
60%|###### | 300/500 [02:58<01:59, 1.67it/s]
60%|###### | 301/500 [02:59<01:59, 1.67it/s]
60%|###### | 302/500 [02:59<01:58, 1.67it/s]
61%|###### | 303/500 [03:00<01:57, 1.67it/s]
61%|###### | 304/500 [03:01<01:57, 1.67it/s]
61%|######1 | 305/500 [03:01<01:56, 1.67it/s]
61%|######1 | 306/500 [03:02<01:56, 1.67it/s]
61%|######1 | 307/500 [03:02<01:55, 1.67it/s]
62%|######1 | 308/500 [03:03<01:54, 1.67it/s]
62%|######1 | 309/500 [03:04<01:54, 1.67it/s]
62%|######2 | 310/500 [03:04<01:53, 1.67it/s]
62%|######2 | 311/500 [03:05<01:53, 1.67it/s]
62%|######2 | 312/500 [03:05<01:52, 1.67it/s]
63%|######2 | 313/500 [03:06<01:51, 1.67it/s]
63%|######2 | 314/500 [03:07<01:51, 1.67it/s]
63%|######3 | 315/500 [03:07<01:50, 1.67it/s]
63%|######3 | 316/500 [03:08<01:50, 1.67it/s]
63%|######3 | 317/500 [03:08<01:49, 1.67it/s]
64%|######3 | 318/500 [03:09<01:48, 1.67it/s]
64%|######3 | 319/500 [03:09<01:48, 1.67it/s]
64%|######4 | 320/500 [03:10<01:47, 1.67it/s]
64%|######4 | 321/500 [03:11<01:47, 1.67it/s]
64%|######4 | 322/500 [03:11<01:46, 1.67it/s]
65%|######4 | 323/500 [03:12<01:45, 1.67it/s]
65%|######4 | 324/500 [03:12<01:45, 1.67it/s]
65%|######5 | 325/500 [03:13<01:44, 1.67it/s]
65%|######5 | 326/500 [03:14<01:44, 1.67it/s]
65%|######5 | 327/500 [03:14<01:43, 1.67it/s]
66%|######5 | 328/500 [03:15<01:42, 1.67it/s]
66%|######5 | 329/500 [03:15<01:42, 1.67it/s]
66%|######6 | 330/500 [03:16<01:41, 1.67it/s]
66%|######6 | 331/500 [03:17<01:41, 1.67it/s]
66%|######6 | 332/500 [03:17<01:40, 1.67it/s]
67%|######6 | 333/500 [03:18<01:39, 1.67it/s]
67%|######6 | 334/500 [03:18<01:39, 1.67it/s]
67%|######7 | 335/500 [03:19<01:38, 1.67it/s]
67%|######7 | 336/500 [03:20<01:38, 1.67it/s]
67%|######7 | 337/500 [03:20<01:37, 1.67it/s]
68%|######7 | 338/500 [03:21<01:36, 1.67it/s]
68%|######7 | 339/500 [03:21<01:36, 1.67it/s]
68%|######8 | 340/500 [03:22<01:35, 1.67it/s]
68%|######8 | 341/500 [03:23<01:35, 1.67it/s]
68%|######8 | 342/500 [03:23<01:34, 1.67it/s]
69%|######8 | 343/500 [03:24<01:33, 1.67it/s]
69%|######8 | 344/500 [03:24<01:33, 1.67it/s]
69%|######9 | 345/500 [03:25<01:32, 1.67it/s]
69%|######9 | 346/500 [03:26<01:32, 1.67it/s]
69%|######9 | 347/500 [03:26<01:31, 1.67it/s]
70%|######9 | 348/500 [03:27<01:30, 1.67it/s]
70%|######9 | 349/500 [03:27<01:30, 1.67it/s]
70%|####### | 350/500 [03:28<01:29, 1.67it/s]
{'loss': 1.4573, 'learning_rate': 5e-05, 'epoch': 0.13}
70%|####### | 350/500 [03:28<01:29, 1.67it/s]
70%|####### | 351/500 [03:29<01:29, 1.67it/s]
70%|####### | 352/500 [03:29<01:28, 1.67it/s]
71%|####### | 353/500 [03:30<01:28, 1.67it/s]
71%|####### | 354/500 [03:30<01:27, 1.67it/s]
71%|#######1 | 355/500 [03:31<01:26, 1.67it/s]
71%|#######1 | 356/500 [03:32<01:26, 1.67it/s]
71%|#######1 | 357/500 [03:32<01:25, 1.67it/s]
72%|#######1 | 358/500 [03:33<01:25, 1.67it/s]
72%|#######1 | 359/500 [03:33<01:24, 1.67it/s]
72%|#######2 | 360/500 [03:34<01:23, 1.67it/s]
72%|#######2 | 361/500 [03:35<01:23, 1.67it/s]
72%|#######2 | 362/500 [03:35<01:22, 1.67it/s]
73%|#######2 | 363/500 [03:36<01:22, 1.67it/s]
73%|#######2 | 364/500 [03:36<01:21, 1.67it/s]
73%|#######3 | 365/500 [03:37<01:20, 1.67it/s]
73%|#######3 | 366/500 [03:38<01:20, 1.67it/s]
73%|#######3 | 367/500 [03:38<01:19, 1.67it/s]
74%|#######3 | 368/500 [03:39<01:19, 1.67it/s]
74%|#######3 | 369/500 [03:39<01:18, 1.67it/s]
74%|#######4 | 370/500 [03:40<01:17, 1.67it/s]
74%|#######4 | 371/500 [03:41<01:17, 1.67it/s]
74%|#######4 | 372/500 [03:41<01:16, 1.67it/s]
75%|#######4 | 373/500 [03:42<01:16, 1.67it/s]
75%|#######4 | 374/500 [03:42<01:15, 1.67it/s]
75%|#######5 | 375/500 [03:43<01:14, 1.67it/s]
75%|#######5 | 376/500 [03:44<01:14, 1.67it/s]
75%|#######5 | 377/500 [03:44<01:13, 1.67it/s]
76%|#######5 | 378/500 [03:45<01:13, 1.67it/s]
76%|#######5 | 379/500 [03:45<01:12, 1.67it/s]
76%|#######6 | 380/500 [03:46<01:11, 1.67it/s]
76%|#######6 | 381/500 [03:47<01:11, 1.67it/s]
76%|#######6 | 382/500 [03:47<01:10, 1.67it/s]
77%|#######6 | 383/500 [03:48<01:10, 1.67it/s]
77%|#######6 | 384/500 [03:48<01:09, 1.67it/s]
77%|#######7 | 385/500 [03:49<01:08, 1.67it/s]
77%|#######7 | 386/500 [03:50<01:08, 1.67it/s]
77%|#######7 | 387/500 [03:50<01:07, 1.67it/s]
78%|#######7 | 388/500 [03:51<01:07, 1.67it/s]
78%|#######7 | 389/500 [03:51<01:06, 1.67it/s]
78%|#######8 | 390/500 [03:52<01:05, 1.67it/s]
78%|#######8 | 391/500 [03:53<01:05, 1.67it/s]
78%|#######8 | 392/500 [03:53<01:04, 1.67it/s]
79%|#######8 | 393/500 [03:54<01:04, 1.67it/s]
79%|#######8 | 394/500 [03:54<01:03, 1.67it/s]
79%|#######9 | 395/500 [03:55<01:02, 1.67it/s]
79%|#######9 | 396/500 [03:56<01:02, 1.67it/s]
79%|#######9 | 397/500 [03:56<01:01, 1.67it/s]
80%|#######9 | 398/500 [03:57<01:01, 1.67it/s]
80%|#######9 | 399/500 [03:57<01:00, 1.67it/s]
80%|######## | 400/500 [03:58<00:59, 1.67it/s]
{'loss': 1.3597, 'learning_rate': 5e-05, 'epoch': 0.15}
80%|######## | 400/500 [03:58<00:59, 1.67it/s]
80%|######## | 401/500 [03:59<00:59, 1.67it/s]
80%|######## | 402/500 [03:59<00:58, 1.67it/s]
81%|######## | 403/500 [04:00<00:58, 1.67it/s]
81%|######## | 404/500 [04:00<00:57, 1.67it/s]
81%|########1 | 405/500 [04:01<00:56, 1.67it/s]
81%|########1 | 406/500 [04:02<00:56, 1.67it/s]
81%|########1 | 407/500 [04:02<00:55, 1.67it/s]
82%|########1 | 408/500 [04:03<00:55, 1.67it/s]
82%|########1 | 409/500 [04:03<00:54, 1.67it/s]
82%|########2 | 410/500 [04:04<00:53, 1.67it/s]
82%|########2 | 411/500 [04:05<00:53, 1.67it/s]
82%|########2 | 412/500 [04:05<00:52, 1.67it/s]
83%|########2 | 413/500 [04:06<00:52, 1.67it/s]
83%|########2 | 414/500 [04:06<00:51, 1.67it/s]
83%|########2 | 415/500 [04:07<00:50, 1.67it/s]
83%|########3 | 416/500 [04:08<00:50, 1.67it/s]
83%|########3 | 417/500 [04:08<00:49, 1.67it/s]
84%|########3 | 418/500 [04:09<00:49, 1.67it/s]
84%|########3 | 419/500 [04:09<00:48, 1.67it/s]
84%|########4 | 420/500 [04:10<00:47, 1.67it/s]
84%|########4 | 421/500 [04:11<00:47, 1.67it/s]
84%|########4 | 422/500 [04:11<00:46, 1.67it/s]
85%|########4 | 423/500 [04:12<00:46, 1.67it/s]
85%|########4 | 424/500 [04:12<00:45, 1.67it/s]
85%|########5 | 425/500 [04:13<00:44, 1.67it/s]
85%|########5 | 426/500 [04:14<00:44, 1.67it/s]
85%|########5 | 427/500 [04:14<00:43, 1.67it/s]
86%|########5 | 428/500 [04:15<00:43, 1.67it/s]
86%|########5 | 429/500 [04:15<00:42, 1.67it/s]
86%|########6 | 430/500 [04:16<00:41, 1.67it/s]
86%|########6 | 431/500 [04:17<00:41, 1.67it/s]
86%|########6 | 432/500 [04:17<00:40, 1.67it/s]
87%|########6 | 433/500 [04:18<00:40, 1.67it/s]
87%|########6 | 434/500 [04:18<00:39, 1.67it/s]
87%|########7 | 435/500 [04:19<00:38, 1.67it/s]
87%|########7 | 436/500 [04:20<00:38, 1.67it/s]
87%|########7 | 437/500 [04:20<00:37, 1.67it/s]
88%|########7 | 438/500 [04:21<00:37, 1.67it/s]
88%|########7 | 439/500 [04:21<00:36, 1.67it/s]
88%|########8 | 440/500 [04:22<00:35, 1.67it/s]
88%|########8 | 441/500 [04:23<00:35, 1.67it/s]
88%|########8 | 442/500 [04:23<00:34, 1.67it/s]
89%|########8 | 443/500 [04:24<00:34, 1.67it/s]
89%|########8 | 444/500 [04:24<00:33, 1.67it/s]
89%|########9 | 445/500 [04:25<00:32, 1.67it/s]
89%|########9 | 446/500 [04:26<00:32, 1.67it/s]
89%|########9 | 447/500 [04:26<00:31, 1.67it/s]
90%|########9 | 448/500 [04:27<00:31, 1.67it/s]
90%|########9 | 449/500 [04:27<00:30, 1.67it/s]
90%|######### | 450/500 [04:28<00:29, 1.67it/s]
{'loss': 1.3616, 'learning_rate': 5e-05, 'epoch': 0.16}
90%|######### | 450/500 [04:28<00:29, 1.67it/s]
90%|######### | 451/500 [04:29<00:29, 1.67it/s]
90%|######### | 452/500 [04:29<00:28, 1.67it/s]
91%|######### | 453/500 [04:30<00:28, 1.67it/s]
91%|######### | 454/500 [04:30<00:27, 1.67it/s]
91%|#########1| 455/500 [04:31<00:26, 1.67it/s]
91%|#########1| 456/500 [04:32<00:26, 1.67it/s]
91%|#########1| 457/500 [04:32<00:25, 1.67it/s]
92%|#########1| 458/500 [04:33<00:25, 1.67it/s]
92%|#########1| 459/500 [04:33<00:24, 1.67it/s]
92%|#########2| 460/500 [04:34<00:23, 1.67it/s]
92%|#########2| 461/500 [04:35<00:23, 1.67it/s]
92%|#########2| 462/500 [04:35<00:22, 1.67it/s]
93%|#########2| 463/500 [04:36<00:22, 1.67it/s]
93%|#########2| 464/500 [04:36<00:21, 1.67it/s]
93%|#########3| 465/500 [04:37<00:20, 1.67it/s]
93%|#########3| 466/500 [04:38<00:20, 1.67it/s]
93%|#########3| 467/500 [04:38<00:19, 1.67it/s]
94%|#########3| 468/500 [04:39<00:19, 1.67it/s]
94%|#########3| 469/500 [04:39<00:18, 1.67it/s]
94%|#########3| 470/500 [04:40<00:17, 1.67it/s]
94%|#########4| 471/500 [04:40<00:17, 1.67it/s]
94%|#########4| 472/500 [04:41<00:16, 1.67it/s]
95%|#########4| 473/500 [04:42<00:16, 1.67it/s]
95%|#########4| 474/500 [04:42<00:15, 1.67it/s]
95%|#########5| 475/500 [04:43<00:14, 1.67it/s]
95%|#########5| 476/500 [04:43<00:14, 1.67it/s]
95%|#########5| 477/500 [04:44<00:13, 1.67it/s]
96%|#########5| 478/500 [04:45<00:13, 1.67it/s]
96%|#########5| 479/500 [04:45<00:12, 1.67it/s]
96%|#########6| 480/500 [04:46<00:11, 1.67it/s]
96%|#########6| 481/500 [04:46<00:11, 1.67it/s]
96%|#########6| 482/500 [04:47<00:10, 1.67it/s]
97%|#########6| 483/500 [04:48<00:10, 1.67it/s]
97%|#########6| 484/500 [04:48<00:09, 1.67it/s]
97%|#########7| 485/500 [04:49<00:08, 1.67it/s]
97%|#########7| 486/500 [04:49<00:08, 1.67it/s]
97%|#########7| 487/500 [04:50<00:07, 1.67it/s]
98%|#########7| 488/500 [04:51<00:07, 1.67it/s]
98%|#########7| 489/500 [04:51<00:06, 1.67it/s]
98%|#########8| 490/500 [04:52<00:05, 1.67it/s]
98%|#########8| 491/500 [04:52<00:05, 1.67it/s]
98%|#########8| 492/500 [04:53<00:04, 1.67it/s]
99%|#########8| 493/500 [04:54<00:04, 1.67it/s]
99%|#########8| 494/500 [04:54<00:03, 1.67it/s]
99%|#########9| 495/500 [04:55<00:02, 1.67it/s]
99%|#########9| 496/500 [04:55<00:02, 1.67it/s]
99%|#########9| 497/500 [04:56<00:01, 1.67it/s]
100%|#########9| 498/500 [04:57<00:01, 1.67it/s]
100%|#########9| 499/500 [04:57<00:00, 1.67it/s]
100%|##########| 500/500 [04:58<00:00, 1.67it/s]
{'loss': 1.3764, 'learning_rate': 5e-05, 'epoch': 0.18}
100%|##########| 500/500 [04:58<00:00, 1.67it/s]
{'train_runtime': 299.2908, 'train_samples_per_second': 53.46, 'train_steps_per_second': 1.671, 'train_loss': 1.7749846496582031, 'epoch': 0.18}
100%|##########| 500/500 [04:59<00:00, 1.67it/s]
100%|##########| 500/500 [04:59<00:00, 1.67it/s]
0%| | 0/43 [00:00<?, ?it/s]
5%|4 | 2/43 [00:00<00:19, 2.14it/s]
7%|6 | 3/43 [00:01<00:26, 1.51it/s]
9%|9 | 4/43 [00:02<00:29, 1.31it/s]
12%|#1 | 5/43 [00:03<00:31, 1.21it/s]
14%|#3 | 6/43 [00:04<00:31, 1.16it/s]
16%|#6 | 7/43 [00:05<00:31, 1.13it/s]
19%|#8 | 8/43 [00:06<00:31, 1.11it/s]
21%|## | 9/43 [00:07<00:30, 1.10it/s]
23%|##3 | 10/43 [00:08<00:30, 1.09it/s]
26%|##5 | 11/43 [00:09<00:29, 1.08it/s]
28%|##7 | 12/43 [00:10<00:28, 1.08it/s]
30%|### | 13/43 [00:11<00:27, 1.08it/s]
33%|###2 | 14/43 [00:12<00:27, 1.07it/s]
35%|###4 | 15/43 [00:13<00:26, 1.07it/s]
37%|###7 | 16/43 [00:14<00:25, 1.07it/s]
40%|###9 | 17/43 [00:14<00:24, 1.07it/s]
42%|####1 | 18/43 [00:15<00:23, 1.07it/s]
44%|####4 | 19/43 [00:16<00:22, 1.07it/s]
47%|####6 | 20/43 [00:17<00:21, 1.07it/s]
49%|####8 | 21/43 [00:18<00:20, 1.07it/s]
51%|#####1 | 22/43 [00:19<00:19, 1.07it/s]
53%|#####3 | 23/43 [00:20<00:18, 1.07it/s]
56%|#####5 | 24/43 [00:21<00:17, 1.07it/s]
58%|#####8 | 25/43 [00:22<00:16, 1.07it/s]
60%|###### | 26/43 [00:23<00:15, 1.07it/s]
63%|######2 | 27/43 [00:24<00:14, 1.07it/s]
65%|######5 | 28/43 [00:25<00:14, 1.07it/s]
67%|######7 | 29/43 [00:26<00:13, 1.07it/s]
70%|######9 | 30/43 [00:27<00:12, 1.07it/s]
72%|#######2 | 31/43 [00:28<00:11, 1.07it/s]
74%|#######4 | 32/43 [00:28<00:10, 1.07it/s]
77%|#######6 | 33/43 [00:29<00:09, 1.07it/s]
79%|#######9 | 34/43 [00:30<00:08, 1.07it/s]
81%|########1 | 35/43 [00:31<00:07, 1.07it/s]
84%|########3 | 36/43 [00:32<00:06, 1.07it/s]
86%|########6 | 37/43 [00:33<00:05, 1.07it/s]
88%|########8 | 38/43 [00:34<00:04, 1.07it/s]
91%|######### | 39/43 [00:35<00:03, 1.07it/s]
93%|#########3| 40/43 [00:36<00:02, 1.07it/s]
95%|#########5| 41/43 [00:37<00:01, 1.07it/s]
98%|#########7| 42/43 [00:38<00:00, 1.11it/s]
100%|##########| 43/43 [00:38<00:00, 1.49it/s]
100%|##########| 43/43 [00:38<00:00, 1.12it/s]
Downloading builder script: 0%| | 0.00/4.53k [00:00<?, ?B/s]
Downloading builder script: 100%|##########| 4.53k/4.53k [00:00<00:00, 23.4MB/s]
Downloading extra modules: 0%| | 0.00/3.32k [00:00<?, ?B/s]
Downloading extra modules: 100%|##########| 3.32k/3.32k [00:00<00:00, 29.6MB/s]
fp16 {'exact_match': 71.77861873226112, 'f1': 81.35189141414303}
cuda_fp16 time {4: 14.085199850001118, '4_compile': 9.62217000005694, 16: 51.11990209998112, '16_compile': 36.41714150012376, 64: 200.92095900008644, '64_compile': 123.100574000091, 256: 785.3004060002604, '256_compile': 474.6465499997612}
<Axes: title={'center': 'Loss vs. # steps'}, xlabel='step', ylabel='loss'>
Pruning BERT to be 2:4 sparse¶
Now that we have our baseline, it’s time we prune BERT. There are many different pruning strategies, but one of the most common is magnitude pruning, which seeks to remove the weights with the lowest L1 norm. Magnitude pruning was used by NVIDIA in all their results and is a common baseline.
To do this, we will use the torch.ao.pruning
package, which contains
a weight-norm (magnitude) sparsifier. These sparsifiers work by applying
mask parametrizations to the weight tensors in a model. This lets them
simulate sparsity by masking out the pruned weights.
We’ll also have to decide what layers of the model to apply sparsity to,
which in this case is all of the nn.Linear
layers, except for the
task-specific head outputs. That’s because semi-structured sparsity has
shape constraints,
and the task-specific nn.Linear
layers do not satisfy them.
sparsifier = WeightNormSparsifier(
# apply sparsity to all blocks
sparsity_level=1.0,
# shape of 4 elements is a block
sparse_block_shape=(1, 4),
# two zeros for every block of 4
zeros_per_block=2
)
# add to config if ``nn.Linear`` and in the BERT model.
sparse_config = [
{"tensor_fqn": f"{fqn}.weight"}
for fqn, module in model.named_modules()
if isinstance(module, nn.Linear) and "layer" in fqn
]
The first step for pruning the model is to insert parametrizations for
masking the weights of the model. This is done by the prepare step.
Anytime we try to access the .weight
we will get mask * weight
instead.
# Prepare the model, insert fake-sparsity parametrizations for training
sparsifier.prepare(model, sparse_config)
print(model.bert.encoder.layer[0].output)
BertOutput(
(dense): ParametrizedLinear(
in_features=3072, out_features=768, bias=True
(parametrizations): ModuleDict(
(weight): ParametrizationList(
(0): FakeSparsity()
)
)
)
(LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
(dropout): Dropout(p=0.1, inplace=False)
)
Then, we’ll take a single pruning step. All pruners implement a
update_mask()
method that updates the mask with the logic being
determined by the pruner implementation. The step method calls this
update_mask
functions for the weights specified in the sparse
config.
We will also evaluate the model to show the accuracy degradation of zero-shot pruning, or pruning without fine-tuning / retraining.
sparsifier.step()
with torch.autocast("cuda"):
with torch.no_grad():
predictions = trainer.predict(tokenized_squad_dataset["validation"])
pruned = compute_metrics(
*predictions.predictions,
tokenized_squad_dataset["validation"],
squad_dataset["validation"],
)
print("pruned eval metrics:", pruned)
0%| | 0/43 [00:00<?, ?it/s]
5%|4 | 2/43 [00:00<00:19, 2.13it/s]
7%|6 | 3/43 [00:01<00:26, 1.50it/s]
9%|9 | 4/43 [00:02<00:29, 1.30it/s]
12%|#1 | 5/43 [00:03<00:31, 1.21it/s]
14%|#3 | 6/43 [00:04<00:32, 1.16it/s]
16%|#6 | 7/43 [00:05<00:32, 1.12it/s]
19%|#8 | 8/43 [00:06<00:31, 1.11it/s]
21%|## | 9/43 [00:07<00:31, 1.09it/s]
23%|##3 | 10/43 [00:08<00:30, 1.08it/s]
26%|##5 | 11/43 [00:09<00:29, 1.08it/s]
28%|##7 | 12/43 [00:10<00:28, 1.07it/s]
30%|### | 13/43 [00:11<00:28, 1.07it/s]
33%|###2 | 14/43 [00:12<00:27, 1.07it/s]
35%|###4 | 15/43 [00:13<00:26, 1.07it/s]
37%|###7 | 16/43 [00:14<00:25, 1.07it/s]
40%|###9 | 17/43 [00:15<00:24, 1.07it/s]
42%|####1 | 18/43 [00:15<00:23, 1.06it/s]
44%|####4 | 19/43 [00:16<00:22, 1.06it/s]
47%|####6 | 20/43 [00:17<00:21, 1.06it/s]
49%|####8 | 21/43 [00:18<00:20, 1.06it/s]
51%|#####1 | 22/43 [00:19<00:19, 1.06it/s]
53%|#####3 | 23/43 [00:20<00:18, 1.06it/s]
56%|#####5 | 24/43 [00:21<00:17, 1.06it/s]
58%|#####8 | 25/43 [00:22<00:16, 1.06it/s]
60%|###### | 26/43 [00:23<00:15, 1.07it/s]
63%|######2 | 27/43 [00:24<00:15, 1.07it/s]
65%|######5 | 28/43 [00:25<00:14, 1.06it/s]
67%|######7 | 29/43 [00:26<00:13, 1.06it/s]
70%|######9 | 30/43 [00:27<00:12, 1.06it/s]
72%|#######2 | 31/43 [00:28<00:11, 1.06it/s]
74%|#######4 | 32/43 [00:29<00:10, 1.06it/s]
77%|#######6 | 33/43 [00:30<00:09, 1.06it/s]
79%|#######9 | 34/43 [00:31<00:08, 1.06it/s]
81%|########1 | 35/43 [00:31<00:07, 1.06it/s]
84%|########3 | 36/43 [00:32<00:06, 1.06it/s]
86%|########6 | 37/43 [00:33<00:05, 1.06it/s]
88%|########8 | 38/43 [00:34<00:04, 1.06it/s]
91%|######### | 39/43 [00:35<00:03, 1.06it/s]
93%|#########3| 40/43 [00:36<00:02, 1.06it/s]
95%|#########5| 41/43 [00:37<00:01, 1.07it/s]
98%|#########7| 42/43 [00:38<00:00, 1.11it/s]
100%|##########| 43/43 [00:38<00:00, 1.48it/s]
100%|##########| 43/43 [00:38<00:00, 1.11it/s]
pruned eval metrics: {'exact_match': 31.069063386944183, 'f1': 43.162708225870595}
In this state, we can start fine-tuning the model, updating the elements
that wouldn’t be pruned to better account for the accuracy loss. Once
we’ve reached a satisfied state, we can call squash_mask
to fuse the
mask and the weight together. This will remove the parametrizations and
we are left with a zeroed-out 2:4 dense model.
trainer.train()
sparsifier.squash_mask()
torch.set_printoptions(edgeitems=4)
print(model.bert.encoder.layer[0].intermediate.dense.weight[:8, :8])
df["sparse_loss"] = pd.DataFrame(trainer.state.log_history)["loss"]
df.plot.line(x='step', y=["loss", "sparse_loss"], title="Loss vs. # steps", ylabel="loss")
0%| | 0/500 [00:00<?, ?it/s]
0%| | 1/500 [00:00<05:11, 1.60it/s]
0%| | 2/500 [00:01<05:01, 1.65it/s]
1%| | 3/500 [00:01<04:57, 1.67it/s]
1%| | 4/500 [00:02<04:56, 1.67it/s]
1%|1 | 5/500 [00:02<04:55, 1.68it/s]
1%|1 | 6/500 [00:03<04:54, 1.68it/s]
1%|1 | 7/500 [00:04<04:53, 1.68it/s]
2%|1 | 8/500 [00:04<04:52, 1.68it/s]
2%|1 | 9/500 [00:05<04:52, 1.68it/s]
2%|2 | 10/500 [00:05<04:51, 1.68it/s]
2%|2 | 11/500 [00:06<04:50, 1.68it/s]
2%|2 | 12/500 [00:07<04:50, 1.68it/s]
3%|2 | 13/500 [00:07<04:49, 1.68it/s]
3%|2 | 14/500 [00:08<04:49, 1.68it/s]
3%|3 | 15/500 [00:08<04:48, 1.68it/s]
3%|3 | 16/500 [00:09<04:48, 1.68it/s]
3%|3 | 17/500 [00:10<04:47, 1.68it/s]
4%|3 | 18/500 [00:10<04:46, 1.68it/s]
4%|3 | 19/500 [00:11<04:46, 1.68it/s]
4%|4 | 20/500 [00:11<04:45, 1.68it/s]
4%|4 | 21/500 [00:12<04:45, 1.68it/s]
4%|4 | 22/500 [00:13<04:44, 1.68it/s]
5%|4 | 23/500 [00:13<04:43, 1.68it/s]
5%|4 | 24/500 [00:14<04:43, 1.68it/s]
5%|5 | 25/500 [00:14<04:42, 1.68it/s]
5%|5 | 26/500 [00:15<04:42, 1.68it/s]
5%|5 | 27/500 [00:16<04:41, 1.68it/s]
6%|5 | 28/500 [00:16<04:40, 1.68it/s]
6%|5 | 29/500 [00:17<04:40, 1.68it/s]
6%|6 | 30/500 [00:17<04:39, 1.68it/s]
6%|6 | 31/500 [00:18<04:39, 1.68it/s]
6%|6 | 32/500 [00:19<04:38, 1.68it/s]
7%|6 | 33/500 [00:19<04:38, 1.68it/s]
7%|6 | 34/500 [00:20<04:37, 1.68it/s]
7%|7 | 35/500 [00:20<04:37, 1.68it/s]
7%|7 | 36/500 [00:21<04:36, 1.68it/s]
7%|7 | 37/500 [00:22<04:35, 1.68it/s]
8%|7 | 38/500 [00:22<04:35, 1.68it/s]
8%|7 | 39/500 [00:23<04:34, 1.68it/s]
8%|8 | 40/500 [00:23<04:34, 1.68it/s]
8%|8 | 41/500 [00:24<04:33, 1.68it/s]
8%|8 | 42/500 [00:25<04:33, 1.68it/s]
9%|8 | 43/500 [00:25<04:32, 1.68it/s]
9%|8 | 44/500 [00:26<04:32, 1.68it/s]
9%|9 | 45/500 [00:26<04:31, 1.68it/s]
9%|9 | 46/500 [00:27<04:30, 1.68it/s]
9%|9 | 47/500 [00:28<04:30, 1.68it/s]
10%|9 | 48/500 [00:28<04:29, 1.68it/s]
10%|9 | 49/500 [00:29<04:29, 1.68it/s]
10%|# | 50/500 [00:29<04:28, 1.68it/s]
{'loss': 1.845, 'learning_rate': 5e-05, 'epoch': 0.02}
10%|# | 50/500 [00:29<04:28, 1.68it/s]
10%|# | 51/500 [00:30<04:28, 1.67it/s]
10%|# | 52/500 [00:30<04:27, 1.67it/s]
11%|# | 53/500 [00:31<04:26, 1.67it/s]
11%|# | 54/500 [00:32<04:26, 1.67it/s]
11%|#1 | 55/500 [00:32<04:25, 1.67it/s]
11%|#1 | 56/500 [00:33<04:25, 1.67it/s]
11%|#1 | 57/500 [00:33<04:24, 1.67it/s]
12%|#1 | 58/500 [00:34<04:24, 1.67it/s]
12%|#1 | 59/500 [00:35<04:23, 1.67it/s]
12%|#2 | 60/500 [00:35<04:23, 1.67it/s]
12%|#2 | 61/500 [00:36<04:22, 1.67it/s]
12%|#2 | 62/500 [00:36<04:21, 1.67it/s]
13%|#2 | 63/500 [00:37<04:21, 1.67it/s]
13%|#2 | 64/500 [00:38<04:20, 1.67it/s]
13%|#3 | 65/500 [00:38<04:20, 1.67it/s]
13%|#3 | 66/500 [00:39<04:19, 1.67it/s]
13%|#3 | 67/500 [00:39<04:19, 1.67it/s]
14%|#3 | 68/500 [00:40<04:18, 1.67it/s]
14%|#3 | 69/500 [00:41<04:18, 1.67it/s]
14%|#4 | 70/500 [00:41<04:17, 1.67it/s]
14%|#4 | 71/500 [00:42<04:16, 1.67it/s]
14%|#4 | 72/500 [00:42<04:16, 1.67it/s]
15%|#4 | 73/500 [00:43<04:15, 1.67it/s]
15%|#4 | 74/500 [00:44<04:15, 1.67it/s]
15%|#5 | 75/500 [00:44<04:14, 1.67it/s]
15%|#5 | 76/500 [00:45<04:13, 1.67it/s]
15%|#5 | 77/500 [00:45<04:13, 1.67it/s]
16%|#5 | 78/500 [00:46<04:12, 1.67it/s]
16%|#5 | 79/500 [00:47<04:12, 1.67it/s]
16%|#6 | 80/500 [00:47<04:11, 1.67it/s]
16%|#6 | 81/500 [00:48<04:11, 1.67it/s]
16%|#6 | 82/500 [00:48<04:10, 1.67it/s]
17%|#6 | 83/500 [00:49<04:09, 1.67it/s]
17%|#6 | 84/500 [00:50<04:09, 1.67it/s]
17%|#7 | 85/500 [00:50<04:08, 1.67it/s]
17%|#7 | 86/500 [00:51<04:08, 1.67it/s]
17%|#7 | 87/500 [00:51<04:07, 1.67it/s]
18%|#7 | 88/500 [00:52<04:06, 1.67it/s]
18%|#7 | 89/500 [00:53<04:06, 1.67it/s]
18%|#8 | 90/500 [00:53<04:05, 1.67it/s]
18%|#8 | 91/500 [00:54<04:05, 1.67it/s]
18%|#8 | 92/500 [00:54<04:04, 1.67it/s]
19%|#8 | 93/500 [00:55<04:04, 1.67it/s]
19%|#8 | 94/500 [00:56<04:03, 1.67it/s]
19%|#9 | 95/500 [00:56<04:02, 1.67it/s]
19%|#9 | 96/500 [00:57<04:02, 1.67it/s]
19%|#9 | 97/500 [00:57<04:01, 1.67it/s]
20%|#9 | 98/500 [00:58<04:01, 1.67it/s]
20%|#9 | 99/500 [00:59<04:00, 1.67it/s]
20%|## | 100/500 [00:59<03:59, 1.67it/s]
{'loss': 1.4599, 'learning_rate': 5e-05, 'epoch': 0.04}
20%|## | 100/500 [00:59<03:59, 1.67it/s]
20%|## | 101/500 [01:00<03:59, 1.67it/s]
20%|## | 102/500 [01:00<03:58, 1.67it/s]
21%|## | 103/500 [01:01<03:58, 1.67it/s]
21%|## | 104/500 [01:02<03:57, 1.67it/s]
21%|##1 | 105/500 [01:02<03:56, 1.67it/s]
21%|##1 | 106/500 [01:03<03:56, 1.67it/s]
21%|##1 | 107/500 [01:03<03:55, 1.67it/s]
22%|##1 | 108/500 [01:04<03:55, 1.67it/s]
22%|##1 | 109/500 [01:05<03:54, 1.67it/s]
22%|##2 | 110/500 [01:05<03:54, 1.67it/s]
22%|##2 | 111/500 [01:06<03:53, 1.67it/s]
22%|##2 | 112/500 [01:06<03:52, 1.67it/s]
23%|##2 | 113/500 [01:07<03:52, 1.67it/s]
23%|##2 | 114/500 [01:08<03:51, 1.67it/s]
23%|##3 | 115/500 [01:08<03:51, 1.67it/s]
23%|##3 | 116/500 [01:09<03:50, 1.67it/s]
23%|##3 | 117/500 [01:09<03:49, 1.67it/s]
24%|##3 | 118/500 [01:10<03:49, 1.67it/s]
24%|##3 | 119/500 [01:11<03:48, 1.66it/s]
24%|##4 | 120/500 [01:11<03:48, 1.66it/s]
24%|##4 | 121/500 [01:12<03:47, 1.67it/s]
24%|##4 | 122/500 [01:12<03:46, 1.67it/s]
25%|##4 | 123/500 [01:13<03:46, 1.67it/s]
25%|##4 | 124/500 [01:14<03:45, 1.67it/s]
25%|##5 | 125/500 [01:14<03:45, 1.67it/s]
25%|##5 | 126/500 [01:15<03:44, 1.67it/s]
25%|##5 | 127/500 [01:15<03:43, 1.67it/s]
26%|##5 | 128/500 [01:16<03:43, 1.67it/s]
26%|##5 | 129/500 [01:17<03:42, 1.67it/s]
26%|##6 | 130/500 [01:17<03:42, 1.67it/s]
26%|##6 | 131/500 [01:18<03:41, 1.67it/s]
26%|##6 | 132/500 [01:18<03:40, 1.67it/s]
27%|##6 | 133/500 [01:19<03:40, 1.66it/s]
27%|##6 | 134/500 [01:20<03:39, 1.66it/s]
27%|##7 | 135/500 [01:20<03:39, 1.66it/s]
27%|##7 | 136/500 [01:21<03:38, 1.66it/s]
27%|##7 | 137/500 [01:21<03:38, 1.66it/s]
28%|##7 | 138/500 [01:22<03:37, 1.66it/s]
28%|##7 | 139/500 [01:23<03:36, 1.66it/s]
28%|##8 | 140/500 [01:23<03:36, 1.66it/s]
28%|##8 | 141/500 [01:24<03:35, 1.66it/s]
28%|##8 | 142/500 [01:24<03:35, 1.66it/s]
29%|##8 | 143/500 [01:25<03:34, 1.66it/s]
29%|##8 | 144/500 [01:26<03:34, 1.66it/s]
29%|##9 | 145/500 [01:26<03:33, 1.66it/s]
29%|##9 | 146/500 [01:27<03:33, 1.66it/s]
29%|##9 | 147/500 [01:27<03:32, 1.66it/s]
30%|##9 | 148/500 [01:28<03:31, 1.66it/s]
30%|##9 | 149/500 [01:29<03:31, 1.66it/s]
30%|### | 150/500 [01:29<03:30, 1.66it/s]
{'loss': 1.3952, 'learning_rate': 5e-05, 'epoch': 0.05}
30%|### | 150/500 [01:29<03:30, 1.66it/s]
30%|### | 151/500 [01:30<03:29, 1.66it/s]
30%|### | 152/500 [01:30<03:29, 1.66it/s]
31%|### | 153/500 [01:31<03:28, 1.66it/s]
31%|### | 154/500 [01:32<03:28, 1.66it/s]
31%|###1 | 155/500 [01:32<03:27, 1.66it/s]
31%|###1 | 156/500 [01:33<03:26, 1.66it/s]
31%|###1 | 157/500 [01:33<03:26, 1.66it/s]
32%|###1 | 158/500 [01:34<03:25, 1.66it/s]
32%|###1 | 159/500 [01:35<03:25, 1.66it/s]
32%|###2 | 160/500 [01:35<03:24, 1.66it/s]
32%|###2 | 161/500 [01:36<03:23, 1.66it/s]
32%|###2 | 162/500 [01:37<03:23, 1.66it/s]
33%|###2 | 163/500 [01:37<03:22, 1.66it/s]
33%|###2 | 164/500 [01:38<03:22, 1.66it/s]
33%|###3 | 165/500 [01:38<03:21, 1.66it/s]
33%|###3 | 166/500 [01:39<03:20, 1.66it/s]
33%|###3 | 167/500 [01:40<03:20, 1.66it/s]
34%|###3 | 168/500 [01:40<03:19, 1.66it/s]
34%|###3 | 169/500 [01:41<03:19, 1.66it/s]
34%|###4 | 170/500 [01:41<03:18, 1.66it/s]
34%|###4 | 171/500 [01:42<03:17, 1.66it/s]
34%|###4 | 172/500 [01:43<03:17, 1.66it/s]
35%|###4 | 173/500 [01:43<03:16, 1.66it/s]
35%|###4 | 174/500 [01:44<03:16, 1.66it/s]
35%|###5 | 175/500 [01:44<03:15, 1.66it/s]
35%|###5 | 176/500 [01:45<03:14, 1.66it/s]
35%|###5 | 177/500 [01:46<03:14, 1.66it/s]
36%|###5 | 178/500 [01:46<03:13, 1.66it/s]
36%|###5 | 179/500 [01:47<03:13, 1.66it/s]
36%|###6 | 180/500 [01:47<03:12, 1.66it/s]
36%|###6 | 181/500 [01:48<03:12, 1.66it/s]
36%|###6 | 182/500 [01:49<03:11, 1.66it/s]
37%|###6 | 183/500 [01:49<03:10, 1.66it/s]
37%|###6 | 184/500 [01:50<03:10, 1.66it/s]
37%|###7 | 185/500 [01:50<03:09, 1.66it/s]
37%|###7 | 186/500 [01:51<03:09, 1.66it/s]
37%|###7 | 187/500 [01:52<03:08, 1.66it/s]
38%|###7 | 188/500 [01:52<03:07, 1.66it/s]
38%|###7 | 189/500 [01:53<03:07, 1.66it/s]
38%|###8 | 190/500 [01:53<03:06, 1.66it/s]
38%|###8 | 191/500 [01:54<03:06, 1.66it/s]
38%|###8 | 192/500 [01:55<03:05, 1.66it/s]
39%|###8 | 193/500 [01:55<03:04, 1.66it/s]
39%|###8 | 194/500 [01:56<03:04, 1.66it/s]
39%|###9 | 195/500 [01:56<03:03, 1.66it/s]
39%|###9 | 196/500 [01:57<03:03, 1.66it/s]
39%|###9 | 197/500 [01:58<03:02, 1.66it/s]
40%|###9 | 198/500 [01:58<03:01, 1.66it/s]
40%|###9 | 199/500 [01:59<03:01, 1.66it/s]
40%|#### | 200/500 [01:59<03:00, 1.66it/s]
{'loss': 1.4239, 'learning_rate': 5e-05, 'epoch': 0.07}
40%|#### | 200/500 [01:59<03:00, 1.66it/s]
40%|#### | 201/500 [02:00<03:00, 1.66it/s]
40%|#### | 202/500 [02:01<02:59, 1.66it/s]
41%|#### | 203/500 [02:01<02:58, 1.66it/s]
41%|#### | 204/500 [02:02<02:58, 1.66it/s]
41%|####1 | 205/500 [02:02<02:57, 1.66it/s]
41%|####1 | 206/500 [02:03<02:56, 1.66it/s]
41%|####1 | 207/500 [02:04<02:56, 1.66it/s]
42%|####1 | 208/500 [02:04<02:55, 1.66it/s]
42%|####1 | 209/500 [02:05<02:55, 1.66it/s]
42%|####2 | 210/500 [02:05<02:54, 1.66it/s]
42%|####2 | 211/500 [02:06<02:54, 1.66it/s]
42%|####2 | 212/500 [02:07<02:53, 1.66it/s]
43%|####2 | 213/500 [02:07<02:52, 1.66it/s]
43%|####2 | 214/500 [02:08<02:52, 1.66it/s]
43%|####3 | 215/500 [02:08<02:51, 1.66it/s]
43%|####3 | 216/500 [02:09<02:50, 1.66it/s]
43%|####3 | 217/500 [02:10<02:50, 1.66it/s]
44%|####3 | 218/500 [02:10<02:49, 1.66it/s]
44%|####3 | 219/500 [02:11<02:49, 1.66it/s]
44%|####4 | 220/500 [02:11<02:48, 1.66it/s]
44%|####4 | 221/500 [02:12<02:47, 1.66it/s]
44%|####4 | 222/500 [02:13<02:47, 1.66it/s]
45%|####4 | 223/500 [02:13<02:46, 1.66it/s]
45%|####4 | 224/500 [02:14<02:46, 1.66it/s]
45%|####5 | 225/500 [02:14<02:45, 1.66it/s]
45%|####5 | 226/500 [02:15<02:44, 1.66it/s]
45%|####5 | 227/500 [02:16<02:44, 1.66it/s]
46%|####5 | 228/500 [02:16<02:43, 1.66it/s]
46%|####5 | 229/500 [02:17<02:43, 1.66it/s]
46%|####6 | 230/500 [02:17<02:42, 1.66it/s]
46%|####6 | 231/500 [02:18<02:41, 1.66it/s]
46%|####6 | 232/500 [02:19<02:41, 1.66it/s]
47%|####6 | 233/500 [02:19<02:40, 1.66it/s]
47%|####6 | 234/500 [02:20<02:40, 1.66it/s]
47%|####6 | 235/500 [02:20<02:39, 1.66it/s]
47%|####7 | 236/500 [02:21<02:38, 1.66it/s]
47%|####7 | 237/500 [02:22<02:38, 1.66it/s]
48%|####7 | 238/500 [02:22<02:37, 1.66it/s]
48%|####7 | 239/500 [02:23<02:37, 1.66it/s]
48%|####8 | 240/500 [02:23<02:36, 1.66it/s]
48%|####8 | 241/500 [02:24<02:36, 1.66it/s]
48%|####8 | 242/500 [02:25<02:35, 1.66it/s]
49%|####8 | 243/500 [02:25<02:34, 1.66it/s]
49%|####8 | 244/500 [02:26<02:34, 1.66it/s]
49%|####9 | 245/500 [02:26<02:33, 1.66it/s]
49%|####9 | 246/500 [02:27<02:32, 1.66it/s]
49%|####9 | 247/500 [02:28<02:32, 1.66it/s]
50%|####9 | 248/500 [02:28<02:31, 1.66it/s]
50%|####9 | 249/500 [02:29<02:31, 1.66it/s]
50%|##### | 250/500 [02:29<02:30, 1.66it/s]
{'loss': 1.353, 'learning_rate': 5e-05, 'epoch': 0.09}
50%|##### | 250/500 [02:29<02:30, 1.66it/s]
50%|##### | 251/500 [02:30<02:30, 1.66it/s]
50%|##### | 252/500 [02:31<02:29, 1.66it/s]
51%|##### | 253/500 [02:31<02:28, 1.66it/s]
51%|##### | 254/500 [02:32<02:28, 1.66it/s]
51%|#####1 | 255/500 [02:32<02:27, 1.66it/s]
51%|#####1 | 256/500 [02:33<02:26, 1.66it/s]
51%|#####1 | 257/500 [02:34<02:26, 1.66it/s]
52%|#####1 | 258/500 [02:34<02:25, 1.66it/s]
52%|#####1 | 259/500 [02:35<02:25, 1.66it/s]
52%|#####2 | 260/500 [02:35<02:24, 1.66it/s]
52%|#####2 | 261/500 [02:36<02:23, 1.66it/s]
52%|#####2 | 262/500 [02:37<02:23, 1.66it/s]
53%|#####2 | 263/500 [02:37<02:22, 1.66it/s]
53%|#####2 | 264/500 [02:38<02:22, 1.66it/s]
53%|#####3 | 265/500 [02:39<02:21, 1.66it/s]
53%|#####3 | 266/500 [02:39<02:20, 1.66it/s]
53%|#####3 | 267/500 [02:40<02:20, 1.66it/s]
54%|#####3 | 268/500 [02:40<02:19, 1.66it/s]
54%|#####3 | 269/500 [02:41<02:19, 1.66it/s]
54%|#####4 | 270/500 [02:42<02:18, 1.66it/s]
54%|#####4 | 271/500 [02:42<02:17, 1.66it/s]
54%|#####4 | 272/500 [02:43<02:17, 1.66it/s]
55%|#####4 | 273/500 [02:43<02:16, 1.66it/s]
55%|#####4 | 274/500 [02:44<02:16, 1.66it/s]
55%|#####5 | 275/500 [02:45<02:15, 1.66it/s]
55%|#####5 | 276/500 [02:45<02:14, 1.66it/s]
55%|#####5 | 277/500 [02:46<02:14, 1.66it/s]
56%|#####5 | 278/500 [02:46<02:13, 1.66it/s]
56%|#####5 | 279/500 [02:47<02:13, 1.66it/s]
56%|#####6 | 280/500 [02:48<02:12, 1.66it/s]
56%|#####6 | 281/500 [02:48<02:11, 1.66it/s]
56%|#####6 | 282/500 [02:49<02:11, 1.66it/s]
57%|#####6 | 283/500 [02:49<02:10, 1.66it/s]
57%|#####6 | 284/500 [02:50<02:10, 1.66it/s]
57%|#####6 | 285/500 [02:51<02:09, 1.66it/s]
57%|#####7 | 286/500 [02:51<02:08, 1.66it/s]
57%|#####7 | 287/500 [02:52<02:08, 1.66it/s]
58%|#####7 | 288/500 [02:52<02:07, 1.66it/s]
58%|#####7 | 289/500 [02:53<02:07, 1.66it/s]
58%|#####8 | 290/500 [02:54<02:06, 1.66it/s]
58%|#####8 | 291/500 [02:54<02:05, 1.66it/s]
58%|#####8 | 292/500 [02:55<02:05, 1.66it/s]
59%|#####8 | 293/500 [02:55<02:04, 1.66it/s]
59%|#####8 | 294/500 [02:56<02:04, 1.66it/s]
59%|#####8 | 295/500 [02:57<02:03, 1.66it/s]
59%|#####9 | 296/500 [02:57<02:02, 1.66it/s]
59%|#####9 | 297/500 [02:58<02:02, 1.66it/s]
60%|#####9 | 298/500 [02:58<02:01, 1.66it/s]
60%|#####9 | 299/500 [02:59<02:01, 1.66it/s]
60%|###### | 300/500 [03:00<02:00, 1.66it/s]
{'loss': 1.3136, 'learning_rate': 5e-05, 'epoch': 0.11}
60%|###### | 300/500 [03:00<02:00, 1.66it/s]
60%|###### | 301/500 [03:00<01:59, 1.66it/s]
60%|###### | 302/500 [03:01<01:59, 1.66it/s]
61%|###### | 303/500 [03:01<01:58, 1.66it/s]
61%|###### | 304/500 [03:02<01:58, 1.66it/s]
61%|######1 | 305/500 [03:03<01:57, 1.66it/s]
61%|######1 | 306/500 [03:03<01:56, 1.66it/s]
61%|######1 | 307/500 [03:04<01:56, 1.66it/s]
62%|######1 | 308/500 [03:04<01:55, 1.66it/s]
62%|######1 | 309/500 [03:05<01:55, 1.66it/s]
62%|######2 | 310/500 [03:06<01:54, 1.66it/s]
62%|######2 | 311/500 [03:06<01:53, 1.66it/s]
62%|######2 | 312/500 [03:07<01:53, 1.66it/s]
63%|######2 | 313/500 [03:07<01:52, 1.66it/s]
63%|######2 | 314/500 [03:08<01:52, 1.66it/s]
63%|######3 | 315/500 [03:09<01:51, 1.66it/s]
63%|######3 | 316/500 [03:09<01:50, 1.66it/s]
63%|######3 | 317/500 [03:10<01:50, 1.66it/s]
64%|######3 | 318/500 [03:10<01:49, 1.66it/s]
64%|######3 | 319/500 [03:11<01:49, 1.66it/s]
64%|######4 | 320/500 [03:12<01:48, 1.66it/s]
64%|######4 | 321/500 [03:12<01:47, 1.66it/s]
64%|######4 | 322/500 [03:13<01:47, 1.66it/s]
65%|######4 | 323/500 [03:13<01:46, 1.66it/s]
65%|######4 | 324/500 [03:14<01:46, 1.66it/s]
65%|######5 | 325/500 [03:15<01:45, 1.66it/s]
65%|######5 | 326/500 [03:15<01:44, 1.66it/s]
65%|######5 | 327/500 [03:16<01:44, 1.66it/s]
66%|######5 | 328/500 [03:16<01:43, 1.66it/s]
66%|######5 | 329/500 [03:17<01:43, 1.66it/s]
66%|######6 | 330/500 [03:18<01:42, 1.66it/s]
66%|######6 | 331/500 [03:18<01:41, 1.66it/s]
66%|######6 | 332/500 [03:19<01:41, 1.66it/s]
67%|######6 | 333/500 [03:19<01:40, 1.66it/s]
67%|######6 | 334/500 [03:20<01:39, 1.66it/s]
67%|######7 | 335/500 [03:21<01:39, 1.66it/s]
67%|######7 | 336/500 [03:21<01:38, 1.66it/s]
67%|######7 | 337/500 [03:22<01:38, 1.66it/s]
68%|######7 | 338/500 [03:22<01:37, 1.66it/s]
68%|######7 | 339/500 [03:23<01:37, 1.66it/s]
68%|######8 | 340/500 [03:24<01:36, 1.66it/s]
68%|######8 | 341/500 [03:24<01:35, 1.66it/s]
68%|######8 | 342/500 [03:25<01:35, 1.66it/s]
69%|######8 | 343/500 [03:25<01:34, 1.66it/s]
69%|######8 | 344/500 [03:26<01:33, 1.66it/s]
69%|######9 | 345/500 [03:27<01:33, 1.66it/s]
69%|######9 | 346/500 [03:27<01:32, 1.66it/s]
69%|######9 | 347/500 [03:28<01:32, 1.66it/s]
70%|######9 | 348/500 [03:29<01:31, 1.66it/s]
70%|######9 | 349/500 [03:29<01:31, 1.66it/s]
70%|####### | 350/500 [03:30<01:30, 1.66it/s]
{'loss': 1.3674, 'learning_rate': 5e-05, 'epoch': 0.13}
70%|####### | 350/500 [03:30<01:30, 1.66it/s]
70%|####### | 351/500 [03:30<01:29, 1.66it/s]
70%|####### | 352/500 [03:31<01:29, 1.66it/s]
71%|####### | 353/500 [03:32<01:28, 1.66it/s]
71%|####### | 354/500 [03:32<01:27, 1.66it/s]
71%|#######1 | 355/500 [03:33<01:27, 1.66it/s]
71%|#######1 | 356/500 [03:33<01:26, 1.66it/s]
71%|#######1 | 357/500 [03:34<01:26, 1.66it/s]
72%|#######1 | 358/500 [03:35<01:25, 1.66it/s]
72%|#######1 | 359/500 [03:35<01:24, 1.66it/s]
72%|#######2 | 360/500 [03:36<01:24, 1.66it/s]
72%|#######2 | 361/500 [03:36<01:23, 1.66it/s]
72%|#######2 | 362/500 [03:37<01:23, 1.66it/s]
73%|#######2 | 363/500 [03:38<01:22, 1.66it/s]
73%|#######2 | 364/500 [03:38<01:21, 1.66it/s]
73%|#######3 | 365/500 [03:39<01:21, 1.66it/s]
73%|#######3 | 366/500 [03:39<01:20, 1.66it/s]
73%|#######3 | 367/500 [03:40<01:20, 1.66it/s]
74%|#######3 | 368/500 [03:41<01:19, 1.66it/s]
74%|#######3 | 369/500 [03:41<01:18, 1.66it/s]
74%|#######4 | 370/500 [03:42<01:18, 1.66it/s]
74%|#######4 | 371/500 [03:42<01:17, 1.66it/s]
74%|#######4 | 372/500 [03:43<01:17, 1.66it/s]
75%|#######4 | 373/500 [03:44<01:16, 1.66it/s]
75%|#######4 | 374/500 [03:44<01:15, 1.66it/s]
75%|#######5 | 375/500 [03:45<01:15, 1.66it/s]
75%|#######5 | 376/500 [03:45<01:14, 1.66it/s]
75%|#######5 | 377/500 [03:46<01:14, 1.66it/s]
76%|#######5 | 378/500 [03:47<01:13, 1.66it/s]
76%|#######5 | 379/500 [03:47<01:12, 1.66it/s]
76%|#######6 | 380/500 [03:48<01:12, 1.66it/s]
76%|#######6 | 381/500 [03:48<01:11, 1.66it/s]
76%|#######6 | 382/500 [03:49<01:11, 1.66it/s]
77%|#######6 | 383/500 [03:50<01:10, 1.66it/s]
77%|#######6 | 384/500 [03:50<01:09, 1.66it/s]
77%|#######7 | 385/500 [03:51<01:09, 1.66it/s]
77%|#######7 | 386/500 [03:51<01:08, 1.66it/s]
77%|#######7 | 387/500 [03:52<01:08, 1.66it/s]
78%|#######7 | 388/500 [03:53<01:07, 1.66it/s]
78%|#######7 | 389/500 [03:53<01:06, 1.66it/s]
78%|#######8 | 390/500 [03:54<01:06, 1.66it/s]
78%|#######8 | 391/500 [03:54<01:05, 1.66it/s]
78%|#######8 | 392/500 [03:55<01:05, 1.66it/s]
79%|#######8 | 393/500 [03:56<01:04, 1.66it/s]
79%|#######8 | 394/500 [03:56<01:03, 1.66it/s]
79%|#######9 | 395/500 [03:57<01:03, 1.66it/s]
79%|#######9 | 396/500 [03:57<01:02, 1.66it/s]
79%|#######9 | 397/500 [03:58<01:02, 1.66it/s]
80%|#######9 | 398/500 [03:59<01:01, 1.66it/s]
80%|#######9 | 399/500 [03:59<01:00, 1.66it/s]
80%|######## | 400/500 [04:00<01:00, 1.66it/s]
{'loss': 1.293, 'learning_rate': 5e-05, 'epoch': 0.15}
80%|######## | 400/500 [04:00<01:00, 1.66it/s]
80%|######## | 401/500 [04:00<00:59, 1.66it/s]
80%|######## | 402/500 [04:01<00:59, 1.66it/s]
81%|######## | 403/500 [04:02<00:58, 1.66it/s]
81%|######## | 404/500 [04:02<00:57, 1.66it/s]
81%|########1 | 405/500 [04:03<00:57, 1.66it/s]
81%|########1 | 406/500 [04:03<00:56, 1.66it/s]
81%|########1 | 407/500 [04:04<00:56, 1.66it/s]
82%|########1 | 408/500 [04:05<00:55, 1.66it/s]
82%|########1 | 409/500 [04:05<00:54, 1.66it/s]
82%|########2 | 410/500 [04:06<00:54, 1.66it/s]
82%|########2 | 411/500 [04:06<00:53, 1.66it/s]
82%|########2 | 412/500 [04:07<00:53, 1.66it/s]
83%|########2 | 413/500 [04:08<00:52, 1.66it/s]
83%|########2 | 414/500 [04:08<00:51, 1.66it/s]
83%|########2 | 415/500 [04:09<00:51, 1.66it/s]
83%|########3 | 416/500 [04:09<00:50, 1.66it/s]
83%|########3 | 417/500 [04:10<00:50, 1.66it/s]
84%|########3 | 418/500 [04:11<00:49, 1.66it/s]
84%|########3 | 419/500 [04:11<00:48, 1.66it/s]
84%|########4 | 420/500 [04:12<00:48, 1.66it/s]
84%|########4 | 421/500 [04:12<00:47, 1.66it/s]
84%|########4 | 422/500 [04:13<00:46, 1.66it/s]
85%|########4 | 423/500 [04:14<00:46, 1.66it/s]
85%|########4 | 424/500 [04:14<00:45, 1.66it/s]
85%|########5 | 425/500 [04:15<00:45, 1.66it/s]
85%|########5 | 426/500 [04:16<00:44, 1.66it/s]
85%|########5 | 427/500 [04:16<00:43, 1.66it/s]
86%|########5 | 428/500 [04:17<00:43, 1.66it/s]
86%|########5 | 429/500 [04:17<00:42, 1.66it/s]
86%|########6 | 430/500 [04:18<00:42, 1.66it/s]
86%|########6 | 431/500 [04:19<00:41, 1.66it/s]
86%|########6 | 432/500 [04:19<00:40, 1.66it/s]
87%|########6 | 433/500 [04:20<00:40, 1.66it/s]
87%|########6 | 434/500 [04:20<00:39, 1.66it/s]
87%|########7 | 435/500 [04:21<00:39, 1.66it/s]
87%|########7 | 436/500 [04:22<00:38, 1.66it/s]
87%|########7 | 437/500 [04:22<00:37, 1.66it/s]
88%|########7 | 438/500 [04:23<00:37, 1.66it/s]
88%|########7 | 439/500 [04:23<00:36, 1.66it/s]
88%|########8 | 440/500 [04:24<00:36, 1.66it/s]
88%|########8 | 441/500 [04:25<00:35, 1.66it/s]
88%|########8 | 442/500 [04:25<00:34, 1.66it/s]
89%|########8 | 443/500 [04:26<00:34, 1.66it/s]
89%|########8 | 444/500 [04:26<00:33, 1.66it/s]
89%|########9 | 445/500 [04:27<00:33, 1.66it/s]
89%|########9 | 446/500 [04:28<00:32, 1.66it/s]
89%|########9 | 447/500 [04:28<00:31, 1.66it/s]
90%|########9 | 448/500 [04:29<00:31, 1.66it/s]
90%|########9 | 449/500 [04:29<00:30, 1.66it/s]
90%|######### | 450/500 [04:30<00:30, 1.66it/s]
{'loss': 1.3336, 'learning_rate': 5e-05, 'epoch': 0.16}
90%|######### | 450/500 [04:30<00:30, 1.66it/s]
90%|######### | 451/500 [04:31<00:29, 1.66it/s]
90%|######### | 452/500 [04:31<00:28, 1.66it/s]
91%|######### | 453/500 [04:32<00:28, 1.66it/s]
91%|######### | 454/500 [04:32<00:27, 1.66it/s]
91%|#########1| 455/500 [04:33<00:27, 1.66it/s]
91%|#########1| 456/500 [04:34<00:26, 1.66it/s]
91%|#########1| 457/500 [04:34<00:25, 1.66it/s]
92%|#########1| 458/500 [04:35<00:25, 1.66it/s]
92%|#########1| 459/500 [04:35<00:24, 1.66it/s]
92%|#########2| 460/500 [04:36<00:24, 1.66it/s]
92%|#########2| 461/500 [04:37<00:23, 1.66it/s]
92%|#########2| 462/500 [04:37<00:22, 1.66it/s]
93%|#########2| 463/500 [04:38<00:22, 1.66it/s]
93%|#########2| 464/500 [04:38<00:21, 1.66it/s]
93%|#########3| 465/500 [04:39<00:21, 1.66it/s]
93%|#########3| 466/500 [04:40<00:20, 1.66it/s]
93%|#########3| 467/500 [04:40<00:19, 1.66it/s]
94%|#########3| 468/500 [04:41<00:19, 1.66it/s]
94%|#########3| 469/500 [04:41<00:18, 1.66it/s]
94%|#########3| 470/500 [04:42<00:18, 1.66it/s]
94%|#########4| 471/500 [04:43<00:17, 1.66it/s]
94%|#########4| 472/500 [04:43<00:16, 1.66it/s]
95%|#########4| 473/500 [04:44<00:16, 1.66it/s]
95%|#########4| 474/500 [04:44<00:15, 1.66it/s]
95%|#########5| 475/500 [04:45<00:15, 1.66it/s]
95%|#########5| 476/500 [04:46<00:14, 1.66it/s]
95%|#########5| 477/500 [04:46<00:13, 1.66it/s]
96%|#########5| 478/500 [04:47<00:13, 1.66it/s]
96%|#########5| 479/500 [04:47<00:12, 1.66it/s]
96%|#########6| 480/500 [04:48<00:12, 1.66it/s]
96%|#########6| 481/500 [04:49<00:11, 1.66it/s]
96%|#########6| 482/500 [04:49<00:10, 1.66it/s]
97%|#########6| 483/500 [04:50<00:10, 1.66it/s]
97%|#########6| 484/500 [04:50<00:09, 1.66it/s]
97%|#########7| 485/500 [04:51<00:09, 1.66it/s]
97%|#########7| 486/500 [04:52<00:08, 1.66it/s]
97%|#########7| 487/500 [04:52<00:07, 1.66it/s]
98%|#########7| 488/500 [04:53<00:07, 1.66it/s]
98%|#########7| 489/500 [04:53<00:06, 1.66it/s]
98%|#########8| 490/500 [04:54<00:06, 1.66it/s]
98%|#########8| 491/500 [04:55<00:05, 1.66it/s]
98%|#########8| 492/500 [04:55<00:04, 1.66it/s]
99%|#########8| 493/500 [04:56<00:04, 1.66it/s]
99%|#########8| 494/500 [04:56<00:03, 1.66it/s]
99%|#########9| 495/500 [04:57<00:03, 1.66it/s]
99%|#########9| 496/500 [04:58<00:02, 1.66it/s]
99%|#########9| 497/500 [04:58<00:01, 1.66it/s]
100%|#########9| 498/500 [04:59<00:01, 1.66it/s]
100%|#########9| 499/500 [04:59<00:00, 1.66it/s]
100%|##########| 500/500 [05:00<00:00, 1.66it/s]
{'loss': 1.2431, 'learning_rate': 5e-05, 'epoch': 0.18}
100%|##########| 500/500 [05:00<00:00, 1.66it/s]
{'train_runtime': 309.7471, 'train_samples_per_second': 51.655, 'train_steps_per_second': 1.614, 'train_loss': 1.402785385131836, 'epoch': 0.18}
100%|##########| 500/500 [05:09<00:00, 1.66it/s]
100%|##########| 500/500 [05:09<00:00, 1.61it/s]
tensor([[ 0.0000, -0.0165, 0.0000, 0.0133, 0.0000, 0.0286, 0.0000, -0.0772],
[ 0.0416, -0.0000, -0.0000, 0.0488, 0.0329, -0.0000, -0.0000, -0.0172],
[-0.0343, -0.0315, 0.0000, 0.0000, -0.0000, -0.0000, 0.0568, -0.0191],
[ 0.0000, -0.0000, -0.0486, 0.0298, 0.0957, -0.0000, -0.0000, -0.0777],
[ 0.0112, -0.0000, 0.0000, -0.0362, -0.0000, 0.0193, 0.0704, -0.0000],
[-0.0000, -0.0338, 0.0000, 0.0295, 0.0347, -0.0180, 0.0000, -0.0000],
[-0.1068, -0.0280, 0.0000, -0.0000, 0.0441, 0.0671, -0.0000, -0.0000],
[-0.0000, 0.0313, -0.0630, -0.0000, 0.0280, -0.0000, 0.0000, -0.0577]],
device='cuda:0', grad_fn=<SliceBackward0>)
<Axes: title={'center': 'Loss vs. # steps'}, xlabel='step', ylabel='loss'>
Accelerating 2:4 sparse models for inference¶
Now that we have a model in this format, we can accelerate it for inference just like in the QuickStart Guide.
model = model.cuda().half()
# accelerate for sparsity
for fqn, module in model.named_modules():
if isinstance(module, nn.Linear) and "layer" in fqn:
module.weight = nn.Parameter(to_sparse_semi_structured(module.weight))
with torch.no_grad():
predictions = trainer.predict(tokenized_squad_dataset["validation"])
start_logits, end_logits = predictions.predictions
metrics_sparse = compute_metrics(
start_logits,
end_logits,
tokenized_squad_dataset["validation"],
squad_dataset["validation"],
)
print("sparse eval metrics: ", metrics_sparse)
sparse_perf = measure_execution_time(
model,
batch_sizes,
tokenized_squad_dataset["validation"],
)
print("sparse perf metrics: ", sparse_perf)
0%| | 0/43 [00:00<?, ?it/s]
5%|4 | 2/43 [00:00<00:18, 2.22it/s]
7%|6 | 3/43 [00:01<00:25, 1.57it/s]
9%|9 | 4/43 [00:02<00:28, 1.36it/s]
12%|#1 | 5/43 [00:03<00:30, 1.26it/s]
14%|#3 | 6/43 [00:04<00:30, 1.21it/s]
16%|#6 | 7/43 [00:05<00:30, 1.18it/s]
19%|#8 | 8/43 [00:06<00:30, 1.16it/s]
21%|## | 9/43 [00:07<00:29, 1.14it/s]
23%|##3 | 10/43 [00:08<00:29, 1.13it/s]
26%|##5 | 11/43 [00:08<00:28, 1.12it/s]
28%|##7 | 12/43 [00:09<00:27, 1.12it/s]
30%|### | 13/43 [00:10<00:26, 1.12it/s]
33%|###2 | 14/43 [00:11<00:26, 1.12it/s]
35%|###4 | 15/43 [00:12<00:25, 1.12it/s]
37%|###7 | 16/43 [00:13<00:24, 1.11it/s]
40%|###9 | 17/43 [00:14<00:23, 1.11it/s]
42%|####1 | 18/43 [00:15<00:22, 1.11it/s]
44%|####4 | 19/43 [00:16<00:21, 1.11it/s]
47%|####6 | 20/43 [00:17<00:20, 1.11it/s]
49%|####8 | 21/43 [00:17<00:19, 1.11it/s]
51%|#####1 | 22/43 [00:18<00:18, 1.11it/s]
53%|#####3 | 23/43 [00:19<00:17, 1.11it/s]
56%|#####5 | 24/43 [00:20<00:17, 1.11it/s]
58%|#####8 | 25/43 [00:21<00:16, 1.11it/s]
60%|###### | 26/43 [00:22<00:15, 1.11it/s]
63%|######2 | 27/43 [00:23<00:14, 1.11it/s]
65%|######5 | 28/43 [00:24<00:13, 1.11it/s]
67%|######7 | 29/43 [00:25<00:12, 1.11it/s]
70%|######9 | 30/43 [00:26<00:11, 1.11it/s]
72%|#######2 | 31/43 [00:26<00:10, 1.11it/s]
74%|#######4 | 32/43 [00:27<00:09, 1.11it/s]
77%|#######6 | 33/43 [00:28<00:08, 1.11it/s]
79%|#######9 | 34/43 [00:29<00:08, 1.11it/s]
81%|########1 | 35/43 [00:30<00:07, 1.11it/s]
84%|########3 | 36/43 [00:31<00:06, 1.11it/s]
86%|########6 | 37/43 [00:32<00:05, 1.11it/s]
88%|########8 | 38/43 [00:33<00:04, 1.11it/s]
91%|######### | 39/43 [00:34<00:03, 1.11it/s]
93%|#########3| 40/43 [00:35<00:02, 1.11it/s]
95%|#########5| 41/43 [00:35<00:01, 1.11it/s]
98%|#########7| 42/43 [00:36<00:00, 1.16it/s]
100%|##########| 43/43 [00:36<00:00, 1.17it/s]
sparse eval metrics: {'exact_match': 71.69347209082308, 'f1': 81.04133166079365}
sparse perf metrics: {4: 14.968345499983116, '4_compile': 7.713922500215631, 16: 33.25561479996395, '16_compile': 25.13990290003676, 64: 129.00221699987924, '64_compile': 98.65719680001348, 256: 749.1008829997554, '256_compile': 410.52460299988525}
Retraining our model after magnitude pruning has recovered nearly all of
the F1 that has been lost when the model was pruned. At the same time we
have achieved a 1.28x speedup for bs=16
. Note that not all shapes are
amenable to performance improvements. When batch sizes are small and
limited time is spent in compute sparse kernels may be slower than their
dense counterparts.
Because semi-structured sparsity is implemented as a tensor subclass, it
is compatible with torch.compile
. When composed with
to_sparse_semi_structured
, we are able to achieve a total 2x speedup
on BERT.
Metrics |
fp16 |
2:4 sparse |
delta / speedup |
compiled |
---|---|---|---|---|
Exact Match (%) |
78.53 |
78.44 |
-0.09 |
|
F1 (%) |
86.93 |
86.49 |
-0.44 |
|
Time (bs=4) |
11.10 |
15.54 |
0.71x |
no |
Time (bs=16) |
19.35 |
15.74 |
1.23x |
no |
Time (bs=64) |
72.71 |
59.41 |
1.22x |
no |
Time (bs=256) |
286.65 |
247.63 |
1.14x |
no |
Time (bs=4) |
7.59 |
7.46 |
1.02x |
yes |
Time (bs=16) |
11.47 |
9.68 |
1.18x |
yes |
Time (bs=64) |
41.57 |
36.92 |
1.13x |
yes |
Time (bs=256) |
159.22 |
142.23 |
1.12x |
yes |
Conclusion¶
In this tutorial, we have shown how to prune BERT to be 2:4 sparse and
how to accelerate a 2:4 sparse model for inference. By taking advantage
of our SparseSemiStructuredTensor
subclass, we were able to achieve a
1.3x speedup over the fp16 baseline, and up to 2x with
torch.compile
. We also demonstrated the benefits of 2:4 sparsity by
fine-tuning BERT to recover any lost F1 (86.92 dense vs 86.48 sparse).
Total running time of the script: ( 17 minutes 24.839 seconds)