+
    ړLj-                     :    R t ^ RIt ! R R4      t ! R R4      tR# )aN  
Helper classes for working with low precision floating point types that
align with the opencompute (OCP) microscaling (MX) specification.
  * MXFP4Tensor: 4-bit E2M1 floating point data
  * MXScaleTensor: 8-bit E8M0 floating point data
Reference: https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
Nc                   H   a  ] tR t^t o R	R ltR tR tR tR tR t	Rt
V tR# )
MXFP4TensorNc                   W0n         VeQ   \        V\        P                  4      '       g   Q R4       hVP                   V n         V P	                  V4      V n        R# Ve#   \        V\        4      '       d   TMV3V n        R# \        R4      h)a<  
Tensor class for working with four bit E2M1 floating point data as defined by the
opencompute microscaling specification.


Parameters:
- data: A torch tensor of float32 numbers to convert to fp4e2m1 microscaling format.
- size: The size of the tensor to create.
- device: The device on which to create the tensor.
N%Parameter data must be a torch tensor.Either parameter data or size must be provided	device
isinstancetorchTensor_from_floatdatatuplesize
ValueErrorselfr   r   r   s   &&&&=/app/.local/lib/python3.14/site-packages/triton/tools/mxfp.py__init__MXFP4Tensor.__init__   sr     dELL11Z3ZZ1++DK((.DI *4 7 7dXDIMNN    c                   \         P                  ! ^ ^V P                  \         P                  V P                  R7      p\         P                  ! ^ ^V P                  \         P                  V P                  R7      p\         P                  ! ^ ^V P                  \         P                  V P                  R7      pV^,          V^,          ,          V,          P                  \         P                  4      V n        V # )    r   dtyper   )r
   randintr   uint8r   typer   )r   SEMs   &   r   randomMXFP4Tensor.random#   s    MM!QTYYekk$++VMM!QTYYekk$++VMM!QTYYekk$++V1fa(1,225;;?	r   c                F   V\         P                  8X  g   Q R4       hV P                  pV^,	          ^,          P                  V4      pV^,	          ^,          P                  V4      pV^,          P                  V4      p\         P                  ! V4      pV^ 8H  V^ 8H  ,          pV( pVP                  4       '       d   W8,          p	WH,          p
WX,          p\         P                  ! RV	4      p\         P                  ! V
^ 8H  W^,
          4      p\         P                  ! V
^ 8H  VR,          RVR,          ,           4      pV\         P                  ! ^V4      ,          V,          pWV&   WgV^8H  ,          ;;,          R,          uu&   VP                  \         P                  4      # )zj
Convert fp4e2m1 data to float32.

Returns:
- A torch tensor of type dtype representing the fp4e2m1 data.
zCCurrently only float32 is supported for fp4e2m1 to float conversion      ?      ?)r
   float32r   r   
zeros_likeanypowwhere)r   r   r   r   r   r    valueis_zeronon_zero_maskS_nzE_nzM_nzsignexponentmantissavalue_nzs   &&              r   toMXFP4Tensor.to+   sV    %l'll%yyai3$$U+ai3$$U+CZe$   #6a1f% #D#D#D99R&D{{419d1H=H{{419dSj#s
:JKHeii844x?H#+-  	a !R'!zz%--((r   c                4   \         P                  ! V4      P                  \         P                  4      p\         P                  ! V4      pV^ 8H  p\         P
                  ! V4      \         P                  ! V4      ,          p\         P                  ! . RO\         P                  V P                  R7      p\         P                  ! ^ ^.\         P                  V P                  R7      p. p. p	. p
V F  pV^ 8X  dZ   ^ pV FO  pVR,          pV^V,          ,          pVP                  V4       V	P                  V4       V
P                  V4       KQ  	  Kc  VP                  4       ^,
          pV FV  pRVR,          ,           pV^V,          ,          pVP                  V4       V	P                  V4       V
P                  V4       KX  	  K  	  \         P                  ! V\         P                  V P                  R7      p\         P                  ! V	\         P                  V P                  R7      p	\         P                  ! V
\         P                  V P                  R7      p
VP                  R	4      pVP                  ^ ,          pVP                  ^4      pVP                  4       P                  4       pVVVP                  R	4      &   \         P                  ! VVP                  ^ 4      ,
          4      p\         P                   ! V^RR7      w  ppVV8H  pVP#                  4       ^8  dT   V
