CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
modular_phi.cpython-310.pyc77 linesDownload Raw Back to __pycache__
1o

2/�Yib-�@sjddlmZmZddlZddlmZddlmZmZddl	m3Z4ddlmZddl
mZddlmZdd	lmZdd5lmZmZddlmZdd
lmZddlmZmZmZmZmZm Z m!Z!m"Z"ddl#m$Z$e�%e&�Z'dZ(dZ)Gdd�de�Z*Gdd�de�Z+Gdd�de�Z,Gdd�de �Z-Gdd�de�Z.Gdd�de�Z/Gdd �d e�Z0Gd!d"�d"e�Z1gd#�Z2dS)$�)�Callable�OptionalN�)�Cache�DynamicCache)�create_causal_mask)�GradientCheckpointingLayer)�BaseModelOutputWithPast)�ALL_ATTENTION_FUNCTIONS)�Unpack)�TransformersKwargs�logging)�deprecate_kwarg�)�CLIPMLP)�LlamaAttention�LlamaForCausalLM�LlamaForSequenceClassification�LlamaForTokenClassification�6LlamaModel�LlamaRotaryEmbedding�apply_rotary_pos_emb�eager_attention_forward�)�	PhiConfigzmicrosoft/phi-1rcs�eZdZdedef�fdd�Zedddd�				dd7ejde	ejejfde8ejde9ed
e10ejde	eje11ejffdd��Z
�ZS)�PhiAttention�config�	layer_idxcs�t��||�tj|j|j|jdd�|_tj|j|j|jdd�|_	tj|j|j|jdd�|_12tj|j|j|jdd�|_|`t
|j|j�|_|j|_|jrotj|j|j|jdd�|_tj|j|j|jdd�|_dSdS)NT�Zbias)�epsZelementwise_affine)�super�__init__�nn�Linear�hidden_sizeZnum_attention_heads�head_dim�q_projZnum_key_value_heads�k_proj�v_proj�denseZo_proj�intZpartial_rotary_factor�rotary_ndims�qk_layernorm�	LayerNorm�layer_norm_eps�q_layernorm�k_layernorm��selfrr��	__class__���E:\DocsHouse\542 percep lab latest\PerceptionLab\PerceptionLab_Portable\python_embed\Lib\site-packages\transformers/models/phi/modular_phi.pyr!%s ���zPhiAttention.__init__�past_key_value�past_key_values�4.58��new_name�versionN�
hidden_states�position_embeddings�attention_mask�cache_position�returncKs�|jdd�}g|�d�|j�R}|�|��|��dd�}	|�|��|��dd�}13|�|��|��dd�}|jrB|�|	�}	|�	|14�}15|\}}
|	dd|j16�f|	d|j17d�f}}|18dd|j19�f|20d|j21d�f}}t||||
�\}}tj
||fdd�}	tj
||fdd�}22|dur�|
||d�}|�|23||j|�\}24}t}|jjdkr�t|jj}|||	|25||f|js�dn|j|jd	�|��\}}|jg|�d�R���}|�|�}||fS)26N�����rr.)�dim)�sin�cosr@�eagerg)Zdropout�scaling)�shaper%r&�viewZ	transposer'r(r,r/r0r+r�torch�cat�updaterrrZ_attn_implementationr27�trainingZattention_dropoutrGZreshape�28contiguousr))r2r=r>r?r8r@�kwargsZinput_shapeZhidden_shapeZquery_statesZ29key_statesZvalue_statesrErDZ	query_rotZ30query_passZkey_rotZkey_passZcache_kwargsZattention_interfaceZattn_outputZattn_weightsr5r5r6�forward6sN313233����34�35zPhiAttention.forward)NN)�__name__�36__module__�__qualname__rr*r!rrJ�Tensor�tuplerr�37LongTensorrP�
__classcell__r5r5r3r6r$s$�������rc@�eZdZdS)�PhiMLPN�rQrRrSr5r5r5r6rYu�rYcs�eZdZdedef�fdd�Zedddd�							38	39				ddejde	ejd
e	ej40de	ede	ede	ede	ej41de	e
ejejfde
eje	e
ejejfffdd��Z�ZS)�PhiDecoderLayerrrcsHt���t||d�|_t|�|_tj|j|j	d�|_42t�|j�|_
dS)N)r�r)r r!r�	self_attnrY�mlpr"r-r$r.�input_layernorm�DropoutZresid_pdrop�
resid_dropoutr1r3r5r6r!zs434445zPhiDecoderLayer.__init__r7r8r9r:NFr=r?�position_ids�output_attentions�	use_cacher@r>rAc	Ksr|}46|�|�}|jd||||||||d�|	��\}}|�|�}|�|�|��}
||
|47}|f}|r7||f7}|S)N)r=r?rcr8rdrer@r>r5)r`r^rbr_)r2r=r?rcr8rdrer@r>rOZresidualZattn_outputsZself_attn_weightsZfeed_forward_hidden_statesZoutputsr5r5r6rP�s*
48�	495051zPhiDecoderLayer.forward)NNNFFNN)rQrRrSrr*r!rrJrTrrVr�boolrU�FloatTensorrPrWr5r5r3r6r\ys:��������	��r\c@rX)�PhiRotaryEmbeddingNrZr5r5r5r6rh�r[rhcs�eZdZdef�fdd�Z									ddeejdeejdeejdee	d	eej52d53eedeedeed
eejdee
defdd�Z�ZS)�PhiModelrcsVt����t��fdd�t�j�D��|_t��j�|_	tj54�j�jd�|_
|`dS)Ncsg|]}t�|��qSr5)r\)�.0r�rr5r6�55<listcomp>�sz%PhiModel.__init__.<locals>.<listcomp>r])r r!r"Z56ModuleList�range�num_hidden_layers�layersraZ57embd_pdrop�
embed_dropoutr-r$r.�final_layernormZnorm�r2rr3rkr6r!�s�zPhiModel.__init__N�	input_idsr?rcr8�
inputs_embedsrerd�output_hidden_statesr@rOrAc58Ks�|dur|n|jj}|dur|n|jj}|dur|n|jj}|du|duAr*td��|jr9|jr9|r9t�d�d}|durB|�	|�}|rN|durNt59|jd�}|	durj|durZ|��nd}tj
|||jd|jd�}	|durs|	�d�}t|j|||	||d�}|�|�}|}
|�|
|�}|r�d	nd}|r�d	nd}|jd|jj�D]&}|r�||
f7}||
f||||||	|d60�|61��}|d}
|r�||df7}q�|�|
�}
|r�||
f7}t|
|r�|nd||d�S)Nz:You must specify exactly one of input_ids or inputs_embedszX`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`.Frkrr)�device)rZinput_embedsr?r@r8rcr5)r?rcr8rdrer@r>)Zlast_hidden_stater8r=Z62attentions)rrdrure�63ValueErrorZgradient_checkpointingrM�loggerZwarning_onceZembed_tokensrZget_seq_lengthrJZarangerHrvZ	unsqueezerrpZ64rotary_embrornrqr	)r2rsr?rcr8rtrerdrur@rOZpast_seen_tokensZcausal_maskr=r>Zall_hidden_statesZall_self_attnsZ
decoder_layerZ
layer_outputsr5r5r6rP�s�
��65�66�67	68��	��697071�zPhiModel.forward)	NNNNNNNNN)rQrRrSrr!rrJrVrTrrgrfrrr	rPrWr5r5r3r6ri�sD��������	�72���ricseZdZ�fdd�Z�ZS)�PhiForCausalLMcs&t��|�tj|j|jdd�|_dS)NTr)r r!r"r#r$Z73vocab_sizeZlm_headrrr3r5r6r!szPhiForCausalLM.__init__)rQrRrSr!rWr5r5r3r6rysryc@rX)�PhiForSequenceClassificationNrZr5r5r5r6rzr[rzc@rX)�PhiForTokenClassificationNrZr5r5r5r6r{#r[r{)ZPhiPreTrainedModelriryrzr{)3�typingrrrJZtorch.nnr"Zcache_utilsrrZ
masking_utilsrZmodeling_layersrZmodeling_outputsr	Zmodeling_utilsr74Zprocessing_utilsr�utilsrr
Zutils.deprecationrZclip.modeling_cliprZllama.modeling_llamarrrrrrrrZconfiguration_phirZ75get_loggerrQrxZ_CHECKPOINT_FOR_DOCZ_CONFIG_FOR_DOCrrYr\rhriryrzr{�__all__r5r5r5r6�<module>s4(76 77Q1k
Aluode/PerceptionLabPortable · CoolFace