Ë
    [^(h‚m  ã                   ó  — d dl Z d dlmZ d dlmZmZmZ d dlZd dlm	Z	m
Z
 d dlmZmZmZmZmZmZmZmZmZmZ g d¢Z edd«      Z G d	„ d
ej0                  «      Z	 ddej0                  dedefd„Z G d„ de«      Z G d„ de«      Zy)é    N)Ú
namedtuple)ÚAnyÚCallableÚOptional)Ú)sparse_semi_structured_from_dense_cutlassÚ'sparse_semi_structured_to_dense_cutlass)
Úfallback_dispatcherÚsemi_sparse_addmmÚsemi_sparse_detachÚsemi_sparse_indicesÚsemi_sparse_linearÚsemi_sparse_mmÚsemi_sparse_scaled_mmÚsemi_sparse_tÚsemi_sparse_valuesÚsemi_sparse_view)ÚSparseSemiStructuredTensorÚ!SparseSemiStructuredTensorCUTLASSÚ$SparseSemiStructuredTensorCUSPARSELTÚto_sparse_semi_structuredÚ_SEMI_STRUCTURED_SPARSE_CONFIGz=sparse_min_rows sparse_min_cols dense_min_rows dense_min_colsc                   ó$  — e Zd ZU dZdZeed<   eej                  e
f   ed<   dZeed<   dZeed<   dZeed<   eed	<   eeef   ed
<   eej$                     ed<   eej$                     ed<   eej$                     ed<   eej$                     ed<   eej$                     ed<   eed<   eed<   g d¢Ze	 	 	 d(dej*                  deej$                     deej$                     deej$                     deej$                     deej$                     dededefd„«       Zdefd„Zdeee   eej*                  eeef   f   fd„Zedeej*                  eeef   dej$                  fd„«       Zej:                  j<                  Zede fd„«       Z!ed)d*d„«       Z"edej$                  ddfd„«       Z#ed ej$                  dej$                  fd!„«       Z$d"„ Z%edej$                  dd fd#„«       Z&dd$œd%ej$                  d&eej$                     dej$                  fd'„Z'y)+r   a»  
    This class implementes semi-structured sparsity as a Tensor subclass.

    Semi-structured sparsity describes a sparsity pattern where n in every 2n elements are sparse,
    depending on the datatype. It is also referred to as 2:4 sparsity or fine-grained
    structured sparsity.

    There are two backends available for semi_structred sparsity, either cuSPARSELt or CUTLASS.
    This class is meant to serve as a base class for both implementations. SparseSemiStructuredCUTLASS
    and SparseSemiStructuredCUSPARSELT both inherit from this class and define three backend-specific items.
    Note that as such, this class cannot be insantiated directly.

    -`_DTYPE_SHAPE_CONSTRAINTS` - A dictionary holding backend specific dense/sparse min shape constraints
    - `def from_dense()` - backend specific compression routines
    - `def _mm()` - backend specifc mm op (either torch._cslt_sparse_mm or torch._sparse_semi_structured_(mm|addmm))
    r   Ú_DEFAULT_ALG_IDÚ_DTYPE_SHAPE_CONSTRAINTSFÚ_FORCE_CUTLASSÚ_FUSE_TRANSPOSEÚ_PROTOTYPE_WARNING_SHOWNÚBACKENDÚSPARSE_DISPATCHÚpackedÚmetaÚpacked_tÚmeta_tÚcompressed_swizzled_bitmaskÚfuse_transpose_cusparseltÚalg_id_cusparselt)r    r!   r"   r#   r$   ÚshapeÚrequires_gradc
                 óÖ  — | j                   sPt        j                  dt        «       d| _         | j	                  «        t
        j                  j                  | «       |�|}
