+
    õäûj 4  ã                   ó  € ^ RI t R R lt^^utt]! ]^,
          4      tRtRtRtRtRt	R	t
^tR
 R ltR R ltR R ltR R ltR tR tR tR%R R lltR%R R lltR R ltR%R R lltR R ltR%R R  lltR! R" ltR# R$ ltR# )&é    Nc                ó0   € V ^8„  d   QhR\         R\         /# )é   ÚnÚreturn©Úint)Úformats   "ÚUC:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\comfy_kitchen\float_utils.pyÚ__annotate__r      s   € ÷ ñ Œsð ”sñ ó    c                 ó"   € ^V ,          ^,
          # ©é   © )r   s   &r
   Ú_n_onesr      s   € Ø��F�a�<Ðr   g      @g      à?g      |@g      À?g      ì@g      °?c                ó<   € V ^8„  d   QhR\         R\         R\         /# )r   ÚxÚmultipler   r   )r	   s   "r
   r   r       s!   € ÷ 7ñ 7Œsð 7œcð 7¤cñ 7r   c                ó<   € W,           ^,
          V,          V,          # )z#Round up x to the nearest multiple.r   )r   r   s   &&r
   Úroundupr       s   € à�\˜AÕ (Õ*¨hÕ6Ð6r   c                óX   € V ^8„  d   QhR\         P                  R\         P                  /# ©r   r   r   ©ÚtorchÚTensor)r	   s   "r
   r   r   %   s"   € ÷ 7ñ 7”U—\‘\ð 7¤e§l¡lñ 7r   c                 óz   € V P                  \        P                  4      P                  \        P                  4      # )N)Útor   Úfloat8_e4m3fnÚfloat32)r   s   &r
   Ú_float8_roundr    %   s&   € Ø�4‰4”×#Ñ#Ó$×'Ñ'¬¯©Ó6Ð6r   c                óp   € V ^8„  d   QhR\         P                  R\        R\        R\         P                  /# ©r   r   ÚebitsÚmbitsr   ©r   r   r   )r	   s   "r
   r   r   )   s6   € ÷ mñ mœuŸ|™|ð m´Cð mÄð mÌÏÉñ mr   c                ó^  € V P                   \        P                  8X  g   Q h^V,           V,           ^8:  g   Q h\        V^,
          4      p\        W,           4      p^W,           ,          p\        \        V,
          ^,
          4      p^\        V4      V,
          ,          \        V^,           4      ^V,          ,          ,          p^^V,
          ,          p\
        V,
          \        V,
          ,           ^,           p	V	\        ,          p
\        P                  ! V
\        P                  R7      P                  \        P                  4      pV P                  \        P                  4      p V R,          pW,          p V P                  \        P                  4      p W8¬  p\        P                  ! \        P                  ! V4      W8  4      p\        P                  ! \        P                  ! WÞ4      4      pW,           pVP                  \        P                  4      pVV
,          pVP                  \        P                  4      pV P                  \        P                  4      pV\        V,
          ,	          ^,          pV\
        ,
          \        ,          V,           pVV,          pVV,          pV\        V,
          ,	          pVP                  \        P                  4      p\        P                  ! W\        P                  R7      p \        P                   ! VVV 4      p \        P                   ! VVV 4      p V\        \"        ,           V,
          V,
          ,	          pVP                  \        P                  4      pVV,          pV V,          p V P                  \        P                  4      # )a%  Convert FP32 numbers to sub-byte floating point numbers with the given
number of exponent and mantissa bits.

Input: torch.Tensor of dtype torch.float
Output: torch.Tensor of dtype torch.uint8, where the bit encoding is stored
in the least significant bits. e.g.
  fp4: bits 0-3 empty and bits 4-7 in fp4_e2m1 encoding
  fp6: bits 0-1 empty and bits 2-7 in fp6_e2m3 or fp6_e3m2 encoding

Note: there are no special values (NaN, inf) support in this code. Values
outside the representable range of Floatx after rounding are clamped to the
maximum Floatx magnitude (sign is preserved).

