o
    Tij                     @   sD   d dl Zdd Zdd ZdddZdd	 Zd
d ZG dd dZdS )    Nc                    s   |st dt|d jtjrtj}tjn
dd l}|j}|j|dd |D | _d  |D ]}|	 r:|j
j  nq. d urN| fdd|D | _
d S d S )N9Cannot fuse linear layers: at least one layer is requiredr   c                 S   s   g | ]}|j qS  )weight.0layerr   r   P/home/ubuntu/.local/lib/python3.10/site-packages/ctranslate2/converters/utils.py
<listcomp>   s    zfuse_linear.<locals>.<listcomp>c                    s0   g | ]}|  r|jn
|jjd  g dqS )r   )dtype)has_biasbiasr   shaper   
bias_dtypezerosr   r   r	      s    )
ValueError
isinstancer   npndarrayconcatenater   torchcatr   r   r
   )speclayersr   r   r   r   r   r   fuse_linear   s,   
r   c                    sl   |st dg d}t|d jtjrtj}ndd l}|j}|D ] t|  | fdd|D |d q d S )Nr   )r   weight_scaleweight_zeror   c                    s   g | ]}t | qS r   )getattrr   paramr   r   r	   5   s    z(fuse_linear_prequant.<locals>.<listcomp>axis)	r   r   r   r   r   r   r   r   setattr)r   r   r!   paramsr   r   r   r   r   fuse_linear_prequant&   s   r$   c                 C   s   |dur=|  || jd | d} | ddd|f }t| || d| ||d}|| ddd|f< |  d| jd S |  || jd | d d| jd dd | jd | jd S )z<Permutes the weight to use the sliced rotary implementation.Nr         )reshaper   permute_for_sliced_rotaryswapaxes)r   	num_heads
rotary_dimrotary_weightr   r   r   r)   9   s   
"r)   c                 C   s   t |jtjs|j }| }n|j}tjt|dd}t|d}||j	}t
|| }t|d}t |jtjsGddl}||}|  j|  _|  j|  _| j|dd9  _dS )zlApplies the activation smoothing technique described in
    https://github.com/mit-han-lab/smoothquant.
    r   r    gh㈵>Nr'   r%   )r   r   r   r   numpyamaxabsolutemaximumastyper
   sqrtr   
from_numpygammabetar(   )
layer_normlinearactivation_scaleslinear_weightweight_scalesscalesr   r   r   r   smooth_activationN   s   


r=   c                 C   s"   d}| D ]}|d| 7 }qt |)NzjThe model you are trying to convert is not supported by CTranslate2. We identified the following reasons:
z
- )r   )reasonsmessagereasonr   r   r   raise_unsupportedk   s
   rA   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )ConfigurationCheckerc                 C   s
   g | _ d S N)_unsupported_reasonsselfr   r   r   __init__v   s   
zConfigurationChecker.__init__c                 C   s   |s
| j | d S d S rC   )rD   append)rF   assert_conditionerror_messager   r   r   __call__y   s   zConfigurationChecker.__call__c                 C   s   | j r
t| j  d S d S rC   )rD   rA   rE   r   r   r   validate}   s   zConfigurationChecker.validateN)__name__
__module____qualname__rG   rK   rL   r   r   r   r   rB   u   s    rB   rC   )r.   r   r   r$   r)   r=   rA   rB   r   r   r   r   <module>   s    "

