Skip to content

Update feature classification labels #1035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions advanced_source/dynamic_quantization_tutorial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
(experimental) Dynamic Quantization on an LSTM Word Language Model
(beta) Dynamic Quantization on an LSTM Word Language Model
==================================================================

**Author**: `James Reed <https://github.com/jamesr66a>`_
Expand All @@ -13,7 +13,7 @@
to int, which can result in smaller model size and faster inference with only a small
hit to accuracy.

In this tutorial, we'll apply the easiest form of quantization -
In this tutorial, we'll apply the easiest form of quantization -
`dynamic quantization <https://pytorch.org/docs/stable/quantization.html#torch.quantization.quantize_dynamic>`_ -
to an LSTM-based next word-prediction model, closely following the
`word language model <https://github.com/pytorch/examples/tree/master/word_language_model>`_
Expand Down
2 changes: 1 addition & 1 deletion advanced_source/static_quantization_tutorial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
(experimental) Static Quantization with Eager Mode in PyTorch
(beta) Static Quantization with Eager Mode in PyTorch
=========================================================

**Author**: `Raghuraman Krishnamoorthi <https://github.com/raghuramank100>`_
Expand Down
12 changes: 6 additions & 6 deletions index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,14 @@ Welcome to PyTorch Tutorials
.. Frontend APIs

.. customcarditem::
:header: (experimental) Introduction to Named Tensors in PyTorch
:header: (prototype) Introduction to Named Tensors in PyTorch
:card_description: Learn how to use PyTorch to train a Deep Q Learning (DQN) agent on the CartPole-v0 task from the OpenAI Gym.
:image: _static/img/thumbnails/cropped/experimental-Introduction-to-Named-Tensors-in-PyTorch.png
:link: intermediate/memory_format_tutorial.html
:tags: Frontend-APIs,Named-Tensor,Best-Practice

.. customcarditem::
:header: (experimental) Channels Last Memory Format in PyTorch
:header: (beta) Channels Last Memory Format in PyTorch
:card_description: Get an overview of Channels Last memory format and understand how it is used to order NCHW tensors in memory preserving dimensions.
:image: _static/img/thumbnails/cropped/experimental-Channels-Last-Memory-Format-in-PyTorch.png
:link: intermediate/memory_format_tutorial.html
Expand Down Expand Up @@ -261,28 +261,28 @@ Welcome to PyTorch Tutorials
:tags: Model-Optimization,Best-Practice

.. customcarditem::
:header: (experimental) Dynamic Quantization on an LSTM Word Language Model
:header: (beta) Dynamic Quantization on an LSTM Word Language Model
:card_description: Apply dynamic quantization, the easiest form of quantization, to a LSTM-based next word prediction model.
:image: _static/img/thumbnails/cropped/experimental-Dynamic-Quantization-on-an-LSTM-Word-Language-Model.png
:link: advanced/dynamic_quantization_tutorial.html
:tags: Text,Quantization,Model-Optimization

.. customcarditem::
:header: (experimental) Dynamic Quantization on BERT
:header: (beta) Dynamic Quantization on BERT
:card_description: Apply the dynamic quantization on a BERT (Bidirectional Embedding Representations from Transformers) model.
:image: _static/img/thumbnails/cropped/experimental-Dynamic-Quantization-on-BERT.png
:link: intermediate/dynamic_quantization_bert_tutorial.html
:tags: Text,Quantization,Model-Optimization

.. customcarditem::
:header: (experimental) Static Quantization with Eager Mode in PyTorch
:header: (beta) Static Quantization with Eager Mode in PyTorch
:card_description: Learn techniques to impove a model's accuracy = post-training static quantization, per-channel quantization, and quantization-aware training.
:image: _static/img/thumbnails/cropped/experimental-Static-Quantization-with-Eager-Mode-in-PyTorch.png
:link: advanced/static_quantization_tutorial.html
:tags: Image/Video,Quantization,Model-Optimization

.. customcarditem::
:header: (experimental) Quantized Transfer Learning for Computer Vision Tutorial
:header: (beta) Quantized Transfer Learning for Computer Vision Tutorial
:card_description: Learn techniques to impove a model's accuracy - post-training static quantization, per-channel quantization, and quantization-aware training.
:image: _static/img/thumbnails/cropped/experimental-Quantized-Transfer-Learning-for-Computer-Vision-Tutorial.png
:link: advanced/static_quantization_tutorial.html
Expand Down
8 changes: 4 additions & 4 deletions intermediate_source/dynamic_quantization_bert_tutorial.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(experimental) Dynamic Quantization on BERT
(beta) Dynamic Quantization on BERT
===========================================

