Skip to content

Commit 90ff2b1

Browse files
author
Svetlana Karslioglu
authored
Merge branch 'main' into issue_1993
2 parents fc0a023 + 994bd83 commit 90ff2b1

File tree

5 files changed

+54
-55
lines changed

5 files changed

+54
-55
lines changed

advanced_source/neural_style_tutorial.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
developed by Leon A. Gatys, Alexander S. Ecker and Matthias Bethge.
1515
Neural-Style, or Neural-Transfer, allows you to take an image and
1616
reproduce it with a new artistic style. The algorithm takes three images,
17-
an input image, a content-image, and a style-image, and changes the input
17+
an input image, a content-image, and a style-image, and changes the input
1818
to resemble the content of the content-image and the artistic style of the style-image.
1919
2020
@@ -70,6 +70,7 @@
7070
# method is used to move tensors or modules to a desired device.
7171

7272
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
73+
torch.set_default_device(device)
7374

7475
######################################################################
7576
# Loading the Images
@@ -261,7 +262,7 @@ def forward(self, input):
261262
# network to evaluation mode using ``.eval()``.
262263
#
263264

264-
cnn = models.vgg19(pretrained=True).features.to(device).eval()
265+
cnn = models.vgg19(pretrained=True).features.eval()
265266

266267

267268

@@ -271,8 +272,8 @@ def forward(self, input):
271272
# We will use them to normalize the image before sending it into the network.
272273
#
273274

274-
cnn_normalization_mean = torch.tensor([0.485, 0.456, 0.406]).to(device)
275-
cnn_normalization_std = torch.tensor([0.229, 0.224, 0.225]).to(device)
275+
cnn_normalization_mean = torch.tensor([0.485, 0.456, 0.406])
276+
cnn_normalization_std = torch.tensor([0.229, 0.224, 0.225])
276277

277278
# create a module to normalize input image so we can easily put it in a
278279
# ``nn.Sequential``
@@ -308,7 +309,7 @@ def get_style_model_and_losses(cnn, normalization_mean, normalization_std,
308309
content_layers=content_layers_default,
309310
style_layers=style_layers_default):
310311
# normalization module
311-
normalization = Normalization(normalization_mean, normalization_std).to(device)
312+
normalization = Normalization(normalization_mean, normalization_std)
312313

