We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
QuantizableMobileNetV3 Can not load pretrained model
### 🐛 Describe the bug

```python

import torchvision
quantized = torchvision.models.quantization.mobilenet_v3_large(pretrained=True)
```
It will occur
```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/conda/lib/python3.8/site-packages/torchvision/models/quantization/mobilenetv3.py", line 180, in mobilenet_v3_large
    return _mobilenet_v3_model(arch, inverted_residual_setting, last_channel, pretrained, progress, quantize, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/torchvision/models/quantization/mobilenetv3.py", line 154, in _mobilenet_v3_model
    _load_weights(arch, model, model_urls.get(arch, None), progress)
  File "/opt/conda/lib/python3.8/site-packages/torchvision/models/quantization/mobilenetv3.py", line 124, in _load_weights
    model.load_state_dict(state_dict)
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1482, in load_state_dict
    raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for QuantizableMobileNetV3:
	Unexpected key(s) in state_dict: "features.4.block.2.scale_activation.activation_post_process.scale", "features.4.block.2.scale_activation.activation_post_process.zero_point", "features.4.block.2.scale_activation.activation_post_process.fake_quant_enabled", "features.4.block.2.scale_activation.activation_post_process.observer_enabled", "features.5.block.2.scale_activation.activation_post_process.scale", "features.5.block.2.scale_activation.activation_post_process.zero_point", "features.5.block.2.scale_activation.activation_post_process.fake_quant_enabled", "features.5.block.2.scale_activation.activation_post_process.observer_enabled", "features.6.block.2.scale_activation.activation_post_process.scale", "features.6.block.2.scale_activation.activation_post_process.zero_point", "features.6.block.2.scale_activation.activation_post_process.fake_quant_enabled", "features.6.block.2.scale_activation.activation_post_process.observer_enabled", "features.11.block.2.scale_activation.activation_post_process.scale", "features.11.block.2.scale_activation.activation_post_process.zero_point", "features.11.block.2.scale_activation.activation_post_process.fake_quant_enabled", "features.11.block.2.scale_activation.activation_post_process.observer_enabled", "features.12.block.2.scale_activation.activation_post_process.scale", "features.12.block.2.scale_activation.activation_post_process.zero_point", "features.12.block.2.scale_activation.activation_post_process.fake_quant_enabled", "features.12.block.2.scale_activation.activation_post_process.observer_enabled", "features.13.block.2.scale_activation.activation_post_process.scale", "features.13.block.2.scale_activation.activation_post_process.zero_point", "features.13.block.2.scale_activation.activation_post_process.fake_quant_enabled", "features.13.block.2.scale_activation.activation_post_process.observer_enabled", "features.14.block.2.scale_activation.activation_post_process.scale", "features.14.block.2.scale_activation.activation_post_process.zero_point", "features.14.block.2.scale_activation.activation_post_process.fake_quant_enabled", "features.14.block.2.scale_activation.activation_post_process.observer_enabled", "features.15.block.2.scale_activation.activation_post_process.scale", "features.15.block.2.scale_activation.activation_post_process.zero_point", "features.15.block.2.scale_activation.activation_post_process.fake_quant_enabled", "features.15.block.2.scale_activation.activation_post_process.observer_enabled".
```

### Versions

PyTorch version: 1.10.0+cu113
Is debug build: False
CUDA used to build PyTorch: 11.3
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.1 LTS (x86_64)
GCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Clang version: Could not collect
CMake version: version 3.19.4
Libc version: glibc-2.31