n|�|}
nt        d«      ‚|
j                  |
j                  |
j                  |	dœ}t        j                  j                  | |fi |¤Ž}||_        ||_        ||_        ||_        ||_        ||_        ||_        |S )a0  
        Create a new instance of the tensor subclass from the compressed sparse representation.

        We have the option to create the subclass with the compressed representations of both X and X', for training.
        For inference, we only need a single representation (either X or X'), while the corresponding other set will be None.

        Depending on the backend selected, certain fields will be set to None. (CUSPARSELT vs CUTLASS)

        Args:
            shape: The shape of the original dense tensor
            packed: The compressed representation of the original dense tensor
            meta: The metadata of the original dense tensor, if it is stored separately
            packed_t: The compressed representation of the transposed original dense tensor
            meta_t: The metadata of the transposed original dense tensor, if it is stored separately
            compressed_swizzled_bitmask: The masks used by the CUTLASS backend to determine which threads should
                                         participate in the computation. Used for pointwise ops.
            fuse_transpose_cusparselt: When running with cuSPARSELt, we have the option to fuse a transposition
                                       with a matmul, which is useful in the case of 2:4 sparse training.
            alg_id_cusparselt: The algorithm id to use when using cuSPARSELT, will have effect on performance

        Returns:
            torch.Tensor: A torch.Tensor wrapper subclass.

        Raises:
            ValueError: If all of the tensor arguments are None.
        zøThe PyTorch API of SparseSemiStructuredTensor is in prototype stage and will change in the near future. Please open a Github issue for features requests and see our documentation on the torch.sparse module for further information about the project.Tz3At least one of packed or packed_t must be provided)ÚdeviceÚdtypeÚlayoutr(   )r   ÚwarningsÚwarnÚUserWarningÚ_load_dispatch_tableÚtorchÚ_dynamoÚallow_in_graphÚ
ValueErrorr*   r+   r,   ÚTensorÚ_make_wrapper_subclassr    r!   r"   r#   r$   r%   r&   )Úclsr'   r    r!   r"   r#   r$   r%   r&   r(   Úprevious_tensorÚkwargsÚtensors                úZ/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/sparse/semi_structured.pyÚ__new__z"SparseSemiStructuredTensor.__new__K   sð   € ðN ×+Ò+Ü�M‰MðHô
 ôð ,0ˆCÔ(ð
 ×$Ñ$Ô&ô �M‰M×(Ñ(¨Ô-àÐØ$‰OØÐ!Ø&‰OäÐRÓSÐSð &×,Ñ,Ø$×*Ñ*Ø%×,Ñ,Ø*ñ	
ˆô —‘×4Ñ4°S¸%ÑJÀ6ÑJˆàˆŒØˆŒØ"ˆŒØˆŒØ-HˆÔ*Ø+DˆÔ(Ø#4ˆÔ Øˆó    Úreturnc                 ój   — t        | d«      sJ ‚| j                  j                  › d| j                  › d�S )Nr'   z(shape=ú))ÚhasattrÚ	__class__Ú__name__r'   )Úselfs    r;   Ú__repr__z#SparseSemiStructuredTensor.__repr__ž   s4   € Ü�t˜WÔ%Ð%Ð%Ø—.‘.×)Ñ)Ð*¨'°$·*±*°¸QÐ?Ð?r=   c                 ó¬   ‡ — t        t        ˆ fd„‰ j                  «      «      }‰ j                  ‰ j                  ‰ j
                  ‰ j                  f}||fS )Nc                 ó    •— t        ‰| «      d uS ©N)Úgetattr)ÚxrD   s    €r;   ú<lambda>z?SparseSemiStructuredTensor.__tensor_flatten__.<locals>.<lambda>¦   s   ø€ œW T¨1Ó-°TÐ9€ r=   )ÚlistÚfilterÚ	__slots__r'   r%   r&   r(   )rD   Úinner_tensorsÚtensor_metas   `  r;   Ú__tensor_flatten__z-SparseSemiStructuredTensor.__tensor_flatten__¢   sV   ø€ ô ÜÓ9¸4¿>¹>ÓJó
