o
    di                     @   s   d dl mZ d dlmZmZ d dlZd dlmZ d dlmZ ddl	m
Z
mZ eeZ	 eejejgddfd	ejd
ee defddZdejfddZdejdefddZdeejejf fddZdejfddZdejdejfddZdd ZdS )    )	getLogger)OptionalUnionN)nn)Conv1D   )BLOCK_PATTERNSSEQLEN_KEYS_TRANFORMERS moduleprefixnamec              	   C   s   |D ]}t | |r|dur||r|| i  S q|| i  S qi }|  D ]\}}|t||||dkr:|d | n|d q&|S )a8  
    Get all the layers with a specific prefix in the module
    Args:
        module (`nn.Module`):
            The module that contains our layers
        layers (`list`, defaults to `[Conv1D, nn.Conv2d, nn.Linear]`):
            Type of the layers that we want to get
        prefix (`Optional[str]`, defaults to `None`):
            Prefix of layers
        name (`str`, defaults to `""`):
            Used for recursion. Don't modify

    Returns:
        `Dict[str,Union[Conv1D, nn.Conv2d, nn.Linear]]`: Mapping of the name of the layer and the actual layer
    Nr
   .)layersr   r   )
isinstance
startswithnamed_childrenupdate
get_layers)r   r   r   r   layerresname1child r   `/lsinfo/ai/hellotax_ai/llm_service/venv_embed/lib/python3.10/site-packages/optimum/gptq/utils.pyr   !   s   

,r   modelc                    sF   dd |   D }tD ]   t fdd|D r   S qtd)a  
    Get the name of the module that contains the transformers blocks by checking if any modules has a specific pattern

    Args:
        model (`nn.Module`):
        The input model
    Returns:
        `str`: The name of the module that contains the Transformer blocks.
    c                 S   s   g | ]\}}|qS r   r   ).0n_r   r   r   
<listcomp>H   s    z/get_block_name_with_pattern.<locals>.<listcomp>c                 3   s    | ]}|  V  qd S N)r   )r   r   pattern_candidater   r   	<genexpr>K   s    z.get_block_name_with_pattern.<locals>.<genexpr>z\Block pattern could not be match. Pass `block_name_to_quantize` argument in `quantize_model`)named_modulesr   any
ValueError)r   modules_namesr   r!   r   get_block_name_with_pattern>   s   
r(   module_namec                    s4   g dddt jdtdtf fdd  | |S )	NFr
   r   r)   r   c                    sZ   |   D ]\}}|dkr|d | n|}||krd n |||d qs+| S )Nr
   r   T)r   )r   append)r   r)   r   name_bisr   new_name_get_preceding_modulesprevious_module_namestop_addingr   r   r.   T   s   
z5get_preceding_modules.<locals>._get_preceding_modules)r
   )r   Modulestr)r   r)   r   r-   r   get_preceding_modulesP   s   "
r3   objc                 C   s    t | tjr	| jS t|  jS r    )r   torchTensordevicenext
parameters)r4   r   r   r   
get_devicec   s   r:   c                    sV   t | dr$| j  t fddtD r$tD ]}| v r# |   S qtd dS )Nconfigc                 3   s    | ]}| v V  qd S r    r   )r   kmodel_configr   r   r#   l   s    zget_seqlen.<locals>.<genexpr>zWe couldn't get the model sequence length. Setting it to 2048. You can overwrite this value by passing `model_seqlen` in` GPTQQuantizer`i   )hasattrr;   to_dictr%   r	   loggerinfo)r   keyr   r=   r   
get_seqleni   s   

rD   r7   c                 C   s   t | |kr| |} | S r    )r:   to)r4   r7   r   r   r   move_tov   s   
rF   c                    sB   t | tjrt|  S t | ttfrt|  fdd| D S | S )Nc                    s   g | ]}t | qS r   )nested_move_to)r   er7   r   r   r      s    z"nested_move_to.<locals>.<listcomp>)r   r5   r6   rF   listtupletype)vr7   r   rI   r   rG   |   s
   
rG   )loggingr   typingr   r   r5   r   transformers.pytorch_utilsr   	constantsr   r	   __name__rA   Conv2dLinearr1   r2   r   r(   r3   r6   r:   rD   r7   rF   rG   r   r   r   r   <module>   s   .