Skip to content

Commit

Permalink
Merge branch 'main' into issues/1449
Browse files Browse the repository at this point in the history
  • Loading branch information
Svetlana Karslioglu authored Jun 8, 2023
2 parents 6b1547a + 3b6d83b commit 660d36e
Show file tree
Hide file tree
Showing 20 changed files with 56 additions and 90 deletions.
3 changes: 0 additions & 3 deletions advanced_source/cpp_frontend.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1216,9 +1216,6 @@ tensors and display them with matplotlib:
.. code-block:: python
from __future__ import print_function
from __future__ import unicode_literals
import argparse
import matplotlib.pyplot as plt
Expand Down
2 changes: 0 additions & 2 deletions advanced_source/neural_style_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
# - ``torchvision.models`` (train or load pretrained models)
# - ``copy`` (to deep copy the models; system package)

from __future__ import print_function

import torch
import torch.nn as nn
import torch.nn.functional as F
Expand Down
2 changes: 1 addition & 1 deletion advanced_source/rpc_ddp_tutorial.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Combining Distributed DataParallel with Distributed RPC Framework
=================================================================
**Authors**: `Pritam Damania <https://github.com/pritamdamania87>`_ and `Yi Wang <https://github.com/SciPioneer>`_
**Authors**: `Pritam Damania <https://github.com/pritamdamania87>`_ and `Yi Wang <https://github.com/wayi1>`_

