o
    ߥi                     @   s   d dl Zd dlZd dlm  mZ dd ZdddZddd	Z	dd
dZ
edkryeddgddggZeddgddggZe	eeZe	edZde
ed  Zee ee ee eeZeeZeeg ddZeeej dS dS )    Nc                    st   t |ttfr, fdd|D }t|dd}t j}|D ]}||d qt |S t |tr6 	|S t
d)z/ Implement multi-dimension unsqueeze function. c                    s*   g | ]}|d kr
|n|t  j d qS )r      )lenshape).0diminput a/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/nlp/space/modules/functions.py
<listcomp>   s    zunsqueeze.<locals>.<listcomp>T)reverser   z/Warning: type(dims) must in (list, tuple, int)!)
isinstancelisttuplesortedr   inserttorchreshapeint	unsqueeze
ValueError)r   dimsr   r   r	   r   r
   r      s   



r   r   绽|=c                 C   sF   t tjj| j }dt |t ||   }| | }t|| S )z$ Basic implement of gumbel_softmax. g        )	r   tensornprandomrandr   logFsoftmax)r   tauepsUgumbelyr	   r	   r
   gumbel_softmax   s   r%   c                 C   sP   |du rd}t | tjr|  } t |tjr| }t| ||}t|S )z+ Implement equal in dygraph mode. (paddle) Nfloat32)r   r   Tensornumpyr   equalastyper   )xr$   dtypeoutr	   r	   r
   r)   !   s   
r)   c                 C   s   dt | || S )z/ Implement not_equal in dygraph mode. (paddle) r   )r)   )r+   r$   r,   r	   r	   r
   	not_equal-   s   r.   __main__      )r   r   r   )r   )r   r   )N)r(   r   r   torch.nn.functionalnn
functionalr   r   r%   r)   r.   __name__r   abcc1floatdprintefgr   r	   r	   r	   r
   <module>   s*   





