+
    "Ý³j/G  ã                   óÚ  € ^ RI t ^ RIt^ RIHt ^ RIHt ^ RIHtHtH	t	 ^ RI
t
^ RIHt ^ RIHtHt ^RIHt ^RIHtHtHtHt ^R	IHt ^R
IHt ^RIHt  ! R R]]4      t] ! R R]4      4       t]! ]4      R R l4       t ] ! R R]4      4       t! ! R R]!4      t"]! ]!4      R R l4       t#] ! R R]4      4       t$ ! R R]$4      t%]! ]$4      R R l4       t& ! R R]4      t'R  R! lt(R# )"é    N)Ú	dataclass)ÚEnum)ÚAnyÚOptionalÚTuple)ÚAOBaseConfig)Ú_QUANTIZE_CONFIG_HANDLERÚ register_quantize_module_handler)ÚFakeQuantizedEmbedding)ÚFakeQuantizeConfigÚFakeQuantizeConfigBaseÚIntxFakeQuantizeConfigÚ_infer_fake_quantize_configs)ÚFakeQuantizedLinear)ÚTwoStepQuantizer©Ú_log_deprecation_warningc                   ó"   € ] tR t^!tRtRtRtRtR# )ÚQATStepzR
Enum value for the `step` field in :class:`~torchao.quantization.qat.QATConfig`.
ÚprepareÚconvert© N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__ÚPREPAREÚCONVERTÚ__static_attributes__r   ó    Ú]/home/luca/screen-checking/.venv/lib/python3.14/site-packages/torchao/quantization/qat/api.pyr   r   !   s   † ñð €GØ„Gr!   r   c                   óT   a € ] tR t^*t o RtR
RR/V 3R lR llltR tV 3R ltR	tV t	R# )Ú	QATConfigaÑ  
Config for applying quantization-aware training (QAT) to a `torch.nn.Module`,
to be used with :func:`~torchao.quantization.quant_api.quantize_`.

This config has two steps, "prepare" and "convert". The prepare step applies
"fake" quantization to the model and should be applied before training, while
the convert step converts the model into an actual quantized model. Fake
quantization here refers to simulating the quantization numerics (e.g. int4)
using high precision arithmetic (e.g. bf16), with the goal of reducing
eventual degradation from quantization.

There are two ways to use this config. The first involves passing a base
post-training quantization (PTQ) config, which we will use to automatically
infer the corresponding fake quantization schemes to use in the prepare phase.
In the convert phase, we will then apply the base PTQ config to the model.
This will be the most common use case.

Example usage::

    from torchao.quantization import (
        quantize_,
        Int4WeightOnlyConfig,
    )
    from torchao.quantization.qat import QATConfig

    base_config = Int4WeightOnlyConfig(group_size=32)
    quantize_(model, QATConfig(base_config, step="prepare"))
    train_loop(model)
    quantize_(model, QATConfig(base_config, step="convert"))

Currently only the following are supported as base configs:

    - :class:`~torchao.quantization.Int4WeightOnlyConfig`

The second way to use this config involves specifying the fake quantization
schemes directly. Users will pass in :class:`~torchao.quantization.qat.FakeQuantizeConfigBase`
for weights and/or activations instead of the base PTQ config. This use case
is mostly for experimentation, e.g. when the corresponding PTQ config does
not exist yet.

Example usage::

    from torchao.quantization import quantize_
    from torchao.quantization.qat import IntxFakeQuantizeConfig

    activation_config = IntxFakeQuantizeConfig(
        torch.int8, "per_token", is_symmetric=False,
    )
    weight_config = IntxFakeQuantizeConfig(
        torch.int4, group_size=32, is_symmetric=True,
    )
    qat_config = QATConfig(
        # must specify one of `base_config` or `weight_config`
        activation_config=act_config,
        weight_config=weight_config,
        step="prepare",
    )
    quantize_(model, qat_config)

Args:
    base_config (Optional[AOBaseConfig]): Base PTQ config to infer the fake
        quantization configs during the prepare phase, and to apply directly
        during the convert phase.
    activation_config (Optional[FakeQuantizeConfigBase]): Custom fake
        quantization config for input activations, always optional.
        Must be None if `base_config` is used.
    weight_config (Optional[FakeQuantizeConfigBase]): Custom fake quantization
        config for weights. Must be None if `base_config` is used.

