{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# For tips on running notebooks in Google Colab, see\n",
    "# https://docs.pytorch.org/tutorials/beginner/colab\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(beta) Accelerating BERT with semi-structured (2:4) sparsity\n",
    "===================================================== **Author**: [Jesse\n",
    "Cai](https://github.com/jcaip)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Overview\n",
    "\n",
    "Like other forms of sparsity, **semi-structured sparsity** is a model\n",
    "optimization technique that seeks to reduce the memory overhead and\n",
    "latency of a neural network at the expense of some model accuracy. It is\n",
    "also known as **fine-grained structured sparsity** or **2:4 structured\n",
    "sparsity**.\n",
    "\n",
    "Semi-structured sparsity derives its name from its unique sparsity\n",
    "pattern, where n out of every 2n elements are pruned. We most often see\n",
    "n=2, hence 2:4 sparsity Semi-structured sparsity is particularly\n",
    "interesting because it can be efficiently accelerated on GPUs and\n",
    "doesn't degrade model accuracy as much as other sparsity patterns.\n",
    "\n",
    "With the introduction of [semi-structured sparsity\n",
    "support](https://pytorch.org/docs/2.1/sparse.html#sparse-semi-structured-tensors),\n",
    "it is possible to prune and accelerate a semi-structured sparse model\n",
    "without leaving PyTorch. We will explain this process in this tutorial.\n",
    "\n",
    "![image](https://pytorch.org/tutorials/_static/img/pruning_flow.jpg)\n",
    "\n",
    "By the end of this tutorial, we will have sparsified a BERT\n",
    "question-answering model to be 2:4 sparse, fine-tuning it to recover\n",
    "nearly all F1 loss (86.92 dense vs 86.48 sparse). Finally, we will\n",
    "accelerate this 2:4 sparse model for inference, yielding a 1.3x speedup.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Requirements\n",
    "\n",
    "-   PyTorch \\>= 2.1.\n",
    "-   A NVIDIA GPU with semi-structured sparsity support (Compute\n",
    "    Capability 8.0+).\n",
    "\n",
    "> \n",
    "> <div style=\"background-color: #54c7ec; color: #fff; font-weight: 700; padding-left: 10px; padding-top: 5px; padding-bottom: 5px\"><strong>NOTE:</strong></div>\n",
    "> ```\n",
    "> \n",
    "> <div style=\"background-color: #f3f4f7; padding-left: 10px; padding-top: 10px; padding-bottom: 10px; padding-right: 10px\">\n",
    "> ```\n",
    "> \n",
    "> <p>This tutorial is tested on an NVIDIA A100 80GB GPU. You may not see similar speedups on newer GPU architectures, For the latest information on semi-structured sparsity support, please refer to the README `here &lt;<a href=\"https://github.com/pytorch/ao/tree/main/torchao/sparsity#torchao-sparsity\">https://github.com/pytorch/ao/tree/main/torchao/sparsity#torchao-sparsity</a>&gt;</p>\n",
    "> ```\n",
    "> \n",
    "> </div>\n",
    "> ```\n",
    "\n",
    "This tutorial is designed for beginners to semi-structured sparsity and\n",
    "sparsity in general. For users with existing 2:4 sparse models,\n",
    "accelerating `nn.Linear` layers for inference with\n",
    "`to_sparse_semi_structured` is quite straightforward. Here is an\n",
    "example:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch.sparse import to_sparse_semi_structured, SparseSemiStructuredTensor\n",
    "from torch.utils.benchmark import Timer\n",
    "\n",
    "# mask Linear weight to be 2:4 sparse\n",
    "mask = torch.Tensor([0, 0, 1, 1]).tile((3072, 2560)).cuda().bool()\n",
    "linear = torch.nn.Linear(10240, 3072).half().cuda().eval()\n",
    "linear.weight = torch.nn.Parameter(mask * linear.weight)\n",
    "\n",
    "x = torch.rand(3072, 10240).half().cuda()\n",
    "\n",
    "with torch.inference_mode():\n",
    "    dense_output = linear(x)\n",
    "    dense_t = Timer(stmt=\"linear(x)\",\n",
    "                    globals={\"linear\": linear,\n",
    "                             \"x\": x}).blocked_autorange().median * 1e3\n",
    "\n",
    "    # accelerate via SparseSemiStructuredTensor\n",
    "    linear.weight = torch.nn.Parameter(to_sparse_semi_structured(linear.weight))\n",
    "\n",
    "    sparse_output = linear(x)\n",
    "    sparse_t = Timer(stmt=\"linear(x)\",\n",
    "                    globals={\"linear\": linear,\n",
    "                             \"x\": x}).blocked_autorange().median * 1e3\n",
    "\n",
    "    # sparse and dense matmul are numerically equivalent\n",
    "    # On an A100 80GB, we see: `Dense: 0.870ms Sparse: 0.630ms | Speedup: 1.382x`\n",
    "    assert torch.allclose(sparse_output, dense_output, atol=1e-3)\n",
    "    print(f\"Dense: {dense_t:.3f}ms Sparse: {sparse_t:.3f}ms | Speedup: {(dense_t / sparse_t):.3f}x\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# What problem does semi-structured sparsity solve?\n",
    "\n",
    "The general motivation behind sparsity is simple: if there are zeros in\n",
    "your network, you can optimize efficiency by not storing or computing\n",
    "those parameters. However, the specifics of sparsity are tricky. Zeroing\n",
    "out parameters doesn't affect the latency / memory overhead of our model\n",
    "out of the box.\n",
    "\n",
    "This is because the dense tensor still contains the pruned (zero)\n",
    "elements, which the dense matrix multiplication kernel will still\n",
    "operate on this elements. In order to realize performance gains, we need\n",
    "to swap out dense kernels for sparse kernels, which skip calculation\n",
    "involving pruned elements.\n",
    "\n",
    "To do this, these kernels work on sparse matrices, which do not store\n",
    "the pruned elements and store the specified elements in a compressed\n",
    "format.\n",
    "\n",
    "For semi-structured sparsity, we store exactly half of the original\n",
    "parameters along with some compressed metadata about how the elements\n",
    "were arranged.\n",
    "\n",
    "![image](https://developer-blogs.nvidia.com/wp-content/uploads/2023/06/2-4-structured-sparsity-pattern.png){.align-center\n",
    ".:width: .80%}\n",
    "\n",
    "There are many different sparse layouts, each with their own benefits\n",
    "and drawbacks. The 2:4 semi-structured sparse layout is particularly\n",
    "interesting for two reasons:\n",
    "\n",
    "-   Unlike previous sparse formats, semi-structured sparsity was\n",
    "    designed to be efficiently accelerated on GPUs. In 2020, NVIDIA\n",
    "    introduced hardware support for semi-structured sparsity with their\n",
    "    Ampere architecture, and have also released fast sparse kernels via\n",
    "    CUTLASS\n",
    "    [cuSPARSELt](https://docs.nvidia.com/cuda/cusparselt/index.html).\n",
    "-   At the same time, semi-structured sparsity tends to have a milder\n",
    "    impact on model accuracy compared to other sparse formats,\n",
    "    especially when accounting for more advanced pruning / fine-tuning\n",
    "    methods. NVIDIA has shown in their [white\n",
    "    paper](https://arxiv.org/abs/2104.08378) that a simple paradigm of\n",
    "    magnitude pruning once to be 2:4 sparse and then retraining the\n",
    "    model yields nearly identical model accuracies.\n",
    "\n",
    "Semi-structured exists in a sweet spot, providing a 2x (theoretical)\n",
    "speedup at a much lower sparsity level (50%), while still being granular\n",
    "enough to preserve model accuracy.\n",
    "\n",
    "  ----------------------------------------------------------------------\n",
    "  Network              Data Set      Metric   Dense FP16   Sparse FP16\n",
    "  -------------------- ------------- -------- ------------ -------------\n",
    "  ResNet-50            ImageNet      Top-1    76.1         76.2\n",
    "\n",
    "  ResNeXt-101_32x8d    ImageNet      Top-1    79.3         79.3\n",
    "\n",
    "  Xception             ImageNet      Top-1    79.2         79.2\n",
    "\n",
    "  SSD-RN50             COCO2017      bbAP     24.8         24.8\n",
    "\n",
    "  MaskRCNN-RN50        COCO2017      bbAP     37.9         37.9\n",
    "\n",
    "  FairSeq Transformer  EN-DE WMT14   BLEU     28.2         28.5\n",
    "\n",
    "  BERT-Large           SQuAD v1.1    F1       91.9         91.9\n",
    "  ----------------------------------------------------------------------\n",
    "\n",
    "Semi-structured sparsity has an additional advantage from a workflow\n",
    "perspective. Because the sparsity level is fixed at 50%, it is easier to\n",
    "decompose the problem of sparsifying a model into two distinct\n",
    "subproblems:\n",
    "\n",
    "-   Accuracy - How can we find a set of 2:4 sparse weights that minimize\n",
    "    the accuracy degradation of our model?\n",
    "-   Performance - How can we accelerate our 2:4 sparse weights for\n",
    "    inference and reduced memory overhead?\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\\begin{aligned}\n",
    "\\begin{bmatrix}\n",
    "   1 & 1 & 0 & 0 \\\\\n",
    "   0 & 0 & 1 & 1 \\\\\n",
    "   1 & 0 & 0 & 0 \\\\\n",
    "   0 & 0 & 1 & 1 \\\\\n",
    "   \\end{bmatrix}\n",
    "\\end{aligned}$$\n",
    "\n",
    "The natural handoff point between these two problems are zeroed-out\n",
    "dense tensors. Our inference solution is designed to compress and\n",
    "accelerate tensors in this format. We anticipate many users coming up\n",
    "with custom masking solution, as this is an active area of research.\n",
    "\n",
    "Now that we've learned a little more about semi-structured sparsity,\n",
    "let's apply it to a BERT model trained on a question answering task,\n",
    "SQuAD.\n",
    "\n",
    "# Intro & Setup\n",
    "\n",
    "Let's start by importing all the packages we need.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# If you are running this in Google Colab, run:\n",
    "# .. code-block: python\n",
    "# \n",
    "#    !pip install datasets transformers evaluate accelerate pandas\n",
    "#\n",
    "import os\n",
    "os.environ[\"WANDB_DISABLED\"] = \"true\"\n",
    "\n",
    "import collections\n",
    "import datasets\n",
    "import evaluate\n",
    "import numpy as np\n",
    "import torch\n",
    "import torch.utils.benchmark as benchmark\n",
    "from torch import nn\n",
    "from torch.sparse import to_sparse_semi_structured, SparseSemiStructuredTensor\n",
    "from torch.ao.pruning import WeightNormSparsifier\n",
    "import transformers\n",
    "\n",
    "# force CUTLASS use if ``cuSPARSELt`` is not available\n",
    "torch.manual_seed(100)\n",
    "\n",
    "# Set default device to \"cuda:0\"\n",
    "torch.set_default_device(torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We'll also need to define some helper functions that are specific to the\n",
    "dataset / task at hand. These were adapted from\n",
    "[this](https://huggingface.co/learn/nlp-course/chapter7/7?fw=pt) Hugging\n",
    "Face course as a reference.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def preprocess_validation_function(examples, tokenizer):\n",
    "    inputs = tokenizer(\n",
    "        [q.strip() for q in examples[\"question\"]],\n",
    "        examples[\"context\"],\n",
    "        max_length=384,\n",
    "        truncation=\"only_second\",\n",
    "        return_overflowing_tokens=True,\n",
    "        return_offsets_mapping=True,\n",
    "        padding=\"max_length\",\n",
    "    )\n",
    "    sample_map = inputs.pop(\"overflow_to_sample_mapping\")\n",
    "    example_ids = []\n",
    "\n",
    "    for i in range(len(inputs[\"input_ids\"])):\n",
    "        sample_idx = sample_map[i]\n",
    "        example_ids.append(examples[\"id\"][sample_idx])\n",
    "        sequence_ids = inputs.sequence_ids(i)\n",
    "        offset = inputs[\"offset_mapping\"][i]\n",
    "        inputs[\"offset_mapping\"][i] = [\n",
    "            o if sequence_ids[k] == 1 else None for k, o in enumerate(offset)\n",
    "        ]\n",
    "\n",
    "    inputs[\"example_id\"] = example_ids\n",
    "    return inputs\n",
    "\n",
    "\n",
    "def preprocess_train_function(examples, tokenizer):\n",
    "    inputs = tokenizer(\n",
    "        [q.strip() for q in examples[\"question\"]],\n",
    "        examples[\"context\"],\n",
    "        max_length=384,\n",
    "        truncation=\"only_second\",\n",
    "        return_offsets_mapping=True,\n",
    "        padding=\"max_length\",\n",
    "    )\n",
    "\n",
    "    offset_mapping = inputs[\"offset_mapping\"]\n",
    "    answers = examples[\"answers\"]\n",
    "    start_positions = []\n",
    "    end_positions = []\n",
    "\n",
    "    for i, (offset, answer) in enumerate(zip(offset_mapping, answers)):\n",
    "        start_char = answer[\"answer_start\"][0]\n",
    "        end_char = start_char + len(answer[\"text\"][0])\n",
    "        sequence_ids = inputs.sequence_ids(i)\n",
    "\n",
    "        # Find the start and end of the context\n",
    "        idx = 0\n",
    "        while sequence_ids[idx] != 1:\n",
    "            idx += 1\n",
    "        context_start = idx\n",
    "        while sequence_ids[idx] == 1:\n",
    "            idx += 1\n",
    "        context_end = idx - 1\n",
    "\n",
    "        # If the answer is not fully inside the context, label it (0, 0)\n",
    "        if offset[context_start][0] > end_char or offset[context_end][1] < start_char:\n",
    "            start_positions.append(0)\n",
    "            end_positions.append(0)\n",
    "        else:\n",
    "            # Otherwise it's the start and end token positions\n",
    "            idx = context_start\n",
    "            while idx <= context_end and offset[idx][0] <= start_char:\n",
    "                idx += 1\n",
    "            start_positions.append(idx - 1)\n",
    "\n",
    "            idx = context_end\n",
    "            while idx >= context_start and offset[idx][1] >= end_char:\n",
    "                idx -= 1\n",
    "            end_positions.append(idx + 1)\n",
    "\n",
    "    inputs[\"start_positions\"] = start_positions\n",
    "    inputs[\"end_positions\"] = end_positions\n",
    "    return inputs\n",
    "\n",
    "\n",
    "def compute_metrics(start_logits, end_logits, features, examples):\n",
    "    n_best = 20\n",
    "    max_answer_length = 30\n",
    "    metric = evaluate.load(\"squad\")\n",
    "\n",
    "    example_to_features = collections.defaultdict(list)\n",
    "    for idx, feature in enumerate(features):\n",
    "        example_to_features[feature[\"example_id\"]].append(idx)\n",
    "\n",
    "    predicted_answers = []\n",
    "    # for example in ``tqdm`` (examples):\n",
    "    for example in examples:\n",
    "        example_id = example[\"id\"]\n",
    "        context = example[\"context\"]\n",
    "        answers = []\n",
    "\n",
    "        # Loop through all features associated with that example\n",
    "        for feature_index in example_to_features[example_id]:\n",
    "            start_logit = start_logits[feature_index]\n",
    "            end_logit = end_logits[feature_index]\n",
    "            offsets = features[feature_index][\"offset_mapping\"]\n",
    "\n",
    "            start_indexes = np.argsort(start_logit)[-1 : -n_best - 1 : -1].tolist()\n",
    "            end_indexes = np.argsort(end_logit)[-1 : -n_best - 1 : -1].tolist()\n",
    "            for start_index in start_indexes:\n",
    "                for end_index in end_indexes:\n",
    "                    # Skip answers that are not fully in the context\n",
    "                    if offsets[start_index] is None or offsets[end_index] is None:\n",
    "                        continue\n",
    "                    # Skip answers with a length that is either < 0\n",
    "                    # or > max_answer_length\n",
    "                    if (\n",
    "                        end_index < start_index\n",
    "                        or end_index - start_index + 1 > max_answer_length\n",
    "                    ):\n",
    "                        continue\n",
    "\n",
    "                    answer = {\n",
    "                        \"text\": context[\n",
    "                            offsets[start_index][0] : offsets[end_index][1]\n",
    "                        ],\n",
    "                        \"logit_score\": start_logit[start_index] + end_logit[end_index],\n",
    "                    }\n",
    "                    answers.append(answer)\n",
    "\n",
    "        # Select the answer with the best score\n",
    "        if len(answers) > 0:\n",
    "            best_answer = max(answers, key=lambda x: x[\"logit_score\"])\n",
    "            predicted_answers.append(\n",
    "                {\"id\": example_id, \"prediction_text\": best_answer[\"text\"]}\n",
    "            )\n",
    "        else:\n",
    "            predicted_answers.append({\"id\": example_id, \"prediction_text\": \"\"})\n",
    "\n",
    "    theoretical_answers = [\n",
    "        {\"id\": ex[\"id\"], \"answers\": ex[\"answers\"]} for ex in examples\n",
    "    ]\n",
    "    return metric.compute(predictions=predicted_answers, references=theoretical_answers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that those are defined, we just need one additional helper function,\n",
    "which will help us benchmark our model.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def measure_execution_time(model, batch_sizes, dataset):\n",
    "    dataset_for_model = dataset.remove_columns([\"example_id\", \"offset_mapping\"])\n",
    "    dataset_for_model.set_format(\"torch\")\n",
    "    batch_size_to_time_sec = {}\n",
    "    for batch_size in batch_sizes:\n",
    "        batch = {\n",
    "            k: dataset_for_model[k][:batch_size].cuda()\n",
    "            for k in dataset_for_model.column_names\n",
    "        }\n",
    "\n",
    "        with torch.no_grad():\n",
    "            baseline_predictions = model(**batch)\n",
    "            timer = benchmark.Timer(\n",
    "                stmt=\"model(**batch)\", globals={\"model\": model, \"batch\": batch}\n",
    "            )\n",
    "            p50 = timer.blocked_autorange().median * 1000\n",
    "            batch_size_to_time_sec[batch_size] = p50\n",
    "\n",
    "            model_c = torch.compile(model, fullgraph=True)\n",
    "            timer = benchmark.Timer(\n",
    "                stmt=\"model(**batch)\", globals={\"model\": model_c, \"batch\": batch}\n",
    "            )\n",
    "            p50 = timer.blocked_autorange().median * 1000\n",
    "            batch_size_to_time_sec[f\"{batch_size}_compile\"] = p50\n",
    "            new_predictions = model_c(**batch)\n",
    "\n",
    "    return batch_size_to_time_sec"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will get started by loading our model and tokenizer, and then setting\n",
    "up our dataset.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# load model\n",
    "model_name = \"bert-base-cased\"\n",
    "tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)\n",
    "model = transformers.AutoModelForQuestionAnswering.from_pretrained(model_name)\n",
    "print(f\"Loading tokenizer: {model_name}\")\n",
    "print(f\"Loading model: {model_name}\")\n",
    "\n",
    "# set up train and val dataset\n",
    "squad_dataset = datasets.load_dataset(\"squad\")\n",
    "tokenized_squad_dataset = {}\n",
    "tokenized_squad_dataset[\"train\"] = squad_dataset[\"train\"].map(\n",
    "    lambda x: preprocess_train_function(x, tokenizer), batched=True\n",
    ")\n",
    "tokenized_squad_dataset[\"validation\"] = squad_dataset[\"validation\"].map(\n",
    "    lambda x: preprocess_validation_function(x, tokenizer),\n",
    "    batched=True,\n",
    "    remove_columns=squad_dataset[\"train\"].column_names,\n",
    ")\n",
    "data_collator = transformers.DataCollatorWithPadding(tokenizer=tokenizer)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Establishing a baseline\n",
    "\n",
    "Next, we'll train a quick baseline of our model on SQuAD. This task asks\n",
    "our model to identify spans, or segments of text, in a given context\n",
    "(Wikipedia articles) that answer a given question. Running the following\n",
    "code gives me an F1 score of 86.9. This is quite close to the reported\n",
    "NVIDIA score and the difference is likely due to BERT-base\n",
    "vs. BERT-large or fine-tuning hyperparameters.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "training_args = transformers.TrainingArguments(\n",
    "    \"trainer\",\n",
    "    num_train_epochs=1,\n",
    "    lr_scheduler_type=\"constant\",\n",
    "    per_device_train_batch_size=32,\n",
    "    per_device_eval_batch_size=256,\n",
    "    logging_steps=50, \n",
    "    # Limit max steps for tutorial runners. Delete the below line to see the reported accuracy numbers. \n",
    "    max_steps=500,\n",
    "    report_to=None,\n",
    ")\n",
    "\n",
    "trainer = transformers.Trainer(\n",
    "    model,\n",
    "    training_args,\n",
    "    train_dataset=tokenized_squad_dataset[\"train\"],\n",
    "    eval_dataset=tokenized_squad_dataset[\"validation\"],\n",
    "    data_collator=data_collator,\n",
    "    tokenizer=tokenizer,\n",
    ")\n",
    "\n",
    "trainer.train()\n",
    "\n",
    "# batch sizes to compare for eval\n",
    "batch_sizes = [4, 16, 64, 256]\n",
    "# 2:4 sparsity require fp16, so we cast here for a fair comparison\n",
    "with torch.autocast(\"cuda\"):\n",
    "    with torch.no_grad():\n",
    "        predictions = trainer.predict(tokenized_squad_dataset[\"validation\"])\n",
    "        start_logits, end_logits = predictions.predictions\n",
    "        fp16_baseline = compute_metrics(\n",
    "            start_logits,\n",
    "            end_logits,\n",
    "            tokenized_squad_dataset[\"validation\"],\n",
    "            squad_dataset[\"validation\"],\n",
    "        )\n",
    "        fp16_time = measure_execution_time(\n",
    "            model,\n",
    "            batch_sizes,\n",
    "            tokenized_squad_dataset[\"validation\"],\n",
    "        )\n",
    "\n",
    "print(\"fp16\", fp16_baseline)\n",
    "print(\"cuda_fp16 time\", fp16_time)\n",
    "\n",
    "import pandas as pd\n",
    "df = pd.DataFrame(trainer.state.log_history)\n",
    "df.plot.line(x='step', y='loss', title=\"Loss vs. # steps\", ylabel=\"loss\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pruning BERT to be 2:4 sparse\n",
    "\n",
    "Now that we have our baseline, it's time we prune BERT. There are many\n",
    "different pruning strategies, but one of the most common is **magnitude\n",
    "pruning**, which seeks to remove the weights with the lowest L1 norm.\n",
    "Magnitude pruning was used by NVIDIA in all their results and is a\n",
    "common baseline.\n",
    "\n",
    "To do this, we will use the `torch.ao.pruning` package, which contains a\n",
    "weight-norm (magnitude) sparsifier. These sparsifiers work by applying\n",
    "mask parametrizations to the weight tensors in a model. This lets them\n",
    "simulate sparsity by masking out the pruned weights.\n",
    "\n",
    "We'll also have to decide what layers of the model to apply sparsity to,\n",
    "which in this case is all of the `nn.Linear` layers, except for the\n",
    "task-specific head outputs. That's because semi-structured sparsity has\n",
    "[shape\n",
    "constraints](https://pytorch.org/docs/2.1/sparse.html#constructing-sparse-semi-structured-tensors),\n",
    "and the task-specific `nn.Linear` layers do not satisfy them.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sparsifier = WeightNormSparsifier(\n",
    "    # apply sparsity to all blocks\n",
    "    sparsity_level=1.0,\n",
    "    # shape of 4 elements is a block\n",
    "    sparse_block_shape=(1, 4),\n",
    "    # two zeros for every block of 4\n",
    "    zeros_per_block=2\n",
    ")\n",
    "\n",
    "# add to config if ``nn.Linear`` and in the BERT model.\n",
    "sparse_config = [\n",
    "    {\"tensor_fqn\": f\"{fqn}.weight\"}\n",
    "    for fqn, module in model.named_modules()\n",
    "    if isinstance(module, nn.Linear) and \"layer\" in fqn\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The first step for pruning the model is to insert parametrizations for\n",
    "masking the weights of the model. This is done by the prepare step.\n",
    "Anytime we try to access the `.weight` we will get `mask * weight`\n",
    "instead.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Prepare the model, insert fake-sparsity parametrizations for training\n",
    "sparsifier.prepare(model, sparse_config)\n",
    "print(model.bert.encoder.layer[0].output)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then, we'll take a single pruning step. All pruners implement a\n",
    "`update_mask()` method that updates the mask with the logic being\n",
    "determined by the pruner implementation. The step method calls this\n",
    "`update_mask` functions for the weights specified in the sparse config.\n",
    "\n",
    "We will also evaluate the model to show the accuracy degradation of\n",
    "zero-shot pruning, or pruning without fine-tuning / retraining.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sparsifier.step()\n",
    "with torch.autocast(\"cuda\"):\n",
    "    with torch.no_grad():\n",
    "        predictions = trainer.predict(tokenized_squad_dataset[\"validation\"])\n",
    "    pruned = compute_metrics(\n",
    "        *predictions.predictions,\n",
    "        tokenized_squad_dataset[\"validation\"],\n",
    "        squad_dataset[\"validation\"],\n",
    "    )\n",
    "print(\"pruned eval metrics:\", pruned)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this state, we can start fine-tuning the model, updating the elements\n",
    "that wouldn't be pruned to better account for the accuracy loss. Once\n",
    "we've reached a satisfied state, we can call `squash_mask` to fuse the\n",
    "mask and the weight together. This will remove the parametrizations and\n",
    "we are left with a zeroed-out 2:4 dense model.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "trainer.train()\n",
    "sparsifier.squash_mask()\n",
    "torch.set_printoptions(edgeitems=4)\n",
    "print(model.bert.encoder.layer[0].intermediate.dense.weight[:8, :8])\n",
    "\n",
    "df[\"sparse_loss\"] = pd.DataFrame(trainer.state.log_history)[\"loss\"]\n",
    "df.plot.line(x='step', y=[\"loss\", \"sparse_loss\"], title=\"Loss vs. # steps\", ylabel=\"loss\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Accelerating 2:4 sparse models for inference\n",
    "\n",
    "Now that we have a model in this format, we can accelerate it for\n",
    "inference just like in the QuickStart Guide.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model = model.cuda().half()\n",
    "# accelerate for sparsity\n",
    "for fqn, module in model.named_modules():\n",
    "    if isinstance(module, nn.Linear) and \"layer\" in fqn:\n",
    "        module.weight = nn.Parameter(to_sparse_semi_structured(module.weight))\n",
    "\n",
    "with torch.no_grad():\n",
    "    predictions = trainer.predict(tokenized_squad_dataset[\"validation\"])\n",
    "start_logits, end_logits = predictions.predictions\n",
    "metrics_sparse = compute_metrics(\n",
    "    start_logits,\n",
    "    end_logits,\n",
    "    tokenized_squad_dataset[\"validation\"],\n",
    "    squad_dataset[\"validation\"],\n",
    ")\n",
    "print(\"sparse eval metrics: \", metrics_sparse)\n",
    "sparse_perf = measure_execution_time(\n",
    "    model,\n",
    "    batch_sizes,\n",
    "    tokenized_squad_dataset[\"validation\"],\n",
    ")\n",
    "print(\"sparse perf metrics: \", sparse_perf)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Retraining our model after magnitude pruning has recovered nearly all of\n",
    "the F1 that has been lost when the model was pruned. At the same time we\n",
    "have achieved a 1.28x speedup for `bs=16`. Note that not all shapes are\n",
    "amenable to performance improvements. When batch sizes are small and\n",
    "limited time is spent in compute sparse kernels may be slower than their\n",
    "dense counterparts.\n",
    "\n",
    "Because semi-structured sparsity is implemented as a tensor subclass, it\n",
    "is compatible with `torch.compile`. When composed with\n",
    "`to_sparse_semi_structured`, we are able to achieve a total 2x speedup\n",
    "on BERT.\n",
    "\n",
    "  ----------------------------------------------------------------------\n",
    "  Metrics             fp16     2:4 sparse    delta / speedup  compiled\n",
    "  ------------------- -------- ------------- ---------------- ----------\n",
    "  Exact Match (%)     78.53    78.44         -0.09            \n",
    "\n",
    "  F1 (%)              86.93    86.49         -0.44            \n",
    "\n",
    "  Time (bs=4)         11.10    15.54         0.71x            no\n",
    "\n",
    "  Time (bs=16)        19.35    15.74         1.23x            no\n",
    "\n",
    "  Time (bs=64)        72.71    59.41         1.22x            no\n",
    "\n",
    "  Time (bs=256)       286.65   247.63        1.14x            no\n",
    "\n",
    "  Time (bs=4)         7.59     7.46          1.02x            yes\n",
    "\n",
    "  Time (bs=16)        11.47    9.68          1.18x            yes\n",
    "\n",
    "  Time (bs=64)        41.57    36.92         1.13x            yes\n",
    "\n",
    "  Time (bs=256)       159.22   142.23        1.12x            yes\n",
    "  ----------------------------------------------------------------------\n",
    "\n",
    "# Conclusion\n",
    "\n",
    "In this tutorial, we have shown how to prune BERT to be 2:4 sparse and\n",
    "how to accelerate a 2:4 sparse model for inference. By taking advantage\n",
    "of our `SparseSemiStructuredTensor` subclass, we were able to achieve a\n",
    "1.3x speedup over the fp16 baseline, and up to 2x with `torch.compile`.\n",
    "We also demonstrated the benefits of 2:4 sparsity by fine-tuning BERT to\n",
    "recover any lost F1 (86.92 dense vs 86.48 sparse).\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