.. tip::
To get the most of this tutorial, we suggest using this
To get the most of this tutorial, we suggest using this
`Colab Version <https://colab.research.google.com/github/pytorch/tutorials/blob/gh-pages/_downloads/dynamic_quantization_bert_tutorial.ipynb>`_. This will allow you to experiment with the information presented below.

**Author**: `Jianyu Huang <https://github.com/jianyuh>`_

**Reviewed by**: `Raghuraman Krishnamoorthi <https://github.com/raghuramank100>`_
Expand Down Expand Up @@ -71,7 +71,7 @@ built-in F1 score calculation helper function.
pip install transformers


Because we will be using the experimental parts of the PyTorch, it is
Because we will be using the beta parts of the PyTorch, it is
recommended to install the latest version of torch and torchvision. You
can find the most recent instructions on local installation `here
<https://pytorch.org/get-started/locally/>`_. For example, to install on
Expand Down
28 changes: 14 additions & 14 deletions intermediate_source/memory_format_tutorial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
"""
(experimental) Channels Last Memory Format in PyTorch
(beta) Channels Last Memory Format in PyTorch
*******************************************************
**Author**: `Vitaly Fedyunin <https://github.com/VitalyFedyunin>`_

Expand All @@ -11,12 +11,12 @@

For example, classic (contiguous) storage of NCHW tensor (in our case it is two 2x2 images with 3 color channels) look like this:

.. figure:: /_static/img/classic_memory_format.png
.. figure:: /_static/img/classic_memory_format.png
:alt: classic_memory_format

Channels Last memory format orders data differently:

.. figure:: /_static/img/channels_last_memory_format.png
.. figure:: /_static/img/channels_last_memory_format.png
:alt: channels_last_memory_format

Pytorch supports memory formats (and provides back compatibility with existing models including eager, JIT, and TorchScript) by utilizing existing strides structure.
Expand All @@ -34,7 +34,7 @@
# Memory Format API
# -----------------------
#
# Here is how to convert tensors between contiguous and channels
# Here is how to convert tensors between contiguous and channels
# last memory formats.

######################################################################
Expand Down Expand Up @@ -104,9 +104,9 @@
######################################################################
# Performance Gains
# -------------------------------------------------------------------------------------------
# The most significant performance gains are observed on NVidia's hardware with
# The most significant performance gains are observed on Nvidia's hardware with
# Tensor Cores support. We were able to archive over 22% perf gains while running '
# AMP (Automated Mixed Precision) training scripts supplied by NVidia https://github.com/NVIDIA/apex.
# AMP (Automated Mixed Precision) training scripts supplied by Nvidia https://github.com/NVIDIA/apex.
#
# ``python main_amp.py -a resnet50 --b 200 --workers 16 --opt-level O2 ./data``

Expand Down Expand Up @@ -144,7 +144,7 @@

######################################################################
# Passing ``--channels-last true`` allows running a model in Channels Last format with observed 22% perf gain.
#
#
# ``python main_amp.py -a resnet50 --b 200 --workers 16 --opt-level O2 --channels-last true ./data``

# opt_level = O2
Expand Down Expand Up @@ -192,7 +192,7 @@
# Converting existing models
# --------------------------
#
# Channels Last support not limited by existing models, as any model can be converted to Channels Last and propagate format through the graph as soon as input formatted correctly.
# Channels Last support not limited by existing models, as any model can be converted to Channels Last and propagate format through the graph as soon as input formatted correctly.
#

# Need to be done once, after model initialization (or load)
Expand All @@ -203,12 +203,12 @@
output = model(input)

#######################################################################
# However, not all operators fully converted to support Channels Last (usually returning
# contiguous output instead). That means you need to verify the list of used operators
# against supported operators list https://github.com/pytorch/pytorch/wiki/Operators-with-Channels-Last-support,
# However, not all operators fully converted to support Channels Last (usually returning
# contiguous output instead). That means you need to verify the list of used operators
# against supported operators list https://github.com/pytorch/pytorch/wiki/Operators-with-Channels-Last-support,
# or introduce memory format checks into eager execution mode and run your model.
#
# After running the code below, operators will raise an exception if the output of the
#
# After running the code below, operators will raise an exception if the output of the
# operator doesn't match the memory format of the input.
#
#
Expand Down Expand Up @@ -282,7 +282,7 @@ def attribute(m):

######################################################################
# If you found an operator that doesn't support Channels Last tensors
# and you want to contribute, feel free to use following developers
# and you want to contribute, feel free to use following developers
# guide https://github.com/pytorch/pytorch/wiki/Writing-memory-format-aware-operators.
#

Expand Down
2 changes: 1 addition & 1 deletion intermediate_source/named_tensor_tutorial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
"""
(experimental) Introduction to Named Tensors in PyTorch
(prototype) Introduction to Named Tensors in PyTorch
*******************************************************
**Author**: `Richard Zou <https://github.com/zou3519>`_

