Skip to content

Commit 5f2b4b5

Browse files
author
Jessica Lin
authored
Merge branch 'jlin27-quant-tutorials' into zaf-quant-transfer-review
2 parents 998b399 + d7bd3cc commit 5f2b4b5

File tree

2 files changed

+24
-35
lines changed

2 files changed

+24
-35
lines changed

intermediate_source/dynamic_quantization_bert_tutorial.py

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,20 @@
3535
# are quantized dynamically (per batch) to int8 when the weights are
3636
# quantized to int8.
3737
#
38-
# In PyTorch, we have ``torch.quantization.quantize_dynamic`` API support
39-
# (https://pytorch.org/docs/stable/quantization.html#torch.quantization.quantize_dynamic),
40-
# which replaces specified modules with dynamic weight-only quantized
38+
# In PyTorch, we have `torch.quantization.quantize_dynamic API
39+
# <https://pytorch.org/docs/stable/quantization.html#torch.quantization.quantize_dynamic>`_
40+
# ,which replaces specified modules with dynamic weight-only quantized
4141
# versions and output the quantized model.
4242
#
4343
# - We demonstrate the accuracy and inference performance results on the
44-
# Microsoft Research Paraphrase Corpus (MRPC) task
45-
# (https://www.microsoft.com/en-us/download/details.aspx?id=52398) in
46-
# the General Language Understanding Evaluation benchmark (GLUE)
47-
# (https://gluebenchmark.com/). The MRPC (Dolan and Brockett, 2005) is
44+
# `Microsoft Research Paraphrase Corpus (MRPC) task <https://www.microsoft.com/en-us/download/details.aspx?id=52398>`_
45+
# in the General Language Understanding Evaluation benchmark `(GLUE)
46+
# <https://gluebenchmark.com/>`_. The MRPC (Dolan and Brockett, 2005) is
4847
# a corpus of sentence pairs automatically extracted from online news
4948
# sources, with human annotations of whether the sentences in the pair
5049
# are semantically equivalent. Because the classes are imbalanced (68%
5150
# positive, 32% negative), we follow common practice and report both
52-
# accuracy and F1 score
53-
# (https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html).
51+
# accuracy and `F1 score <https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html>`_
5452
# MRPC is a common NLP task for language pair classification, as shown
5553
# below.
5654
#
@@ -66,8 +64,10 @@
6664
#
6765
# To start this tutorial, let’s first follow the installation instructions
6866
# in PyTorch and HuggingFace Github Repo: -
69-
# https://github.com/pytorch/pytorch/#installation -
70-
# https://github.com/huggingface/transformers#installation
67+
#
68+
# * https://github.com/pytorch/pytorch/#installation -
69+
#
70+
# * https://github.com/huggingface/transformers#installation
7171
#
7272
# In addition, we also install ``sklearn`` package, as we will reuse its
7373
# built-in F1 score calculation helper function.
@@ -82,8 +82,8 @@
8282
######################################################################
8383
# Because we will be using the experimental parts of the PyTorch, it is
8484
# recommended to install the latest version of torch and torchvision. You
85-
# can find the most recent instructions on local installation here
86-
# https://pytorch.org/get-started/locally/. For example, to install on
85+
# can find the most recent instructions on local installation `here
86+
# <https://pytorch.org/get-started/locally/>`_. For example, to install on
8787
# Mac:
8888
#
8989
# .. code:: shell
@@ -140,10 +140,10 @@
140140
# Download the dataset
141141
# --------------------
142142
#
143-
# Before running MRPC tasks we download the GLUE data
144-
# (https://gluebenchmark.com/tasks) by running this script
145-
# (https://gist.github.com/W4ngatang/60c2bdb54d156a41194446737ce03e2e,
146-
# https://github.com/nyu-mll/GLUE-baselines/blob/master/download_glue_data.py)
143+
# Before running MRPC tasks we download the `GLUE data
144+
# <https://gluebenchmark.com/tasks>`_ by running this `script
145+
# <https://gist.github.com/W4ngatang/60c2bdb54d156a41194446737ce03e2e>`_ followed by
146+
# `download_glue_data <https://github.com/nyu-mll/GLUE-baselines/blob/master/download_glue_data.py>`_.
147147
# and unpack it to some directory “glue_data/MRPC”.
148148
#
149149
#
@@ -167,8 +167,7 @@
167167
# Convert the texts into features
168168
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
169169
#
170-
# glue_convert_examples_to_features (
171-
# https://github.com/huggingface/transformers/blob/master/transformers/data/processors/glue.py)
170+
# `glue_convert_examples_to_features <https://github.com/huggingface/transformers/blob/master/transformers/data/processors/glue.py>`_.
172171
# load a data file into a list of ``InputFeatures``.
173172
#
174173
# - Tokenize the input sequences;
@@ -181,8 +180,7 @@
181180
# F1 metric
182181
# ~~~~~~~~~
183182
#
184-
# The F1 score
185-
# (https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html)
183+
# The `F1 score <https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html>`_
186184
# can be interpreted as a weighted average of the precision and recall,
187185
# where an F1 score reaches its best value at 1 and worst score at 0. The
188186
# relative contribution of precision and recall to the F1 score are equal.
@@ -208,7 +206,7 @@
208206
#
209207
# To fine-tune the pre-trained BERT model (“bert-base-uncased” model in
210208
# HuggingFace transformers) for the MRPC task, you can follow the command
211-
# in (https://github.com/huggingface/transformers/tree/master/examples):
209+
# in `examples<https://github.com/huggingface/transformers/tree/master/examples>`_"
212210
#
213211
# ::
214212
#
@@ -317,10 +315,8 @@ def set_seed(seed):
317315
# Define the tokenize and evaluation function
318316
# -------------------------------------------
319317
#
320-
# We reuse the tokenize and evaluation function from
321-
# https://github.com/huggingface/transformers/blob/master/examples/run_glue.py.
318+
# We reuse the tokenize and evaluation function from `huggingface <https://github.com/huggingface/transformers/blob/master/examples/run_glue.py>`_.
322319
#
323-
324320
# coding=utf-8
325321
# Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc. team.
326322
# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
@@ -555,8 +551,8 @@ def time_model_evaluation(model, configs, tokenizer):
555551
# set multi-thread by ``torch.set_num_threads(N)`` (``N`` is the number of
556552
# intra-op parallelization threads). One preliminary requirement to enable
557553
# the intra-op parallelization support is to build PyTorch with the right
558-
# backend such as OpenMP, Native, or TBB
559-
# (https://pytorch.org/docs/stable/notes/cpu_threading_torchscript_inference.html#build-options).
554+
# `backend <https://pytorch.org/docs/stable/notes/cpu_threading_torchscript_inference.html#build-options>`_
555+
# such as OpenMP, Native or TBB.
560556
# You can use ``torch.__config__.parallel_info()`` to check the
561557
# parallelization settings. On the same MacBook Pro using PyTorch with
562558
# Native backend for parallelization, we can get about 46 seconds for

intermediate_source/quantized_transfer_learning_tutorial.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
# ~~~~~~~~~
7979
#
8080
# .. note :: This section is identical to the original transfer learning tutorial.
81-
#
8281
# We will use ``torchvision`` and ``torch.utils.data`` packages to load
8382
# the data.
8483
#
@@ -369,18 +368,13 @@ def create_combined_model(model_fe):
369368
)
370369
return new_model
371370

372-
<<<<<<< Updated upstream
373-
new_model = create_combined_model(model_fe)
374-
375-
376-
=======
377-
>>>>>>> Stashed changes
378371
######################################################################
379372
# .. warning:: Currently the quantized models can only be run on CPU.
380373
# However, it is possible to send the non-quantized parts of the model to a GPU.
381374
#
382375

383376
import torch.optim as optim
377+
new_model = create_combined_model(model_fe)
384378
new_model = new_model.to('cpu')
385379

386380
criterion = nn.CrossEntropyLoss()
@@ -518,4 +512,3 @@ def create_combined_model(model_fe):
518512
plt.ioff()
519513
plt.tight_layout()
520514
plt.show()
521-

0 commit comments

Comments
 (0)