CoolFace
Apppublic

Aluode/PerceptionLabPortable

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

2.�Yi.-�@s�ddlmZddlmZddlZddlmZddlmZddl	m3Z4mZmZm
Z
ddlmZddlmZdd	lmZmZmZmZe�e�ZGd5d�dej�ZeGdd
�d
��ZeGdd�d��ZeGdd�d��ZdS)�)�partial)�OptionalN�)�Cache)�BaseModelOutputWithPast�QuestionAnsweringModelOutput� SequenceClassifierOutputWithPast�TokenClassifierOutput)�	AutoModel)�Unpack)�TransformersKwargs�auto_docstring�can_return_tuple�loggingcs$eZdZdZdZ�fdd�Z�ZS)�GradientCheckpointingLayeraBase class for layers with gradient checkpointing.6 7    This class enables gradient checkpointing functionality for a layer. By default, gradient checkpointing is disabled8    (`gradient_checkpointing = False`). When `model.set_gradient_checkpointing()` is called, gradient checkpointing is9    enabled by setting `gradient_checkpointing = True` and assigning a checkpointing function to `_gradient_checkpointing_func`.10 11    Important:12 13        When using gradient checkpointing with `use_reentrant=True`, inputs that require gradients (e.g. hidden states)14        must be passed as positional arguments (`*args`) rather than keyword arguments to properly propagate gradients.15 16        Example:17 18            ```python19            >>> # Correct - hidden_states passed as positional arg20            >>> out = self.layer(hidden_states, attention_mask=attention_mask)21 22            >>> # Incorrect - hidden_states passed as keyword arg23            >>> out = self.layer(hidden_states=hidden_states, attention_mask=attention_mask)24            ```25    Fcs|jr|jrd}|jj}d|�d�}d|vr$|dr$d|d<|d7}d}d|vr8|ddur8d|d<|d7}d}d	|vrL|d	durLd|d	<|d267}d}d|vr`|ddur`d|d<|d7}d}|rn|�d
�d}t�|�|jtt	�j27fi|��g|�R�St	�j28|i|��S)NFz7Caching is incompatible with gradient checkpointing in z	. Setting�	use_cachez `use_cache=False`,TZpast_key_valuez `past_key_value=None`,�past_key_valuesz `past_key_values=None`,Z29layer_pastz `layer_past=None`,�,�.)�gradient_checkpointingZtraining�	__class__�__name__�rstrip�logger�warning_onceZ_gradient_checkpointing_funcr�super�__call__)�self�args�kwargsZdo_warnZ30layer_name�message�r���E:\DocsHouse\542 percep lab latest\PerceptionLab\PerceptionLab_Portable\python_embed\Lib\site-packages\transformers/modeling_layers.pyr<s231"z#GradientCheckpointingLayer.__call__)r�32__module__�__qualname__�__doc__rr�
__classcell__r"r"r!r#r#src�eZdZdZ�fdd�Zee							ddeej	deej33deej	deed	eejd34eej	dee
deed
efdd���Z�ZS)� GenericForSequenceClassification�modelcsJt��|�|j|_t||jt�|��tj|j	|jdd�|_35|��dS)NF)Zbias)r�__init__�36num_labels�setattr�base_model_prefixr37�from_config�nn�Linear�hidden_size�score�	post_init�r�configr!r"r#r+es38z)GenericForSequenceClassification.__init__N�	input_ids�attention_mask�position_idsr�
inputs_embeds�labelsrr�returncKs2t||j�|f|||||d�|��}	|	j}39|�|40�}|dur$|jd}n|jd}|jjdur7|dkr7td��|jjdur@d}
n1|dure||jjk�|j	t41j�}t42j|jd|j	t43jd�}||�
d�}
nd}
t�|jj�d��|t44j||j	d�|
f}d}|dur�|j||||jd	�}t|||	j|	j|	jd45�S)N�r8r9rr:rrrz=Cannot handle batch sizes > 1 if no padding token is defined.�����)�deviceZdtypez� will not detect padding tokens in `inputs_embeds`. Results may be unexpected if using padding tokens in conjunction with `inputs_embeds.`)r?)�logitsr;�
pooled_logitsr6)�lossr@r�
hidden_states�46attentions)�getattrr.�last_hidden_stater3�shaper6Zpad_token_id�47ValueError�tor?�torchZint32ZarangeZargmaxrrrr�
loss_functionrrrCrD)rr7r8r9rr:r;rrZtransformer_outputsrCr@Z48batch_sizeZlast_non_pad_tokenZnon_pad_maskZ
token_indicesrArBr"r"r#�forwardosN49
���	5051��z(GenericForSequenceClassification.forward�NNNNNNN)rr$r%r.r+rr
rrJ�52LongTensor�Tensorr�FloatTensor�boolrrrrLr'r"r"r!r#r)as>53��������	�54�r)cs�eZdZdZ�fdd�Zdd�Zdd�Zee							dd	e	e55jd56e	e57jde	e58jde	e
d
e	e59jde	e60jde	e61jdeedefdd���Z�ZS)�GenericForQuestionAnsweringr*cs<t��|�t||jt�|��t�|jd�|_	|�62�dS)N�)rr+r-r.r63r/r0r1r2�64qa_outputsr4r5r!r"r#r+�sz$GenericForQuestionAnswering.__init__cCst||j�jS�N�rEr.Zembed_tokens)rr"r"r#�get_input_embeddings�sz0GenericForQuestionAnswering.get_input_embeddingscCs|t||j�_dSrUrV)r�valuer"r"r#�set_input_embeddings�sz0GenericForQuestionAnswering.set_input_embeddingsNr7r8r9rr:�start_positions�
end_positionsrr<cKs�t||j�|f||||d�|��}	|	j}65|�|66�}|jddd�\}}
|�d���}|
�d���}
d}|durF|durF|j||
||fi|��}t|||
|	j	|	j67d�S)N)r8r9rr:rr>)�dim)rB�start_logits�68end_logitsrCrD)rEr.rFrT�splitZsqueeze�69contiguousrKrrCrD)rr7r8r9rr:rZr[r�outputs�sequence_outputr@r]r^rBr"r"r#rL�s270
���	71�z#GenericForQuestionAnswering.forwardrM)rr$r%r.r+rWrYrr
rrJrNrOrrPrrrrLr'r"r"r!r#rR�sB	��������	�72�rRcr()�GenericForTokenClassificationr*cs�t��|�|j|_t||jt�|��t|dd�dur |j}nt|dd�dur,|j	}nd}t73�|�|_t74�
|j|j�|_|��dS)N�classifier_dropout�hidden_dropoutg�������?)rr+r,r-r.r75r/rErdrer0ZDropout�dropoutr1r2r3r4)rr6rdr!r"r#r+�sz&GenericForTokenClassification.__init__Nr7r8r9rr:r;rrr<c
Ksnt||j�|f|||||d�|��}	|	j}76|�|77�}78|�|79�}d}|dur-|�|||j�}t|||	j|	j	d�S)Nr=)rBr@rCrD)80rEr.rFrfr3rKr6r	rCrD)
rr7r8r9rr:r;rrrarbr@rBr"r"r#rL�s.81
���	8283�z%GenericForTokenClassification.forwardrM)rr$r%r.r+rr
rrJrNrOrrPrQrrr	rLr'r"r"r!r#rc�s>��������	�84�rc)�	functoolsr�typingrrJZtorch.nnr0Zcache_utilsrZmodeling_outputsrrrr	Zmodels.autor85Zprocessing_utilsr�utilsrr
rrZ86get_loggerrr�Modulerr)rRrcr"r"r"r#�<module>s"
87>J<
Aluode/PerceptionLabPortable · CoolFace