P                  ^ 4      P%                  VR	4      pV^ 8H  P                  \         P&                  4      pVVR,          ,
          p\         P(                  ! V^R7      pV	V,          pV
V,          pVP                  VP                  4      pVP                  VP                  4      p^ W&   ^ W&   V^,          V^,          ,          V,          P                  \         P                  4      # )
a  
Convert float32 numbers to mxf4 e2m1 format.
* No encodings are reserved for Inf or NaN in mxf4.
* Conversion from float supports roundTiesToEven rounding mode.
* If a value exceeds the mxf4 representable range after rounding,
  clamps to the maximum mxf4 magnitude, preserving the sign.
* If a value has magnitude less than the minimum subnormal magnitude
  in mxf4 after rounding, converts to zero.

Parameters:
- values: A torch tensor of float32 numbers to convert to fp4 format.
r   r   r$   r%   T)dimkeepdimgư>r:   )r            r&   )r
   signbitr   r   absisnanisinftensorr   appenditemr'   viewshape	unsqueezemaxminsumexpandint32argmin)r   valuesr   
abs_valuesr-   
is_invalidE_bitsM_bitscandidate_valuescandidate_Ecandidate_Mr   r3   r    significandr,   
candidatesabs_values_flatNabs_values_expandedmax_candidate_valueerrors
min_errors_is_tieM_bits_expandedtie_breakerbest_indices
E_selected
M_selecteds   &&                            r   r   MXFP4Tensor._from_floatN   s$    MM&!&&u{{3YYv&
?[[(5;;v+>>

 l%++dkkRq!fEKKLAAvA"#c'K'1h;7E$++E2&&q)&&q)   668a<A"%C-K'1h;7E$++E2&&q)&&q)   ( \\"2%--PTP[P[\
ll;ekk$++Vll;ekk$++V$//"-!!!$-77: )nn.335/B
+, .1E1Ea1HHI
 		&a>
AJ&::<!)33A6==aDO*a/55ekkBK{T12F||F2 .
 .
OOJ,,-OOJ,,-

aAF#a'--ekk::r   c                   V P                   p^ Tu;8:  d   VP                  8  g   Q R4       h Q R4       hVP                  V4      pV^,           ^,          pV^,          ^ 8w  dt   ^ .^VP                  ,          ,          pVP                  V,
          ^,
          ^,          ^,           p^WV&   \        P                  P
                  P                  W%R^ R7      p\        VP                  4      pWGV&   VP                  V^,           ^4       VP                  ! V!  pVP                  V^,           ^ 4      pVP                  V^,           ^4      p	V	^,          V,          p
V
# )z
Packs two e2m1 elements into a single uint8 along the specified dimension.

Parameters:
- dim: The dimension along which to pack the elements.

Returns:
- A torch tensor of dtype uint8 with two e2m1 elements packed into one uint8.
zHThe dimension to pack along is not within the range of tensor dimensionsconstant)moder,   )r   ndimr   r
   nn
functionalpadlistrH   insertreshapeselect)r   r:   r   size_along_dimnew_size_along_dim	pad_sizes	pad_index	new_shapelowhighpackeds   &&         r   to_packed_tensorMXFP4Tensor.to_packed_tensor   s<    yyC#$))# 	WV	W# 	WV	W# 3,q0Q6 A"q499}-IS1,1A5I#$I 88&&**4ST*UD$	+#q!$||Y'kk#'1%{{37A&!)s"r   c                   V^,	          ^,          pV^,          p\         P                  ! WT3V^,           R7      p\        VP                  4      pVRV Wr,          ^,          .,           Wr^,           R ,           pVP                  ! V!  p	W2,          ^,          ^ 8w  dD   \        R4      .V	P                  ,          p
\        ^ W2,          4      W&   V	\        V
4      ,          p	V	P                  \         P                  4      # )a}  
Unpacks a tensor where two fp4 elements are packed into a single uint8.

Parameters:
- packed_tensor: The packed tensor
- dim: The dimension along which the tensor was packed.
- original_shape: The shape of the original tensor before packing.

Returns:
- A tensor with the original data unpacked into uint8 elements containing one
  fp4e2m1 element in the least significant bits.
r<   N)
r
   stackro   rH   rq   slicerk   r   r   r   )r   packed_tensorr:   original_shapery   rx   stackedrH   rw   r   indicess   &&&&       r   unpack_packed_tensor MXFP4Tensor.unpack_packed_tensor   s     "c)c!++sksQw7 W]]#$3K5:>"22U78_D		* "a'T{mdii/G N$78GLg'Dyy%%r   r   r   r   NNN)__name__
