Ë
    GêñiÍ  ã                  óè   — d dl mZ d dlmZ d dlmZmZ ddlmZ ddl	m
Z
mZ  ej                  e«      Z e«       r
d dlZd dlmZ 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Z G d	„ d
e«      Z G d„ de«      Zy)é    )Úannotations)ÚAny)Úis_torch_availableÚloggingé   )ÚConversionOps)Úget_module_from_nameÚshould_convert_moduleNc           
     ó�  — ddl m} |€g }t        | j                  «       «      D ]Ÿ  \  }}t	        |t
        j                  «      sŒ!t        ||«      sŒ.|j                  d«      \  }	}
}|	r| j                  |	«      n| } ||s|j                  nd|s|j                  nd|s|j                  dund|||d¬«      }t        |||«       Œ¡ | S )aý  
    Replace nn.Linear modules with empty SINQLinear modules.

    Args:
        model: The model to modify
        modules_to_not_convert: List of module names to skip
        quant_config: SINQ quantization config dict (None for pre-quantized models)
        compute_dtype: Computation dtype for the quantized layers
        device: Device string for the quantized layers
        pre_quantized: Whether loading a pre-quantized checkpoint

    Returns:
        The modified model with SINQLinear modules
    r   )Ú
SINQLinearNú.FT)Úin_featuresÚout_featuresÚbiasÚquant_configÚcompute_dtypeÚdeviceÚuse_unpack_kernel)Úsinq.sinqlinear_hfr   ÚlistÚnamed_modulesÚ
isinstanceÚnnÚLinearr
   Ú
rpartitionÚget_submoduler   r   r   Úsetattr)ÚmodelÚmodules_to_not_convertr   r   r   Úpre_quantizedr   Ú	full_nameÚmoduleÚparent_pathÚ_Ú
child_nameÚparentÚ
sinq_layers                 ú`/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/transformers/integrations/sinq.pyÚreplace_with_sinq_linearr)       sÔ   € õ, .àÐ%Ø!#Ðä! %×"5Ñ"5Ó"7Ó8ò 0Ñˆ	�6Ü˜&¤"§)¡)Ô,ØÜ$ YÐ0FÔGØà%.×%9Ñ%9¸#Ó%>Ñ"ˆ�Q˜
Ù5@�×$Ñ$ [Ô1ÀeˆáÙ2?˜×*Ò*ÀTÙ4A˜×,Ò,ÀtÙ2?�&—+‘+ TÑ)ÀUØ%Ø'ØØ"ô
ˆ
ô 	�˜
 JÕ/ð'0ð* €Ló    c                  ó4   — e Zd ZdZd„ Z	 	 	 d	 	 	 	 	 	 	 dd„Zy)ÚSinqQuantizea'  
    Param-level ConversionOp for SINQ (from FP weights).

    At load time, for each `Linear.weight` that should be quantized:
      - The SINQLinear module already exists (created in _process_model_before_weight_loading)
      - We just call quantize() on it with the loaded weight tensor
    c                ó   — || _         y ©N©Úhf_quantizer©Úselfr0   s     r(   Ú__init__zSinqQuantize.__init__\   ó
   € Ø(ˆÕr*   Nc                óò   — t        t        |j                  «       «      «      \  }}t        |t        «      r|d   n|}t        ||«      \  }	}
|	j                  |«       |�|j                  |«       d|	_        i S )Nr   T)	ÚnextÚiterÚitemsr   r   r	   ÚquantizeÚdiscardÚ_is_hf_initialized)r2   Ú
input_dictr   Úfull_layer_nameÚmissing_keysÚkwargsr$   ÚvaluesÚweight_tensorr"   Útensor_names              r(   ÚconvertzSinqQuantize.convert_   sr   € ô œ˜j×.Ñ.Ó0Ó1Ó2‰	ˆˆ6Ü%/°¼Ô%=˜˜qš	À6ˆä2°5¸/ÓJÑˆ�à�‰˜Ô&àÐ#Ø× Ñ  Ô1à$(ˆÔ!àˆ	r*   )NNN©r<   zdict[str, Any]r   ztorch.nn.Module | Noner=   z
str | NoneÚreturnzdict[str, torch.Tensor]©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r3   rC   © r*   r(   r,   r,   S   sB   „ ñò)ð )-Ø&*Øðà"ðð &ðð $ð	ð 
!ôr*   r,   c                  ó2   — e Zd ZdZd„ Z	 	 d	 	 	 	 	 	 	 dd„Zy)ÚSinqDeserializea0  
    ConversionOp for loading *pre-quantized* SINQ checkpoints.

    Checkpoint layout (what `SINQLinear.state_dict` produces) is, per module:
        <prefix>.W_q
        <prefix>.bias
        <prefix>.meta

    WeightConverter in the quantizer is configured so that:
      - we group ".W_q", ".meta", ".bias" as input_dict
      - conceptually treat them as belonging to "<prefix>.weight"
      - and call this SinqDeserialize.convert to load the state into the existing SINQLinear.

    The returned dict is {} because we load directly into the module.
    c                ó   — || _         y r.   r/   r1   s     r(   r3   zSinqDeserialize.__init__‡   r4   r*   Nc                óÄ  — t        |j                  «       «      D ]  \  }}t        |t         «      sŒ|d   ||<   Œ  |j                  d«      }|j                  d«      }|j                  d«      }	|�|€;t	        t        |j                  «       «      «      }t        |t         «      r|d   }||iS t        ||«      \  }
}||dœ}|	�|	|d<   |
j                  |«       d|
_	        i S )Nr   z.W_qz.metaz.bias)ÚW_qÚmetar   T)
r   r8   r   Úgetr6   r7   r@   r	   Úload_state_dictr;   )r2   r<   r   r=   r?   ÚkÚvrP   rQ   r   r"   r$   Ústates                r(   rC   zSinqDeserialize.convertŠ   sô   € ô ˜×)Ñ)Ó+Ó,ò 	%‰DˆAˆqÜ˜!œTÕ"Ø ! !¡�
˜1’ð	%ð �n‰n˜VÓ$ˆØ�~‰~˜gÓ&ˆØ�~‰~˜gÓ&ˆð ˆ;˜$˜,Ü”T˜*×+Ñ+Ó-Ó.Ó/ˆAÜ˜!œTÔ"Ø�a‘D�Ø# QÐ'Ð'ä(¨°Ó@‰	ˆ�ð Øñ
ˆð ÐØ ˆE�&‰Mà×Ñ˜uÔ%Ø$(ˆÔ!àˆ	r*   )NNrD   rF   rK   r*   r(   rM   rM   v   s?   „ ñò )ð )-Ø&*ð	#à"ð#ð &ð#ð $ð	#ð 
!ô#r*   rM   )NNNzcuda:0F)r   útorch.nn.Moduler   zlist[str] | Noner   zdict | Noner   ztorch.dtyper   Ústrr    ÚboolrE   rW   )Ú
__future__r   Útypingr   Útransformers.utilsr   r   Úcore_model_loadingr   Úquantizers.quantizers_utilsr	   r
   Ú
get_loggerrG   ÚloggerÚtorchÚtorch.nnr   r)   r,   rM   rK   r*   r(   ú<module>rc      s¨   ðõ #å ç :å .ß Uð 
ˆ×	Ñ	˜HÓ	%€áÔÛÝð
 04Ø $Ø!%ØØð0Øð0à,ð0ð ð0ð ð	0ð
 ð0ð ð0ð ó0ôf �=ô  ôF7�mõ 7r*   