ó
    Këgj+m  ã                   ó®  • S SK Jr  S SKrS SKJr  S SKJs  Jr  S SKrS SK	J
r  S SKJr  S SKJrJrJr  S SKJr  S SKJr  S SKJrJrJr  S SKJrJr   S S	KJr  S
r \(       d  SOSr!\RD                  " S S S.5      \RF                  S\RH                  S\RH                  S\RH                  S\RH                  4S j5       5       r%\" S5         S:S\&S-  S\&S-  S\RN                  S-  4S jj5       r(\RF                  S\RH                  S\RH                  S\RH                  S\RH                  S\RH                  S\RH                  4S  j5       r)\RF                  S!\RH                  S\RH                  4S" j5       r*\" S5                S;S#\RV                  S$\RX                  S%\RV                  S&\RV                  S'\-S\&S\&S\&S(\-S\.S)\/S*\&S+\/4S, jj5       r0\" S5      S-\RV                  S.\RV                  S/\RV                  S0\RV                  4S1 j5       r1 " S2 S3\Rd                  Rf                  5      r4          S;S#\RV                  S$\RX                  S%\RV                  S&\RV                  S'\-S\&S\&S\&S(\-S\.S)\/S*\&S+\/S4\5\RV                  \RV                  4   4S5 jjr6 " S6 S7\Rn                  5      r8 " S8 S9\5      r9g! \\4 a    Sr GNzf = f)<é    )ÚpartialN)Ú
DeviceMesh)Ú	ReplicateÚShardÚdistribute_module)ÚParallelStyle)Údispatch)ÚexpÚlogÚtanh)ÚIS_AMDÚinput_guard)ÚDTensori €  é    é   c                 ó   • U S   S L$ )NÚscale© ©Úargss    Ú´C:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\fla/modules/fused_linear_cross_entropy.pyÚ<lambda>r   (   s   € ˜d 7™m°4Ñ7ó    c                 ó   • U S   S L$ )NÚsoftcappingr   r   s    r   r   r   )   s   €  D¨Ñ$7¸tÑ$Cr   )Ú	HAS_SCALEÚHAS_SOFTCAPPINGÚDÚBr   r   c                 ó’  • [         R                  " S5      R                  [         R                  5      [         R                  " S5      R                  [         R                  5      p˜X•-  [         R                  " SU5      -   n
X¤:  n[         R
                  " XU-  -   U
-   U[        S5      * S9nU(       a  XÂ-  nU(       a  U[        XÃ-  5      -  n[         R                  " US5      n[        [         R                  " [        XÍ-
  5      S5      5      U-   n[         R                  " X[         R                  " XE5      -  -   U	-   U5        g )Nr   é   Úinf©ÚmaskÚother)ÚtlÚ
program_idÚtoÚint64ÚarangeÚloadÚfloatr   Úmaxr   Úsumr
   ÚstoreÚcdiv)ÚxÚzr   r   r   r   r   r   Úi_nÚi_dÚo_dÚm_dÚb_xÚb_mÚb_zs                  r   Úlogsumexp_fwd_kernelr:   '   sì   € ô �}Š}˜QÓ×"Ñ"¤2§8¡8Ó,¬b¯mªm¸AÓ.>×.AÑ.AÄ"Ç(Á(Ó.KˆØ
‰'”B—I’I˜a “OÑ
#€CØ
‰'€Cä
�'Š'�!˜A‘g‘+ Ñ#¨#´e¸E³l°]Ñ
C€CÞØ‰kˆÞØœD Ñ!2Ó3Ñ3ˆÜ
�&Š&��a‹.€CÜ
Œb�fŠf”S˜™“^ QÓ'Ó
(¨3Ñ
.€CÜ‡H‚HˆQ”r—w’w˜q“}Ñ$Ñ$ sÑ*¨CÕ0r   Úmodulesr   r   Údtypec           	      óÄ  • U R                   nU R                  SUS   5      n U R                   u  pV[        [        R                  " U5      S5      n[        R
                  " Xg5      nU R                  XX[        R                  S9n	[        XX4   " U U	UUUUS9  U	R                  S5      R                  " US S 6 n	Ub%  U[        R                  :w  a  U	R                  U5      n	U	$ )Néÿÿÿÿi   ©r<   )r1   r2   r   r   r   r   )ÚshapeÚviewÚminÚtritonÚnext_power_of_2r0   Ú	new_emptyÚtorchr,   r:   Ú	logsumexpr(   )
r1   r   r   r<   r@   ÚNr   r   ÚNDr2   s
             r   Úlogsumexp_fwdrJ   D   sË   € ð �G‰G€EØ	�‰ˆr�5˜‘9Ó€AØ�7‰7�D€AÜŒF×"Ò" 1Ó% yÓ1€AÜ	�Š�QÓ	€Bà	�‰�A¤§¡ˆÐ-€AÜ˜!˜Ò!Ø
Ø
ØØØ
Ø
òð 	
�‰�B‹×Ò˜e C R˜jÐ)€AØÑ˜U¤e§k¡kÓ1Ø�D‰D�‹KˆØ€Hr   Úlabel_smoothingÚlogit_scaleÚlogit_softcappingÚ	reductionÚVÚBVc           	      ó
  • [         R                  " S5      R                  [         R                  5      n[         R                  " X«5      n[         R
                  " X,-   5      nXU
-  -  n Xå:X  aJ  [        SX«5       H9  nU[         R                  " SU5      -   n[         R                  " U U-   SUU
:  S9  M;     g[         R
                  " X-   5      R                  [         R                  5      U-  nUb  [        UU-  5      nUU-  nSUU-  -
  n[         R
                  " X-   5      nUU-
  nSnXj-  n[         R                  " 5         [        SU5       GH  nUU-  [         R                  " SU5      -   n[         R
                  " U U-   UU
:  [        S5      S9R                  [         R                  5      U-  nUb  [        UU-  5      nUU-  nOUnUS:”  a6  U[         R                  " [         R                  " UU
:  U* U-  S5      5      -  n[        UU-
  5      U-
  U-  nUb  USWU-  -
  -  nU	S:X  a  UU-  n[         R                  " U U-   UUU
:  S9  [         R                  " 5         GM     US:”  a  US	U-
  -  UUU-  -   -   n[         R
                  " X-   5      nUb  WnOSnU	S:X  a  UU-  nUUS	-
  U-  U-  U-  -  nOUUS	-
  U-  U-  -  n[         R                  " X<-   U5        [         R                  " X-   U5        g)
aP  
This kernel computes both cross entropy loss and the gradient of the input.
We only consider hard label + mean reduction for now.
Please refer to https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html for the math.

Args:
    logits:
        Pointer to logits tensor.
    lse:
        Pointer to logsumexp tensor.
    target: Pointer to target tensor.
    loss:
        Pointer to tensor to store the loss.
    V (int):
        The number of columns in the input tensor.
    total (int):
        The number of non-ignored classes.
    ignore_index (int):
        The index to ignore in the target.
    label_smoothing (float):
        The amount of smoothing when computing the loss, where 0.0 means no smoothing.
    reduction (str):
        The string for the reduction to apply
    BV (int):
        The block size for vocab.
r   ç        ©r$   Nç      ð?z-infr#   Úmeanr!   )r&   r'   r(   r)   r0   r+   Úranger*   r/   Úfloat32r   Údebug_barrierr,   r.   Úwherer
   )ÚlogitsÚlseÚtargetÚlossÚtotalÚignore_indexrK   rL   rM   rN   rO   rP   r3   ÚNVÚb_yÚiÚo_vÚb_lÚb_t_yÚb_softcap_deriv_yÚb_lseÚb_lossr9   ÚepsÚivÚb_logitsÚb_tÚb_cappedÚb_pÚb_sc_factors                                 r   Úcross_entropy_kernelrp   `   sõ  € ôX �-Š-˜Ó
×
Ñ
œbŸh™hÓ
'€CÜ	�Š�‹€Bô �'Š'�&‘,Ó
€Cð �A‰gÑ€Fà
Óä�q˜!–ˆAØ”b—i’i  2Ó&Ñ&ˆCÜ�HŠH�V˜c‘\ 3¨S°1©WÔ5ñ !ð 	ô �'Š'�&‘,Ó
×
"Ñ
"¤2§:¡:Ó
.°Ñ
<€CØÑ$Ü�SÐ,Ñ,Ó-ˆØ %Ñ'ˆà %¨%¡-Ñ/ÐÜ�GŠG�C‘IÓ€Eð �S‰[€Fð €CØ
Ñ
€Cô ×ÒÔô �A�r�lˆØ�2‰gœŸ	š	 ! RÓ(Ñ(ˆÜ—7’7˜6 C™<¨c°A©g¼UÀ6»]ÑK×NÑNÌrÏzÉzÓZÐ]hÑhˆØÑ(Ü�xÐ"3Ñ3Ó4ˆCØ(¨3Ñ.‰HàˆHØ˜QÓà”2—6’6œ"Ÿ(š( 3¨¡7¨S¨D°8©O¸SÓAÓBÑBˆCÜ�8˜eÑ#Ó$ sÑ*¨kÑ9ˆàÑ(Ø˜˜s S™y™Ñ)ˆCØ˜ÓØ˜‘+ˆCÜ
�Š�˜#‘˜s¨¨q©Ò1ä
×Ò×ñ' ð> ˜ÓØ˜1˜Ñ.Ñ/°3¸È5Ñ9PÑ3PÑQˆô �'Š'�&‘,Ó
€Cð Ñ$Ø'‰àˆð �FÓØ˜%‘ˆØ� !Ñ# uÑ,¨{Ñ:¸[ÑHÑH‰à� !Ñ# {Ñ2°[Ñ@Ñ@ˆä‡H‚HˆT‰Z˜Ô Ü‡H‚HˆV‰\˜3Õr   rH   c                 ó8  • [         R                  " S5      R                  [         R                  5      nXC-  [         R                  " SU5      -   n[         R
                  " U5      n[         R
                  " X-   XR:  S9n[         R                  " X-   Xv-  XR:  S9  g)am  
This function multiplies each element of the tensor pointed by x with the value pointed by g.
The multiplication is performed in-place on the tensor pointed by x.

Parameters:
x:
    Pointer to the input tensor.
g:
    Pointer to the gradient output value.
N (int):
    The number of columns in the input tensor.
B (int):
    The block size for Triton operations.
r   rS   N)r&   r'   r(   r)   r*   r+   r/   )r1   ÚgrH   r   Úi_xÚo_xÚb_gr7   s           r   Úelementwise_mul_kernelrv   ô   sp   € ô. �-Š-˜Ó
×
Ñ
œbŸh™hÓ
'€CØ
‰'”B—I’I˜a “OÑ
#€Cô �'Š'�!‹*€CÜ
�'Š'�!‘' ¡Ñ
(€CÜ‡H‚HˆQ‰W�c‘i c¡gÓ.r   r1   r\   ÚweightÚbiasr_   Ú
num_chunksÚ
use_l2warpÚl2_penalty_factorÚaccumulate_grad_in_fp32c                 ó°  • U R                   n/ U R                  QUR                  S   P7u  pïn[        [        [        R
                  " U5      5      n[        U[        R                  " UU5      5      n[        R
                  " [        R                  " UU5      5      n[        R                  " UU5      n[        R                  " XS9nU(       a  [        R                  OUR                  nS nUb#  U(       a  [        R                  OUR                  nUb  [        R                  " X-US9OS nUb  [        R                  " X=US9OS n[        R                  " Xí[        R                  S9nUR                  U5      R                  5       R                  5       n[!        U5       GHò  nUU-  [        US-   U-  U5      nnU UU n["        R$                  " UX#5      nUb  UR                  U:w  a  UR'                  US9nUUU n [)        UXg[        R                  S9n!UUU n"U
(       a  [        R*                  " USSS	9u  n#n$[,        UR                  S   4   " UU!U U"UUUUUU	UU[.        S
9  U
(       a¦  [        R                  " U5      n%X¾-  n&W#U&-  n'U%R1                  SW$U'5        Ub2  [        R2                  " UU%R5                  5       R'                  US9UUS9  Ub$  [        R6                  " UU%R                  SUS9US9  [        R8                  " U%U5      n(OSn([        R8                  " UU5      U(-   UUU& Ub2  [        R2                  " UUR5                  5       R'                  US9UUS9  Uc  GMÎ  [        R6                  " UUR                  SUS9US9  GMõ     UR                  5       nUb  UR'                  U5      nUb  UR'                  U5      nUUUU4$ )Nr   ©Údevice)r   r<   r!   r?   )r   r   r<   r>   T)Úkeepdim)rZ   r[   r\   r]   r^   r_   rK   rL   rM   rN   rO   rP   Ú	num_warps)ÚinputÚmat1Úmat2Úout)r‚   r%   r…   rR   )r   r@   rB   ÚMAX_FUSED_SIZErC   rD   r0   rF   Ú
zeros_likerW   r<   Úzerosr,   Úner.   ÚitemrV   ÚFÚlinearr(   rJ   r-   rp   ÚSTATIC_WARPSÚscatter_ÚaddmmÚtÚaddÚmm))r1   r\   rw   rx   r_   rK   rL   rM   ry   rN   rz   r{   r|   r   rH   ÚHrO   rP   ÚNCÚCÚdxÚ
grad_dtypeÚbias_grad_dtypeÚdwÚdbr]   r^   ÚicÚstartÚendÚc_xÚc_logitsÚc_targetÚc_lseÚc_lossÚc_maxxÚc_idsÚg_logits_l2Ú	l2_factorÚpenalty_gradÚdx_l2_contributions)                                            r   Ú"fused_linear_cross_entropy_forwardr©     sˆ  € ð  �X‰X€Fð (ˆq�w‰wÐ'˜Ÿ™ Q™Ñ'�G€Aˆ!Ü	Œ^œV×3Ò3°AÓ6Ó	7€Bô 
ˆZœŸš Q¨Ó*Ó	+€BÜ×ÒœvŸ{š{¨1¨bÓ1Ó2€AÜ	�Š�Q˜Ó	€Bô 
×	Ò	˜!Ñ	+€BÞ"9”—’¸v¿|¹|€JØ€OØÑÞ+Bœ%Ÿ-š-ÈÏ
É
ˆð GMÑFXŒ×	Ò	˜&°zÒ	BÐ^b€BàIMÑIYŒ×	Ò	˜$°_Ò	EÐ_c€Bä�;Š;�q¬u¯{©{Ñ;€Dà�I‰I�lÓ#×'Ñ'Ó)×.Ñ.Ó0€Eä�B�iˆØ˜!‘VœS " q¡&¨A¡¨qÓ1ˆsˆà��cˆlˆô —8’8˜C Ó.ˆØÑ #§)¡)¨zÓ"9Ø—&‘&˜z�&Ð*ˆCØ˜% Ð$ˆô ˜h¨kÔ`e×`kÑ`kÑlˆð �e˜C�ˆÞÜ!ŸIšI h°¸DÑA‰MˆF�Eô 	˜hŸn™n¨QÑ/Ð1Ò2ØØØØØØ%Ø+Ø#Ø/ØØØÜ"ò	
ö ä×*Ò*¨8Ó4ˆKð *Ñ-ˆIØ! IÑ-ˆLØ× Ñ   U¨LÔ9ð Ñ!Ü—’ØØ$Ÿ™›×+Ñ+°*Ð+Ð=ØØò	ð ÑÜ—	’	 ¨+¯/©/¸!À?¨/Ð*SÐY[Ò\ä!&§¢¨+°vÓ!>Ñà!$Ðô Ÿš ¨6Ó2Ð5GÑGˆˆ5�ˆàÑÜ�KŠKØØ—Z‘Z“\—_‘_¨:�_Ð6ØØò	ð ÔÜ�IŠI˜B h§l¡l°1¸O lÐ&LÐRTÕUñY ð\ �8‰8‹:€DØ	�~Ø�U‰U�6‹]ˆØ	�~Ø�U‰U�4‹[ˆØ��R˜ÐÐr   Údor–   r™   rš   c                 ó6  • [         R                  " U [         R                  " SU R                  S95      (       aÝ  UR                  u  pE[        [        [        R                  " U5      5      n[        [        R                  " XE-  U5      4   " UU XE-  U[        S9  Ub=  UR                  u  pu[        [        R                  " Xu-  U5      4   " UU Xu-  U[        S9  Ub9  UR                  S   n[        [        R                  " Xv5      4   " UU UU[        S9  XU4$ )NrT   r~   )r1   rr   rH   r   r�   r   )rF   r‰   Útensorr   r@   rB   r†   rC   rD   rv   r0   r�   )rª   r–   r™   rš   rH   r“   r   rO   s           r   Ú#fused_linear_cross_entropy_backwardr­   �  sþ   € ô ‡x‚x�”E—L’L ¨R¯Y©YÑ7×8Ñ8ð �x‰x‰ˆÜ”¤× 6Ò 6°qÓ 9Ó:ˆä¤§¢¨A©E°1Ó 5Ð7Ò8ØØØ‰cØÜ"ò	
ð ‰>Ø—8‘8‰DˆAÜ"¤F§K¢K°±°qÓ$9Ð#;Ò<ØØØ‘#ØÜ&òð ‰>Ø—‘˜‘ˆAÜ"¤F§K¢K°Ó$5Ð#7Ò8ØØØØÜ&òð �2ˆ:Ðr   c                   óè   • \ rS rSr\\          SS\R                  S\R                  S\R                  S\R                  S\	S\
S	\
S
\
S\	S\S\S\
S\4S jj5       5       r\\S 5       5       rSrg)ÚFusedLinearCrossEntropyFunctioniÊ  Nr1   r\   rw   rx   r_   rK   rL   rM   ry   rN   rz   r{   r|   c                 óÖ   • [        UUUUUUUUU	U
UUU5      u  pïnnU R                  UR                  5       Ub  UR                  5       OSUb  UR                  5       5        U$ S5        U$ )aJ  
Fusing the last linear layer with cross-entropy loss
    Reference: https://github.com/mgmalek/efficient_cross_entropy

Handle the forward and backward pass of the final linear layer via cross-entropy loss by avoiding
the materialization of the large logits tensor. Since Cross Entropy Loss is the last layer, we can
compute the gradient at the forward pass. By doing so, we don't have to store the x and target
for the backward pass.

x (torch.Tensor): [batch_size * seq_len, hidden_size]
target (torch.LongTensor): [batch_size * seq_len]
    where each value is in [0, vocab_size).
weight (torch.Tensor): [vocab_size, hidden_size]
    where `vocab_size` is the number of classes.
bias (Optional[torch.Tensor]): [vocab_size]
    where `vocab_size` is the number of classes.
ignore_index:
    the index to ignore in the target.
label_smoothing:
    the amount of smoothing when computing the loss, where 0.0 means no smoothing.
logit_scale: float = 1.0,
    A scaling factor applied to the logits. Default: 1.0
logit_softcapping: float = None,
    If > 0, apply logit softcapping: logits = softcap * tanh(logits / softcap).
    Default: 0.0
num_chunks: int
    The number of chunks to split the input tensor into for processing.
    This can help optimize memory usage and computation speed.
    Default: 8
reduction:
    Specifies the reduction to apply to the output: 'mean' | 'sum'.
    'mean': the weighted mean of the output is taken,
    'sum': the output will be summed.
    Default: 'mean'.
use_l2warp: bool = False,
    Whether to use L2 regularization on the logits to prevent overconfidence.
    Default: False
l2_penalty_factor: float = 1e-4,
    The L2Warp penalty factor. Default: 1e-4
accumulate_grad_in_fp32: bool = True,
    Whether to accumulate weight and bias gradients in fp32 before casting them
    back to the parameter dtype. Default: True
N)r©   Úsave_for_backwardÚdetach)Úctxr1   r\   rw   rx   r_   rK   rL   rM   ry   rN   rz   r{   r|   r]   r–   r™   rš   s                     r   ÚforwardÚ'FusedLinearCrossEntropyFunction.forwardÌ  sŒ   € ôz >ØØØØØØØØØØØØØ#ó
Ñˆ�"�bð  	×ÑØ�I‰I‹KØ!Ñ-ˆB�I‰IŒK°4ØÑ+ˆB�I‰I‹Kô	
ð
 ˆð 26ô	
ð
 ˆr   c                 óZ   • U R                   u  p#n[        XX45      u  p#nUS X4S S S S S S S S S 4$ ©N)Úsaved_tensorsr­   )r³   rª   r–   r™   rš   s        r   ÚbackwardÚ(FusedLinearCrossEntropyFunction.backward   sC   € ð ×&Ñ&‰
ˆ�Ü8¸ÀÓH‰
ˆ�Ø�4˜  t¨T°4¸¸tÀTÈ4ÐQUÐUÐUr   r   ©
NéœÿÿÿrR   rT   Né   rU   Fç-Cëâ6?T)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ústaticmethodr   rF   ÚTensorÚ
LongTensorÚintr,   ÚstrÚboolr´   r¹   Ú__static_attributes__r   r   r   r¯   r¯   Ê  sü   † àØð "Ø Ø!$Ø Ø#'ØØØ Ø#'Ø(,ñPà�<‰<ðPð × Ñ ðPð —‘ð	Pð
 �l‰lðPð ðPð ðPð ðPð !ðPð ðPð ðPð ðPð !ðPð "&ôPó ó ðPðd ØñVó ó óVr   r¯   Úreturnc                 óD   • [         R                  U UUUUUUUUU	U
UU5      $ )a6  
Args:
    x (torch.Tensor): [batch_size * seq_len, hidden_size]
    target (torch.LongTensor): [batch_size * seq_len]
        where each value is in [0, vocab_size).
    weight (torch.Tensor): [vocab_size, hidden_size]
        where `vocab_size` is the number of classes.
    bias (Optional[torch.Tensor]): [vocab_size]
        where `vocab_size` is the number of classes.
    ignore_index: int.
        If target == ignore_index, the loss is set to 0.0.
    label_smoothing: float
    logit_scale: float
        A scaling factor applied to the logits. Default: 1.0
    logit_softcapping: float
        If > 0, apply logit softcapping: logits = softcap * tanh(logits / softcap).
        Default: 0.0
    num_chunks: int
        The number of chunks to split the input tensor into for processing.
        This can help optimize memory usage and computation speed.
        Default: 8
    reduction:
        Specifies the reduction to apply to the output: 'mean' | 'sum'.
        'mean': the weighted mean of the output is taken,
        'sum': the output will be summed.
        Default: 'mean'.
    use_l2warp:
        Whether to add the L2Warp logit regularization gradient. The penalty is normalized by
        the full number of input tokens, matching `fla.modules.l2warp.l2_warp`.
        Default: `False`.
    l2_penalty_factor:
        The L2Warp penalty factor. Default: `1e-4`.
    accumulate_grad_in_fp32:
        Whether to accumulate weight and bias gradients in fp32 before casting them
        back to the parameter dtype. Default: `True`.
Returns:
    losses: [batch,], float
)r¯   Úapply)r1   r\   rw   rx   r_   rK   rL   rM   ry   rN   rz   r{   r|   s                r   Úfused_linear_cross_entropy_lossrÍ   (  s?   € ôj +×0Ñ0Ø	ØØØØØØØØØØØØóð r   c                   ó  ^ • \ rS rSr         SS\S\S\S\S\S\S	\S
\S\4U 4S jjjr\	R                  R                   SS\	R                  S\	R                  S\	R                  S\	R                  S-  4S jj5       rSrU =r$ )ÚFusedLinearCrossEntropyLossin  Nr_   rK   rL   rM   ry   rN   rz   r{   r|   c
                 ó°   >• [         T
U ]  5         US;   d   SU S35       eXl        X l        X0l        X@l        XPl        X`l        Xpl        X€l	        X�l
        g)a¢  
Args:
    ignore_index: int.
        If target == ignore_index, the loss is set to 0.0.
    label_smoothing: float
    logit_scale: float
        A scaling factor applied to the logits. Default: 1.0
    logit_softcapping: float
        If > 0, apply logit softcapping: logits = softcap * tanh(logits / softcap).
        Default: 0.0
    num_chunks: int
        The number of chunks to split the input tensor into for processing.
        This can help optimize memory usage and computation speed.
        Default: 8
    reduction:
        Specifies the reduction to apply to the output: 'mean' | 'sum'.
        'mean': the weighted mean of the output is taken,
        'sum': the output will be summed.
        Default: 'mean'.
    use_l2warp:
        Whether to add the L2Warp logit regularization gradient. The penalty is normalized by
        the full number of input tokens, matching `fla.modules.l2warp.l2_warp`.
        Default: `False`.
    l2_penalty_factor:
        The L2Warp penalty factor. Default: `1e-4`.
    accumulate_grad_in_fp32:
        Whether to accumulate weight and bias gradients in fp32 before casting them
        back to the parameter dtype. Default: `True`.
)rU   r.   zreduction: z is not supportedN)ÚsuperÚ__init__r_   rK   rL   rM   ry   rN   rz   r{   r|   )Úselfr_   rK   rL   rM   ry   rN   rz   r{   r|   Ú	__class__s             €r   rÒ   Ú$FusedLinearCrossEntropyLoss.__init__p  sa   ø€ ôR 	‰ÑÔà˜OÓ+ÐW¨{¸9¸+ÐEVÐ-WÓWÐ+à(ÔØ.ÔØ&ÔØ!2ÔØ$ŒØ"ŒØ$ŒØ!2ÔØ'>Õ$r   r1   r\   rw   rx   c                 ó<  • [        UR                  SUR                  S   5      UR                  S5      UUU R                  U R                  U R
                  U R                  U R                  U R                  U R                  U R                  U R                  S9nU$ )a}  
Args:
    x (torch.Tensor): [batch_size, seq_len, hidden_size]
    target (torch.LongTensor): [batch_size, seq_len]
        where each value is in [0, V).
    weight (torch.Tensor): [vocab_size, hidden_size]
        where `vocab_size` is the number of classes.
    bias (Optional[torch.Tensor]): [vocab_size]
        where `vocab_size` is the number of classes.
Returns:
    loss
r>   )rw   rx   r_   rK   rL   rM   ry   rN   rz   r{   r|   )rÍ   rA   r@   r_   rK   rL   rM   ry   rN   rz   r{   r|   )rÓ   r1   r\   rw   rx   r]   s         r   r´   Ú#FusedLinearCrossEntropyLoss.forward§  s‡   € ô( /Ø�F‰F�2�q—w‘w˜r‘{Ó#Ø�K‰K˜‹OØØØ×*Ñ*Ø ×0Ñ0Ø×(Ñ(Ø"×4Ñ4Ø—‘Ø—n‘nØ—‘Ø"×4Ñ4Ø$(×$@Ñ$@ñ
ˆð ˆr   )	r|   r_   r{   rK   rL   rM   ry   rN   rz   )	r¼   rR   rT   Nr½   rU   Fr¾   Tr·   )r¿   rÀ   rÁ   rÂ   rÆ   r,   rÇ   rÈ   rÒ   rF   ÚcompilerÚdisablerÄ   rÅ   r´   rÉ   Ú__classcell__©rÔ   s   @r   rÏ   rÏ   n  sã   ø† ð !Ø!$Ø Ø#'ØØØ Ø#'Ø(,ñ5?àð5?ð ð5?ð ð	5?ð
 !ð5?ð ð5?ð ð5?ð ð5?ð !ð5?ð "&÷5?ð 5?ðn ‡^�^×Ñð %)ñ"à�<‰<ð"ð × Ñ ð"ð —‘ð	"ð
 �l‰l˜TÑ!ô"ó ö"r   rÏ   c                   óœ   ^ • \ rS rSrSSS.S\S\4U 4S jjjr\S 5       r\S	 5       r	S
\
R                  S\S\
R                  4S jrSrU =r$ )ÚLinearLossParalleliÍ  r!   F)Úsequence_dimÚuse_local_outputrÞ   rß   c                óP   >• [         TU ]  5         [        U5      4U l        X l        g r·   )rÑ   rÒ   r   Úsequence_shardingrß   )rÓ   rÞ   rß   rÔ   s      €r   rÒ   ÚLinearLossParallel.__init__Î  s&   ø€ ô 	‰ÑÔä"'¨Ó"5Ð!7ˆÔØ 0Õr   c                 ó~  • Uu  pEpg[        U[        5      (       d  [        R                  " XCU 5      nUR                  U :w  a  UR	                  U SS9n[        U[        5      (       d   [        R                  " XS[        5       /5      nUR                  U :w  a  UR	                  U SS9n[        U[        5      (       d   [        R                  " Xc[        5       /5      nUR                  [        5       /:w  a  UR	                  [        5       /SS9nUb5  [        U[        5      (       d   [        R                  " Xs[        5       /5      nUb2  UR                  [        5       /:w  a  UR	                  [        5       /SS9nUR                  5       UR                  5       UR                  5       Ub  UR                  5       4$ U4$ )NT)Ú
placementsÚasync_op)Ú
isinstancer   Ú
from_localrä   Úredistributer   Úto_local)rá   ÚmodÚinputsÚdevice_meshr1   r\   rw   rx   s           r   Ú_prepare_input_fnÚ$LinearLossParallel._prepare_input_fnÙ  sq  € à"(Ñˆ�6ä˜!œW×%Ñ%ä×"Ò" 1Ð3DÓEˆAØ�<‰<Ð,Ó,Ø—‘Ð*;Àd�ÐKˆAÜ˜&¤'×*Ñ*Ü×'Ò'¨¼i»k¸]ÓKˆFØ×ÑÐ 1Ó1Ø×(Ñ(Ð4EÐPTÐ(ÐUˆFä˜&¤'×*Ñ*Ü×'Ò'¨¼i»k¸]ÓKˆFØ×Ñ¤£ Ó-à×(Ñ(´Y³[°MÈDÐ(ÐQˆFàÑ¤J¨t´W×$=Ñ$=Ü×%Ò% d¼)»+¸ÓGˆDØÑ §¡´I³K°=Ó @Ø×$Ñ$´³°ÈÐ$ÐMˆDà�z‰z‹|˜VŸ_™_Ó.°·±Ó0AÐVZÑVfÀ4Ç=Á=Ã?ÐpÐpÐlpÐpÐpr   c                 ó4   • U (       a  UR                  5       $ U$ r·   )ré   )rß   rê   Úoutputsrì   s       r   Ú_prepare_output_fnÚ%LinearLossParallel._prepare_output_fnô  s   € æ%5ˆw×ÑÓ!ÐB¸7ÐBr   Úmodulerì   rÊ   c           
      ó”   • [        UUS [        U R                  U R                  5      [        U R                  U R
                  5      S9$ )N)Úpartition_fnÚinput_fnÚ	output_fn)r   r   rí   rá   rñ   rß   )rÓ   ró   rì   s      r   Ú_applyÚLinearLossParallel._applyø  sD   € Ü ØØØÜ˜T×3Ñ3°T×5KÑ5KÓLÜ˜d×5Ñ5°t×7LÑ7LÓMñ
ð 	
r   )rá   rß   )r¿   rÀ   rÁ   rÂ   rÆ   rÈ   rÒ   rÃ   rí   rñ   ÚnnÚModuler   rø   rÉ   rÚ   rÛ   s   @r   rÝ   rÝ   Í  s|   ø† ð Ø!&ò		1ð ð	1ð ÷		1ð 	1ð ñqó ðqð4 ñCó ðCð
˜RŸY™Yð 
°Zð 
ÀBÇIÁI÷ 
ò 
r   rÝ   )NNNr»   ):Ú	functoolsr   rF   Útorch.nnrú   Útorch.nn.functionalÚ
functionalr‹   rC   Útriton.languageÚlanguager&   Útorch.distributedr   Útorch.distributed.tensorr   r   r   Ú!torch.distributed.tensor.parallelr   Úfla.modules.backendsr	   Úfla.ops.utils.opr
   r   r   Ú	fla.utilsr   r   r   ÚImportErrorÚAttributeErrorr†   r�   Ú
heuristicsÚjitÚ	constexprr:   r,   r<   rJ   rp   rv   rÄ   rÅ   rÆ   rÇ   rÈ   r©   r­   ÚautogradÚFunctionr¯   ÚtuplerÍ   rû   rÏ   rÝ   r   r   r   Ú<module>r     s  ðõ ã Ý ß Ð Û Ý Ý (ß HÑ HÝ ;å )ß +Ñ +ß )ðÝ0ð €Þ‰r R€ð ×ÒÙ7ÙCñó ð ‡�ð1ð
 
‡|�|ð1ð 
‡|�|ð1ð �|‰|ð1ð —\‘\ó1ó ó	ð
1ñ0 
ˆ)Óð Ø $Ø $ñ	à�4‰<ðð ˜‘ðð �;‰;˜Ñô	ó ðð6 ‡�ðP ð —\‘\ðP ð —‘ðP ð —|‘|ðP ð �|‰|ðP ð 
‡|�|ðP ð 	�‰óP ó ðP ðf ‡�ð/ð 
‡|�|ð/ð 
‡|�|ó	/ó ð/ñ> 
ˆ)Óð
 ØØ ØØ#ØØØØ#Ø$(ñEØ‡|�|ðEà×ÑðEð �L‰LðEð �,‰,ð	Eð
 ðEð ðEð ðEð ðEð ðEð ðEð ðEð ðEð "ôEó ðEñP 
ˆ)Óð)Ø�‰ð)à�‰ð)ð 	�‰ð)ð 	�‰ó	)ó ð)ôX[V e§n¡n×&=Ñ&=ô [VðD ØØ ØØ#ØØØØ#Ø$(ñCØ‡|�|ðCà×ÑðCð �L‰LðCð �,‰,ð	Cð
 ðCð ðCð ðCð ðCð ðCð ðCð ðCð ðCð "ðCð ˆ5�<‰<˜Ÿ™Ð%Ñ&õCôL\ "§)¡)ô \ô~2
˜õ 2
øðc 	�^Ð$ó ØƒGðús   ÁK Ë
KËK