Background 1: last answer in https://stackoverflow.com/questions/8981913/how-to-perform-round-to-even-with-floating-point-numbers  # noqa: E501
Background 2: Computer Organization and Design, RISC-V edition, Chapter 3.5
)Údtypel        )r'   r   Úfloatr   Ú	MBITS_F32ÚF32_EXP_BIASÚtensorÚint32Úviewr   Úlogical_andÚlogical_notÚ
logical_orr   Úuint8Ú	full_likeÚwhereÚ	EBITS_F32)r   r#   r$   Úexp_biasÚmax_intÚ	sign_maskÚmagic_adderÚ
max_normalÚ
min_normalÚ
denorm_expÚdenorm_mask_intÚdenorm_mask_floatÚsignÚsaturate_maskÚdenormal_maskÚnormal_maskÚ
denormal_xÚnormal_xÚmant_oddÚ
val_to_addÚsign_lps   &&&                  r
   Ú_f32_to_floatx_unpackedrG   )   s¶  € ð" �7‰7”e—k‘kÔ!Ð!Ð!Øˆu�9�uÕ Ô!Ð!Ð!ô �u˜q•yÓ!€HÜ�e•mÓ$€GØ�e•mÕ$€Iäœ) eÕ+¨aÕ/Ó0€Kð ”w˜u“~¨Õ0Õ1´W¸UÀQ½YÓ5GÈ1ÈeÍ8Õ5TÕU€Jð �q˜8•|Õ$€Jô 
˜Õ	 ä�uÕõ	ð õ		ð ð !¤IÕ-€Oô Ÿš _¼E¿K¹KÔH×MÑMÌeÏmÉmÓ\Ðð
 	