__module____qualname____firstlineno__r   r!   r6   r   r{   r   __static_attributes____classdictcell____classdict__s   @r   r   r      s-     O*!)FV;p!F& &r   r   c                   @   a  ] tR t^t o RR ltRR ltR tR tRtV t	R# )	MXScaleTensorNc                   W0n         VeQ   \        V\        P                  4      '       g   Q R4       hVP                   V n         V P	                  V4      V n        R# Ve#   \        V\        4      '       d   TMV3V n        R# \        R4      h)a  
Tensor class for working with microscaling E8M0 block scale factors.

Parameters:
- data: A torch tensor of float32 numbers to convert to fp8e8m0 microscaling format.
- size: The size of the tensor to create.
- device: The device on which to create the tensor.
Nr   r   r   r   s   &&&&r   r   MXScaleTensor.__init__   sr     dELL11Z3ZZ1++DK((.DI *4 7 7dXDIMNNr   c                   ^pVf   ^ MC\        ^ \        \        P                  ! \        P                  ! V4      4      4      V,           4      pVf   ^MM\        ^\        ^ \        \        P                  ! \        P                  ! V4      4      4      V,           4      4      pWE8:  g   Q R4       h\        P                  ! WE^,           V P                  \        P                  V P                  R7      pW`n
        V # )zX
Generate random E8M0 data within a specified range.
* Excludes the NaN encoding (255).
z&Low must be less than or equal to highr   )rJ   intr
   log2rD   rK   r   r   r   r   r   )r   rx   ry   biasmin_exponentmax_exponentr   s   &&&    r   r!   MXScaleTensor.random   s    
 KqSC

5<<PSCT8U4VY]4]-^"lsCQEJJu||\`OaDb@cfj@j9k0l+U-UU+MM,q(8tyyPUP[P[dhdodop	r   c                (   V\         P                  8X  g   Q R 4       hV P                  P                  V4      pV^8H  pVP	                  4       p^ WC&   V^,
          p\         P
                  ! RV4      p\         P                  Wc&   VP                  V4      # )zBCurrently only float32 is supported for f8e8m0 to float conversiong       @)r
   r'   r   r   cloner*   nan)r   r   r   is_nane_biaseder,   s   &&     r   r6   MXScaleTensor.to  s{    %k'kk%yy~~e$#+::<sN		#q!		zz%  r   c                   \         P                  ! V\         P                  V P                  R7      p\         P                  ! V4      \         P
                  ! V4      ,          V^ 8*  ,          p^W#&   W( ,          p\         P                  ! \         P                  ! V4      4      pV^,           pVP                  \         P                  4      p\         P                  ! V^ ^4      pVP                  \         P                  4      W#( &   V# )a  
Convert float32 numbers to E8M0 format.
* Values <= 0, NaNs, and Infs are converted to the NaN encoding (255).
* Positive values are converted by computing the floor of log2(value) to get the exponent.

Parameters:
- values: A torch tensor of float32 numbers to convert to E8M0 format.
r9   )r
   
empty_liker   r   rB   rC   floorr   r   rN   clamp)	r   rP   resultrR   valid_valuesr   r   e_biased_inte_biased_clampeds	   &&       r   r   MXScaleTensor._from_float  s     !!&DKKP[[(5;;v+>>&A+N
 k*KK

<01s7}}U[[1 ;;|Q<.33EKK@{r   r   r   )NN)
r   r   r   r   r   r!   r6   r   r   r   r   s   @r   r   r      s     O&	! r   r   )__doc__r
   r   r    r   r   <module>r      s(    Z& Z&zD Dr   