ˆð �J‰JØ×*Ñ*Ø×"Ñ"Ø×Ñð	
ˆð ˜kÐ)Ð)r=   rP   c                 óÒ   — |\  }}}} | ||j                  dd «      |j                  dd «      |j                  dd «      |j                  dd «      |j                  dd «      |||¬«	      S )Nr    r!   r"   r#   r$   ©	r'   r    r!   r"   r#   r$   r%   r&   r(   )Úget)	r7   rO   rP   Ú
outer_sizeÚouter_strider'   r%   r&   r(   s	            r;   Ú__tensor_unflatten__z/SparseSemiStructuredTensor.__tensor_unflatten__°   s…   € ð NYÑJˆÐ(Ð*;¸]ÙØØ ×$Ñ$ X¨tÓ4Ø×"Ñ" 6¨4Ó0Ø"×&Ñ& z°4Ó8Ø ×$Ñ$ X¨tÓ4Ø(5×(9Ñ(9Ø-¨tó)ð '@Ø/Ø'ô
ð 	
r=   c                 óÀ   — |j                   | j                  vr%t        | j                  › d|j                  › d�«      ‚ | j                  |j                      ||||«      S )NzI only supports a specific set of operations, can't perform requested op (r@   )Ú_overloadpacketr   ÚNotImplementedErrorrC   )r7   ÚfuncÚtypesÚargsr9   s        r;   Ú__torch_dispatch__z-SparseSemiStructuredTensor.__torch_dispatch__É   sh   € à×Ñ s×':Ñ':Ñ:Ü%Ø—<‘<�.ð !/Ø/3¯}©}¨o¸Qð@óð ð 9ˆs×"Ñ" 4×#7Ñ#7Ñ8¸¸uÀdÈFÓSÐSr=   Nc                 ó|  — t        | dd«      �€.t        j                  j                  j                  t
        t        j                  j                  j                  t        t        j                  j                  j                  t        t        j                  j                  j                  t        t        j                  j                  j                  t        t        j                  j                  j                  t        t        j                  j                  j                  t         t        j                  j                  j"                  t$        t        j                  j                  j&                  t$        t        j                  j                  j(                  t*        t        j                  j                  j,                  t.        t        j                  j                  j0                  t        t        j                  j                  j2                  t4        i| _        |�| j6                  j9                  |«       yyy)zT
        Loads the op overload sparse dispatch table for the current class.
        r   N)rI   r1   ÚopsÚatenÚvaluesr   Úindicesr   Úis_same_sizer	   Údetach_Údetachr   Útr   Úviewr   Úmmr   ÚmatmulÚaddmmr
   Úlinearr   Ú_to_copyÚ
_scaled_mmr   r   Úupdate)r7   Úcustom_dispatch_tables     r;   r0   z/SparseSemiStructuredTensor._load_dispatch_tableÒ   sF  € ô
 �3Ð)¨4Ó0Ñ8ä—	‘	—‘×%Ñ%Ô'9Ü—	‘	—‘×&Ñ&Ô(;Ü—	‘	—‘×+Ñ+Ô-@Ü—	‘	—‘×&Ñ&Ô(;Ü—	‘	—‘×%Ñ%Ô'9Ü—	‘	—‘× Ñ ¤-Ü—	‘	—‘×#Ñ#Ô%5Ü—	‘	—‘×!Ñ!¤>Ü—	‘	—‘×%Ñ%¤~Ü—	‘	—‘×$Ñ$Ô&7Ü—	‘	—‘×%Ñ%Ô'9Ü—	‘	—‘×'Ñ'Ô)<Ü—	‘	—‘×)Ñ)Ô+@ð#ˆCÔð %Ð0Ø×#Ñ#×*Ñ*Ð+@ÕAð 1ð! 9r=   Úoriginal_tensorc           	      ó\  — |j                   st        d|j                  › d�«      ‚|j                  «       dk7  rt        d|j                  «       › d�«      ‚|j	                  «       st        d«      ‚|j
                  | j                  vrt        d|j
                  › d| › d	�«      ‚|j                  \  }}| j                  |j
                     j                  }| j                  |j
                     j                  }||k  s||z  s
||k  s||z  rt        d
|j                  › d|› d|› d�«      ‚y)z_
        Assert that the given tensor is valid for semi-structured sparse compression.
        zError original_tensor.device= z= is not supported! Only CUDA tensors are currently supported.é   zError original_tensor.dim = z; is not supported! Only 2d tensors are currently supported.zXError original_tensor is not contiguous!Only contiguous tensors are currently supported.zError original_tensor.dtype z is not a supported dtype for ú!zError original_tensor.shape zS is not supported! Both dimensions must be larger or equal than and a multiple of (z, r@   N)
Úis_cudaÚRuntimeErrorr*   ÚdimÚis_contiguousr+   r   r'   Úsparse_min_rowsÚsparse_min_cols)r7   rq   ÚmÚnÚmin_rowsÚmin_colss         r;   Ú _validate_device_dim_dtype_shapez;SparseSemiStructuredTensor._validate_device_dim_dtype_shapeê   sr  € ð ×&Ò&ÜØ0°×1GÑ1GÐ0Hð I=ð =óð ð ×ÑÓ  AÒ%ÜØ.¨×/BÑ/BÓ/DÐ.Eð F;ð ;óð ð ×,Ñ,Ô.ÜðCóð ð × Ñ ¨×(DÑ(DÑDÜØ.¨×/DÑ/DÐ.EÐEcÐdgÐchÐhiÐjóð ð
 ×$Ñ$‰ˆˆ1Ø×/Ñ/°×0EÑ0EÑF×VÑVˆØ×/Ñ/°×0EÑ0EÑF×VÑVˆØˆxŠ<˜1˜xš<¨1¨xª<¸1¸xº<äØ.¨×/DÑ/DÐ.Eð FSØS[ÐR\Ð\^Ð_gÐ^hÐhiðkóð ð <Hr=   Údense_inputc                 ó†  — |j                  «       dk(  sJ ‚|j                  \  }}| j                  |j                     j                  }| j                  |j                     j
                  }||k  s||z  r| |z  nd}||k  s||z  r| |z  nd}|s|r.t        j                  j                  j                  |d|d|f«      S |S )z¢
        Calculates padding for dense tensor and pads tensor if necessary.
        If padding is not required, this function returns the original tensor.
        rs   r   )
