
    OiP                         S r SSKJrJr  SSKJr   SSKJr  SrS\4S	 jrS\4S
 jrS\\\4   4S jrg! \	 a    Sr\R                  " S5         N7f = f)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                      [         $ )z#Check if PEFT library is available.)PEFT_AVAILABLE     :/mnt/workspace/ACE-Step-1.5/acestep/training/lora_utils.pycheck_peft_availabler      s    r   c                     [        U S5      (       aR  [        U R                  S5      (       a7  U R                  R                  5       U l        [        R                  " S5        U $ [        R
                  " S5        U $ )zMerge 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      s^     ui  WU]]<N%O%O6689: L 	<=Lr   c                 F   SSS/ S.nSnSn/ nU R                  5        H^  u  pVX&R                  5       -  nSU;   d  M  X6R                  5       -  nUR                  SS5      S   nXt;  d  MM  UR                  U5        M`     X!S'   X1S'   US:  US	'   XAS
'   US:  a  X2-  US'   U$ )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--/%d?;;=(K++h215K.##K0 0 (%"QD ,	a(7\Kr   N)__doc__typingr   r   logurur   peftr   r	   ImportErrorr   boolr   r   strr&   r
   r   r   <module>r.      sq     WNd 
 *$DcN $?  WN
NNUVWs   5 AA