r"""Converts a float model to dynamic (i.e. weights-only) quantized model. Replaces specified modules with dynamic weight-only quantized versions and output the quantized model. For simplest usage provide `dtype` argument that can be float16 or qint8. Weight-only quantization by defaul
(model, qconfig_spec=None, dtype=torch.qint8,
mapping=None, inplace=False)
| 384 | return model |
| 385 | |
| 386 | def quantize_dynamic(model, qconfig_spec=None, dtype=torch.qint8, |
| 387 | mapping=None, inplace=False): |
| 388 | r"""Converts a float model to dynamic (i.e. weights-only) quantized model. |
| 389 | |
| 390 | Replaces specified modules with dynamic weight-only quantized versions and output the quantized model. |
| 391 | |
| 392 | For simplest usage provide `dtype` argument that can be float16 or qint8. Weight-only quantization |
| 393 | by default is performed for layers with large weights size - i.e. Linear and RNN variants. |
| 394 | |
| 395 | Fine grained control is possible with `qconfig` and `mapping` that act similarly to `quantize()`. |
| 396 | If `qconfig` is provided, the `dtype` argument is ignored. |
| 397 | |
| 398 | Args: |
| 399 | model: input model |
| 400 | qconfig_spec: Either: |
| 401 | |
| 402 | - A dictionary that maps from name or type of submodule to quantization |
| 403 | configuration, qconfig applies to all submodules of a given |
| 404 | module unless qconfig for the submodules are specified (when the |
| 405 | submodule already has qconfig attribute). Entries in the dictionary |
| 406 | need to be QConfig instances. |
| 407 | |
| 408 | - A set of types and/or submodule names to apply dynamic quantization to, |
| 409 | in which case the `dtype` argument is used to specify the bit-width |
| 410 | |
| 411 | inplace: carry out model transformations in-place, the original module is mutated |
| 412 | mapping: maps type of a submodule to a type of corresponding dynamically quantized version |
| 413 | with which the submodule needs to be replaced |
| 414 | |
| 415 | """ |
| 416 | torch._C._log_api_usage_once("quantization_api.quantize.quantize_dynamic") |
| 417 | if qconfig_spec is None: |
| 418 | if dtype == torch.qint8: |
| 419 | qconfig_spec = { |
| 420 | nn.Linear : default_dynamic_qconfig, |
| 421 | nn.LSTM : default_dynamic_qconfig, |
| 422 | nn.GRU : default_dynamic_qconfig, |
| 423 | nn.LSTMCell : default_dynamic_qconfig, |
| 424 | nn.RNNCell : default_dynamic_qconfig, |
| 425 | nn.GRUCell : default_dynamic_qconfig, |
| 426 | } |
| 427 | elif dtype == torch.float16: |
| 428 | qconfig_spec = { |
| 429 | nn.Linear : float16_dynamic_qconfig, |
| 430 | nn.LSTM : float16_dynamic_qconfig, |
| 431 | nn.GRU : float16_dynamic_qconfig, |
| 432 | nn.LSTMCell : float16_dynamic_qconfig, |
| 433 | nn.RNNCell : float16_dynamic_qconfig, |
| 434 | nn.GRUCell : float16_dynamic_qconfig, |
| 435 | } |
| 436 | elif dtype == torch.quint8: |
| 437 | qconfig_spec = { |
| 438 | nn.EmbeddingBag : float_qparams_weight_only_qconfig, |
| 439 | nn.Embedding : float_qparams_weight_only_qconfig, |
| 440 | } |
| 441 | elif dtype == torch.quint4x2: |
| 442 | qconfig_spec = { |
| 443 | nn.EmbeddingBag : float_qparams_weight_only_qconfig_4bit, |
searching dependent graphs…