Skip to content

Commit

Permalink
Bootstrap examples for PyTorch in quick start (#3655)
Browse files Browse the repository at this point in the history
Adding bootstrap example

Signed-off-by: Priyanka Dangi <quic_pdangi@quicinc.com>
  • Loading branch information
quic-hitameht authored Dec 17, 2024
1 parent d531a9f commit 86daca1
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 2 deletions.
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ignore=CVS,
apply_bn.py,
create_quantizationsimmodel.py,
evaluate.py,
installation_verification.py,
export.py,
pass_calibration_data.py,
prepare_model.py
Expand Down
79 changes: 77 additions & 2 deletions Docs/beta/install/quick-start.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,87 @@ Type the following command to install AIMET for PyTorch framework using pip pack
python3 -m pip install "aimet-torch>=2"
Next steps
Verification
==========

See `Simple example` to test your installation.
Type the following command to ensure AIMET is installed via pip.

.. code-block:: bash
python3 -m pip show aimet-torch
If installed properly, this command will produce no warnings and display information about the package.


Let's run some sample PyTorch code to confirm that we can create QuantSim and perform calibration:

**Step 1**: Let's handle imports and other setup.

.. literalinclude:: ../snippets/torch/installation_verification.py
:language: python
:start-after: [step_1]
:end-before: [step_2]

**Step 2**: We will create QuantSim and ensure the model contains quantization ops.

.. literalinclude:: ../snippets/torch/installation_verification.py
:language: python
:start-after: [step_2]
:end-before: [step_3]

The model should be composed of Quantized nn.Modules, similar to the output shown below:

::

MobileNetV2(
(features): Sequential(
(0): Conv2dNormActivation(
(0): QuantizedConv2d(
3, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False
(param_quantizers): ModuleDict(
(weight): QuantizeDequantize(shape=(32, 1, 1, 1), qmin=-128, qmax=127, symmetric=True)
)
(input_quantizers): ModuleList(
(0): QuantizeDequantize(shape=(), qmin=0, qmax=65535, symmetric=False)
)
(output_quantizers): ModuleList(
(0): None
)
)
)
...
)


**Step 3**: We perform calibration. As a proof of concept, random input is being passed in. However, dataloaders are commonly used in real world cases.

.. literalinclude:: ../snippets/torch/installation_verification.py
:language: python
:start-after: [step_3]
:end-before: [step_4]

**Step 4**: We perform evaluation.

.. literalinclude:: ../snippets/torch/installation_verification.py
:language: python
:start-after: [step_4]

The output generated should be of type DequantizedTensor and similar to the one shown below.

::

DequantizedTensor([[-1.7466, 0.8405, 1.8606, ..., -0.9714, 0.8366, 2.2363],
[-1.6091, 1.0449, 1.7788, ..., -0.9904, 1.0861, 2.2431],
[-1.5307, 0.8442, 1.5157, ..., -0.7793, 0.6327, 2.3861],
...,
[-1.3610, 1.4499, 2.2068, ..., -0.8188, 1.1155, 2.5962],
[-1.1619, 1.2217, 2.1050, ..., -0.5301, 0.9150, 2.1458],
[-1.6340, 0.9826, 2.2459, ..., -1.0769, 0.9054, 2.2315]],
device='cuda:0', grad_fn=<AliasBackward0>)


See the :ref:`User guide <opt-guide-index>` to read about the model optimization workflow.

See the :ref:`Examples <examples-index>` to try AIMET quantization techniques on your pre-trained models.


66 changes: 66 additions & 0 deletions Docs/beta/snippets/torch/installation_verification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- mode: python -*-
# =============================================================================
# @@-COPYRIGHT-START-@@
#
# Copyright (c) 2024, Qualcomm Innovation Center, Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# SPDX-License-Identifier: BSD-3-Clause
#
# @@-COPYRIGHT-END-@@
# =============================================================================
# pylint: disable=missing-docstring
[step_1]
import torch
from torchvision.models import mobilenet_v2

device = "cuda:0" if torch.cuda.is_available() else "cpu"
model = mobilenet_v2(weights='DEFAULT').eval().to(device)
dummy_input = torch.randn((10, 3, 224, 224), device=device)
[step_2]
from aimet_torch.v2.quantsim import QuantizationSimModel
from aimet_common.defs import QuantScheme
from aimet_common.quantsim_config.utils import get_path_for_per_channel_config

sim = QuantizationSimModel(model,
dummy_input,
quant_scheme=QuantScheme.training_range_learning_with_tf_init,
config_file=get_path_for_per_channel_config(),
default_param_bw=8,
default_output_bw=16)

print(sim)
[step_3]
def forward_pass(model):
with torch.no_grad():
model(torch.randn((10, 3, 224, 224), device=device))

sim.compute_encodings(forward_pass)
[step_4]
output = sim.model(dummy_input)
print(output)

0 comments on commit 86daca1

Please sign in to comment.