
    OiP                         d Z ddlmZmZ ddlmZ 	 ddlmZ dZdefd	Zdefd
Zdeeef   fdZy# e	$ r dZ ej                  d       Y 3w xY w)z
LoRA Utilities for ACE-Step

Provides utilities for inspecting and merging LoRA adapters.
Uses PEFT (Parameter-Efficient Fine-Tuning) library for LoRA implementation.
    )DictAny)logger)	PeftModelTFz@PEFT library not installed. LoRA training will not be available.returnc                      t         S )z#Check if PEFT library is available.)PEFT_AVAILABLE     R/mnt/workspace/acestep.cpp/tests/../../ACE-Step-1.5/acestep/training/lora_utils.pycheck_peft_availabler      s    r   c                     t        | d      rLt        | j                  d      r6| j                  j                         | _        t        j                  d       | S t        j
                  d       | S )a  Merge LoRA weights into the base model.

    This permanently integrates the LoRA adaptations into the model weights.
    After merging, the model can be used without PEFT.

    Args:
        model: Model with LoRA adapters

    Returns:
        Model with merged weights
    decodermerge_and_unloadz#LoRA weights merged into base modelz#Model does not support LoRA merging)hasattrr   r   r   infowarning)models    r   merge_lora_weightsr      sX     ui WU]]<N%O6689: L 	<=Lr   c                 D   dddg d}d}d}g }| j                         D ][  \  }}||j                         z  }d|v s||j                         z  }|j                  dd      d   }||vsK|j                  |       ] ||d<   ||d<   |dkD  |d	<   ||d
<   |dkD  r||z  |d<   |S )zGet information about LoRA adapters in the model.

    Args:
        model: Model to inspect

    Returns:
        Dictionary with LoRA information
    Fr   )has_loralora_paramstotal_paramsmodules_with_loralora_z.lora_   r   r   r   r   
lora_ratio)named_parametersnumelrsplitappend)r   r   r   r   lora_modulesnameparammodule_names           r   get_lora_infor&   .   s     	D LKL--/e%d?5;;=(K++h215K,.##K0 0 (D%D"QD ,D	a(<7\Kr   N)__doc__typingr   r   logurur   peftr   r	   ImportErrorr   boolr   r   strr&   r
   r   r   <module>r.      ss     WNd 
 *$DcN $?  WNFNNUVWs   2 AA