rw   r'   r   r+   Údense_min_rowsÚdense_min_colsr1   ÚnnÚ
functionalÚpad)r7   r€   r{   r|   r}   r~   Úto_pad_mÚto_pad_ns           r;   Ú_pad_dense_inputz+SparseSemiStructuredTensor._pad_dense_input  sÇ   € ð �‰Ó  AÒ%Ð%Ð%ð × Ñ ‰ˆˆ1Ø×/Ñ/°×0AÑ0AÑB×QÑQˆØ×/Ñ/°×0AÑ0AÑB×QÑQˆð %&¨¢L°A¸²L�A�2˜’=ÀaˆØ$%¨¢L°A¸²L�A�2˜’=ÀaˆÙ‘xÜ—8‘8×&Ñ&×*Ñ*¨;¸¸HÀaÈÐ8RÓSÐSàÐr=   c                 ó    — | j                   d   }t        j                  | t        j                  || j                  | j
                  ¬«      «      S )Néÿÿÿÿ)r+   r*   )r'   r1   ri   Úeyer+   r*   )rD   Úcols     r;   Úto_densez#SparseSemiStructuredTensor.to_dense+  s5   € Ø�j‰j˜‰nˆÜ�x‰x˜œeŸi™i¨°4·:±:ÀdÇkÁkÔRÓSÐSr=   c                 ó   — t         ‚rH   ©rZ   ©r7   rq   s     r;   Ú
from_densez%SparseSemiStructuredTensor.from_dense/  s   € ä!Ð!r=   ©ÚbiasÚBr”   c                ó   — t         ‚rH   r�   )rD   r•   r”   r9   s       r;   Ú_mmzSparseSemiStructuredTensor._mm3  s
   € ô "Ð!r=   )Fr   FrH   )r>   N)(rC   Ú