Python version: 3.8.5 (default, Sep  4 2020, 07:30:14)  [GCC 7.3.0] (64-bit runtime)
Python platform: Linux-5.4.0-90-generic-x86_64-with-glibc2.10
Is CUDA available: True
CUDA runtime version: 11.2.67
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3090
Nvidia driver version: 470.82.00
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.1.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.1.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.1.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.1.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.1.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.1.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.1.0
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.2
[pip3] pytorch-transformers==1.1.0
[pip3] torch==1.10.0+cu113
[pip3] torchaudio==0.10.0+cu113
[pip3] torchtext==0.11.0
[pip3] torchvision==0.11.1+cu113
[conda] magma-cuda110             2.5.2                         5    local
[conda] mkl                       2019.4                      243
[conda] mkl-include               2019.4                      243
[conda] nomkl                     3.0                           0
[conda] numpy                     1.19.2           py38h6163131_0
[conda] numpy-base                1.19.2           py38h75fe3a5_0
[conda] pytorch-transformers      1.1.0                    pypi_0    pypi
[conda] torch                     1.10.0+cu113             pypi_0    pypi
[conda] torchaudio                0.10.0+cu113             pypi_0    pypi
[conda] torchtext                 0.11.0                   pypi_0    pypi
[conda] torchvision               0.11.1+cu113             pypi_0    pypi

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `torchvision/models/quantization/mobilenetv3.py`
Content:
```
1 from typing import Any, List, Optional
2 
3 import torch
4 from torch import nn, Tensor
5 from torch.quantization import QuantStub, DeQuantStub, fuse_modules
6 
7 from ..._internally_replaced_utils import load_state_dict_from_url
8 from ...ops.misc import ConvNormActivation, SqueezeExcitation
9 from ..mobilenetv3 import InvertedResidual, InvertedResidualConfig, MobileNetV3, model_urls, _mobilenet_v3_conf
10 from .utils import _replace_relu
11 
12 
13 __all__ = ["QuantizableMobileNetV3", "mobilenet_v3_large"]
14 
15 quant_model_urls = {
16     "mobilenet_v3_large_qnnpack": "https://download.pytorch.org/models/quantized/mobilenet_v3_large_qnnpack-5bcacf28.pth",
17 }
18 
19 
20 class QuantizableSqueezeExcitation(SqueezeExcitation):
21     _version = 2
22 
23     def __init__(self, *args: Any, **kwargs: Any) -> None:
24         kwargs["scale_activation"] = nn.Hardsigmoid
25         super().__init__(*args, **kwargs)
26         self.skip_mul = nn.quantized.FloatFunctional()
27 
28     def forward(self, input: Tensor) -> Tensor:
29         return self.skip_mul.mul(self._scale(input), input)
30 
31     def fuse_model(self) -> None:
32         fuse_modules(self, ["fc1", "activation"], inplace=True)
33 
34     def _load_from_state_dict(
35         self,
36         state_dict,
37         prefix,
38         local_metadata,
39         strict,
40         missing_keys,
41         unexpected_keys,
42         error_msgs,
43     ):
44         version = local_metadata.get("version", None)
45 
46         if version is None or version < 2:
47             default_state_dict = {
48                 "scale_activation.activation_post_process.scale": torch.tensor([1.0]),
49                 "scale_activation.activation_post_process.zero_point": torch.tensor([0], dtype=torch.int32),
50                 "scale_activation.activation_post_process.fake_quant_enabled": torch.tensor([1]),
51                 "scale_activation.activation_post_process.observer_enabled": torch.tensor([1]),
52             }
53             for k, v in default_state_dict.items():
54                 full_key = prefix + k
55                 if full_key not in state_dict:
56                     state_dict[full_key] = v
57 
58         super()._load_from_state_dict(
59             state_dict,
60             prefix,
61             local_metadata,
62             strict,
63             missing_keys,
64             unexpected_keys,
65             error_msgs,
66         )
67 
68 
69 class QuantizableInvertedResidual(InvertedResidual):
70     # TODO https://github.com/pytorch/vision/pull/4232#pullrequestreview-730461659
71     def __init__(self, *args: Any, **kwargs: Any) -> None:
72         super().__init__(se_layer=QuantizableSqueezeExcitation, *args, **kwargs)  # type: ignore[misc]
73         self.skip_add = nn.quantized.FloatFunctional()
74 
75     def forward(self, x: Tensor) -> Tensor:
76         if self.use_res_connect:
77             return self.skip_add.add(x, self.block(x))
78         else:
79             return self.block(x)
80 
81 
82 class QuantizableMobileNetV3(MobileNetV3):
83     def __init__(self, *args: Any, **kwargs: Any) -> None:
84         """
85         MobileNet V3 main class
86 
87         Args:
88            Inherits args from floating point MobileNetV3
89         """
90         super().__init__(*args, **kwargs)
91         self.quant = QuantStub()
92         self.dequant = DeQuantStub()
93 
94     def forward(self, x: Tensor) -> Tensor:
95         x = self.quant(x)
96         x = self._forward_impl(x)
97         x = self.dequant(x)
98         return x
99 
100     def fuse_model(self) -> None:
101         for m in self.modules():
102             if type(m) is ConvNormActivation:
103                 modules_to_fuse = ["0", "1"]
104                 if len(m) == 3 and type(m[2]) is nn.ReLU:
105                     modules_to_fuse.append("2")
106                 fuse_modules(m, modules_to_fuse, inplace=True)
107             elif type(m) is QuantizableSqueezeExcitation:
108                 m.fuse_model()
109 
110 
111 def _load_weights(arch: str, model: QuantizableMobileNetV3, model_url: Optional[str], progress: bool) -> None:
112     if model_url is None:
113         raise ValueError(f"No checkpoint is available for {arch}")
114     state_dict = load_state_dict_from_url(model_url, progress=progress)
115     model.load_state_dict(state_dict)
116 
117 
118 def _mobilenet_v3_model(
119     arch: str,
120     inverted_residual_setting: List[InvertedResidualConfig],
121     last_channel: int,
122     pretrained: bool,
123     progress: bool,
124     quantize: bool,
125     **kwargs: Any,
126 ) -> QuantizableMobileNetV3:
127 
128     model = QuantizableMobileNetV3(inverted_residual_setting, last_channel, block=QuantizableInvertedResidual, **kwargs)
129     _replace_relu(model)
130 
131     if quantize:
132         backend = "qnnpack"
133 
134         model.fuse_model()
135         model.qconfig = torch.quantization.get_default_qat_qconfig(backend)
136         torch.quantization.prepare_qat(model, inplace=True)
137 
138         if pretrained:
139             _load_weights(arch, model, quant_model_urls.get(arch + "_" + backend, None), progress)
140 
141         torch.quantization.convert(model, inplace=True)
142         model.eval()
143     else:
144         if pretrained:
145             _load_weights(arch, model, model_urls.get(arch, None), progress)
146 
147     return model
148 
149 
150 def mobilenet_v3_large(
151     pretrained: bool = False,
152     progress: bool = True,
153     quantize: bool = False,
154     **kwargs: Any,
155 ) -> QuantizableMobileNetV3:
156     """
157     Constructs a MobileNetV3 Large architecture from
158     `"Searching for MobileNetV3" <https://arxiv.org/abs/1905.02244>`_.
159 
160     Note that quantize = True returns a quantized model with 8 bit
161     weights. Quantized models only support inference and run on CPUs.
162     GPU inference is not yet supported
163 
164     Args:
165      pretrained (bool): If True, returns a model pre-trained on ImageNet.
166      progress (bool): If True, displays a progress bar of the download to stderr
167      quantize (bool): If True, returns a quantized model, else returns a float model
168     """
169     arch = "mobilenet_v3_large"
170     inverted_residual_setting, last_channel = _mobilenet_v3_conf(arch, **kwargs)
171     return _mobilenet_v3_model(arch, inverted_residual_setting, last_channel, pretrained, progress, quantize, **kwargs)
172
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/torchvision/models/quantization/mobilenetv3.py b/torchvision/models/quantization/mobilenetv3.py
--- a/torchvision/models/quantization/mobilenetv3.py
+++ b/torchvision/models/quantization/mobilenetv3.py
@@ -43,7 +43,7 @@
     ):
         version = local_metadata.get("version", None)
 
-        if version is None or version < 2:
+        if hasattr(self, "qconfig") and (version is None or version < 2):
             default_state_dict = {
                 "scale_activation.activation_post_process.scale": torch.tensor([1.0]),
                 "scale_activation.activation_post_process.zero_point": torch.tensor([0], dtype=torch.int32),

{"golden_diff": "diff --git a/torchvision/models/quantization/mobilenetv3.py b/torchvision/models/quantization/mobilenetv3.py\n--- a/torchvision/models/quantization/mobilenetv3.py\n+++ b/torchvision/models/quantization/mobilenetv3.py\n@@ -43,7 +43,7 @@\n     ):\n         version = local_metadata.get(\"version\", None)\n \n-        if version is None or version < 2:\n+        if hasattr(self, \"qconfig\") and (version is None or version < 2):\n             default_state_dict = {\n                 \"scale_activation.activation_post_process.scale\": torch.tensor([1.0]),\n                 \"scale_activation.activation_post_process.zero_point\": torch.tensor([0], dtype=torch.int32),\n", "issue": "QuantizableMobileNetV3 Can not load pretrained model\n### \ud83d\udc1b Describe the bug\n\n```python\r\n\r\nimport torchvision\r\nquantized = torchvision.models.quantization.mobilenet_v3_large(pretrained=True)\r\n```\r\nIt will occur\r\n```\r\nTraceback (most recent call last):\r\n  File \"<stdin>\", line 1, in <module>\r\n  File \"/opt/conda/lib/python3.8/site-packages/torchvision/models/quantization/mobilenetv3.py\", line 180, in mobilenet_v3_large\r\n    return _mobilenet_v3_model(arch, inverted_residual_setting, last_channel, pretrained, progress, quantize, **kwargs)\r\n  File \"/opt/conda/lib/python3.8/site-packages/torchvision/models/quantization/mobilenetv3.py\", line 154, in _mobilenet_v3_model\r\n    _load_weights(arch, model, model_urls.get(arch, None), progress)\r\n  File \"/opt/conda/lib/python3.8/site-packages/torchvision/models/quantization/mobilenetv3.py\", line 124, in _load_weights\r\n    model.load_state_dict(state_dict)\r\n  File \"/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py\", line 1482, in load_state_dict\r\n    raise RuntimeError('Error(s) in loading state_dict for {}:\\n\\t{}'.format(\r\nRuntimeError: Error(s) in loading state_dict for QuantizableMobileNetV3:\r\n\tUnexpected key(s) in state_dict: \"features.4.block.2.scale_activation.activation_post_process.scale\", \"features.4.block.2.scale_activation.activation_post_process.zero_point\", \"features.4.block.2.scale_activation.activation_post_process.fake_quant_enabled\", \"features.4.block.2.scale_activation.activation_post_process.observer_enabled\", \"features.5.block.2.scale_activation.activation_post_process.scale\", \"features.5.block.2.scale_activation.activation_post_process.zero_point\", \"features.5.block.2.scale_activation.activation_post_process.fake_quant_enabled\", \"features.5.block.2.scale_activation.activation_post_process.observer_enabled\", \"features.6.block.2.scale_activation.activation_post_process.scale\", \"features.6.block.2.scale_activation.activation_post_process.zero_point\", \"features.6.block.2.scale_activation.activation_post_process.fake_quant_enabled\", \"features.6.block.2.scale_activation.activation_post_process.observer_enabled\", \"features.11.block.2.scale_activation.activation_post_process.scale\", \"features.11.block.2.scale_activation.activation_post_process.zero_point\", \"features.11.block.2.scale_activation.activation_post_process.fake_quant_enabled\", \"features.11.block.2.scale_activation.activation_post_process.observer_enabled\", \"features.12.block.2.scale_activation.activation_post_process.scale\", \"features.12.block.2.scale_activation.activation_post_process.zero_point\", \"features.12.block.2.scale_activation.activation_post_process.fake_quant_enabled\", \"features.12.block.2.scale_activation.activation_post_process.observer_enabled\", \"features.13.block.2.scale_activation.activation_post_process.scale\", \"features.13.block.2.scale_activation.activation_post_process.zero_point\", \"features.13.block.2.scale_activation.activation_post_process.fake_quant_enabled\", \"features.13.block.2.scale_activation.activation_post_process.observer_enabled\", \"features.14.block.2.scale_activation.activation_post_process.scale\", \"features.14.block.2.scale_activation.activation_post_process.zero_point\", \"features.14.block.2.scale_activation.activation_post_process.fake_quant_enabled\", \"features.14.block.2.scale_activation.activation_post_process.observer_enabled\", \"features.15.block.2.scale_activation.activation_post_process.scale\", \"features.15.block.2.scale_activation.activation_post_process.zero_point\", \"features.15.block.2.scale_activation.activation_post_process.fake_quant_enabled\", \"features.15.block.2.scale_activation.activation_post_process.observer_enabled\".\r\n```\n\n### Versions\n\nPyTorch version: 1.10.0+cu113\r\nIs debug build: False\r\nCUDA used to build PyTorch: 11.3\r\nROCM used to build PyTorch: N/A\r\n\r\nOS: Ubuntu 20.04.1 LTS (x86_64)\r\nGCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0\r\nClang version: Could not collect\r\nCMake version: version 3.19.4\r\nLibc version: glibc-2.31\r\n\r\nPython version: 3.8.5 (default, Sep  4 2020, 07:30:14)  [GCC 7.3.0] (64-bit runtime)\r\nPython platform: Linux-5.4.0-90-generic-x86_64-with-glibc2.10\r\nIs CUDA available: True\r\nCUDA runtime version: 11.2.67\r\nGPU models and configuration: GPU 0: NVIDIA GeForce RTX 3090\r\nNvidia driver version: 470.82.00\r\ncuDNN version: Probably one of the following:\r\n/usr/lib/x86_64-linux-gnu/libcudnn.so.8.1.0\r\n/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.1.0\r\n/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.1.0\r\n/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.1.0\r\n/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.1.0\r\n/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.1.0\r\n/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.1.0\r\nHIP runtime version: N/A\r\nMIOpen runtime version: N/A\r\n\r\nVersions of relevant libraries:\r\n[pip3] numpy==1.19.2\r\n[pip3] pytorch-transformers==1.1.0\r\n[pip3] torch==1.10.0+cu113\r\n[pip3] torchaudio==0.10.0+cu113\r\n[pip3] torchtext==0.11.0\r\n[pip3] torchvision==0.11.1+cu113\r\n[conda] magma-cuda110             2.5.2                         5    local\r\n[conda] mkl                       2019.4                      243\r\n[conda] mkl-include               2019.4                      243\r\n[conda] nomkl                     3.0                           0\r\n[conda] numpy                     1.19.2           py38h6163131_0\r\n[conda] numpy-base                1.19.2           py38h75fe3a5_0\r\n[conda] pytorch-transformers      1.1.0                    pypi_0    pypi\r\n[conda] torch                     1.10.0+cu113             pypi_0    pypi\r\n[conda] torchaudio                0.10.0+cu113             pypi_0    pypi\r\n[conda] torchtext                 0.11.0                   pypi_0    pypi\r\n[conda] torchvision               0.11.1+cu113             pypi_0    pypi\n", "before_files": [{"content": "from typing import Any, List, Optional\n\nimport torch\nfrom torch import nn, Tensor\nfrom torch.quantization import QuantStub, DeQuantStub, fuse_modules\n\nfrom ..._internally_replaced_utils import load_state_dict_from_url\nfrom ...ops.misc import ConvNormActivation, SqueezeExcitation\nfrom ..mobilenetv3 import InvertedResidual, InvertedResidualConfig, MobileNetV3, model_urls, _mobilenet_v3_conf\nfrom .utils import _replace_relu\n\n\n__all__ = [\"QuantizableMobileNetV3\", \"mobilenet_v3_large\"]\n\nquant_model_urls = {\n    \"mobilenet_v3_large_qnnpack\": \"https://download.pytorch.org/models/quantized/mobilenet_v3_large_qnnpack-5bcacf28.pth\",\n}\n\n\nclass QuantizableSqueezeExcitation(SqueezeExcitation):\n    _version = 2\n\n    def __init__(self, *args: Any, **kwargs: Any) -> None:\n        kwargs[\"scale_activation\"] = nn.Hardsigmoid\n        super().__init__(*args, **kwargs)\n        self.skip_mul = nn.quantized.FloatFunctional()\n\n    def forward(self, input: Tensor) -> Tensor:\n        return self.skip_mul.mul(self._scale(input), input)\n\n    def fuse_model(self) -> None:\n        fuse_modules(self, [\"fc1\", \"activation\"], inplace=True)\n\n    def _load_from_state_dict(\n        self,\n        state_dict,\n        prefix,\n        local_metadata,\n        strict,\n        missing_keys,\n        unexpected_keys,\n        error_msgs,\n    ):\n        version = local_metadata.get(\"version\", None)\n\n        if version is None or version < 2:\n            default_state_dict = {\n                \"scale_activation.activation_post_process.scale\": torch.tensor([1.0]),\n                \"scale_activation.activation_post_process.zero_point\": torch.tensor([0], dtype=torch.int32),\n                \"scale_activation.activation_post_process.fake_quant_enabled\": torch.tensor([1]),\n                \"scale_activation.activation_post_process.observer_enabled\": torch.tensor([1]),\n            }\n            for k, v in default_state_dict.items():\n                full_key = prefix + k\n                if full_key not in state_dict:\n                    state_dict[full_key] = v\n\n        super()._load_from_state_dict(\n            state_dict,\n            prefix,\n            local_metadata,\n            strict,\n            missing_keys,\n            unexpected_keys,\n            error_msgs,\n        )\n\n\nclass QuantizableInvertedResidual(InvertedResidual):\n    # TODO https://github.com/pytorch/vision/pull/4232#pullrequestreview-730461659\n    def __init__(self, *args: Any, **kwargs: Any) -> None:\n        super().__init__(se_layer=QuantizableSqueezeExcitation, *args, **kwargs)  # type: ignore[misc]\n        self.skip_add = nn.quantized.FloatFunctional()\n\n    def forward(self, x: Tensor) -> Tensor:\n        if self.use_res_connect:\n            return self.skip_add.add(x, self.block(x))\n        else:\n            return self.block(x)\n\n\nclass QuantizableMobileNetV3(MobileNetV3):\n    def __init__(self, *args: Any, **kwargs: Any) -> None:\n        \"\"\"\n        MobileNet V3 main class\n\n        Args:\n           Inherits args from floating point MobileNetV3\n        \"\"\"\n        super().__init__(*args, **kwargs)\n        self.quant = QuantStub()\n        self.dequant = DeQuantStub()\n\n    def forward(self, x: Tensor) -> Tensor:\n        x = self.quant(x)\n        x = self._forward_impl(x)\n        x = self.dequant(x)\n        return x\n\n    def fuse_model(self) -> None:\n        for m in self.modules():\n            if type(m) is ConvNormActivation:\n                modules_to_fuse = [\"0\", \"1\"]\n                if len(m) == 3 and type(m[2]) is nn.ReLU:\n                    modules_to_fuse.append(\"2\")\n                fuse_modules(m, modules_to_fuse, inplace=True)\n            elif type(m) is QuantizableSqueezeExcitation:\n                m.fuse_model()\n\n\ndef _load_weights(arch: str, model: QuantizableMobileNetV3, model_url: Optional[str], progress: bool) -> None:\n    if model_url is None:\n        raise ValueError(f\"No checkpoint is available for {arch}\")\n    state_dict = load_state_dict_from_url(model_url, progress=progress)\n    model.load_state_dict(state_dict)\n\n\ndef _mobilenet_v3_model(\n    arch: str,\n    inverted_residual_setting: List[InvertedResidualConfig],\n    last_channel: int,\n    pretrained: bool,\n    progress: bool,\n    quantize: bool,\n    **kwargs: Any,\n) -> QuantizableMobileNetV3:\n\n    model = QuantizableMobileNetV3(inverted_residual_setting, last_channel, block=QuantizableInvertedResidual, **kwargs)\n    _replace_relu(model)\n\n    if quantize:\n        backend = \"qnnpack\"\n\n        model.fuse_model()\n        model.qconfig = torch.quantization.get_default_qat_qconfig(backend)\n        torch.quantization.prepare_qat(model, inplace=True)\n\n        if pretrained:\n            _load_weights(arch, model, quant_model_urls.get(arch + \"_\" + backend, None), progress)\n\n        torch.quantization.convert(model, inplace=True)\n        model.eval()\n    else:\n        if pretrained:\n            _load_weights(arch, model, model_urls.get(arch, None), progress)\n\n    return model\n\n\ndef mobilenet_v3_large(\n    pretrained: bool = False,\n    progress: bool = True,\n    quantize: bool = False,\n    **kwargs: Any,\n) -> QuantizableMobileNetV3:\n    \"\"\"\n    Constructs a MobileNetV3 Large architecture from\n    `\"Searching for MobileNetV3\" <https://arxiv.org/abs/1905.02244>`_.\n\n    Note that quantize = True returns a quantized model with 8 bit\n    weights. Quantized models only support inference and run on CPUs.\n    GPU inference is not yet supported\n\n    Args:\n     pretrained (bool): If True, returns a model pre-trained on ImageNet.\n     progress (bool): If True, displays a progress bar of the download to stderr\n     quantize (bool): If True, returns a quantized model, else returns a float model\n    \"\"\"\n    arch = \"mobilenet_v3_large\"\n    inverted_residual_setting, last_channel = _mobilenet_v3_conf(arch, **kwargs)\n    return _mobilenet_v3_model(arch, inverted_residual_setting, last_channel, pretrained, progress, quantize, **kwargs)\n", "path": "torchvision/models/quantization/mobilenetv3.py"}], "after_files": [{"content": "from typing import Any, List, Optional\n\nimport torch\nfrom torch import nn, Tensor\nfrom torch.quantization import QuantStub, DeQuantStub, fuse_modules\n\nfrom ..._internally_replaced_utils import load_state_dict_from_url\nfrom ...ops.misc import ConvNormActivation, SqueezeExcitation\nfrom ..mobilenetv3 import InvertedResidual, InvertedResidualConfig, MobileNetV3, model_urls, _mobilenet_v3_conf\nfrom .utils import _replace_relu\n\n\n__all__ = [\"QuantizableMobileNetV3\", \"mobilenet_v3_large\"]\n\nquant_model_urls = {\n    \"mobilenet_v3_large_qnnpack\": \"https://download.pytorch.org/models/quantized/mobilenet_v3_large_qnnpack-5bcacf28.pth\",\n}\n\n\nclass QuantizableSqueezeExcitation(SqueezeExcitation):\n    _version = 2\n\n    def __init__(self, *args: Any, **kwargs: Any) -> None:\n        kwargs[\"scale_activation\"] = nn.Hardsigmoid\n        super().__init__(*args, **kwargs)\n        self.skip_mul = nn.quantized.FloatFunctional()\n\n    def forward(self, input: Tensor) -> Tensor:\n        return self.skip_mul.mul(self._scale(input), input)\n\n    def fuse_model(self) -> None:\n        fuse_modules(self, [\"fc1\", \"activation\"], inplace=True)\n\n    def _load_from_state_dict(\n        self,\n        state_dict,\n        prefix,\n        local_metadata,\n        strict,\n        missing_keys,\n        unexpected_keys,\n        error_msgs,\n    ):\n        version = local_metadata.get(\"version\", None)\n\n        if hasattr(self, \"qconfig\") and (version is None or version < 2):\n            default_state_dict = {\n                \"scale_activation.activation_post_process.scale\": torch.tensor([1.0]),\n                \"scale_activation.activation_post_process.zero_point\": torch.tensor([0], dtype=torch.int32),\n                \"scale_activation.activation_post_process.fake_quant_enabled\": torch.tensor([1]),\n                \"scale_activation.activation_post_process.observer_enabled\": torch.tensor([1]),\n            }\n            for k, v in default_state_dict.items():\n                full_key = prefix + k\n                if full_key not in state_dict:\n                    state_dict[full_key] = v\n\n        super()._load_from_state_dict(\n            state_dict,\n            prefix,\n            local_metadata,\n            strict,\n            missing_keys,\n            unexpected_keys,\n            error_msgs,\n        )\n\n\nclass QuantizableInvertedResidual(InvertedResidual):\n    # TODO https://github.com/pytorch/vision/pull/4232#pullrequestreview-730461659\n    def __init__(self, *args: Any, **kwargs: Any) -> None:\n        super().__init__(se_layer=QuantizableSqueezeExcitation, *args, **kwargs)  # type: ignore[misc]\n        self.skip_add = nn.quantized.FloatFunctional()\n\n    def forward(self, x: Tensor) -> Tensor:\n        if self.use_res_connect:\n            return self.skip_add.add(x, self.block(x))\n        else:\n            return self.block(x)\n\n\nclass QuantizableMobileNetV3(MobileNetV3):\n    def __init__(self, *args: Any, **kwargs: Any) -> None:\n        \"\"\"\n        MobileNet V3 main class\n\n        Args:\n           Inherits args from floating point MobileNetV3\n        \"\"\"\n        super().__init__(*args, **kwargs)\n        self.quant = QuantStub()\n        self.dequant = DeQuantStub()\n\n    def forward(self, x: Tensor) -> Tensor:\n        x = self.quant(x)\n        x = self._forward_impl(x)\n        x = self.dequant(x)\n        return x\n\n    def fuse_model(self) -> None:\n        for m in self.modules():\n            if type(m) is ConvNormActivation:\n                modules_to_fuse = [\"0\", \"1\"]\n                if len(m) == 3 and type(m[2]) is nn.ReLU:\n                    modules_to_fuse.append(\"2\")\n                fuse_modules(m, modules_to_fuse, inplace=True)\n            elif type(m) is QuantizableSqueezeExcitation:\n                m.fuse_model()\n\n\ndef _load_weights(arch: str, model: QuantizableMobileNetV3, model_url: Optional[str], progress: bool) -> None:\n    if model_url is None:\n        raise ValueError(f\"No checkpoint is available for {arch}\")\n    state_dict = load_state_dict_from_url(model_url, progress=progress)\n    model.load_state_dict(state_dict)\n\n\ndef _mobilenet_v3_model(\n    arch: str,\n    inverted_residual_setting: List[InvertedResidualConfig],\n    last_channel: int,\n    pretrained: bool,\n    progress: bool,\n    quantize: bool,\n    **kwargs: Any,\n) -> QuantizableMobileNetV3:\n\n    model = QuantizableMobileNetV3(inverted_residual_setting, last_channel, block=QuantizableInvertedResidual, **kwargs)\n    _replace_relu(model)\n\n    if quantize:\n        backend = \"qnnpack\"\n\n        model.fuse_model()\n        model.qconfig = torch.quantization.get_default_qat_qconfig(backend)\n        torch.quantization.prepare_qat(model, inplace=True)\n\n        if pretrained:\n            _load_weights(arch, model, quant_model_urls.get(arch + \"_\" + backend, None), progress)\n\n        torch.quantization.convert(model, inplace=True)\n        model.eval()\n    else:\n        if pretrained:\n            _load_weights(arch, model, model_urls.get(arch, None), progress)\n\n    return model\n\n\ndef mobilenet_v3_large(\n    pretrained: bool = False,\n    progress: bool = True,\n    quantize: bool = False,\n    **kwargs: Any,\n) -> QuantizableMobileNetV3:\n    \"\"\"\n    Constructs a MobileNetV3 Large architecture from\n    `\"Searching for MobileNetV3\" <https://arxiv.org/abs/1905.02244>`_.\n\n    Note that quantize = True returns a quantized model with 8 bit\n    weights. Quantized models only support inference and run on CPUs.\n    GPU inference is not yet supported\n\n    Args:\n     pretrained (bool): If True, returns a model pre-trained on ImageNet.\n     progress (bool): If True, displays a progress bar of the download to stderr\n     quantize (bool): If True, returns a quantized model, else returns a float model\n    \"\"\"\n    arch = \"mobilenet_v3_large\"\n    inverted_residual_setting, last_channel = _mobilenet_v3_conf(arch, **kwargs)\n    return _mobilenet_v3_model(arch, inverted_residual_setting, last_channel, pretrained, progress, quantize, **kwargs)\n", "path": "torchvision/models/quantization/mobilenetv3.py"}]}