Expand Down
10 changes: 5 additions & 5 deletions intermediate_source/quantized_transfer_learning_tutorial.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(experimental) Quantized Transfer Learning for Computer Vision Tutorial
(beta) Quantized Transfer Learning for Computer Vision Tutorial
========================================================================

.. tip::
To get the most of this tutorial, we suggest using this
`Colab Version <https://colab.research.google.com/github/pytorch/tutorials/blob/gh-pages/_downloads/quantized_transfer_learning_tutorial.ipynb>`_.
This will allow you to experiment with the information presented below.
To get the most of this tutorial, we suggest using this
`Colab Version <https://colab.research.google.com/github/pytorch/tutorials/blob/gh-pages/_downloads/quantized_transfer_learning_tutorial.ipynb>`_.
This will allow you to experiment with the information presented below.

**Author**: `Zafar Takhirov <https://github.com/z-a-f>`_

Expand Down Expand Up @@ -62,7 +62,7 @@ such as installations and data loading/visualizations.
Installing the Nightly Build
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Because you will be using the experimental parts of the PyTorch, it is
Because you will be using the beta parts of the PyTorch, it is
recommended to install the latest version of ``torch`` and
``torchvision``. You can find the most recent instructions on local
installation `here <https://pytorch.org/get-started/locally/>`_.
Expand Down
2 changes: 1 addition & 1 deletion intermediate_source/rpc_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Getting Started with Distributed RPC Framework

This tutorial uses two simple examples to demonstrate how to build distributed
training with the `torch.distributed.rpc <https://pytorch.org/docs/master/rpc.html>`__
package which is first introduced as an experimental feature in PyTorch v1.4.
package which is first introduced as a prototype feature in PyTorch v1.4.
Source code of the two examples can be found in
`PyTorch examples <https://github.com/pytorch/examples>`__.

Expand Down
64 changes: 32 additions & 32 deletions recipes_source/recipes/dynamic_quantization.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@