__module__Ú__qualname__Ú__doc__r   ÚintÚ__annotations__Údictr1   r+   r   r   Úboolr   r   Ústrr   r   r5   rN   ÚstaticmethodÚSizer<   rE   ÚtuplerL   rQ   ÚclassmethodrW   Ú_CÚ_disabled_torch_function_implÚ__torch_function__r   r^   r0   r   r‰   rŽ   r’   r—   © r=   r;   r   r   &   sÝ  … ñð" €O�SÓØ" 5§;¡;Ð0NÐ#NÑOÓOØ €N�DÓ Ø!€O�TÓ!Ø%*Ð˜dÓ*àƒLØ˜( HÐ,Ñ-Ó-à�U—\‘\Ñ"Ó"Ø
�5—<‘<Ñ
 Ó Ø�u—|‘|Ñ$Ó$Ø�U—\‘\Ñ"Ó"Ø!)¨%¯,©,Ñ!7Ó7Ø#Ó#ØÓâW€Iàð +0Ø!"Ø#ñPà�z‰zðPð ˜Ÿ™Ñ&ðPð �u—|‘|Ñ$ð	Pð
 ˜5Ÿ<™<Ñ(ðPð ˜Ÿ™Ñ&ðPð &.¨e¯l©lÑ%;ðPð $(ðPð ðPð òPó ðPðd@˜#ó @ð*à	ˆt�C‰y˜% §
¡
¨D°#°tÐ ;Ñ<Ð<Ñ	=ó*ð ð
ð ˜5Ÿ:™: t¨S°$Ð6Ñ7ð
ð 
�‰ò
ó ð
ð, Ÿ™×?Ñ?ÐàðT¸cò Tó ðTð óBó ðBð. ð(¸u¿|¹|ð (ÐPTò (ó ð(ðT ð¨5¯<©<ð ¸E¿L¹Lò ó ðò*Tð ð"¨¯©ð "Ð:Vò "ó ð"ð (,ò	"à�<‰<ð"ð �u—|‘|Ñ$ð	"ð 
�‰ô"r=   r   rq   Ú
transposedr>   c                 óè   — |rt        j                  dt        d¬«       t        j                  rt
        j                  j                  nt
        j                  j                  }|j                  | «      S )a	  
    This function converts a dense tensor into a sparse semi-structured tensor.
    It will return a SparseSemiStructuredTensor, a subclass of torch.Tensor.

    This function will check to ensure the dense tensor has the right dtype, size, dims, and device.
    We currently only support semi-structured sparse tensors for 2d CUDA tensors.
    Additionally, your tensor must be a positive multiple of the mininum sparse block size, given in
    `_DTYPE_TO_SHAPE_CONSTRAINTS` for each dtype (float32, float16, bfloat16, int8).

    Args:
        original_tensor (Tensor): the dense tensor to convert
        transposed (bool, optional): deprecated arg to be removed in another release. Do not use.
    Returns:
        SparseSemiStructuredTensor: A sparse semi-structured tensor created from the given original_tensor
    Raises:
        None
    Example:
        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
        >>> A = torch.Tensor([0, 0, 1, 1]).tile((128, 32)).half().cuda()
        tensor([[0., 0., 1.,  ..., 0., 1., 1.],
                [0., 0., 1.,  ..., 0., 1., 1.],
                [0., 0., 1.,  ..., 0., 1., 1.],
                ...,
                [0., 0., 1.,  ..., 0., 1., 1.],
                [0., 0., 1.,  ..., 0., 1., 1.],
                [0., 0., 1.,  ..., 0., 1., 1.]], device='cuda:0', dtype=torch.float16)
        >>> A_sparse = to_sparse_semi_structured(A)
        SparseSemiStructuredTensor(shape=torch.Size([128, 128]))
        >>> A_sparse.values()
        tensor([[1., 1., 1.,  ..., 1., 1., 1.],
                [1., 1., 1.,  ..., 1., 1., 1.],
                [1., 1., 1.,  ..., 1., 1., 1.],
                ...,
                [1., 1., 1.,  ..., 1., 1., 1.],
                [1., 1., 1.,  ..., 1., 1., 1.],
                [1., 1., 1.,  ..., 1., 1., 1.]], device='cuda:0', dtype=torch.float16),
        >>> A_sparse.indices()
        tensor([[-4370, -4370, -4370,  ..., -4370, -4370, -4370],
                [-4370, -4370, -4370,  ..., -4370, -4370, -4370],
                [-4370, -4370, -4370,  ..., -4370, -4370, -4370],
                ...,
                [-4370, -4370, -4370,  ..., -4370, -4370, -4370],
                [-4370, -4370, -4370,  ..., -4370, -4370, -4370],
                [-4370, -4370, -4370,  ..., -4370, -4370, -4370]], device='cuda:0', dtype=torch.int16))
    z­Setting transpose from `to_sparse_semi_structured` is deprecated and will be removed in a future release. `SparseSemiStructuredTensor` only support contiguous input tensors.rs   )Ú
stacklevel)
r-   r.   ÚFutureWarningr   r   r1   Úsparser   r   r’   )rq   r¨   ÚSPARSE_SUBCLASSs      r;   r   r   =  sa   € ñb Ü�‰ðRô Øõ	
ô &×4Ò4ô 	�‰×6Ò6ä�\‰\×>Ñ>ð ð ×%Ñ% oÓ6Ð6r=   c                   óŽ  ‡ — e Zd ZdZdZej                   edddd«      ej                   edddd«      ej                   edddd«      ej                   edddd«      iZed	ej                  d
d fd„«       Zˆ fd„Ze	 dd	ej                  d
dfd„«       Zddœdej                  deej                     d
ej                  fd„Zˆ xZS )r   a£  
    This class implements semi-structured sparsity for the CUTLASS backend.


    In this implementation, the specified elements and metadata are stored seprately,
    in packed and meta respectively.

    When _FORCE_CUTLASS is set, or when cuSPARSELt is not available, this subclass calls into _sparse_semi_structured_(mm|addmm) and
    sparse_semi_structured_from_dense for conversion to the compressed format.
    Úcutlassé   é€   é    é@   é   é   rq   r>   c           	      ó†   — | j                  |«       t        |«      \  }} | |j                  ||d d d |j                  ¬«      S )N©r    r!   r"   r#   r$   r(   )r   r   r'   r(   )r7   rq   Úsparse_tensor_cutlassÚmeta_tensor_cutlasss       r;   r’   z,SparseSemiStructuredTensorCUTLASS.from_dense•  sU   € ð 	×,Ñ,¨_Ô=ô 6°oÓFñ	
