o
    ߥi|$                     @   s   d dl Z d dlmZ d dlZd dlmZ d dlZG dd dejZG dd dejZ	G dd dejZ
G d	d
 d
ejZG dd dejZG dd dejZdS )    N)OrderedDictc                       *   e Zd ZdZd fdd	Zdd Z  ZS )PromptaQ  The implementation of vision prompt tuning method.

    Visual prompt tuning (VPT) is proposed to initialize tunable prompt tokens
    and prepend to the original tokens in the first layer or multiple layers.
    'Visual Prompt Tuning' by Jia et al.(2022)
    See https://arxiv.org/abs/2203.12119

    Attributes:
        dim: An integer indicating the embedding dimension.
        layer_num: An integer indicating number of layers.
        prompt_length: An integer indicating the length of vision prompt tuning.
        prompt_type: A string indicating the type of vision prompt tuning.
    Nc                    sN   t t|   || _|| _|| _|| _tt	
d||| _tj| j d S N   )superr   __init__dim	layer_numprompt_lengthprompt_typenn	Parametertorchzerosprompt_tokeninitxavier_uniform_)selfr	   r
   r   r   	__class__ e/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/cv/vision_efficient_tuning/petl.pyr      s   zPrompt.__init__c                 C   sl   |j \}}}| j|dd}| jdkrtj||fdd}|S tj|d d d | j d d f |fdd}|S )Nr   r   r	   )shaper   expandr
   r   catr   )r   xBNCr   r   r   r   forward#   s   
,zPrompt.forwardNN__name__
__module____qualname____doc__r   r"   __classcell__r   r   r   r   r   
   s    
r   c                       s<   e Zd ZdZddejf fdd	Zdd Zd	ddZ  Z	S )
Adaptera  The implementation of adapter tuning method.

    Adapters project input tokens by an MLP layer.
    'Parameter-Efficient Transfer Learning for NLP' by Houlsby et al.(2019)
    See http://arxiv.org/abs/1902.00751

    Attributes:
        dim: An integer indicating the embedding dimension.
        adapter_length: An integer indicating the length of adapter tuning.
        adapter_type: A string indicating the type of adapter tuning.
    Nc                    sP   t t|   || _|| _|| _t||| _| | _	t||| _
|   d S N)r   r*   r   r	   adapter_lengthadapter_typer   Linearln1activateln2init_weights)r   r	   r,   r-   	act_layerr   r   r   r   ;   s   zAdapter.__init__c                 C   s   dd }|  | d S )Nc                 S   s4   t | tjrtj| j tjj| jdd d S d S )Ngư>)std)
isinstancer   r.   r   r   weightnormal_bias)mr   r   r   _init_weightsM   s   z+Adapter.init_weights.<locals>._init_weights)apply)r   r:   r   r   r   r2   K   s   zAdapter.init_weightsc                 C   s.   |  | | |}|d u r|}|| }|S r+   )r1   r0   r/   )r   r   identityoutr   r   r   r"   T   s
   zAdapter.forwardr+   )
r%   r&   r'   r(   r   GELUr   r2   r"   r)   r   r   r   r   r*   .   s    	r*   c                       .   e Zd ZdZ		d fdd	Zdd Z  ZS )LoRAa'  The implementation of LoRA tuning method.

    LoRA constructs an additional layer with low-rank decomposition matrices of the weights in the network.
    'LoRA: Low-Rank Adaptation of Large Language Models' by Hu et al.(2021)
    See https://arxiv.org/abs/2106.09685

    Attributes:
        dim: An integer indicating the embedding dimension.
        num_heads: An integer indicating number of attention heads.
        lora_length: An integer indicating the length of LoRA tuning.
        lora_type: A string indicating the type of LoRA tuning.
    Nc                    s|   t t|   || _|| _tj||dd| _tjj	| jj
tdd tj||d dd| _tj| jj
 || _|| _d S )NFr8      )a   )r   r@   r   r	   	num_headsr   r.   lora_ar   kaiming_uniform_r6   mathsqrtlora_bzeros_lora_length	lora_type)r   r	   rE   rL   rM   r   r   r   r   j   s   
