📄 simple_layers.cpython-312.pyc
/home/palash/git/iron_learn/python_scripts/transformer/__pycache__/simple_layers.cpython-312.pyc
Language: pyc • Lines: 110
�

���i����ddlZ	ejjd�j	�Gd�d�ZGd�d�Z	Gd�d�Z
Gd	�d
�Zy#e$rZede���YdZ[�>dZ[wwxYw)�NzGPU not found or error: c��eZdZd�Zd�Zd�Zy)�Linearc��||_||_tjj	||�tj
d|z�z|_y)Ng@)�in_features�out_features�np�random�randn�sqrt�weight)�selfrrs   �G/home/palash/git/iron_learn/python_scripts/transformer/simple_layers.py�__init__zLinear.__init__	s<��&���(����i�i�o�o�k�<�@�2�7�7�3�Q\�K\�C]�]���c�P�||_tj||j�S�N)�inputsr�dotr�r
�xs  r�forwardzLinear.forwards������v�v�a����%�%rc���tj||jj�}tj|jj|�}|xj||zzc_|Sr)rrr�Tr)r
�grad_output�
learning_rate�
grad_input�grad_weights     r�backwardzLinear.backwardsK���V�V�K������7�
��f�f�T�[�[�]�]�K�8�����}�{�2�2���rN��__name__�
__module__�__qualname__rrr�rrrrs��^�
&�rrc��eZdZd�Zd�Zd�Zy)�ReLUc��d|_yr��input�r
s rrz
ReLU.__init__s	����
rc�<�||_tjd|�S�Nr)r(r�maximumrs  rrzReLU.forwards����
��z�z�!�Q��rc�*�d||jdk<|Sr+r')r
rs  rrz
ReLU.backward s��'(��D�J�J�!�O�$��rNrr#rrr%r%s��� �rr%c��eZdZd�Zd�Zd�Zy)�Softmaxc��d|_yr)�outputr)s rrzSoftmax.__init__%s	����rc��tj|tj|dd��z
�}|tj|dd��z|_|jS�N�����T)�axis�keepdims)r�exp�max�sumr1)r
r�exp_xs   rrzSoftmax.forward(sE�����q�2�6�6�!�"�t�<�<�=���b�f�f�U��d�C�C����{�{�rc�p�|j|tj||jzdd��z
z}|Sr3)r1rr9)r
rrs   rrzSoftmax.backward-s3���[�[�K�"�&�&��t�{�{�9R�Y[�fj�2k�$k�l�
��rNrr#rrr/r/$s����
rr/c��eZdZd�Zd�Zd�Zy)�CrossEntropyLossc� �d|_d|_yr)�predictions�targetsr)s rrzCrossEntropyLoss.__init__2s�������rc�@�||_||_|jjd}|jtj|�|jf}tj
|dd�}tj|dz�}tj|�|z}|S)NrgV瞯�<g�������?)r?r@�shaper�arange�clip�logr9)r
r?r@�m�p�log_likelihood�losss       rrzCrossEntropyLoss.forward6s���&�������L�L���q�!�����R�Y�Y�q�\�4�<�<�7�8���G�G�A�u�i�(���&�&��U��+�+���v�v�n�%��)���rc���|jjd}|jj�}|t	j
|�|jfxxdzcc<||z}|S)Nr�)r@rBr?�copyrrC)r
rF�grads   rrzCrossEntropyLoss.backward@sY���L�L���q�!�����$�$�&���R�Y�Y�q�\�4�<�<�
'�(�A�-�(���	���rNrr#rrr=r=1s����rr=)�cupyr�cuda�Device�use�	Exception�e�printrr%r/r=r#rr�<module>rUst���*��G�G�N�N�1������� 
�
������Y�*�	�$�Q�C�
(�)�)��*�s�)A�A2�A-�-A2