Ø!ØáØ×!Ñ!Ø(Ø$ØØØ(,Ø)×7Ñ7ô
ð 	
r=   c                 óÆ   •— | j                   �| j                  €J ‚| j                   j                  dk(  r t        | j                  | j                   «      S t        ‰| �  «       S )Nrs   )r!   r    Úndimr   ÚsuperrŽ   )rD   rB   s    €r;   rŽ   z*SparseSemiStructuredTensorCUTLASS.to_dense¨  s]   ø€ Ø�y‰yÐ$¨¯©Ð)@Ð@Ð@ð �y‰y�~‰~ Ò"ô	 4Ø—‘Ø—	‘	óð	
ô ‘Ñ!Ó#ð	
r=   r   c           	      óp   — t        j                  ||d¬«      \  }}}}} | |j                  |||||d¬«      S )að  
        This function takes in a unpruned dense tensor and runs a (branchless) static sort across a 4x4 tile.

        It greedily picks the largest values in the tile, upholding the 2:4 sparsity constraint across both rows and columns.
        The algorithm used to prune the matrix is implemented in `_sparse_semi_structured_tile`.

        Then it creates the packed and meta tensors for the compressed sparse representation of the pruned dense tensor.
        It also calculates the packed_t and meta_t tensors for the compressed sparse representation of the transposed
        pruned dense tensor.
        Since we cannot transpose the compressed representations, we store both for the fw/bw pass respectively.

        Finally, this function also computes a compressed swizzled bitmask that encodes the sparsity pattern
        This can be used in the backward pass to mask the gradients.

        [9 1 7 4]                       [9 0 7 0]
        [1 2 3 0]                       [0 2 0 0]
        [8 3 5 4] -> prune 4x4 tile  -> [8 0 0 4] -> pack to CUTLASS semi-structured -> packed
        [1 2 6 2]                       [0 0 6 2]                                    -> metadata

                                                  -> pack to transposed CUTLASS      -> packed_t
                                                     semi-structured representation  -> metadata_t

                                                  -> compute swizzled bitmask        -> compressed_swizzled_bitmask


        The equivalent PyTorch code to create the same five outputs from the dense tensor can be found below:
        ```
        from torch.sparse import SparseSemiStructuredTensorCUTLASS
        from torch.sparse._semi_structured_conversions import _sparse_semi_structured_tile, _compute_compressed_swizzled_bitmask

        pruned = _sparse_semi_structured_tile(dense)
        packed_cutlass, meta_cutlass = sparse_semi_structured_from_dense_cutlass(pruned)
        packed_t_cutlass, meta_t_cutlass = sparse_semi_structured_from_dense_cutlass(pruned.t().contiguous())
        bitmask = _compute_compressed_swizzled_bitmask(pruned)

        SparseSemiStructuredTensorCUTLASS(dense.shape, packed_cutlass, meta_cutlass, packed_t_cutlass, meta_t_cutlass, bitmask)
        ```
        T©Ú	algorithmÚuse_cutlassFr·   ©r1   Ú_sparse_semi_structured_tiler'   ©r7   rq   r¿   r    r!   r"   r#   r$   s           r;   Úprune_dense_static_sortz9SparseSemiStructuredTensorCUTLASS.prune_dense_static_sort³  sX   € ôb ×.Ñ.Ø y¸dô
