+
    Pj                         R t ^ RIt^ RIt^ RIt^ RIHt ]P                  ! ]4      tRR0t	R t
R R ltR tR R	 ltR
 R ltR R ltR# )as  
Dynamic int8 quantization for pocket-tts.

Uses torchao if available (torch 2.10+ with C++ extensions), otherwise
falls back to torch.ao.quantization (deprecated but functional on torch 2.5-2.9).

Quantizes attention (Q/K/V/output projections) and FFN (linear1/linear2) layers
in the FlowLM transformer. The flow matching network and Mimi VAE decoder
remain in float32.
N	attentionffnc                      ^ RI p V P                  P                  R4      f   R# ^ RIp\	        VR4      '       g   \        VRR4      '       g   R#  R#   \         d     R# i ; i)zDetect the best available quantization backend.

Returns "torchao" if torchao is installed with working C++ extensions,
otherwise returns "torch.ao".
Ntorchaoztorch.ao_C_SKIPPED_CPP_EXTENSIONSF)importlib.utilutil	find_specr   hasattrgetattrImportError)	importlibr   s     p/Users/ahmed/devFolder/Ultron/claude-voice/gateway/.venv/lib/python3.14/site-packages/pocket_tts/quantization.py_get_backendr      sg    >>##I.67D!!:SUZ)[)[ *\   s   !A )A A"!A"c                8    V ^8  d   QhR\         P                  /# )   module)nnModule)formats   "r   __annotate__r   -   s     ? ?RYY ?    c                0    ^ RI HpHp V! W! 4       4       R# )z.Apply int8 dynamic quantization using torchao.)%Int8DynamicActivationInt8WeightConfig	quantize_N)torchao.quantizationr   r   )r   r   r   s   &  r   _quantize_module_torchaor   -   s    Uf;=>r   c                    \         P                  ! 4       R9   d"   R\        P                  P                  n        R# \        P                  P                  P
                  R8X  d"   R\        P                  P                  n        R# R# )zKSet the quantization engine for torch.ao (QNNPACK for ARM, FBGEMM for x86).qnnpacknonefbgemmN)arm64aarch64)platformmachinetorchbackends	quantizedengine r   r   _ensure_quantization_enginer+   4   sU    11*3  '		!	!	(	(F	2*2  ' 
3r   c                z    V ^8  d   QhR\         P                  R\        \        ,          R\         P                  /# )r   flow_lmquantize_groupsreturnr   r   setstr)r   s   "r   r   r   <   s-      		 CH  r   c                    V'       g   \         P                  R4       V # \        4       p\         P                  RV4       VR8X  d   \        W4       V # \	        W4       V # )a  
Apply dynamic int8 quantization to the specified layer groups of a FlowLM model.

Automatically selects the best available backend:
- torchao (torch 2.10+): optimized C++ kernels, faster on both ARM and x86
- torch.ao (torch 2.5-2.9): deprecated but functional fallback

Args:
    flow_lm: The FlowLM model (model.flow_lm)
    quantize_groups: Set of group keys to quantize.
        Valid keys: "attention", "ffn", "flow_net"

Returns:
    The quantized model (modified in-place).
z<No quantization groups specified, returning model unchanged.zUsing quantization backend: %sr   )loggerinfor   _apply_torchao_apply_torch_ao)r-   r.   backends   && r   apply_dynamic_int8r9   <   sU      RSnG
KK0':)w0 N 	1Nr   c                ^    V ^8  d   QhR\         P                  R\        \        ,          RR/# r   r-   r.   r/   Nr0   )r   s   "r   r   r   [   s)     ( (BII (C (T (r   c                   RV9   d   \        V P                  4       V P                  P                   F  pRV9   d   \        VP                  4       RV9   g   K(  \
        P                  ! VP                  4      p\
        P                  ! VP                  4      p\        V4       \        V4       V^ ,          Vn        V^ ,          Vn        K  	  R# )z)Apply quantization using torchao backend.flow_netr   r   N)	r   r=   transformerlayers	self_attnr   
Sequentiallinear1linear2)r-   r.   layerwrapper1wrapper2s   &&   r   r6   r6   [   s    _$ !1!12$$++/)$U__5O#}}U]]3H}}U]]3H$X.$X.$QKEM$QKEM ,r   c                ^    V ^8  d   QhR\         P                  R\        \        ,          RR/# r;   r0   )r   s   "r   r   r   m   s)      RYY S d r   c                   ^ RI Hp \        4        RV9   d4   V! V P                  \        P
                  0\        P                  RR7       V P                  P                   F  pRV9   d4   V! VP                  \        P
                  0\        P                  RR7       RV9   g   KF  V! \        P                  ! VP                  4      \        P
                  0\        P                  R7      ^ ,          Vn        V! \        P                  ! VP                  4      \        P
                  0\        P                  R7      ^ ,          Vn        K  	  R# )	z@Apply quantization using deprecated torch.ao backend (fallback).)quantize_dynamicr=   T)dtypeinplacer   r   )rJ   N)torch.ao.quantizationrI   r+   r=   r   Linearr&   qint8r>   r?   r@   rA   rB   rC   )r-   r.   rI   rD   s   &&  r   r7   r7   m   s    6!_$))BII;ekkSWX$$++/)U__ryykVZ[O#,emm,ryykEM -emm,ryykEM ,r   )__doc__loggingr$   r&   torch.nnr   	getLogger__name__r4   RECOMMENDED_CONFIGr   r   r+   r9   r6   r7   r*   r   r   <module>rU      sS   	    			8	$ "5) *?3>($r   