zLoRA.__init__c                 C   sz   |j \}}}| | |}|||d| j|| j ddddd}|d\}	}
}||	 ||
 || }}}|||fS )NrD      r   r      )r   rJ   rF   reshaperE   permuteunbind)r   r   qkvr   r    r!   	qkv_deltaq_deltak_deltav_deltar   r   r   r"   |   s   

zLoRA.forwardr#   r$   r   r   r   r   r@   \   s    r@   c                       r?   )Prefixa'  The implementation of prefix tuning method.

    Prefix tuning optimizes the task-specific vector in the multi-head attention layer.
    'Prefix-tuning: Optimizing continuous prompts for generation' by Li & Liang(2021)
    See https://arxiv.org/abs/2101.00190

    Attributes:
        dim: An integer indicating the embedding dimension.
        num_heads: An integer indicating number of attention heads.
        prefix_length: An integer indicating the length of prefix tuning.
        prefix_type: A string indicating the type of prefix tuning.
    Nc                    sr   t t|   || _|| _|| _|| _tt	
d||| _tt	
d||| _tj| j tj| j d S r   )r   rZ   r   r	   rE   prefix_lengthprefix_typer   r   r   r   
prefix_keyprefix_valuer   r   )r   r	   rE   r[   r\   r   r   r   r      s   zPrefix.__init__c           
      C   s   |j \}}}| j|dd|| j| j| j| j dddd}| j|dd|| j| j| j| j dddd}	t	j
||fddt	j
||	fdd}}|||fS )Nr   r   rN   r   rD   r   )r   r]   r   rP   r[   rE   r	   rQ   r^   r   r   )
r   r   rS   rT   rU   r   r    r!   r]   r^   r   r   r   r"      s"   





zPrefix.forwardr#   r$   r   r   r   r   rZ      s    rZ   c                       r   )SideTunea  The implementation of vision side-tuning method.

    Side-Tuning only needs to train one side network and
    weights the output of pre-trained model and side network.
    'Side-Tuning: A Baseline for Network Adaptation via Additive Side Networks'
    by Zhang et al.(2019)
    See https://arxiv.org/abs/1912.13503

    Attributes:
        sidetune_length: An integer indicating the linear dimension.
        sidetune_type: A string indicating the type of side network.
    Nc                    s   t t|   || _|| _| dkrt| jd| _| dkrFtj	j
dd}ttd|jfd|jfdt fd	tjd
| jddfg| _ttd| _d S )Nfcn4)out_dimsalexnetT)
pretrainedfeaturesavgpoolflattenfci $  FrA   g        )r   r_   r   sidetune_lengthsidetune_typelowerFCN4sidetorchvisionmodelsrb   r   
Sequentialr   rd   re   Flattenr.   r   r   tensoralpha)r   rh   ri   mmr   r   r   r      s   
zSideTune.__init__c                 C   s.   t | j}| |}|| d| |  }|S r   )r   sigmoidrr   rl   )r   r   x_basealpha_squashedx_sidex_outr   r   r   r"      s   
zSideTune.forwardr#   r$   r   r   r   r   r_      s    r_   c                       r   )rk   z@The implementation of simple FCN4 network for side network.
    r   c                    s  t t| jdi | ttjddddddddtddt | _ttjddddddddtddt | _	ttjddddddddtddt | _
ttjdd	ddddddtdd	t | _td
| _|dkr~td	|| _d S d | _d S )NrD      r   F)kernel_sizestridepaddingr8   dilationrN   r       @   )r   r   r   )r   rk   r   r   ro   Conv2d	GroupNormReLUconv1conv2conv3conv4AdaptiveAvgPool2dpoolr.   rg   )r   ra   kwargsr   r   r   r      sj   				
zFCN4.__init__c                 C   s\   |  |}| |}| |}| |}| |}||dd}| jd ur,| |}|S )Nr   r   )r   r   r   r   r   viewsizerg   )r   r   r   r   r   r"   	  s   






zFCN4.forward)r   r$   r   r   r   r   rk      s    -rk   )rH   collectionsr   r   torch.nnr   rm   Moduler   r*   r@   rZ   r_   rk   r   r   r   r   <module>   s   $.+,%