o
    ٷi	                     @  sv   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZmZmZmZ dddZd ddZ	d!d"ddZdS )#    )annotationsN)PretrainedConfig)
LoRAConfig)%DiffusionColumnParallelLinearWithLoRA+DiffusionMergedColumnParallelLinearWithLoRA(DiffusionMergedQKVParallelLinearWithLoRA"DiffusionQKVParallelLinearWithLoRA!DiffusionReplicatedLinearWithLoRA"DiffusionRowParallelLinearWithLoRAmodule_namestrtarget_modules	list[str]returnboolc                   s    ddl t fdd|D S )zFfrom vllm/lora/model_manager.py _match_target_modules, helper functionr   Nc                 3  s,    | ]} d | d p| kV  qdS )z.*\.$N)match).0target_moduler   re R/home/ubuntu/.local/lib/python3.10/site-packages/vllm_omni/diffusion/lora/utils.py	<genexpr>   s
    
z(_match_target_modules.<locals>.<genexpr>)regexany)r   r   r   r   r   _match_target_modules   s   r   supported_modulesset[str]packed_modules_mappingdict[str, list[str]] | Nonec                 C  s8   t | }|s|S | D ]\}}|| v r|| q|S )u  Expand expected LoRA module suffixes for packed (fused) projections.

    Some diffusion models use packed projections like `to_qkv` or `w13`, while
    LoRA checkpoints are typically saved against the logical sub-projections
    (e.g. `to_q`/`to_k`/`to_v`, `w1`/`w3`). The packed layer name is present in
    `supported_modules`, but the sublayer names are not. Expanding the set
    ensures these sublayer keys are not dropped when loading a LoRA checkpoint.

    The packed→sublayer mapping is model-specific (see each diffusion model's
    `packed_modules_mapping`) so new packed layers are added alongside the model
    implementation rather than hard-coded in the LoRA framework.
    )setitemsupdate)r   r   expandedpacked_name	sub_namesr   r   r   *_expand_expected_modules_for_packed_layers   s   
r'   layer	nn.Module	max_lorasintlora_configr   packed_modules_listmodel_configPretrainedConfig | Nonec                 C  sN   t tttttg}|D ]}|j| |||dr$|| }|||| |  S q
| S )zN
    Diffusion-specific layer replacement. similar to vLLM's `from_layer`
    )source_layerr,   r-   r.   )r   r   r   r   r
   r	   can_replace_layercreate_lora_weights)r(   r*   r,   r-   r.   diffusion_lora_classeslora_clsinstancer   r   r   from_layer_diffusion9   s&   	
r6   )r   r   r   r   r   r   )r   r   r   r    r   r   )N)r(   r)   r*   r+   r,   r   r-   r   r.   r/   r   r)   )
__future__r   torch.nnnntransformersr   vllm_omni.config.lorar   vllm_omni.diffusion.lora.layersr   r   r   r   r	   r
   r   r'   r6   r   r   r   r   <module>   s    



 