Keyword args:
    step (str): One of "prepare" or "convert", determines the QAT phase

Raises:
    ValueError: If `base_config` and `activation_config` are both specified
    ValueError: If `base_config` and `weight_config` are both specified
    ValueError: If none of `base_config`, `activation_config`, or
        `weight_config` are specified
    ValueError: If either `activation_config` or `weight_config` is specified
         and `step` is "convert"
    ValueError: If `step` is not one of "prepare" or "convert"
    ValueError: If the config is applied on a module that is not a
        `torch.nn.Linear` or `torch.nn.Embedding`, or it is applied on
        `torch.nn.Embedding` with an activation config
NÚstepr   c                ób   <€ V ^8„  d   QhRS[ S[,          RS[ S[,          RS[ S[,          RS[/# ©é   Úbase_configÚactivation_configÚweight_configr%   ©r   r   r   r   )ÚformatÚ__classdict__s   "€r"   Ú__annotate__ÚQATConfig.__annotate__ˆ   sD   ø€ ÷ ñ á™lÕ+ðñ $Ñ$:Õ;ðñ  Ñ 6Õ7ð	ñ ñr!   c               óV   € Wn         W n        W0n        W@n        V P	                  4        R # ©N)r)   r*   r+   r%   Ú__post_init__)Úselfr)   r*   r+   r%   s   &&&&$r"   Ú__init__ÚQATConfig.__init__ˆ   s'   € ð 'ÔØ!2ÔØ*ÔØŒ	Ø×ÑÖr!   c                óŽ  € \         P                  P                  R 4       V P                  P	                  4       V n        \
         Uu. uF  qP                  NK  	  ppV P                  V9  d   \        RV 24      hV P                  e   V P                  e   \        R4      hV P                  e   V P                  e   \        R4      hV P                  \
        P                  8X  d>   \        V P                  V P                  V P                  34      '       g   \        R4      hV P                  \
        P                  8X  d(   V P                  f   V P                  e   \        R4      h\        V P                  \        4      '       d5   V P                  P                   P"                  p\        V RV RV R	24      hR# u upi )
z"torchao.quantization.qat.QATConfigz`step` must be one of Nz9Cannot specify both `base_config` and `activation_config`z5Cannot specify both `base_config` and `weight_config`zWMust specify `base_config`, `activation_config`, or `weight_config` in the prepare stepzICannot specify `weight_config` or `activation_config` in the convert stepz~ was passed as `base_config`. Did you mean to do the following instead?
    qat_config = QATConfig(
        activation_config=z(...),
        weight_config=z$(...),
        step="prepare",
    ))ÚtorchÚ_CÚ_log_api_usage_oncer%   Úlowerr   ÚvalueÚ
ValueErrorr)   r*   r+   r   Úanyr   Ú
isinstancer   Ú	__class__r   )r4   ÚsÚall_step_valuesÚconfig_types   &   r"   r3   ÚQATConfig.__post_init__–   s”  € Ü�‰×$Ñ$Ð%IÔJØ—I‘I—O‘OÓ%ˆŒ	Ý,3Ó4©G qŸ7œ7©GˆÐ4Ø�9‰9˜OÔ+ÜÐ5°oÐ5FÐGÓHÐHØ×ÑÒ'¨D×,BÑ,BÒ,NÜØKóð ð ×ÑÒ'¨D×,>Ñ,>Ò,JÜÐTÓUÐUØ�9‰9œŸ™Ô'´Ø×Ñ˜t×5Ñ5°t×7IÑ7IÐJ÷1
ò 1
ô Øióð ð �9‰9œŸ™Ô'Ø×"Ñ"Ò.°$×2DÑ2DÒ2PäØ[óð ô �d×&Ñ&Ô(>×?Ò?Ø×*Ñ*×4Ñ4×=Ñ=ˆKÜØ�-ð  -à-8¨Mð :)Ø)4¨ð 6ðóð ñ @ùò- 5s   ÁGc                óz   <€ V ^8„  d   Qh/ S[ S[,          ;R&   S[ S[,          ;R&   S[ S[,          ;R&   S[;R&   # r'   r,   )r-   r.   s   "€r"   r/   r0   *   sQ   ø‡ ‚ ñn ™,Õ'Ñ'ño ñp  Ñ 6Õ7Ñ7ñq ñr Ñ2Õ3Ñ3ñs ñt �Mòu r!   )r*   r)   r%   r+   )NNN)
r   r   r   r   r   r5   r3   Ú__annotate_func__r    Ú__classdictcell__©r.   s   @r"   r$   r$   *   s-   ø‡ € ñSñxð "÷ó ò"÷Y ƒ r!   r$   c                óŒ   € V ^8„  d   QhR\         P                  P                  R\        R\         P                  P                  /# ©r(   ÚmoduleÚconfigÚreturn)r8   ÚnnÚModuler$   )r-   s   "r"   r/   r/   ¼   s;   € ÷ wñ wÜ�H‰H�O‰Oðwäðwô ‡X�X‡_�_ñwr!   c                ó´  € ^ RI HpHpHpHp VP
                  pVP                  pV\        P                  8X  EdJ   Ve   \        V4      w  r‰MVP                  pVP                  p	\        V \        P                  P                  4      '       dŒ   \        W”4      '       d*   Ve   \        W„4      '       g   Q hVP!                  WV	4      # \        W’4      '       d*   Ve   \        W‚4      '       g   Q hVP!                  WV	4      # \"        P                   ! WV	4      # \        V \        P                  P$                  4      '       d&   Ve   \'        R4      h\(        P*                  ! W	4      # \'        R\-        V 4      ,          4      hV\        P.                  8X  g   Q RV,          4       hVP                  e   Q R4       hVP                  e   Q R4       h\        V \"        \(        VV34      '       g   V # / p