ñ	
ØØØØØ'ñ
 Ø×!Ñ!ØØØØØ(CØô
ð 	
r=   Nr“   r•   r”   c                óä  — t        |t        «      rt        d«      ‚| j                  j                  }| j
                  dk7  s|j
                  dk7  rt        d|› d�«      ‚| j                  �| j                  €t        d|› d�«      ‚|€,t        j                  | j                  | j                  |«      }n,t        j                  || j                  | j                  |«      }|d | j                  d    S )NúZ`SparseSemiStructuredTensor @ SparseSemiStructuredTensor` is not supported by the hardwarers   ú`ú)` matmul: Broadcasting is not implementedú$` matmul: operation is not supportedr   )Ú
isinstancer   r4   rB   rC   r»   rZ   r    r!   r1   Ú_sparse_semi_structured_mmÚ_sparse_semi_structured_addmmr'   )rD   r•   r”   r9   Úcls_nameÚress         r;   r—   z%SparseSemiStructuredTensorCUTLASS._mmò  sé   € ô �aÔ3Ô4ÜØlóð ð —>‘>×*Ñ*ˆØ�9‰9˜Š>˜QŸV™V qš[Ü%Ø�H�:ÐFÐGóð ð �;‰;Ð $§)¡)Ð"3Ü%Ø�H�:ÐAÐBóð ð ˆ|Ü×6Ñ6°t·{±{ÀDÇIÁIÈqÓQ‘ä×9Ñ9Ø˜$Ÿ+™+ t§y¡y°!ó�ð �˜Ÿ™ A™Ð'Ð'r=   ©Ú )rC   r˜   r™   rš   r   r1   Úint8r   Úfloat16Úbfloat16Úfloat32r   r£   r5   r’   rŽ   rÄ   r   r—   Ú__classcell__)rB   s   @r;   r   r   �  sù   ø„ ñ	ð €Gà�
‰
Ñ2°2°s¸BÀÓCØ�‰Ñ5°b¸"¸aÀÓCØ�‰Ñ6°r¸2¸qÀ!ÓDØ�‰Ñ5°b¸"¸aÀÓCð	 Ðð ð
Ø#Ÿl™lð
à	,ò
ó ð
ô$	
ð à68ñ<
Ø#Ÿl™lð<
à	%ò<
ó ð<
ð~ BFò(Ø—‘ð(Ø(0°·±Ñ(>ð(à	�‰÷(r=   r   c                   ó|  — e Zd ZdZdZej                   edddd«      ej                   edddd«      ej                   edddd«      ej                   edddd«      iZedej                  dd fd„«       Ze	 ddej                  dd	fd
„«       Zddœdej                  deej                     dej                  fd„Zy)r   a‚  
    The cuSPARSELt backend expects the specified elements and the metadata to be stored in a single tensor:
    packed = [ specified elements of original tensor | metadata ]
    For an original tensor of size (m, k) we expect the first m * k // 2 elements to be the kept elements
    The rest of the tensor is metadata. Since there is only one tensor, we only use the packed and packed_t
    attributes respectively.

    cuSPARSELt also supports transposition fusion, which is necessary for performant 2:4 sparse training, as well
    as specifying alg_id, a config that affects the performance of the matmul depending on matmul sizes.
    Ú
cusparseltr²   r°   r´   rq   r>   c                 óÌ   — | j                  |«        | |j                  t        j                  |«      d d d d t        j
                  t        j                  |j                  ¬«	      S )NrS   )r   r'   r1   Ú_cslt_compressr   r   r   r(   r‘   s     r;   r’   z/SparseSemiStructuredTensorCUSPARSELT.from_dense   s]   € ð 	×,Ñ,¨_Ô=ÙØ!×'Ñ'Ü×'Ñ'¨Ó8ØØØØ(,Ü&@×&PÑ&PÜ8×HÑHØ)×7Ñ7ô

ð 
	
r=   r   c           	      óp   — t        j                  ||d¬«      \  }}}}} | |j                  |||||d¬«      S )a  
        This function does the same thing as described in SparseSemiStructuredCUTLASS, but uses the cuSPASRELt metadata
        layout and sparse matmul.

        The only functional difference is that cuSPARSELt stores `metadata` and `packed` together into a single tensor.

        [9 1 7 4]                       [9 0 7 0]
        [1 2 3 0]                       [0 2 0 0]
        [8 3 5 4] -> prune 4x4 tile  -> [8 0 0 4] -> pack to cuSPARSELT semi-structured -> packed
        [1 2 6 2]                       [0 0 6 2]

                                                  -> pack to transposed cuSPARSELt      -> packed_t
                                                     semi-structured representation

                                                  -> compute swizzled bitmask           -> compressed_swizzled_bitmask


        The equivalent PyTorch code to create the same three outputs from the dense tensor can be found below:
        ```
        from torch.sparse import SparseSemiStructuredTensorCUSPARSELT
        from torch.sparse._semi_structured_conversions import _sparse_semi_structured_tile, _compute_compressed_swizzled_bitmask

        pruned = _sparse_semi_structured_tile(dense)
        packed_cusparselt = torch._cslt_compress(pruned)
        packed_t_cusparselt = torch._cslt_compress(pruned.t().contiguous())
        bitmask = _compute_compressed_swizzled_bitmask(pruned)

        SparseSemiStructuredTensorCUSPARSELT(dense.shape, packed_cutlass, None, packed_t_cutlass, None, bitmask)
        ```
        Fr¾   r·   rÁ   rÃ   s           r;   rÄ   z<SparseSemiStructuredTensorCUSPARSELT.prune_dense_static_sort1  sX   € ôP ×.Ñ.Ø y¸eô
ñ	
ØØØØØ'ñ
 Ø×!Ñ!ØØØØØ(CØô
ð 	
r=   Nr“   r•   r”   c                óÂ  — t        |t        «      rt        d«      ‚| j                  dk7  s|j                  dk7  r#t	        d| j
                  j                  › d�«      ‚|j                  | j                  k7  rit	        d| j
                  j                  › dt        | j                  «      › dt        |j                  «      › d| j                  › d|j                  › d	�«      ‚|�‚|j                  | j                  k7  rit	        d| j
                  j                  › dt        | j                  «      › dt        |j                  «      › d
| j                  › d|j                  › d�«      ‚| j                  t        j                  k(  r\t	        d| j
                  j                  › dt        | j                  «      › dt        |j                  «      › d| j                  › d�	«      ‚| j                  €#t	        d| j
                  j                  › d�«      ‚t        j                  | j                  ||| j                  | j                  ¬«      }| j                  r|j!                  «       S |S )NrÆ   rs   rÇ   rÈ   z` matmul: trying to do `A=z @ B=z`, with A.dtype=z and B.dtype=zH. This operation is only supported when A and B have the same data type.z + C`, with A.dtype=B.dtype=z and C.dtype=zK. This operation is only supported when A, B and C have the same data type.z`, with A.dtype=B.dtype=zO. mm is not supported for float8_e4m3fn, please use `torch._scaled_mm` instead.rÉ   )r”   Útranspose_resultÚalg_id)rÊ   r   r4   r»   rZ   rB   rC   r+   r¢   r'   r1   Úfloat8_e4m3fnr    Ú_cslt_sparse_mmr%   r&   rg   )rD   r•   r”   r9   rÎ   s        r;   r—   z(SparseSemiStructuredTensorCUSPARSELT._mmg  sK  € ô �aÔ3Ô4ÜØlóð ð �9‰9˜Š>˜QŸV™V qš[Ü%Ø�D—N‘N×+Ñ+Ð,Ð,UÐVóð ð �7‰7�d—j‘jÒ Ü%Ø�D—N‘N×+Ñ+Ð,Ð,FÄuÈTÏZÉZÓGXÐFYÐY^Ô_dÐef×elÑelÓ_mÐ^nð o Ø $§
