Team Ai
Apppublic

xdecoder/Instruct-X-Decoder

sourceHugging Faceafl-3.0updated 3y agoView on Hugging Face
163likes
position_encoding.cpython-38.pyc27 linesDownload Raw Back to __pycache__
1U

2E$�c�	�@s6dZddlZddlZddlmZGdd�dej�ZdS)z33Various positional encodings for the transformer.4�N)�nncs6eZdZdZd
�fdd�	Zddd	�Zddd�Z�ZS)�PositionEmbeddingSinez�5    This is a more standard version of the position embedding, very similar to the one6    used by the Attention is all you need paper, generalized to work on images.7    �@�'FNcsPt���||_||_||_|dk	r4|dkr4td��|dkrFdtj}||_dS)NFz+normalize should be True if scale is passed�)	�super�__init__�
num_pos_feats�temperature�	normalize�8ValueError�math�pi�scale)�selfr	r9rr��	__class__��;/data/arXiv/demo/Demo/xdecoder/modules/position_encoding.pyrs1011zPositionEmbeddingSine.__init__c	Cs�|dkr4tj|�d�|�d�|�d�f|jtjd�}|}|jd|jd�}|jd|jd�}|jr�d}||dd�dd�dd�f||j}||dd�dd�dd�f||j}tj	|j12|j|jd	�}|jd|d|j13}|dd�dd�dd�df|}|dd�dd�dd�df|}	tj|dd�dd�dd�ddd�f�
�|dd�dd�dd�ddd�f��fd14d��d�}tj|	dd�dd�dd�ddd�f�
�|	dd�dd�dd�ddd�f��fd15d��d�}	tj|	|fdd��dddd�}16|17S)Nrr�)�device�dtype�)rg�����ư>�����)rr�)�dim)�torch�zeros�sizer�bool�cumsumrrr�aranger	r18�stack�sin�cos�flatten�cat�permute)r�x�maskZnot_maskZy_embedZx_embed�epsZdim_tZpos_xZpos_y�posrrr�forwards6,((  J��J��zPositionEmbeddingSine.forwardrcsZd|jj}d�|j�d�|j�d�|j�d�|j�g}|g�fdd�|D�}d�|�S)	NzPositional encoding znum_pos_feats: {}ztemperature: {}z
normalize: {}z	scale: {}csg|]}d�|�qS)� r)�.0�line��_repr_indentrr�19<listcomp>?sz2PositionEmbeddingSine.__repr__.<locals>.<listcomp>�20)r�__name__�formatr	r21rr�join)rr1�head�body�linesrr0r�__repr__6s22232425�zPositionEmbeddingSine.__repr__)rrFN)N)r)r4�26__module__�__qualname__�__doc__rr,r:�
__classcell__rrrrrs27r)r=r
rr�Modulerrrrr�<module>s