.. note::
|edit| View and edit this tutorial in `github <https://github.com/pytorch/tutorials/blob/main/advanced_source/rpc_ddp_tutorial.rst>`__.
Expand Down
5 changes: 0 additions & 5 deletions beginner_source/chatbot_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,6 @@
# After that, let’s import some necessities.
#

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import torch
from torch.jit import script, trace
import torch.nn as nn
Expand Down
1 change: 0 additions & 1 deletion beginner_source/data_loading_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"""

from __future__ import print_function, division
import os
import torch
import pandas as pd
Expand Down
5 changes: 0 additions & 5 deletions beginner_source/deploy_seq2seq_hybrid_frontend_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@
# maximum length output that the model is capable of producing.
#

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import torch
import torch.nn as nn
import torch.nn.functional as F
Expand Down
24 changes: 9 additions & 15 deletions beginner_source/fgsm_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
# into the implementation.
#

from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
Expand All @@ -99,13 +98,6 @@
import numpy as np
import matplotlib.pyplot as plt

# NOTE: This is a hack to get around "User-agent" limitations when downloading MNIST datasets
# see, https://github.com/pytorch/vision/issues/3497 for more information
from six.moves import urllib
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)


######################################################################
# Implementation
Expand Down Expand Up @@ -141,6 +133,8 @@
epsilons = [0, .05, .1, .15, .2, .25, .3]
pretrained_model = "data/lenet_mnist_model.pth"
use_cuda=True
# Set random seed for reproducibility
torch.manual_seed(42)


######################################################################
Expand Down Expand Up @@ -179,18 +173,18 @@ def forward(self, x):
test_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=False, download=True, transform=transforms.Compose([
transforms.ToTensor(),
])),
])),
batch_size=1, shuffle=True)

# Define what device we are using
print("CUDA Available: ",torch.cuda.is_available())
device = torch.device("cuda" if (use_cuda and torch.cuda.is_available()) else "cpu")
device = torch.device("cuda" if use_cuda and torch.cuda.is_available() else "cpu")

# Initialize the network
model = Net().to(device)

# Load the pretrained model
model.load_state_dict(torch.load(pretrained_model, map_location='cpu'))
model.load_state_dict(torch.load(pretrained_model, weights_only=True, map_location='cpu'))

# Set the model in evaluation mode. In this case this is for the Dropout layers
model.eval()
Expand Down Expand Up @@ -290,7 +284,7 @@ def test( model, device, test_loader, epsilon ):
if final_pred.item() == target.item():
correct += 1
# Special case for saving 0 epsilon examples
if (epsilon == 0) and (len(adv_examples) < 5):
if epsilon == 0 and len(adv_examples) < 5:
adv_ex = perturbed_data.squeeze().detach().cpu().numpy()
adv_examples.append( (init_pred.item(), final_pred.item(), adv_ex) )
else:
Expand All @@ -301,7 +295,7 @@ def test( model, device, test_loader, epsilon ):

# Calculate final accuracy for this epsilon
final_acc = correct/float(len(test_loader))
print("Epsilon: {}\tTest Accuracy = {} / {} = {}".format(epsilon, correct, len(test_loader), final_acc))
print(f"Epsilon: {epsilon}\tTest Accuracy = {correct} / {len(test_loader)} = {final_acc}")

# Return the accuracy and an adversarial example
return final_acc, adv_examples
Expand Down Expand Up @@ -387,9 +381,9 @@ def test( model, device, test_loader, epsilon ):
plt.xticks([], [])
plt.yticks([], [])
if j == 0:
plt.ylabel("Eps: {}".format(epsilons[i]), fontsize=14)
plt.ylabel(f"Eps: {epsilons[i]}", fontsize=14)
orig,adv,ex = examples[i][j]
plt.title("{} -> {}".format(orig, adv))
plt.title(f"{orig} -> {adv}")
plt.imshow(ex, cmap="gray")
plt.tight_layout()
plt.show()
Expand Down
2 changes: 0 additions & 2 deletions beginner_source/transfer_learning_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
# License: BSD
# Author: Sasank Chilamkurthy

from __future__ import print_function, division

import torch
import torch.nn as nn
import torch.optim as optim
Expand Down
9 changes: 8 additions & 1 deletion conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import pytorch_sphinx_theme
import torch
import glob
import random
import shutil
from custom_directives import IncludeDirective, GalleryItemDirective, CustomGalleryItemDirective, CustomCalloutItemDirective, CustomCardItemDirective
import distutils.file_util
Expand Down Expand Up @@ -85,6 +86,11 @@

# -- Sphinx-gallery configuration --------------------------------------------

def reset_seeds(gallery_conf, fname):
torch.manual_seed(42)
torch.set_default_device(None)
random.seed(10)

sphinx_gallery_conf = {
'examples_dirs': ['beginner_source', 'intermediate_source',
'advanced_source', 'recipes_source', 'prototype_source'],
Expand All @@ -94,7 +100,8 @@
'backreferences_dir': None,
'first_notebook_cell': ("# For tips on running notebooks in Google Colab, see\n"
"# https://pytorch.org/tutorials/beginner/colab\n"
"%matplotlib inline")
"%matplotlib inline"),
'reset_modules': (reset_seeds)
}

if os.getenv('GALLERY_PATTERN'):
Expand Down
1 change: 0 additions & 1 deletion intermediate_source/char_rnn_classification_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
``{language: [names ...]}``. The generic variables "category" and "line"
(for language and name in our case) are used for later extensibility.
"""
from __future__ import unicode_literals, print_function, division
from io import open
import glob
import os
Expand Down
1 change: 0 additions & 1 deletion intermediate_source/char_rnn_generation_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
and end up with a dictionary ``{language: [names ...]}``.
"""
from __future__ import unicode_literals, print_function, division
from io import open
import glob
import os
Expand Down
19 changes: 2 additions & 17 deletions intermediate_source/dynamic_quantization_bert_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ In this step we import the necessary Python modules for the tutorial.

.. code:: python
from __future__ import absolute_import, division, print_function
import logging
import numpy as np
import os
Expand Down Expand Up @@ -255,8 +253,6 @@ model before and after the dynamic quantization.
torch.manual_seed(seed)
set_seed(42)
# Initialize a global random number generator
global_rng = random.Random()
2.2 Load the fine-tuned BERT model
Expand Down Expand Up @@ -528,20 +524,9 @@ We can serialize and save the quantized model for the future use using

.. code:: python
def ids_tensor(shape, vocab_size, rng=None, name=None):
def ids_tensor(shape, vocab_size):
# Creates a random int32 tensor of the shape within the vocab size
if rng is None:
rng = global_rng
total_dims = 1
for dim in shape:
total_dims *= dim
values = []
for _ in range(total_dims):
values.append(rng.randint(0, vocab_size - 1))
return torch.tensor(data=values, dtype=torch.long, device='cpu').view(shape).contiguous()
return torch.randint(0, vocab_size, shape=shape, dtype=torch.int, device='cpu')
input_ids = ids_tensor([8, 128], 2)
token_type_ids = ids_tensor([8, 128], 2)
Expand Down
11 changes: 7 additions & 4 deletions intermediate_source/mario_rl_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
# Super Mario environment for OpenAI Gym
import gym_super_mario_bros

from tensordict import TensorDict
from torchrl.data import TensorDictReplayBuffer, LazyMemmapStorage

######################################################################
# RL Definitions
Expand Down Expand Up @@ -348,7 +350,7 @@ def act(self, state):
class Mario(Mario): # subclassing for continuity
def __init__(self, state_dim, action_dim, save_dir):
super().__init__(state_dim, action_dim, save_dir)
self.memory = deque(maxlen=100000)
self.memory = TensorDictReplayBuffer(storage=LazyMemmapStorage(100000))
self.batch_size = 32

def cache(self, state, next_state, action, reward, done):
Expand All @@ -373,14 +375,15 @@ def first_if_tuple(x):
reward = torch.tensor([reward], device=self.device)
done = torch.tensor([done], device=self.device)

self.memory.append((state, next_state, action, reward, done,))
# self.memory.append((state, next_state, action, reward, done,))
self.memory.add(TensorDict({"state": state, "next_state": next_state, "action": action, "reward": reward, "done": done}, batch_size=[]))

def recall(self):
"""
Retrieve a batch of experiences from memory
"""
batch = random.sample(self.memory, self.batch_size)
state, next_state, action, reward, done = map(torch.stack, zip(*batch))
batch = self.memory.sample(self.batch_size)
state, next_state, action, reward, done = (batch.get(key) for key in ("state", "next_state", "action", "reward", "done"))
return state, next_state, action.squeeze(), reward.squeeze(), done.squeeze()


Expand Down
7 changes: 3 additions & 4 deletions intermediate_source/seq2seq_translation_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
:alt:
To improve upon this model we'll use an `attention
mechanism <https://arxiv.org/abs/1409.0473>`__, which lets the decoder
mechanism <https://arxiv.org/abs/1508.04025>`__, which lets the decoder
learn to focus over a specific range of the input sequence.
**Recommended Reading:**
Expand All @@ -66,8 +66,8 @@
Statistical Machine Translation <https://arxiv.org/abs/1406.1078>`__
- `Sequence to Sequence Learning with Neural
Networks <https://arxiv.org/abs/1409.3215>`__
- `Neural Machine Translation by Jointly Learning to Align and
Translate <https://arxiv.org/abs/1409.0473>`__
- `Effective Approaches to Attention-based Neural Machine
Translation <https://arxiv.org/abs/1508.04025>`__
- `A Neural Conversational Model <https://arxiv.org/abs/1506.05869>`__
You will also find the previous tutorials on
Expand All @@ -78,7 +78,6 @@
**Requirements**
"""
from __future__ import unicode_literals, print_function, division
from io import open
import unicodedata
import string
Expand Down
1 change: 0 additions & 1 deletion intermediate_source/spatial_transformer_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
# License: BSD
# Author: Ghassen Hamrouni

