CoolFace
Apppublic

fred-dev/comfy_ui_ali

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
ops.cpython-312.pyc82 linesDownload Raw Back to __pycache__
1�

2���gj;����dZddlZddlZddlZddlmZmZddlZejjZ3dd�Zdd�ZGd�d�Z
Gd�d	�ZGd4�de�Zd�ZGd
�de�Zdd�Zdd�Zy)a�5    This file is part of ComfyUI.6    Copyright (C) 2024 Stability AI7 8    This program is free software: you can redistribute it and/or modify9    it under the terms of the GNU General Public License as published by10    the Free Software Foundation, either version 3 of the License, or11    (at your option) any later version.12 13    This program is distributed in the hope that it will be useful,14    but WITHOUT ANY WARRANTY; without even the implied warranty of15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the16    GNU General Public License for more details.17 18    You should have received a copy of the GNU General Public License19    along with this program.  If not, see <https://www.gnu.org/licenses/>.20�N)�args�PerformanceFeaturec�r�tjj||j|j||��S)N��non_blocking�copy)�comfy�model_management�cast_to�dtype�device)�weight�inputrrs    �T/Users/fredrodrigues/Library/CloudStorage/Dropbox/Code/comfyui_hardware/comfy/ops.py�
cast_to_inputrs.���!�!�)�)�&�%�+�+�u�|�|�Zf�mq�)�r�r�c�,�|� |�|j}|�|}|�|j}d}tjj	|�}|j21�at
|j�dkD}tjj|j22||||��}|r|jD]23}||�}�t
|j�dkD}tjj|j||||��}	|r|jD]24}||	�}	�|	|fS)Nrr)rr
r	r25�device_supports_non_blocking�bias�len�
bias_functionr�weight_functionr)26�srrr
�27bias_dtyperr�has_function�frs28          r�cast_bias_weightrs�����=��K�K�E����J��>��\�\�F��D��)�)�F�F�v�N�L��v�v���1�?�?�+�a�/���%�%�-�-�a�f�f�j�&�Wc�jv�-�w����_�_����w��%��q�(�(�)�A�-�L�
�
#�
#�
+�
+�A�H�H�e�V�R^�eq�
+�
r�F���"�"�A��v�Y�F�#��4�<�rc��eZdZdZgZgZy)�CastWeightBiasOpFN)�__name__�29__module__�__qualname__�comfy_cast_weightsrr�rrrr7s�����O��Mrrc�\�eZdZGd�dejj30e�ZGd�dejje�ZGd�dejje�ZGd�dejje�Z	Gd	�d31ejje�Z32Gd�dejje�ZGd
�dejje�ZGd�dejje�Z
Gd�dejje�Zed��Zy)�disable_weight_initc�*��eZdZd�Zd�Z�fd�Z�xZS)�disable_weight_init.Linearc��y�Nr$��selfs r�reset_parametersz+disable_weight_init.Linear.reset_parameters>���rc�v�t||�\}}tjjj	|||�Sr*)r�torch�nn�33functional�linear�r,rrrs    r�forward_comfy_cast_weightsz5disable_weight_init.Linear.forward_comfy_cast_weightsAs1��+�D�%�8�L�F�D��8�8�&�&�-�-�e�V�T�B�Brc���|js0t|j�dkDst|j�dkDr|j|i|��St�|�|i|��S�Nr�r#rrrr5�super�forward�r,r�kwargs�	__class__s   �rr:z"disable_weight_init.Linear.forwardE�]����&�&�#�d�.B�.B�*C�a�*G�3�t�Oa�Oa�Kb�ef�Kf�6�t�6�6��G��G�G��w���7��7�7r�r r!r"r-r5r:�
__classcell__�r=s@r�Linearr(=s���	�	C�	8�	8rrBc�*��eZdZd�Zd�Z�fd�Z�xZS)�disable_weight_init.Conv1dc��yr*r$r+s rr-z+disable_weight_init.Conv1d.reset_parametersLr.rc�F�t||�\}}|j|||�Sr*�r�
_conv_forwardr4s    rr5z5disable_weight_init.Conv1d.forward_comfy_cast_weightsO�'��+�D�%�8�L�F�D��%�%�e�V�T�:�:rc���|js0t|j�dkDst|j�dkDr|j|i|��St�|�|i|��Sr7r8r;s   �rr:z"disable_weight_init.Conv1d.forwardSr>rr?rAs@r�Conv1drDK����	�	;�	8�	8rrKc�*��eZdZd�Zd�Z�fd�Z�xZS)�disable_weight_init.Conv2dc��yr*r$r+s rr-z+disable_weight_init.Conv2d.reset_parametersZr.rc�F�t||�\}}|j|||�Sr*rGr4s    rr5z5disable_weight_init.Conv2d.forward_comfy_cast_weights]rIrc���|js0t|j�dkDst|j�dkDr|j|i|��St�|�|i|��Sr7r8r;s   �rr:z"disable_weight_init.Conv2d.forwardar>rr?rAs@r�Conv2drNYrLrrRc�*��eZdZd�Zd�Z�fd�Z�xZS)�disable_weight_init.Conv3dc��yr*r$r+s rr-z+disable_weight_init.Conv3d.reset_parametershr.rc�F�t||�\}}|j|||�Sr*rGr4s    rr5z5disable_weight_init.Conv3d.forward_comfy_cast_weightskrIrc���|js0t|j�dkDst|j�dkDr|j|i|��St�|�|i|��Sr7r8r;s   �rr:z"disable_weight_init.Conv3d.forwardor>rr?rAs@r�Conv3drTgrLrrXc�*��eZdZd�Zd�Z�fd�Z�xZS)�disable_weight_init.GroupNormc��yr*r$r+s rr-z.disable_weight_init.GroupNorm.reset_parametersvr.rc��t||�\}}tjjj	||j34|||j�Sr*)rr0r1r2�35group_norm�36num_groups�epsr4s    rr5z8disable_weight_init.GroupNorm.forward_comfy_cast_weightsysA��+�D�%�8�L�F�D��8�8�&�&�1�1�%����&�RV�X\�X`�X`�a�arc���|js0t|j�dkDst|j�dkDr|j|i|��St�|�|i|��Sr7r8r;s   �rr:z%disable_weight_init.GroupNorm.forward}r>rr?rAs@r�	GroupNormrZus���	�	b�	8�	8rrac�*��eZdZd�Zd�Z�fd�Z�xZS)�disable_weight_init.LayerNormc��yr*r$r+s rr-z.disable_weight_init.LayerNorm.reset_parameters�r.rc���|j�t||�\}}nd}d}tjjj||j|||j�Sr*)rrr0r1r2�37layer_norm�normalized_shaper_r4s    rr5z8disable_weight_init.LayerNorm.forward_comfy_cast_weights�sX���{�{�&�/��e�<���������8�8�&�&�1�1�%��9N�9N�PV�X\�^b�^f�^f�g�grc���|js0t|j�dkDst|j�dkDr|j|i|��St�|�|i|��Sr7r8r;s   �rr:z%disable_weight_init.LayerNorm.forward�r>rr?rAs@r�	LayerNormrc�s���	�	h�	8�	8rric�,��eZdZd�Zdd�Z�fd�Z�xZS)�#disable_weight_init.ConvTranspose2dc��yr*r$r+s rr-z4disable_weight_init.ConvTranspose2d.reset_parameters�r.rc38�R�d}|j|||j|j|j||j�}t||�\}}tjjj||||j|j||j|j�S)N�)�_output_padding�stride�padding�kernel_size�dilationrr0r1r2�conv_transpose2d�groups�r,r�output_size�num_spatial_dims�output_paddingrrs       rr5z>disable_weight_init.ConvTranspose2d.forward_comfy_cast_weights���� ��!�1�1��{�D�K�K����t�?O�?O� �$�-�-�1�N�,�D�%�8�L�F�D��8�8�&�&�7�7��v�t�T�[�[�$�,�,�����T�]�]�<�
<rc���|js0t|j�dkDst|j�dkDr|j|i|��St�|�|i|��Sr7r8r;s   �rr:z+disable_weight_init.ConvTranspose2d.forward�r>rr*r?rAs@r�ConvTranspose2drk�����	�		<�	8�	8rr|c�,��eZdZd�Zdd�Z�fd�Z�xZS)�#disable_weight_init.ConvTranspose1dc��yr*r$r+s rr-z4disable_weight_init.ConvTranspose1d.reset_parameters�r.rc39�R�d}|j|||j|j|j||j�}t||�\}}tjjj||||j|j||j|j�S)N�)rorprqrrrsrr0r1r2�conv_transpose1drurvs       rr5z>disable_weight_init.ConvTranspose1d.forward_comfy_cast_weights�rzrc���|js0t|j�dkDst|j�dkDr|j|i|��St�|�|i|��Sr7r8r;s   �rr:z+disable_weight_init.ConvTranspose1d.forward�r>rr*r?rAs@r�ConvTranspose1dr�r}rr�c�,��eZdZd�Zdd�Z�fd�Z�xZS)�disable_weight_init.Embeddingc��d|_yr*)rr+s rr-z.disable_weight_init.Embedding.reset_parameters�s���D�I�rc	��|}|jjtjk(s'|jjtjk(rd}t||j|��\}}tjjj|||j|j|j|j|j�j|��S)N�r
r)r)rrr0�float16�bfloat16rr
r1r2�	embedding�padding_idx�max_norm�	norm_type�scale_grad_by_freq�sparse�to)r,r�	out_dtype�output_dtyperrs      rr5z8disable_weight_init.Embedding.forward_comfy_cast_weights�s���$�L��{�{� � �E�M�M�1�T�[�[�5F�5F�%�.�.�5X� �	�+�D����Y�W�L�F�D��8�8�&�&�0�0����@P�@P�RV�R_�R_�ae�ao�ao�qu�rI�rI�KO�KV�KV�W�Z�Z�am�Z�n�
nrc����|js0t|j�dkDst|j�dkDr|j|i|��Sd|vr|jd�t
�|�|i|��S)Nrr�)r#rrrr5�popr9r:r;s   �rr:z%disable_weight_init.Embedding.forward�sq����&�&�#�d�.B�.B�*C�a�*G�3�t�Oa�Oa�Kb�ef�Kf�6�t�6�6��G��G�G��&�(��J�J�{�+��w���7��7�7rr*r?rAs@r�	Embeddingr��s���	�	n�	8�	8rr�c�z�|dk(r|j|i|��S|dk(r|j|i|��Std|����)Nrn�zunsupported dimensions: )rRrX�40ValueError)r�dimsrr<s    r�conv_ndzdisable_weight_init.conv_nd�sO���1�9��1�8�8�T�,�V�,�,�
�Q�Y��1�8�8�T�,�V�,�,��7��v�>�?�?rN)r r!r"r0r1rBrrKrRrXrarir|r�r��classmethodr�r$rrr&r&<s���8������"2�8�8������"2�8�8������"2�8�8������"2�8�8�E�H�H�&�&�(8�8�8�E�H�H�&�&�(8�8�$8�%�(�(�2�2�4D�8�*8�%�(�(�2�2�4D�8�*8�E�H�H�&�&�(8�8�(�@��@rr&c��eZdZGd�dej�ZGd�dej41�ZGd�dej�ZGd�dej�ZGd	�d42ej�ZGd�dej�Z	Gd
�dej�Z43Gd�dej�ZGd�dej�Zy)�manual_castc��eZdZdZy)�manual_cast.LinearTN�r r!r"r#r$rrrBr�����!�rrBc��eZdZdZy)�manual_cast.Conv1dTNr�r$rrrKr��r�rrKc��eZdZdZy)�manual_cast.Conv2dTNr�r$rrrRr��r�rrRc��eZdZdZy)�manual_cast.Conv3dTNr�r$rrrXr��r�rrXc��eZdZdZy)�manual_cast.GroupNormTNr�r$rrrar��r�rrac��eZdZdZy)�manual_cast.LayerNormTNr�r$rrrir��r�rric��eZdZdZy)�manual_cast.ConvTranspose2dTNr�r$rrr|r��r�rr|c��eZdZdZy)�manual_cast.ConvTranspose1dTNr�r$rrr�r��r�rr�c��eZdZdZy)�manual_cast.EmbeddingTNr�r$rrr�r��r�rr�N)
r r!r"r&rBrKrRrXrarir|r�r�r$rrr�r��s���"�$�+�+�"�"�$�+�+�"�"�$�+�+�"�"�$�+�+�"�"�'�1�1�"�"�'�1�1�"�"�-�=�=�"�"�-�=�=�"�"�'�1�1�"rr�c���|jj}|tjfvryd}t	|j44�dk(rd}|j
d�}|j45}|j}t	|j46�dk(�r�t||||��\}}|j�}|j}|j}	|�1tjd|jtj��}n|j|j�}|	�ntjd|jtj��}	tj|d	d47|��}|j!d|d�j|�}nT|	j|j�}	|d
|	zj|�zj!d|d�j|�}|�tj"|||||	|��}48ntj"||||	|��}49t%|50t&�r|51d}52|r|53j!|dd�S|54j!d|d|jj55df�Sy)NFrnTr�r�)rrr$r�i@���i�)�min�max�out�����g�?)r�r�scale_a�scale_b)r�r�r�r)rrr0�
float8_e4m3fnr�shape�	unsqueezer�t�scale_weight�scale_input�onesr
�float32r��clamp�reshape�56_scaled_mm�57isinstance�tuple)r,rr�	tensor_2d�input_shape�input_dtype�wrr�r��os           r�58fp8_linearr��s���K�K���E��U�(�(�)�)���I�59�5�;�;��1���	�����"���+�+�K��+�+�K�60�5�;�;��1��"�4��e��T���4�
�C�C�E���(�(���&�&���� �:�:�b����U�]�]�S�L�'�?�?�5�<�<�8�L����*�*�R����E�M�M�R�K��K�K��4�S�e�D�E��M�M�"�k�!�n�5�8�8��?�E�%�.�.����6�K��c�K�/�3�3�K�@�@�I�I�"�k�Z[�n�]�`�`�af�g�E���� � ���[�t�U`�jv�w�A�� � ���[�+�_k�l�A��a����!��A���9�9�[��^�R�0�0��y�y�"�k�!�n�d�k�k�.?�.?��.B�C�D�D�rc�6�eZdZGd�dej�Zy)�fp8_opsc��eZdZd�Zd�Zy)�fp8_ops.Linearc� �d|_d|_yr*)r�r�r+s rr-zfp8_ops.Linear.reset_parameters*s�� $�D��#�D��rc��t||�}|�|St||�\}}tjjj|||�Sr*)r�rr0r1r2r3)r,rr�rrs     rr5z)fp8_ops.Linear.forward_comfy_cast_weights/sG���T�5�)�C����61�+�D�%�8�L�F�D��8�8�&�&�-�-�e�V�T�B�BrN)r r!r"r-r5r$rrrBr�)s
��	�62	CrrBN�r r!r"r�rBr$rrr�r�(s��C��#�#�Crr�c�|����tjdj����G���fd�dt�}|S)Nz6Using scaled fp8: fp8 matrix mult: {}, scale input: {}c�@��eZdZG���fd�dej�Zy)�%scaled_fp8_ops.<locals>.scaled_fp8_opc�F���eZdZ��fd�Z�fd�Z�fd�Zdd�Zdd�Z�xZS)�,scaled_fp8_ops.<locals>.scaled_fp8_op.Linearc�2�����|d<t�|�|i|��y)Nr)r9�__init__)r,rr<r=�override_dtypes   ��rr�z5scaled_fp8_ops.<locals>.scaled_fp8_op.Linear.__init__;s$���!�-�&4�F�7�O��� �$�1�&�1rc����t|d�shtjjj	tj63d|jjtj��d��|_	�sd|_64t|d�shtjjj	tj65d|jjtj��d��|_66y)Nr�r$r�F)�data�
requires_gradr�)�hasattrr0r1�	parameter�	Parameterr�rr
r�r�r�)r,r�s �rr-z=scaled_fp8_ops.<locals>.scaled_fp8_op.Linear.reset_parameters@s�����t�^�4�(-���(:�(:�(D�(D�%�*�*�UW�`d�`k�`k�`r�`r�z�{H�{H�KI�Y^�(D�)_�D�%�"�'+�D�$��t�]�3�',�x�x�'9�'9�'C�'C����TV�_c�_j�_j�_q�_q�y~�zG�zG�JH�X]�'C�(^�D�$�rc�����rt||�}|�|St||�\}}|j�|j�kr]tjj67j
|||jj|j|j��z|�Stjj68j
||jj|j|j��z||�S�Nr�)r�r�numelr0r1r2r3r�r�r
r)r,rr�rr�fp8_matrix_mults     �rr5zGscaled_fp8_ops.<locals>.scaled_fp8_op.Linear.forward_comfy_cast_weightsKs����"�$�T�5�1�C���"�69�/��e�<�����<�<�>�E�K�K�M�1� �8�8�.�.�5�5�e�V�d�FW�FW�FZ�FZ�bh�bo�bo�w}�xD�xD�FZ�GE�>E�GK�L�L� �8�8�.�.�5�5�e�d�>O�>O�>R�>R�Z`�Zg�Zg�ou�o{�o{�>R�>|�6|�E�GK�L�Lrc���|r6||jj|j|j��z}|S||jj|j|j��zSr�)r�r�r
r)r,r�inplacer<s    r�convert_weightz;scaled_fp8_ops.<locals>.scaled_fp8_op.Linear.convert_weightXs^����d�/�/�2�2�&�-�-�v�|�|�2�\�\�F�!�M�!�D�$5�$5�$8�$8��
�
�U[�Ua�Ua�$8�$b�b�brc�p�tjj||jj	|j70|j��z|jj|��}|r&|jjj|�ytjj|d��|_y)Nr�)�seedF)r�)
r	�float�stochastic_roundingr�r�r
rrr��copy_r0r1r�)r,r�inplace_updater�r<s     r�71set_weightz7scaled_fp8_ops.<locals>.scaled_fp8_op.Linear.set_weight_s������8�8��$�BS�BS�BV�BV�^d�^k�^k�sy�s�s�BV�CA�:A�CG�CN�CN�CT�CT�[_�8�`��!��K�K�$�$�*�*�6�2�"'�(�(�"4�"4�V�5�"4�"Q�D�Kr)F)FN)	r r!r"r�r-r5r�r�r@)r=r�r�r�s@���rrBr�:s���
2�72	
�
L�
c�
RrrBNr�)r�r�r�s���r�
scaled_fp8_opr�9s���*	R�*	R�[�'�'�*	Rrr�)�logging�info�formatr�)r�r�r�r�s``` r�scaled_fp8_opsr�7s<����L�L�I�P�P�Q`�bm�n�o�+R�+R��+R�Z�rc���tjj|�}|�t|xr|||��S|r*|s tj73tjvr|stS|�||k(rtStS)N)r�r�r�)r	r74�supports_fp8_computer�r�Fp8MatrixMultiplicationr�fastr�r&r�)�weight_dtype�
compute_dtype�load_device�disable_fast_fp8�fp8_optimizations�75scaled_fp8�fp8_computes       r�pick_operationsrhsz���(�(�=�=�k�J�K����k�.O�>O�]n�@J�K�	K�	�	�0�H�H�D�I�I�U�������
� =�"�"��r)FT)NNNN)FFN)NFFN)�__doc__r0r��comfy.model_managementr	�comfy.cli_argsrr�comfy.floatr76rrrrr&r�r�r�r�rr$rr�<module>rs|���$
���3��77�78 �79 �80(�81(��s��2��82^@�^@�B"�%�"�:,�\
C�k�
C�/�br