
    *`i7                     h   d dl mZ d dlmZmZmZmZmZ d dlZd dl	m
Z
 d dlmZmZ d dlmZ d dlmZ d dlmZ d d	lmZ g d
Z	 ddeeef         deeef         dee         fdZ	 	 ddeeef         dedee         deeeef         ddf         fdZ	 	 ddej        j        dedee         defdZdS )    )Path)Dict	GeneratorOptionalTupleUnionN)BaseCompressor)CompressionFormatSparsityCompressionConfig)get_weight_mappings)	safe_open)	save_file)Tensor)load_compressedsave_compressedsave_compressed_modeltensors	save_pathcompression_formatc           
         | t          |           dk    rt          d          |pt          j        j        }|t          j                    v s\|t          j                    v sGt          d| dt          t          j                    t          j                    z                        t          j	        |          }|
                    |           }t          ||           dS )a_  
    Save compressed tensors to disk. If tensors are not compressed,
    save them as is.

    :param tensors: dictionary of tensors to compress
    :param save_path: path to save compressed tensors
    :param compression_format: compression format used for the tensors
    :return: compression config, if tensors were compressed - None otherwise
    Nr   z0No tensors or empty tensors provided to compresszUnknown compression format: z. Must be one of )len
ValueErrorr
   densevaluer	   registered_namesregistered_aliasessetload_from_registrycompressr   )r   r   r   
compressorcompressed_tensorss        z/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/compressed_tensors/compressors/helpers.pyr   r   "   s     #g,,!++KLLL ,L/@/F/L 	n=????!B!D!DDDm+= m m!."A"C"CnFgFiFi"ijjm m
 
 	
  23EFFJ#,,W55 ),,,,,    cpur!   compression_configdevicereturnc              #     K   | !t          |                                           st          d          ||j        t          j        j        k    rpt          |           }|                                D ]J\  }}t          |d|          5 }|
                    |          }||fV  ddd           n# 1 swxY w Y   KdS |j        }t          j        ||          }	|	                    | |          E d{V  dS )a  
    Load compressed tensors from disk.
    If tensors are not compressed, load them as is.

    :param compressed_tensors: path to compressed tensors.
        This can be a path to a file or a directory containing
        one or multiple safetensor files (if multiple - in the format
        assumed by huggingface)
    :param compression_config: compression config to use for decompressing tensors.
    :param device: device to move tensors to. If None, tensors are loaded on CPU.
    :param return_dict: if True, return a dictionary of decompressed tensors
    :return a generator that yields the name and tensor of the decompressed tensor
    Nz&No compressed tensors provided to loadpt)	frameworkr&   )config)r&   )r   existsr   formatr
   r   r   r   itemsr   
get_tensorr	   r   
decompress)
r!   r%   r&   weight_mappingsweight_namefile_with_weight_namefweightr   r    s
             r"   r   r   F   sw     $ !.@)A)A)H)H)J)J!ABBB 	"$(9(?(EEE ..@AA2A2G2G2I2I 	* 	*.K.0DPPP *TUk22!6))))* * * * * * * * * * * * * * *	* 	* 06#6'9
 
 

 (();F(KKKKKKKKKKKs   B33B7	:B7	Tmodelfilenameforce_contiguousc                     |                                  }|rd |                                D             }	 t          |||           dS # t          $ r(}t	          |          }|dz  }t          |          d}~ww xY w)a  
    Wrapper around safetensors `save_model` helper function, which allows for
    saving compressed model to disk.

    Note: The model is assumed to have a
        state_dict with  unique entries

    :param model: model to save on disk
    :param filename: filename location to save the file
    :param compression_format: compression format used for the model
    :param force_contiguous: forcing the state_dict to be saved as contiguous tensors
    c                 >    i | ]\  }}||                                 S  )
contiguous).0kvs      r"   
<dictcomp>z)save_compressed_model.<locals>.<dictcomp>   s&    GGGDAqaGGGr#   )r   z_ Or use save_compressed_model(..., force_contiguous=True), read the docs for potential caveats.N)
state_dictr.   r   r   str)r6   r7   r   r8   rA   emsgs          r"   r   r   o   s    $ !!##J HGGJ4D4D4F4FGGG

HASTTTTTT   !ffppoos   A
 

A<#A77A<)N)Nr$   )NT)pathlibr   typingr   r   r   r   r   torchcompressed_tensors.compressorsr	   compressed_tensors.configr
   r   )compressed_tensors.utils.safetensors_loadr   safetensorsr   safetensors.torchr   r   __all__rB   r   r   nnModuleboolr   r;   r#   r"   <module>rQ      s         : : : : : : : : : : : : : :  9 9 9 9 9 9 R R R R R R R R I I I I I I ! ! ! ! ! ! ' ' ' ' ' '         7;!- !-#v+!-S$Y!- !!23!- !- !- !-L 59!&L &Lc4i(&L1&L SM&L uS&[!4-.	&L &L &L &LX 7;!	 8? !!23 	     r#   