313314
# just in order to have an iterable access to or list of content/style
314315
# losses
@@ -373,7 +374,7 @@ def get_style_model_and_losses(cnn, normalization_mean, normalization_std,
373374
#
374375
# ::
375376
#
376-
# input_img = torch.randn(content_img.data.size(), device=device)
377+
# input_img = torch.randn(content_img.data.size())
377378

378379
# add the original input image to the figure:
379380
plt.figure()

beginner_source/examples_autograd/polynomial_autograd.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,23 @@
1818
import math
1919

2020
dtype = torch.float
21-
device = torch.device("cpu")
22-
# device = torch.device("cuda:0") # Uncomment this to run on GPU
21+
device = "cuda" if torch.cuda.is_available() else "cpu"
22+
torch.set_default_device(device)
2323

2424
# Create Tensors to hold input and outputs.
2525
# By default, requires_grad=False, which indicates that we do not need to
2626
# compute gradients with respect to these Tensors during the backward pass.
27-
x = torch.linspace(-math.pi, math.pi, 2000, device=device, dtype=dtype)
27+
x = torch.linspace(-math.pi, math.pi, 2000, dtype=dtype)
2828
y = torch.sin(x)
2929

3030
# Create random Tensors for weights. For a third order polynomial, we need
3131
# 4 weights: y = a + b x + c x^2 + d x^3
3232
# Setting requires_grad=True indicates that we want to compute gradients with
3333
# respect to these Tensors during the backward pass.
34-
a = torch.randn((), device=device, dtype=dtype, requires_grad=True)
35-
b = torch.randn((), device=device, dtype=dtype, requires_grad=True)
36-
c = torch.randn((), device=device, dtype=dtype, requires_grad=True)
37-
d = torch.randn((), device=device, dtype=dtype, requires_grad=True)
34+
a = torch.randn((), dtype=dtype, requires_grad=True)
35+
b = torch.randn((), dtype=dtype, requires_grad=True)
36+
c = torch.randn((), dtype=dtype, requires_grad=True)
37+
d = torch.randn((), dtype=dtype, requires_grad=True)
3838

3939
learning_rate = 1e-6
4040
for t in range(2000):

prototype_source/quantization_in_pytorch_2_0_export_tutorial.rst

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,54 +14,46 @@ have significantly higher model coverage, better programmability, and
1414
a simplified UX.
1515

1616
Prerequisites:
17-
-----------------------
17+
^^^^^^^^^^^^^^^^
1818

19-
- `Understanding of torchdynamo concepts in PyTorch <https://pytorch.org/docs/stable/dynamo/index.html>`__
20-
- `Understanding of the quantization concepts in PyTorch <https://pytorch.org/docs/master/quantization.html#quantization-api-summary>`__
21-
- `Understanding of FX Graph Mode post training static quantization <https://pytorch.org/tutorials/prototype/fx_graph_mode_ptq_static.html>`__
22-
- `Understanding of BackendConfig in PyTorch Quantization FX Graph Mode <https://pytorch.org/tutorials/prototype/backend_config_tutorial.html?highlight=backend>`__
23-
- `Understanding of QConfig and QConfigMapping in PyTorch Quantization FX Graph Mode <https://pytorch.org/tutorials/prototype/backend_config_tutorial.html#set-up-qconfigmapping-that-satisfies-the-backend-constraints>`__
19+
- `Torchdynamo concepts in PyTorch <https://pytorch.org/docs/stable/dynamo/index.html>`__
20+
- `Quantization concepts in PyTorch <https://pytorch.org/docs/master/quantization.html#quantization-api-summary>`__
21+
- `FX Graph Mode post training static quantization <https://pytorch.org/tutorials/prototype/fx_graph_mode_ptq_static.html>`__
22+
- `BackendConfig in PyTorch Quantization FX Graph Mode <https://pytorch.org/tutorials/prototype/backend_config_tutorial.html?highlight=backend>`__
23+
- `QConfig and QConfigMapping in PyTorch Quantization FX Graph Mode <https://pytorch.org/tutorials/prototype/backend_config_tutorial.html#set-up-qconfigmapping-that-satisfies-the-backend-constraints>`__
24+
25+
Introduction:
26+
^^^^^^^^^^^^^^^^
2427

2528
Previously in ``FX Graph Mode Quantization`` we were using ``QConfigMapping`` for users to specify how the model to be quantized
2629
and ``BackendConfig`` to specify the supported ways of quantization in their backend.
2730
This API covers most use cases relatively well, but the main problem is that this API is not fully extensible
2831
without involvement of the quantization team:
2932

30-
- This API has limitation around expressing quantization intentions for complicated operator patterns such as in the discussion of
31-
`Issue-96288 <https://github.com/pytorch/pytorch/issues/96288>`__ to support ``conv add`` fusion.
32-
Supporting ``conv add`` fusion also requires some changes to current already complicated pattern matching code such as in the
33-
`PR-97122 <https://github.com/pytorch/pytorch/pull/97122>`__.
34-
- This API also has limitation around supporting user's advanced quantization intention to quantize their model. For example, if backend
35-
developer only wants to quantize inputs and outputs when the ``linear`` has a third input, it requires co-work from quantization
36-
team and backend developer.
37-
- This API uses ``QConfigMapping`` and ``BackendConfig`` as separate object. ``QConfigMapping`` describes user's
38-
intention of how they want their model to be quantized. ``BackendConfig`` describes what kind of quantization a backend support.
39-
``BackendConfig`` is backend specific, but ``QConfigMapping`` is not. And user can provide a ``QConfigMapping``
40-
that is incompatible with a specific ``BackendConfig``. This is not a great UX. Ideally, we can structure this better
41-
by making both configuration (``QConfigMapping``) and quantization capability (``BackendConfig``) backend
42-
specific. So there will be less confusion about incompatibilities.
43-
- In ``QConfig``, we are exposing observer/fake_quant classes as an object for user to configure quantization.
44-
This increases the things that user needs to care about, e.g. not only the ``dtype`` but also how the
45-
observation should happen. These could potentially be hidden from user to make user interface simpler.
46-
47-
To address these scalability issues,
33+
- This API has limitation to support advanced quantization intention and complicated quantization operator patterns
34+
as in the discussion of `Issue-96288 <https://github.com/pytorch/pytorch/issues/96288>`__ to support ``conv add`` fusion.
35+
- This API uses ``QConfigMapping`` and ``BackendConfig`` as separate object in quantization configuration
36+
which may cause confusion about incompatibilities between these two objects. Also these quantization configurations require
37+
too much quantization details users need to know which can be hidden from user interface to make it simpler.
38+
39+
To address these issues,
4840
`Quantizer <https://github.com/pytorch/pytorch/blob/3e988316b5976df560c51c998303f56a234a6a1f/torch/ao/quantization/_pt2e/quantizer/quantizer.py#L160>`__
4941
is introduced for quantization in PyTorch 2.0 export. ``Quantizer`` is a class that users can use to
5042
programmatically set the quantization specifications for input and output of each node in the model graph. It adds flexibility
5143
to the quantization API and allows modeling users and backend developers to configure quantization programmatically.
5244
This will allow users to express how they want an operator pattern to be observed in a more explicit
53-
way by annotating the appropriate nodes. A backend specific quantizer inherited from base quantizer,
54-
some methods that need to be implemented:
55-
56-
- `annotate method <https://github.com/pytorch/pytorch/blob/3e988316b5976df560c51c998303f56a234a6a1f/torch/ao/quantization/_pt2e/quantizer/qnnpack_quantizer.py#L269>`__
57-
is used to annotate nodes in the graph with
58-
`QuantizationAnnotation <https://github.com/pytorch/pytorch/blob/07104ca99c9d297975270fb58fda786e60b49b38/torch/ao/quantization/_pt2e/quantizer/quantizer.py#L144>`__
59-
objects to convey the desired way of quantization.
45+
way by annotating the appropriate nodes.
6046

6147
Imagine a backend developer who wishes to integrate a third-party backend
6248
with PyTorch's quantization 2.0 flow. To accomplish this, they would only need
63-
to define the backend specific quantizer. The high level architecture of
64-
quantization 2.0 with quantizer could look like this:
49+
to define the backend specific quantizer. A backend specific quantizer inherited from base quantizer.
50+
The main method that need to be implemented for the backend specific quantizer is the
51+
`annotate method <https://github.com/pytorch/pytorch/blob/3e988316b5976df560c51c998303f56a234a6a1f/torch/ao/quantization/_pt2e/quantizer/qnnpack_quantizer.py#L269>`__
52+
which is used to annotate nodes in the graph with
53+
`QuantizationAnnotation <https://github.com/pytorch/pytorch/blob/07104ca99c9d297975270fb58fda786e60b49b38/torch/ao/quantization/_pt2e/quantizer/quantizer.py#L144>`__
54+
objects to convey the desired way of quantization.
55+
56+
The high level architecture of quantization 2.0 with quantizer could look like this:
6557

6658
::
6759

@@ -136,6 +128,9 @@ Taking QNNPackQuantizer as an example, the overall Quantization 2.0 flow could b
136128

137129
# Step 4: Lower Reference Quantized Model into the backend
138130

131+
Annotation API:
132+
^^^^^^^^^^^^^^^^^^^
133+
139134
``Quantizer`` uses annotation API to convey quantization intent for different operators/patterns.
140135
Annotation API mainly consists of
141136
`QuantizationSpec <https://github.com/pytorch/pytorch/blob/1ca2e993af6fa6934fca35da6970308ce227ddc7/torch/ao/quantization/_pt2e/quantizer/quantizer.py#L38>`__
@@ -366,8 +361,8 @@ functions that are used in the example:
366361
`get_bias_qspec <https://github.com/pytorch/pytorch/blob/47cfcf566ab76573452787335f10c9ca185752dc/torch/ao/quantization/_pt2e/quantizer/utils.py#L53>`__
367362
can be used to get the ``QuantizationSpec`` from ``QuantizationConfig`` for a specific pattern.
368363

369-
6. Conclusion
370-
---------------------
364+
Conclusion
365+
^^^^^^^^^^^^^^^^^^^
371366

372367
With this tutorial, we introduce the new quantization path in PyTorch 2.0. Users can learn about
373368
how to define a ``BackendQuantizer`` with the ``QuantizationAnnotation API`` and integrate it into the quantization 2.0 flow.

recipes_source/recipes/amp_recipe.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,14 @@ def make_model(in_size, out_size, num_layers):
7676
num_batches = 50
7777
epochs = 3
7878

79+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
80+
torch.set_default_device(device)
81+
7982
# Creates data in default precision.
8083
# The same data is used for both default and mixed precision trials below.
8184
# You don't need to manually change inputs' ``dtype`` when enabling mixed precision.
82-
data = [torch.randn(batch_size, in_size, device="cuda") for _ in range(num_batches)]
83-
targets = [torch.randn(batch_size, out_size, device="cuda") for _ in range(num_batches)]
85+
data = [torch.randn(batch_size, in_size) for _ in range(num_batches)]
86+
targets = [torch.randn(batch_size, out_size) for _ in range(num_batches)]
8487

8588
loss_fn = torch.nn.MSELoss().cuda()
8689

@@ -116,7 +119,7 @@ def make_model(in_size, out_size, num_layers):
116119
for epoch in range(0): # 0 epochs, this section is for illustration only
117120
for input, target in zip(data, targets):
118121
# Runs the forward pass under ``autocast``.
119-
with torch.autocast(device_type='cuda', dtype=torch.float16):
122+
with torch.autocast(device_type=device, dtype=torch.float16):
120123
output = net(input)
121124
# output is float16 because linear layers ``autocast`` to float16.
122125
assert output.dtype is torch.float16
@@ -151,7 +154,7 @@ def make_model(in_size, out_size, num_layers):
151154

152155
for epoch in range(0): # 0 epochs, this section is for illustration only
153156
for input, target in zip(data, targets):
154-
with torch.autocast(device_type='cuda', dtype=torch.float16):
157+
with torch.autocast(device_type=device, dtype=torch.float16):
155158
output = net(input)
156159
loss = loss_fn(output, target)
157160

@@ -184,7 +187,7 @@ def make_model(in_size, out_size, num_layers):
184187
start_timer()
185188
for epoch in range(epochs):
186189
for input, target in zip(data, targets):
187-
with torch.autocast(device_type='cuda', dtype=torch.float16, enabled=use_amp):
190+
with torch.autocast(device_type=device, dtype=torch.float16, enabled=use_amp):
188191
output = net(input)
189192
loss = loss_fn(output, target)
190193
scaler.scale(loss).backward()
@@ -202,7 +205,7 @@ def make_model(in_size, out_size, num_layers):
202205

203206
for epoch in range(0): # 0 epochs, this section is for illustration only
204207
for input, target in zip(data, targets):
205-
with torch.autocast(device_type='cuda', dtype=torch.float16):
208+
with torch.autocast(device_type=device, dtype=torch.float16):
206209
output = net(input)
207210
loss = loss_fn(output, target)
208211
scaler.scale(loss).backward()

recipes_source/recipes/tuning_guide.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ def fused_gelu(x):
357357
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
358358
# Instead of calling ``torch.rand(size).cuda()`` to generate a random tensor,
359359
# produce the output directly on the target device:
360-
# ``torch.rand(size, device=torch.device('cuda'))``.
360+
# ``torch.rand(size, device='cuda')``.
361361
#
362362
# This is applicable to all functions which create new tensors and accept
363363
# ``device`` argument:

0 commit comments

Comments
 (0)