Rp\1        V R	4      '       d…   \        V P2                  P4                  \6        4      '       d[   V P2                  P4                  P8                  '       d5   V P2                  P:                  V
R
&   V P2                  P<                  V
R&   Rp\        V \"        W534      '       d   V P?                  4       p M6\        V \(        4      '       d   V PA                  4       p M\'        RV  R24      hVek   V'       dF   \1        VR4      '       d4   \B        PD                  ! R4       \F        PH                  ! V4      pRVn%        \L        \-        V4      ,          ! W3/ V
B # V # )ad  
During the prepare step, perform module swap to apply fake quantization.
If the base PTQ config is specified, derive the fake quantization configs from it.

During the convert step, first perform module swap to revert all fake quantized
modules to the corresponding built-in `torch.nn.Module`s, then apply the
base config directly to quantize the module.
)ÚMXFakeQuantizeConfigÚMXFakeQuantizedLinearÚNVFP4FakeQuantizeConfigÚNVFP4FakeQuantizedLinearNú;Activation fake quantization is not supported for embeddingú-Module of type '%s' does not have QAT supportz!unexpected step '%s' in QATConfigzunexpected `activation_config`zunexpected `weight_config`FÚweight_fake_quantizerÚcustom_scaleÚcustom_zero_pointTzEncountered unexpected module z, should never happenÚintx_choose_qparams_algorithmz'Disabling intx_choose_qparams_algorithm)'Útorchao.prototype.qatrQ   rR   rS   rT   r)   r%   r   r   r   r*   r+   r?   r8   rN   ÚLinearÚfrom_linearr   Ú	Embeddingr=   r   Úfrom_embeddingÚtyper   ÚhasattrrW   rL   r   Úrange_learningÚscaleÚ
zero_pointÚ	to_linearÚto_embeddingÚloggingÚdebugÚcopyÚdeepcopyrZ   r	   )rK   rL   rQ   rR   rS   rT   r)   r%   Ú
act_configr+   ÚkwargsÚhas_custom_scale_and_zero_points   &&          r"   Ú_qat_config_transformrn   »   s2  € ÷ó ð ×$Ñ$€KØ�;‰;€DØŒw�‰ÕØÒ"Ü*FÀ{Ó*SÑ'ˆZ˜à×1Ñ1ˆJØ"×0Ñ0ˆMÜ�fœeŸh™hŸo™o×.Ò.Ü˜-×AÒAØ!Ò)¬ZØ÷.ò .ð ð ð 0×;Ñ;Ø¨óð ô ˜M×@Ò@Ø!Ò)¬ZØ÷.ò .ð ð ð -×8Ñ8Ø¨óð ô +×6Ò6Ø¨óð ô ˜¤§¡× 2Ñ 2×3Ò3ØÒ%Ü ØQóð ô *×8Ò8¸ÓOÐOäØ?Ä$ÀvÃ,ÕNóð ð
 ”w—‘Ô&ÐRÐ(KÈdÕ(RÓRÐ&Ø×'Ñ'Ò/ÐQÐ1QÓQÐ/Ø×#Ñ#Ò+ÐIÐ-IÓIÐ+ô Øä#Ü&Ø%Ø(ð	÷
ò 
ð ˆMð ˆØ*/Ð'ä�FÐ3×4Ò4Ü˜6×7Ñ7×>Ñ>Ô@V×WÒWØ×,Ñ,×3Ñ3×B×BÐBà%+×%AÑ%A×%GÑ%GˆF�>Ñ"Ø*0×*FÑ*F×*QÑ*QˆFÐ&Ñ'Ø.2Ð+ô ØÜ Ð"7ÐR÷
ò 
ð ×%Ñ%Ó'‰FÜ˜Ô 6×7Ò7Ø×(Ñ(Ó*‰FäØ0°°Ð8MÐNóð ð Ò"ç.´7ØÐ<÷4ò 4ô —’ÐGÔHÜ"Ÿmšm¨KÓ8�Ø<@�Ô9Ü+¬D°Ó,=Ö>ØñØ'-ñð ð ˆMr!   c                   ó>   a € ] tR tRt o RtRtRtR tV 3R ltRt	V t
R# )Ú#IntXQuantizationAwareTrainingConfigi6  ay  
(Deprecated) Please use :class:`~torchao.quantization.qat.QATConfig` instead.

Config for applying fake quantization to a `torch.nn.Module`.
to be used with :func:`~torchao.quantization.quant_api.quantize_`.

Example usage::

    from torchao.quantization import quantize_
    from torchao.quantization.qat import IntxFakeQuantizeConfig
    activation_config = IntxFakeQuantizeConfig(
        torch.int8, "per_token", is_symmetric=False,
    )
    weight_config = IntxFakeQuantizeConfig(
        torch.int4, group_size=32, is_symmetric=True,
    )
    quantize_(
        model,
        IntXQuantizationAwareTrainingConfig(activation_config, weight_config),
    )

Note: If the config is applied on a module that is not
`torch.nn.Linear` or `torch.nn.Embedding`, or it is applied on
`torch.nn.Embedding` with an activation config, then we will raise
ValueError as these are not supported.
Nc                ó   € \        V 4       R # r2   r   ©r4   s   &r"   r3   Ú1IntXQuantizationAwareTrainingConfig.__post_init__V  ó
   € Ü  Ö&r!   c                óR   <€ V ^8„  d   Qh/ S[ S[,          ;R&   S[ S[,          ;R&   # )r(   r*   r+   )r   r   )r-   r.   s   "€r"   r/   Ú0IntXQuantizationAwareTrainingConfig.__annotate__6  s+   ø‡ ‚ ñ:  Ñ 6Õ7Ñ>ñ; ñ< Ñ2Õ3Ñ:ò= r!   r   )r   r   r   r   r   r*   r+   r3   rF   r    rG   rH   s   @r"   rp   rp   6  s$   ø‡ € ñð6 ;?ÐØ6:€Mò'÷A ƒ r!   rp   c                   ó   € ] tR tRtRtR# )Ú intx_quantization_aware_trainingi[  r   N©r   r   r   r   r    r   r!   r"   rx   rx   [  ó   † Ûr!   rx   c                óŒ   € V ^8„  d   QhR\         P                  P                  R\        R\         P                  P                  /# rJ   )r8   rN   rO   rp   )r-   s   "r"   r/   r/   `  s;   € ÷ Vñ VÜ�H‰H�O‰OðVä/ðVô ‡X�X‡_�_ñVr!   c                 óŽ  € T pVP                   pVP                  p\        V\        P                  P
                  4      '       d   \        P                  ! VVV4      # \        V\        P                  P                  4      '       d&   Ve   \        R4      h\        P                  ! W$4      # \        R\        V4      ,          4      h)NrU   rV   )r*   r+   r?   r8   rN   r\   r   r]   r^   r=   r   r_   r`   )rK   rL   Úmodr*   r+   s   &&   r"   Ú+_intx_quantization_aware_training_transformr~   _  s©   € ð
 €CØ×0Ñ0ÐØ×(Ñ(€Mä�#”u—x‘x—‘×'Ò'Ü"×.Ò.ØØØó
ð 	
ô
 
�CœŸ™×+Ñ+×	,Ò	,ØÒ(ÜØMóð ô &×4Ò4°SÓHÐHäÐHÌ4ÐPSË9ÕTÓUÐUr!   c                   ó*   a € ] tR tRt o RtR tRtV tR# )Ú'FromIntXQuantizationAwareTrainingConfigix  ag  
(Deprecated) Please use :class:`~torchao.quantization.qat.QATConfig` instead.

Config for converting a model with fake quantized modules,
such as :func:`~torchao.quantization.qat.linear.FakeQuantizedLinear`
and :func:`~torchao.quantization.qat.linear.FakeQuantizedEmbedding`,
back to model with the original, corresponding modules without
fake quantization. This should be used with
:func:`~torchao.quantization.quant_api.quantize_`.

Example usage::

    from torchao.quantization import quantize_
    quantize_(
        model_with_fake_quantized_linears,
        FromIntXQuantizationAwareTrainingConfig(),
    )
c                ó   € \        V 4       R # r2   r   rr   s   &r"   r3   Ú5FromIntXQuantizationAwareTrainingConfig.__post_init__�  rt   r!   r   N)r   r   r   r   r   r3   r    rG   rH   s   @r"   r€   r€   x  s   ø‡ € ñ÷&'ð 'r!   r€   c                   ó   € ] tR tRtRtR# )Ú%from_intx_quantization_aware_trainingi’  r   Nry   r   r!   r"   r„   r„   ’  rz   r!   r„   c                óŒ   € V ^8„  d   QhR\         P                  P                  R\        R\         P                  P                  /# )r(   r}   rL   rM   )r8   rN   rO   r€   )r-   s   "r"   r/   r/   —  s6   € ÷ ñ Ü	�‰�‰ðä3ðô ‡X�X‡_�_ñr!   c                óž   € \        V \        4      '       d   V P                  4       # \        V \        4      '       d   V P	                  4       # V # )z„
If the given module is a fake quantized module, return the original
corresponding version of the module without fake quantization.
)r?   r   re   r   rf   )r}   rL   s   &&r"   Ú0_from_intx_quantization_aware_training_transformr‡   –  s@   € ô �#Ô*×+Ò+Ø�}‰}‹ÐÜ	�CÔ/×	0Ò	0Ø×ÑÓ!Ð!àˆ
r!   c                   óZ   a € ] tR tRt o RtV 3R lR ltV 3R lR ltV 3R lR ltR	tV t	R
# )ÚComposableQATQuantizeri§  a*  
Composable quantizer that users can use to apply multiple QAT quantizers easily.
Quantizers will be applied in the order they are specified in the constructor.

Note: the quantizers provided must apply to different modules in the model,
e.g. nn.Linear and nn.Embedding, otherwise the behavior will be undefined.

Example usage::

    my_quantizer = ComposableQATQuantizer([
        QATQuantizer1(),
        QATQuantizer2(),
        QATQuantizer3(),
    ])
    model = my_quantizer.prepare(model)
    train(model)
    model = my_quantizer.convert(model)
c                ó0   <€ V ^8„  d   QhRS[ S[,          /# )r(   Ú
quantizers)Úlistr   )r-   r.   s   "€r"   r/   Ú#ComposableQATQuantizer.__annotate__»  s   ø€ ÷ %ñ %¡4Ñ(8Õ#9ñ %r!   c                óP   € \         P                  P                  R 4       Wn        R# )z/torchao.quantization.qat.ComposableQATQuantizerN)r8   r9   r:   r‹   )r4   r‹   s   &&r"   r5   ÚComposableQATQuantizer.__init__»  s   € Ü�‰×$Ñ$Ð%VÔWØ$Žr!   c                ó‚   <€ V ^8„  d   QhRS[ P                  P                  RS[RS[RS[ P                  P                  /# ©r(   ÚmodelÚargsrl   rM   ©r8   rN   rO   r   )r-   r.   s   "€r"   r/   r�   ¿  ó=   ø€ ÷ ñ Ù—X‘X—_‘_ðÙ-0ðÙ<?ðá	�‰�‰ñr!   c                óN   € V P                    F  pVP                  V4      pK  	  V# r2   )r‹   r   ©r4   r’   r“   rl   Ú	quantizers   &&*, r"   r   ÚComposableQATQuantizer.prepare¿  ó'   € ð ŸœˆIØ×%Ñ% eÓ,ŠEñ )àˆr!   c                ó‚   <€ V ^8„  d   QhRS[ P                  P                  RS[RS[RS[ P                  P                  /# r‘   r”   )r-   r.   s   "€r"   r/   r�   Æ  r•   r!   c                óN   € V P                    F  pVP                  V4      pK  	  V# r2   )r‹   r   r—   s   &&*, r"   r   ÚComposableQATQuantizer.convertÆ  rš   r!   )r‹   N)
r   r   r   r   r   r5   r   r   r    rG   rH   s   @r"   r‰   r‰   §  s(   ø‡ € ñ÷&%ð %÷ð ÷ö r!   r‰   c                óv   € V ^8„  d   QhR\         P                  P                  R\        \        R3,          RR/# )r(   r’   Úexample_inputs.rM   N)r8   rN   rO   r   r   )r-   s   "r"   r/   r/   Î  s6   € ÷ ñ Ü�8‰8�?‰?ðäœ#˜s˜(•Oðð 
ñr!   c                ó’   a€ \         P                  P                  R4       ^ RIHo R V3R llpV P                  V4       V ! V!   R# )zµ
(Prototype) Initialize the scales and zero points on all
:class:`~torchao.quantization.qat.fake_quantizer.IntxFakeQuantizerBase`
in the model based on the provided example inputs.
z3torchao.quantization.qat.initialize_fake_quantizers)ÚIntxFakeQuantizerc                óL   € V ^8„  d   QhR\         P                  P                  /# )r(   Úm)r8   rN   rO   )r-   s   "r"   r/   Ú0initialize_fake_quantizers.<locals>.__annotate__Ü  s   € ÷ "ñ "œEŸH™HŸO™Oñ "r!   c                 ó>   <€ \        V S4      '       d
   R V n        R# R# )TN)r?   Ú_initialized)r£   r¡   s   &€r"   Ú_set_initializedÚ4initialize_fake_quantizers.<locals>._set_initializedÜ  s   ø€ Ü�aÐ*×+Ò+Ø!ˆAŽNñ ,r!   N)r8   r9   r:   Ú'torchao.quantization.qat.fake_quantizerr¡   Úapply)r’   rŸ   r§   r¡   s   && @r"   Úinitialize_fake_quantizersr«   Î  s?   ø€ ô 
‡H�H× Ñ Ð!VÔWõ J÷"ð "ð 
‡K�KÐ Ô!Ù	ˆ>Ôr!   ))ri   rg   Údataclassesr   Úenumr   Útypingr   r   r   r8   Útorchao.core.configr   Ú%torchao.quantization.transform_moduler	   r
   Ú	embeddingr   Úfake_quantize_configr   r   r   r   Úlinearr   Útwo_step_quantizerr   Úutilsr   Ústrr   r$   rn   rp   rx   r~   r€   r„   r‡   r‰   r«   r   r!   r"   Ú<module>r·      s#  ðó Û Ý !Ý ß 'Ñ 'ã å ,÷õ
 .÷ó õ (Ý 0Ý +ôˆc�4ô ð ôM�ó Mó ðMñ` " )Ó,ôwó -ðwðt ô '¨,ó  'ó ð 'ôH	Ð'Jô 	ñ "Ð"EÓFôVó GðVð0 ô'¨ló 'ó ð'ô2	Ð,Sô 	ñ "Ð"IÓJôó Kðô $Ð-ô $÷Nr!   