o
    ý°“i  ã                
   @   sà   d dl mZmZ d dlZd dlmZmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZ dgZ	ddee d	ee d
ee defdd„Z		ddejjd
ee dee fdd„Z		ddejjd
ee dee dee fdd„ZdS )é    )ÚListÚOptionalN)ÚCompressionFormatÚSparsityStructure)ÚQuantizationArgsÚQuantizationStrategyÚQuantizationType)Úis_module_quantized)ÚloggerÚ,infer_and_set_per_module_quantization_formatÚ
input_argsÚweight_argsÚsparsity_structureÚreturnc                 C   sÐ   t |ƒt jk}|duo| du }|jdkr&|jtjjkr&|jdkr#tj	S tj
S |rL|jdv o3|jtjjk}|s9tjS |rI|jtjjtjjfv rItjS tjS |jtjjkr[|jdkr[tjS |jtjjkretjS tjS )a   
    Using the weight and input quantization args as well as an optional
    sparsity structure, determine the compression format that should be
    applied to a given module

    :param input_args: input quantization parameters
    :param weight_args: weight quantization parameters
    :param sparsity_structure: optional (global) modle sparsity
        structure
    :return CompresssionFormat for the module
    Né   é    )r   é   r   )r   ÚTWO_FOURÚnum_bitsÚtyper   ÚFLOATÚvalueÚ
group_sizer   Úmxfp4_pack_quantizedÚnvfp4_pack_quantizedÚINTÚnaive_quantizedÚstrategyr   ÚCHANNELÚGROUPÚ	marlin_24Úpack_quantizedÚfloat_quantizedÚint_quantized)r   r   r   Úis_24_structureÚis_weight_onlyÚis_valid_pack© r'   úT/home/ubuntu/.local/lib/python3.10/site-packages/compressed_tensors/config/format.pyÚ_get_quant_compression_format   s2   ÿ

þþ
r)   ÚmoduleÚquantization_formatc                 C   sŠ   | j j}| j j}|du rdS t|||ƒ}|dur(||jkr"t d¡ || j _dS | j jdur>| j j|jkr<t d¡ dS dS |j| j _dS )a[  
    Determine and set the per module quantization format given quantization args
    and sparsity structure.

    :param module: module which has its quantization inferred
    :param sparsity_structure: optional sparsity applied to the module
    :param quantization_format: optional global format to override
        the per module formats

    Nz\The provided format for the module does not match the inferred format. Compression may fail )Úquantization_schemeÚweightsÚinput_activationsr)   r   r
   ÚwarningÚformat)r*   r   r+   Úweight_schemeÚinput_schemeÚcompression_formatr'   r'   r(   Úset_per_module_formatV   s(   ÿ
ÿÿÿr4   Úmodelc                 C   sp   g }|   ¡ D ]$}t|ƒr*t|dƒsJ ‚t|||ƒ |jjr*|jj|vr*| |jj¡ qt|ƒdkr3|S tj	j
gS )a“  
    Infers the quantization format for a model based on its state and provided
    compression arguments. Updates thhe quantization_scheme.format value
    based on the inferred format. Returns the unique list of formats in the model.
    All None formats are mapped to CompressionFormat.dense.value

    For a summary of the formats, see `docs/guides/compression_formats.md`.

    :param model: model to check for quantization
    :param sparsity_structure: optional sparsity applied to the module
    :param quantization_format: optional global format to override
        the per module formats
    :return compression format appropriate for the model
    r,   r   )Úmodulesr	   Úhasattrr4   r,   r0   ÚappendÚlenr   Údenser   )r5   r   r+   Úunique_formatsÚ	submoduler'   r'   r(   r   ƒ   s   ÿ€
)N)NN)Útypingr   r   ÚtorchÚcompressed_tensors.configr   r   Úcompressed_tensors.quantizationr   r   r   Ú%compressed_tensors.quantization.utilsr	   Úlogurur
   Ú__all__Ústrr)   ÚnnÚModuler4   r   r'   r'   r'   r(   Ú<module>   sJ   ýÿþý
ü9ýÿþ
ý/ýÿþýü