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'   ]/home/ubuntu/veenaModal/venv/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
/