from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
Expand Down
6 changes: 3 additions & 3 deletions intermediate_source/torch_compile_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@

def foo(x, y):
a = torch.sin(x)
b = torch.cos(x)
b = torch.cos(y)
return a + b
opt_foo1 = torch.compile(foo)
print(opt_foo1(torch.randn(10, 10), torch.randn(10, 10)))
Expand All @@ -80,7 +80,7 @@ def foo(x, y):
@torch.compile
def opt_foo2(x, y):
a = torch.sin(x)
b = torch.cos(x)
b = torch.cos(y)
return a + b
print(opt_foo2(torch.randn(10, 10), torch.randn(10, 10)))

Expand All @@ -105,7 +105,7 @@ def forward(self, x):
#
# Let's now demonstrate that using ``torch.compile`` can speed
# up real models. We will compare standard eager mode and
# ``torch.compile`` by evaluating and training ResNet-18 on random data.
# ``torch.compile`` by evaluating and training a ``torchvision`` model on random data.
#
# Before we start, we need to define some utility functions.

Expand Down
24 changes: 21 additions & 3 deletions prototype_source/fx_graph_mode_ptq_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,27 @@ def evaluate(model_, data_source):
.set_object_type(nn.LSTM, default_dynamic_qconfig)
.set_object_type(nn.Linear, default_dynamic_qconfig)
)
# Deepcopying the original model because quantization api changes the model inplace and we want
# Load model to create the original model because quantization api changes the model inplace and we want
# to keep the original model for future comparison
model_to_quantize = copy.deepcopy(model)


model_to_quantize = LSTMModel(
ntoken = ntokens,
ninp = 512,
nhid = 256,
nlayers = 5,
)

model_to_quantize.load_state_dict(
torch.load(
model_data_filepath + 'word_language_model_quantize.pth',
map_location=torch.device('cpu')
)
)

model_to_quantize.eval()


prepared_model = prepare_fx(model_to_quantize, qconfig_mapping, example_inputs)
print("prepared model:", prepared_model)
quantized_model = convert_fx(prepared_model)
Expand Down Expand Up @@ -289,4 +307,4 @@ def time_model_evaluation(model, test_data):
# 3. Conclusion
# -------------
# This tutorial introduces the api for post training dynamic quantization in FX Graph Mode,
# which dynamically quantizes the same modules as Eager Mode Quantization.
# which dynamically quantizes the same modules as Eager Mode Quantization.
Loading

0 comments on commit 660d36e

Please sign in to comment.