¡
˜|¨=¸¿¹¸	ð BYðYóð ð
 Ð §
¡
¨d¯j©jÒ 8Ü%Ø�D—N‘N×+Ñ+Ð,Ð,FÄuÈTÏZÉZÓGXÐFYÐY^Ô_dÐef×elÑelÓ_mÐ^nð o(Ø(,¯
©
 |°=ÀÇÁÀ	ð J\ð\óð ð �:‰:œ×,Ñ,Ò,Ü%Ø�D—N‘N×+Ñ+Ð,Ð,FÄuÈTÏZÉZÓGXÐFYÐY^Ô_dÐef×elÑelÓ_mÐ^nð o(Ø(,¯
©
 |ð 4`ð`óð ð
 �;‰;ÐÜ%Ø�D—N‘N×+Ñ+Ð,Ð,PÐQóð ô ×'Ñ'Ø—‘ØØØ!%×!?Ñ!?Ø×-Ñ-ôˆCð #×<Ò<�3—5‘5“7ÐEÀ#ÐEr=   rÏ   )rC   r˜   r™   rš   r   r1   rÞ   r   rÑ   rÒ   rÓ   r   r£   r5   r’   rÄ   r   r—   r§   r=   r;   r   r     sù   „ ñ	ð €Gà×ÑÑ;¸BÀÀBÈÓKØ�
‰
Ñ2°2°r¸2¸rÓBØ�‰Ñ5°b¸"¸aÀÓCØ�‰Ñ6°r¸2¸qÀ!ÓDð	 Ðð ð
Ø#Ÿl™lð
à	/ò
ó ð
ð  à68ñ3
Ø#Ÿl™lð3
à	%ò3
ó ð3
ðl BFò*FØ—‘ð*FØ(0°·±Ñ(>ð*Fà	�‰ô*Fr=   r   )F)r-   Úcollectionsr   Útypingr   r   r   r1   Ú)torch.sparse._semi_structured_conversionsr   r   Ú!torch.sparse._semi_structured_opsr	   r
   r   r   r   r   r   r   r   r   Ú__all__r   r5   r   rž   r   r   r   r§   r=   r;   ú<module>rå      s£   ðã Ý "ß *Ñ *ã ÷÷÷ ÷ ò€ñ ",Ø$ØCó"Ð ôT" §¡ô T"ðr ñA7Ø—\‘\ðA7àðA7ð  óA7ôHH(Ð(Bô H(ôVEFÐ+Eõ EFr=   