# define a very, very simple LSTM for demonstration purposes
# in this case, we are wrapping nn.LSTM, one layer, no pre or post processing
# inspired by
# inspired by
# https://pytorch.org/tutorials/beginner/nlp/sequence_models_tutorial.html, by Robert Guthrie
# and https://pytorch.org/tutorials/advanced/dynamic_quantization_tutorial.html
class lstm_for_demonstration(nn.Module):
"""Elementary Long Short Term Memory style model which simply wraps nn.LSTM
Not to be used for anything other than demonstration.
"""
Not to be used for anything other than demonstration.
"""
def __init__(self,in_dim,out_dim,depth):
super(lstm_for_demonstration,self).__init__()
self.lstm = nn.LSTM(in_dim,out_dim,depth)
Expand All @@ -142,7 +142,7 @@ def forward(self,inputs,hidden):
out,hidden = self.lstm(inputs,hidden)
return out, hidden


torch.manual_seed(29592) # set the seed for reproducibility

#shape parameters
Expand All @@ -154,32 +154,32 @@ def forward(self,inputs,hidden):
# random data for input
inputs = torch.randn(sequence_length,batch_size,model_dimension)
# hidden is actually is a tuple of the initial hidden state and the initial cell state
hidden = (torch.randn(lstm_depth,batch_size,model_dimension), torch.randn(lstm_depth,batch_size,model_dimension))
hidden = (torch.randn(lstm_depth,batch_size,model_dimension), torch.randn(lstm_depth,batch_size,model_dimension))


######################################################################
# 2: Do the Quantization
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#
# Now we get to the fun part. First we create an instance of the model
# called float\_lstm then we are going to quantize it. We're going to use
# the
#
#
# ::
#
#
# torch.quantization.quantize_dynamic()
#
#
# function here (`see
# documentation <https://pytorch.org/docs/stable/quantization.html#torch.quantization.quantize_dynamic>`__)
# which takes the model, then a list of the submodules which we want to
# have quantized if they appear, then the datatype we are targeting. This
# function returns a quantized version of the original model as a new
# module.
#
#
# That's all it takes.
#
#

# here is our floating point instance
# here is our floating point instance
float_lstm = lstm_for_demonstration(model_dimension, model_dimension,lstm_depth)

# this is the call that does the work
Expand All @@ -206,7 +206,7 @@ def forward(self,inputs,hidden):
# (for example you can set model dimension to something like 80) this will
# converge towards 4x smaller as the stored model size dominated more and
# more by the parameter values.
#
#

def print_size_of_model(model, label=""):
torch.save(model.state_dict(), "temp.p")
Expand All @@ -221,23 +221,23 @@ def print_size_of_model(model, label=""):
print("{0:.2f} times smaller".format(f/q))

# note that this value is wrong in PyTorch 1.4 due to https://github.com/pytorch/pytorch/issues/31468
# this will be fixed in 1.5 with https://github.com/pytorch/pytorch/pull/31540
# this will be fixed in 1.5 with https://github.com/pytorch/pytorch/pull/31540


######################################################################
# 4. Look at Latency
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The second benefit is that the quantized model will typically run
# faster. This is due to a combinations of effects including at least:
#
#
# 1. Less time spent moving parameter data in
# 2. Faster INT8 operations
#
#
# As you will see the quantized version of this super-simple network runs
# faster. This will generally be true of more complex networks but as they
# say "your milage may vary" depending on a number of factors including
# the structure of the model and the hardware you are running on.
#
#

# compare the performance
print("Floating point FP32")
Expand All @@ -255,10 +255,10 @@ def print_size_of_model(model, label=""):
# trained one. However, I think it is worth quickly showing that the
# quantized network does produce output tensors that are "in the same
# ballpark" as the original one.
#
#
# For a more detailed analysis please see the more advanced tutorials
# referenced at the end of this recipe.
#
#

# run the float model
out1, hidden1 = float_lstm(inputs, hidden)
Expand All @@ -270,7 +270,7 @@ def print_size_of_model(model, label=""):
mag2 = torch.mean(abs(out2)).item()
print('mean absolute value of output tensor values in the INT8 model is {0:.5f}'.format(mag2))

# compare them
# compare them
mag3 = torch.mean(abs(out1-out2)).item()
print('mean absolute value of the difference between the output tensors is {0:.5f} or {1:.2f} percent'.format(mag3,mag3/mag1*100))

Expand All @@ -281,26 +281,26 @@ def print_size_of_model(model, label=""):
# We've explained what dynamic quantization is, what benefits it brings,
# and you have used the ``torch.quantization.quantize_dynamic()`` function
# to quickly quantize a simple LSTM model.
#
#
# This was a fast and high level treatment of this material; for more
# detail please continue learning with `(experimental) Dynamic Quantization on an LSTM Word Language Model Tutorial <https://pytorch.org/tutorials/advanced/dynamic\_quantization\_tutorial.html>`_.
#
#
# detail please continue learning with `(beta) Dynamic Quantization on an LSTM Word Language Model Tutorial <https://pytorch.org/tutorials/advanced/dynamic\_quantization\_tutorial.html>`_.
#
#
# Additional Resources
# =========
# Documentation
# ~~~~~~~~~~~~~~
#
#
# `Quantization API Documentaion <https://pytorch.org/docs/stable/quantization.html>`_
#
#
# Tutorials
# ~~~~~~~~~~~~~~
#
# `(experimental) Dynamic Quantization on BERT <https://pytorch.org/tutorials/intermediate/dynamic\_quantization\_bert\_tutorial.html>`_
#
# `(experimental) Dynamic Quantization on an LSTM Word Language Model <https://pytorch.org/tutorials/advanced/dynamic\_quantization\_tutorial.html>`_
#
#
# `(beta) Dynamic Quantization on BERT <https://pytorch.org/tutorials/intermediate/dynamic\_quantization\_bert\_tutorial.html>`_
#
# `(beta) Dynamic Quantization on an LSTM Word Language Model <https://pytorch.org/tutorials/advanced/dynamic\_quantization\_tutorial.html>`_
#
# Blogs
# ~~~~~~~~~~~~~~
# ` Introduction to Quantization on PyTorch <https://pytorch.org/blog/introduction-to-quantization-on-pytorch/>`_
#
#