
    qi                    r    d dl mZ d dlZd dlZd dlmZ ddlmZmZ ddl	m
Z
 erd dlmZ ddZd	 Zd
 Zd Zy)    )annotationsN)TYPE_CHECKING   )is_torch_available	strtobool)QuantizationMethod)nnc                    t               sydd l}|j                  j                         sydd l}t        | |j                  j                  j                        xs t        | dd      S )NFr   _is_fsdp_managed_module)	r   torchdistributedis_availabletorch.distributed.fsdp
isinstancefsdpFullyShardedDataParallelgetattr)moduler   s     P/opt/pipecat/venv/lib/python3.12/site-packages/transformers/integrations/fsdp.pyis_fsdp_managed_moduler      s]    ))+!fe//44MMN RY)5S     c                 D   t               rdd l} | j                  j                         xrv | j                  j	                         xrZ t        t        j                  j                  dd            dk(  xr, t        t        j                  j                  dd            dk(  S y)Nr   ACCELERATE_USE_FSDPFalse   FSDP_CPU_RAM_EFFICIENT_LOADINGF)	r   r   r   r   is_initializedr   osenvironget)r   s    r   is_fsdp_enabledr!   ,   s     **, Z!!002Z"**..)>HIQNZ "**..)I7STXYY		
 r   c                 n    ddl m}  dt        t        j                  |       j
                        v rddiS i S )z
    Returns checkpoint kwargs for FSDP model saving.

    Checks if the `adapter_only` parameter is supported by `save_fsdp_model` from accelerate
    and returns the appropriate kwargs.
    r   save_fsdp_modeladapter_onlyT)accelerate.utilsr$   listinspect	signature
parametersr#   s    r   get_fsdp_ckpt_kwargsr+   :   s5     1g//@KKLL%%	r   c                   ddl m} ddlm} t	        | j
                  |      r! ||       |j                  j                  _        t        | dd      t        j                  k(  rq| j                  j                  j                  j                  rF|j                  j                  j!                  | j                  j                  j                  d       yyy)aG  
    Updates the FSDP plugin for PEFT LoRA/QLoRA compatibility.

    When using FSDP with PEFT LoRA, the auto wrap policy needs to be updated to additionally wrap
    LoRA trainable layers separately. When using FSDP with QLoRA, the mixed precision policy needs
    to be updated to use the quantization storage data type.
    r   )
PeftConfig)fsdp_auto_wrap_policyquantization_methodNT)override)peftr-   peft.utils.otherr.   r   active_peft_configstatefsdp_pluginauto_wrap_policyr   r   BITS_AND_BYTEShf_quantizerquantization_configbnb_4bit_quant_storageis_floating_pointset_mixed_precision)modelacceleratorr-   r.   s       r   update_fsdp_plugin_peftr?   I   s      6%**J79Nu9U%%6,d37I7X7XX22II[[%%9922IITX 	: 	
 \ 	Yr   )r   z	nn.Modulereturnbool)
__future__r   r(   r   typingr   utilsr   r   utils.quantization_configr   r   r	   r   r!   r+   r?    r   r   <module>rG      s5    #  	   1 :  
r   