�‰Œu�{‰{Ó€AØˆz�>€Dð 	
�€Að 	
�‰Œu�{‰{Ó€Að ‘O€MÜ×%Ò%¤e×&7Ò&7¸Ó&FÈÉÓW€MÜ×#Ò#¤E×$4Ò$4°]Ó$RÓS€Kð Õ&€JØ—‘¤§¡Ó-€JØ�/Õ!€JØ—‘œuŸ{™{Ó+€Jð
 �v‰v”e—k‘kÓ"€HàœY¨Õ.Õ/°1Õ4€HàœlÕ*¬yÕ8¸KÕG€JØ�
Õ€Hà�Õ€HàœI¨Õ-Õ.€HØ�{‰{œ5Ÿ;™;Ó'€Hô
 	�Š˜¬%¯+©+Ô6€AÜ�Š�M :¨qÓ1€AÜ�Š�K ¨1Ó-€Að ”y¤9Õ,¨uÕ4°uÕ<Õ=€GØ�j‰jœŸ™Ó%€Gð
 ˜	Õ!€GØ	ˆG�€Aà�4‰4”—‘ÓÐr   c                óp   € V ^8„  d   QhR\         P                  R\        R\        R\         P                  /# r"   r%   )r	   s   "r
   r   r   ™   s6   € ÷ T$ñ T$œuŸ|™|ð T$´Cð T$Äð T$ÌÏÉñ T$r   c                óÎ  € V P                   \        P                  8X  g   Q h^V,           V,           ^8:  g   Q h^W,           ,          p\        V^,
          4      p\        V4      pW,          pW,          pV^ 8H  p\        P                  ! V^ 8„  Wr,	          ^ 8H  4      p	Wr,	          p
W¤,
          \
        ,           pVP                  \        P                  4      \        ,          pWu,          P                  \        P                  4      pV\        V,
          ,          pW½,          p^ Wè&   ^V,
          \
        ,           pV^8X  d   Wò,
          \        ,          Wé&   M¤\        V4       F~  p\        ^V,          ^V^,           ,          4       FV  pVV,
          pV^V,          ,
          V\        ,           V,
          ,          pVV,
          \        ,          pW½,           WÌV8H  &   KX  	  K€  	  \        P                  ! WœV4      pVP                  \        P                  4      \        V,
          \        ,           V,
          ,          pVV,          pVP                  \        P                  4      # )a‰  Convert sub-byte floating point numbers with the given number of exponent
and mantissa bits to FP32.

Input: torch.Tensor of dtype uint8, where the bit encoding is stored
in the least significant bits. e.g.
  fp4: bits 0-3 empty and bits 4-7 in fp4_e2m1 encoding
  fp6: bits 0-1 empty and bits 2-7 in fp6_e2m3 or fp6_e3m2 encoding
Output: torch.Tensor of dtype fp32 with the dequantized value
)r'   r   r1   r   r.   r*   r   r,   r)   Úranger3   r4   r-   r(   )r   r#   r$   r7   r5   Úmantissa_maskrF   Úx_posÚ	zero_maskr@   Úexp_biased_lpÚexp_biased_f32Úmantissa_lp_int32Úmantissa_f32ÚresultÚdenormal_exp_biasedÚiÚmantissa_cmpÚ
left_shiftÚsign_f32s   &&&                 r
   Ú_floatx_unpacked_to_f32rX   ™   só  € ð �7‰7”e—k‘kÔ!Ð!Ð!Øˆu�9�uÕ Ô!Ð!Ð!à�e•mÕ$€IÜ�u˜q•yÓ!€HÜ˜E“N€Mð �m€Gð �K€Eð
 ˜‘
€Iô
 ×%Ò% u¨q¡y°Uµ^ÈÑ4IÓK€Mð •N€MØ"Õ-´Õ<€NØ#×&Ñ&¤u§{¡{Ó3´yÕ@€Nð Õ.×2Ñ2´5·;±;Ó?ÐØ$¬°UÕ):Õ;€LØÕ*€Fð
 €FÑà˜h�,¬Õ5Ðð �„zØ!4Õ!<ÄÕ JˆÒô �u–ˆAÜ % a¨1¥f¨a°A¸µE­lÖ ;�ð # Q�Y�
Ø ,°°QµÕ 7¸ZÌ)Õ=SÐV[Õ=[Õ\�Ø"5¸
Õ"BÄyÕ!P�ð
 HVÕGdÐ!°|Ñ"CÓDó !<ñ ô —’˜]¸vÓFˆð �z‰zœ%Ÿ+™+Ó&¬9°uÕ+<¼yÕ+HÈ5Õ+PÕQ€HØ�hÕ€Fà�;‰;”u—{‘{Ó#Ð#r   c                 óp   € V R,          ^,          ^ 8X  g   Q V  R24       h. V RR OV R,          ^,          N5# )r   z last dim not divisible by twoNéÿÿÿÿr   ©Úsizes   &r
   Ú	down_sizer]   ð   sC   € Ø��8�a�<˜1ÔÐE  Ð&DÐEÓEÐØ&ˆT�#�2ˆYÐ&˜˜R� A�Ñ&Ð&r   c                 ó2   € . V R R OV R,          ^,          N5# )NrZ   r   r[   s   &r
   Úup_sizer_   õ   s    € Ø%ˆT�#�2ˆYÐ%˜˜R� 1�Ñ%Ð%r   c                 ó.   € W,           ^,
          V,          # r   r   )ÚaÚbs   &&r
   Úceil_divrc   ù   s   € Ø�E�A�I˜!ÕÐr   c                ód   € V ^8„  d   QhR\         P                  R\        R\         P                  /# )r   Ú
uint8_dataÚhi_firstr   )r   r   Úbool)r	   s   "r
   r   r   ý   s.   € ÷ Pñ Pœ5Ÿ<™<ð P´4ð PÄ5Ç<Á<ñ Pr   c                 ón  € V P                   pVR,          ^,          ^ 8X  g   Q hV P                  4       P                  R4      p V'       d7   V R,          ^,          V R,          ,          P                  \        V4      4      # V R,          ^,          V R,          ,          P                  \        V4      4      # )r   :NNr   :r   Nr   rZ   )ÚshapeÚ
contiguousr-   r]   )re   rf   ri   s   && r
   Ú
pack_uint4rk   ý   s‘   € Ø×Ñ€EØ��9�q�=˜AÔÐÐØ×&Ñ&Ó(×-Ñ-¨bÓ1€JßØ˜3• 1Õ$ z°$Õ'7Õ7×=Ñ=¼iÈÓ>NÓOÐOà˜4Õ  AÕ%¨
°3­Õ7×=Ñ=¼iÈÓ>NÓOÐOr   c                óD   € V ^8„  d   QhR\         R\        P                  /# )r   rf   r   ©rg   r   r   )r	   s   "r
   r   r     s   € ÷ ñ ¤tð ´u·|±|ñ r   c                ó¾  € V P                  4       '       g   Q hV P                  pV ^,	          P                  \        P                  4      pV ^,          P                  \        P                  4      pV'       d4   \        P
                  ! W4.RR7      P                  \        V4      4      pV# \        P
                  ! WC.RR7      P                  \        V4      4      pV# )z?Get the original weight from the normalized float weight format)ÚdimrZ   )Úis_contiguousri   r   r   r1   Ústackr-   r_   )re   rf   ri   ÚhiÚloÚunpackeds   &&    r
   Úunpack_uint4ru     s¦   € à×#Ñ#×%Ò%Ð%Ð%Ø×Ñ€Eà
˜�/×	Ñ	œeŸk™kÓ	*€BØ
�vÕ
×	!Ñ	!¤%§+¡+Ó	.€BßÜ—;’; ˜x¨RÔ0×5Ñ5´g¸e³nÓEˆð €Oô —;’; ˜x¨RÔ0×5Ñ5´g¸e³nÓEˆØ€Or   c                óX   € V ^8„  d   QhR\         P                  R\         P                  /# )r   Úpackedr   r   )r	   s   "r
   r   r     s"   € ÷ ;ñ ;œŸ™ð ;¬%¯,©,ñ ;r   c                ój   € V ^,          V ^,	          ,          P                  \        P                  4      # )aF  Exchange the high and low nibbles of each byte in a uint8 tensor.

This converts between hi_first=True and hi_first=False FP4 packing
without re-quantizing: ``0xAB`` becomes ``0xBA``.

Args:
    packed: A uint8 tensor of nibble-packed FP4 values.

Returns:
    A new uint8 tensor with the nibble order reversed in every byte.
)r   r   r1   )rw   s   &r
   Úswap_nibblesry     s&   € ð �q�[˜V q�[Õ)×-Ñ-¬e¯k©kÓ:Ð:r   c                óD   € V ^8„  d   QhR\         R\        P                  /# )r   Úflattenr   rm   )r	   s   "r
   r   r   $  s   € ÷ "8ñ "8¤dð "8´e·l±lñ "8r   c                ó   € V P                   w  r#\        V^€4      p\        V^4      pV^€,          pV^,          pT pW#3Wg38w  d9   \        P                  ! Wg3V P                  V P
                  R7      pWRV1RV13&   VP                  V^€V^4      P                  ^ ^^^4      p	V	P                  R^^ ^4      P                  ^^4      P                  R^ ^4      p
V'       d   V
P                  4       # V
P                  Wg4      # )a9  
Rearrange a large matrix by breaking it into blocks and applying the rearrangement pattern.
See:
    https://docs.nvidia.com/cuda/cublas/index.html#d-block-scaling-factors-layout

Args:
    input_matrix: Input tensor of shape (H, W)
Returns:
    Rearranged tensor of shape (32*ceil_div(H,128), 16*ceil_div(W,4))
)Údevicer'   NrZ   )ri   rc   r   Úzerosr}   r'   r-   ÚpermuteÚreshapeÚ	transposer{   )Úinput_matrixr{   ÚrowsÚcolsÚn_row_blocksÚn_col_blocksÚpadded_rowsÚpadded_colsÚpaddedÚblocksÚ
rearrangeds   &&         r
   Ú
to_blockedrŒ   $  s  € ð ×#Ñ#�J€DÜ˜D #Ó&€LÜ˜D !Ó$€Lð  Õ$€KØ Õ"€Kà€FØ€|˜Ð1Ô1Ü—’ØÐ&Ø×&Ñ&Ø×$Ñ$ô
ˆð
  ,ˆu�ˆu�e�t�eˆ|Ñð �[‰[˜ s¨L¸!Ó<×DÑDÀQÈÈ1ÈaÓP€FØ—‘  A r¨1Ó-×7Ñ7¸¸1Ó=×EÑEÀbÈ"ÈbÓQ€JßØ×!Ñ!Ó#Ð#à×Ñ˜kÓ7Ð7r   c                óP   € V ^8„  d   QhR\         R\         R\        P                  /# )r   Únum_rowsÚnum_colsr   )r   r   r   )r	   s   "r
   r   r   I  s%   € ÷ +ñ +¬3ð +¼#ð +Ä%Ç,Á,ñ +r   c                óz  € \        V^€4      p\        V^4      pV^€,          pV^,          pV P                  R^ ^4      pVP                  R^ ^^4      P                  ^^4      pVP                  W4^^ ^4      p	V	P                  W4^€^4      p
V
P                  ^ ^^^4      pVP                  WV4      pVRV1RV13,          # )a/  
Reverse the cuBLAS tiled layout back to normal (H, W) layout.

Args:
    blocked_matrix: Swizzled tensor from cuBLAS layout (padded_rows, padded_cols)
    num_rows: Desired output rows (unpadded)
    num_cols: Desired output cols (unpadded)
Returns:
    Unswizzled tensor of shape (num_rows, num_cols)
NrZ   )rc   r€   r�   r   )Úblocked_matrixrŽ   r�   r…   r†   r‡   rˆ   Ústep1Ústep2Ústep3Ústep4Ústep5Ú	unblockeds   &&&          r
   Úfrom_blockedr˜   I  sÅ   € ô ˜H cÓ*€LÜ˜H aÓ(€Là Õ$€KØ Õ"€Kà×"Ñ" 2 r¨2Ó.€EØ�M‰M˜"˜b ! QÓ'×1Ñ1°!°QÓ7€EØ�M‰M˜,°a¸¸QÓ?€EØ�M‰M˜,°c¸1Ó=€EØ�M‰M˜!˜Q  1Ó%€EØ—‘˜kÓ7€IØ�Y�h�Y 	  	Ð)Õ*Ð*r   c                ó$   € V ^8„  d   QhR\         /# )r   rf   )rg   )r	   s   "r
   r   r   c  s   € ÷ ñ œtñ r   c                 ó8   € \        WR 7      p\        V^^4      pV# ))rf   )ru   rX   )ra   rf   Úa_u8Úa_f32s   &&  r
   Úfp4_x2_to_f32r�   c  s   € Ü˜Ô-€DÜ# D¨!¨QÓ/€EØ€Lr   c                óX   € V ^8„  d   QhR\         P                  R\         P                  /# r   r   )r	   s   "r
   r   r   h  s"   € ÷ &ñ &”5—<‘<ð &¤E§L¡Lñ &r   c                 óÆ  € V P                   \        P                  8X  g   Q R 4       hV P                  \        P                  4      pV\
        ,	          ^ÿ,          pV\        \
        4      ,          pV^\
        ^,
          ,          8¬  pW$P                  \        P                  4      ,           p\        P                  ! V^ ^ÿ4      pVP                  \        P                  4      # )zInput must be float32)
r'   r   r   r-   r,   r)   r   r   Úclampr1   )r   Úx_intÚ
biased_expÚmantissaÚround_ups   &    r
   Úf32_to_e8m0r¥   h  s�   € Ø�7‰7”e—m‘mÔ#Ð<Ð%<Ó<Ð#Ø�F‰F”5—;‘;Ó€EØœ9Õ$¨Õ,€Jð ”wœyÓ)Õ)€HØ˜A¤)¨a¥-Õ0Ñ1€HØŸk™k¬%¯+©+Ó6Õ6€Jä—’˜Z¨¨CÓ0€Jà�=‰=œŸ™Ó%Ð%r   c                óX   € V ^8„  d   QhR\         P                  R\         P                  /# r   r   )r	   s   "r
   r   r   x  s"   € ÷ &ñ &”5—<‘<ð &¤E§L¡Lñ &r   c                 óB  € V P                   \        P                  8X  g   Q R 4       hV P                  \        P                  4      pV\
        ,          p\        P                  ! V^ 8H  \        P                  ! V4      V4      pVP                  \        P                  4      # )zInput must be uint8)
r'   r   r1   r   r,   r)   r3   Ú
zeros_liker-   r   )r   r¢   rR   s   &  r
   Úe8m0_to_f32r©   x  sn   € Ø�7‰7”e—k‘kÔ!Ð8Ð#8Ó8Ð!Ø—‘”e—k‘kÓ"€JØœ9Õ$€FÜ�[Š[˜ q™¬%×*:Ò*:¸6Ó*BÀFÓK€Fà�;‰;”u—}‘}Ó%Ð%r   )T)r   r   r4   r)   r*   ÚF4_E2M1_MAXÚF4_E2M1_EPSÚF8_E4M3_MAXÚF8_E4M3_EPSÚF8_E5M2_MAXÚF8_E5M2_EPSÚ	E8M0_BIASr   r    rG   rX   r]   r_   rc   rk   ru   ry   rŒ   r˜   r�   r¥   r©   r   r   r
   Ú<module>r±      s¡   ðó õð ˜"Ð €	ˆ9Ù�y 1•}Ó%€à€Ø€Ø€Ø€Ø€Ø€ð €	õ7õ
7õmõ`T$òn'ò
&ò÷P÷õ;÷"8õJ+÷4õ
&÷ &r   