Ë
    ùÿæiq  ã                   ó  — d dl Z d dlmZ d dlmZ d dl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)ÚCallable)ÚAny)Ú)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j"                  dz  ed<   ej"                  dz  ed<   ej"                  dz  ed<   ej"                  dz  ed<   ej"                  dz  ed<   eed<   eed<   g d¢Ze	 	 	 d(dej(                  dej"                  dz  dej"                  dz  dej"                  dz  dej"                  dz  dej"                  dz  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j8                  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j"                  dz  dej"                  fd'„Z&y)+r   a¼  
    This class implements 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 instantiated directly.

    -`_DTYPE_SHAPE_CONSTRAINTS` - A dictionary holding backend specific dense/sparse min shape constraints
    - `def from_dense()` - backend specific compression routines
    - `def _mm()` - backend specific 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_DISPATCHNÚpackedÚmetaÚpacked_tÚmeta_tÚcompressed_swizzled_bitmaskÚfuse_transpose_cusparseltÚalg_id_cusparselt)r   r    r!   r"   r#   ÚshapeÚrequires_gradc
                 óÔ  — | j                   sRt        j                  dt        d¬«       d| _         | j	                  «        t
        j                  j                  | «       |�|}
n|�|}
nt        d«      ‚t
        j                  j                  | ||
j                  |
j                  |
j                  |	¬«      }||_        ||_        ||_        ||_        ||_        ||_        ||_        |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.é   ©Ú
stacklevelTz3At 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Ú
ValueErrorÚTensorÚ_make_wrapper_subclassr,   r-   r.   r   r    r!   r"   r#   r$   r%   )Úclsr&   r   r    r!   r"   r#   r$   r%   r'   Úprevious_tensorÚtensors               úq/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torch/sparse/semi_structured.pyÚ__new__z"SparseSemiStructuredTensor.__new__L   sî   € ðN ×+Ò+Ü�M‰MðHô
 Øõ	ð ,0ˆCÔ(ð
 ×$Ñ$Ô&ô �M‰M×(Ñ(¨Ô-àÐØ$‰OØÐ!Ø&‰OäÐRÓSÐSä—‘×4Ñ4ØØØ"×)Ñ)Ø!×'Ñ'Ø"×)Ñ)Ø'ð 5ó 
ˆð ˆŒØˆŒØ"ˆŒØˆŒØ-HˆÔ*Ø+DˆÔ(Ø#4ˆÔ Øˆó    Úreturnc                 ó|   — t        | d«      st        d«      ‚| j                  j                  › d| j                  › d�S )Nr&   ztensor has no shape attributez(shape=Ú))ÚhasattrÚAssertionErrorÚ	__class__Ú__name__r&   )Úselfs    r<   Ú__repr__z#SparseSemiStructuredTensor.__repr__¡   s:   € Ü�t˜WÔ%Ü Ð!@ÓAÐAØ—.‘.×)Ñ)Ð*¨'°$·*±*°¸QÐ?Ð?r>   c                 ó¬   ‡ — t        t        ˆ fd„‰ j                  «      «      }‰ j                  ‰ j                  ‰ j
                  ‰ j                  f}||fS )Nc                 ó    •— t        ‰| «      d uS ©N)Úgetattr)ÚxrF   s    €r<   Ú<lambda>z?SparseSemiStructuredTensor.__tensor_flatten__.<locals>.<lambda>ª   s   ø€ œW T¨1Ó-°TÑ9r>   )ÚlistÚfilterÚ	__slots__r&   r$   r%   r'   )rF   Úinner_tensorsÚtensor_metas   `  r<   Ú__tensor_flatten__z-SparseSemiStructuredTensor.__tensor_flatten__¦   sV   ø€ ô ÜÓ9¸4¿>¹>ÓJó
ˆð �J‰JØ×*Ñ*Ø×"Ñ"Ø×Ñð	
ˆð ˜kÐ)Ð)r>   rR   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)	r9   rQ   rR   Ú
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 (rA   )Ú_overloadpacketr   ÚNotImplementedErrorrE   )r9   ÚfuncÚtypesÚargsÚkwargss        r<   Ú__torch_dispatch__z-SparseSemiStructuredTensor.__torch_dispatch__Î   sh   € à×Ñ s×':Ñ':Ñ:Ü%Ø—<‘<�.ð !/Ø/3¯}©}¨o¸Qð@óð ð 9ˆs×"Ñ" 4×#7Ñ#7Ñ8¸¸uÀdÈFÓSÐSr>   c                 ó|  — 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)rK   r3   ÚopsÚatenÚvaluesr   Úindicesr   Úis_same_sizer   Údetach_Údetachr
   Útr   Úviewr   Úmmr   ÚmatmulÚaddmmr	   Úlinearr   Ú_to_copyÚ
_scaled_mmr   r   Úupdate)r9   Úcustom_dispatch_tables     r<   r2   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.r)   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, rA   N)
Úis_cudaÚRuntimeErrorr,   ÚdimÚis_contiguousr-   r   r&   Úsparse_min_rowsÚsparse_min_cols)r9   rt   Ú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k7  rt        d|j                  «       › d�«      ‚|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.
        r)   zdense_input must be 2D, got ÚDr   )ry   rC   r&   r   r-   Údense_min_rowsÚdense_min_colsr3   ÚnnÚ
functionalÚpad)r9   r‚   r}   r~   r   r€   Úto_pad_mÚto_pad_ns           r<   Ú_pad_dense_inputz+SparseSemiStructuredTensor._pad_dense_input  sß   € ð �?‰?Ó Ò!Ü Ð#?ÀÇÁÓ@QÐ?RÐRSÐ!TÓUÐUð × Ñ ‰ˆˆ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&   r3   rl   Úeyer-   r,   )rF   Úcols     r<   Úto_densez#SparseSemiStructuredTensor.to_dense1  s5   € Ø�j‰j˜‰nˆÜ�x‰x˜œeŸi™i¨°4·:±:ÀdÇkÁkÔRÓSÐSr>   c                 ó   — t         ‚rJ   ©r\   ©r9   rt   s     r<   Ú
from_densez%SparseSemiStructuredTensor.from_dense5  s   € ä!Ð!r>   ©ÚbiasÚBr—   c                ó   — t         ‚rJ   r“   )rF   r˜   r—   r`   s       r<   Ú_mmzSparseSemiStructuredTensor._mm9  s
   € ô "Ð!r>   )Fr   FrJ   )r?   N)'rE   Ú
__module__Ú__qualname__Ú__doc__r   ÚintÚ__annotations__Údictr3   r-   r   r   Úboolr   r   Ústrr   r7   rP   ÚstaticmethodÚSizer=   rG   ÚtuplerN   rS   ÚclassmethodrY   Ú_CÚ_disabled_torch_function_implÚ__torch_function__r   ra   r2   r�   rŒ   r‘   r•   rš   © r>   r<   r   r   '   sÝ  … ñð" €O�SÓØ" 5§;¡;Ð0NÐ#NÑOÓOØ €N�DÓ Ø!€O�TÓ!Ø%*Ð˜dÓ*àƒLØ˜( HÐ,Ñ-Ó-à�L‰L˜4ÑÓØ
�,‰,˜Ñ
ÓØ�l‰l˜TÑ!Ó!Ø�L‰L˜4ÑÓØ!&§¡°Ñ!4Ó4Ø#Ó#ØÓâW€Iàð +0Ø!"Ø#ñRà�z‰zðRð —‘˜tÑ#ðRð �l‰l˜TÑ!ð	Rð
 —,‘, Ñ%ðRð —‘˜tÑ#ðRð &+§\¡\°DÑ%8ðRð $(ðRð ðRð òRó ðRðh@˜#ó @ð
*à	ˆ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ò "ó ð"ð %)ò	"à�<‰<ð"ð �l‰l˜TÑ!ð	"ð 
�‰ô"r>   r   rt   Ú
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 minimum 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.r)   r*   )
r/   r0   ÚFutureWarningr   r   r3   Úsparser   r   r•   )rt   r«   ÚSPARSE_SUBCLASSs      r<   r   r   C  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j                  dz  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 separately,
    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é   é€   é    é@   é   é   rt   r?   c           	      ó†   — | j                  |«       t        |«      \  }} | |j                  ||d d d |j                  ¬«      S )N©r   r    r!   r"   r#   r'   )r�   r   r&   r'   )r9   rt   Úsparse_tensor_cutlassÚmeta_tensor_cutlasss       r<   r•   z,SparseSemiStructuredTensorCUTLASS.from_dense›  sW   € ð 	×,Ñ,¨_Ô=ô 6°oÓFñ	
Ø!Øñ Ø×!Ñ!Ø(Ø$ØØØ(,Ø)×7Ñ7ô
ð 	
r>   c                 óØ   •— | j                   �| j                  €t        d«      ‚| j                   j                  dk(  r t	        | j                  | j                   «      S t
        ‰| �  «       S )Nz meta and packed must not be Noner)   )r    r   rC   Úndimr   Úsuperr‘   )rF   rD   s    €r<   r‘   z*SparseSemiStructuredTensorCUTLASS.to_dense¯  sc   ø€ Ø�9‰9Ð §¡Ð 3Ü Ð!CÓDÐDð �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¹   )r3   Ú_sparse_semi_structured_tiler&   ©r9   rt   rÁ   r   r    r!   r"   r#   s           r<   Úprune_dense_static_sortz9SparseSemiStructuredTensorCUTLASS.prune_dense_static_sort»  sX   € ôz ×.Ñ.Ø 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 hardwarer)   Ú`ú)` matmul: Broadcasting is not implementedú$` matmul: operation is not supportedr   )Ú
isinstancer   r6   rD   rE   r½   r\   r   r    r3   Ú_sparse_semi_structured_mmÚ_sparse_semi_structured_addmmr&   )rF   r˜   r—   r`   Ú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>   ©Ú )rE   r›   rœ   r�   r   r3   Úint8r   Úfloat16Úbfloat16Úfloat32r   r¦   r7   r•   r‘   rÅ   rš   Ú__classcell__)rD   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ñI
Ø#Ÿl™lðI
à	%òI
ó ðI
ðX ?Cò(Ø—‘ð(Ø(-¯©°tÑ(;ð(à	�‰÷(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j                  dz  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¶   rt   r?   c                 óÌ   — | j                  |«        | |j                  t        j                  |«      d d d d t        j
                  t        j                  |j                  ¬«	      S )NrU   )r�   r&   r3   Ú_cslt_compressr   r   r   r'   r”   s     r<   r•   z/SparseSemiStructuredTensorCUSPARSELT.from_dense5  s]   € ð 	×,Ñ,¨_Ô=áØ!×'Ñ'Ü×'Ñ'¨Ó8ØØØØ(,Ü&@×&PÑ&PÜ8×HÑHØ)×7Ñ7ô

ð 
	
r>   r   c           	      óì   — t        j                  ||d¬«      \  }}}}}|j                  |j                  d   d«      }|j                  |j                  d   d«      } | |j                  |||||d¬«      S )a=  
        This function does the same thing as described in SparseSemiStructuredCUTLASS, but uses the cuSPARSELt 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¹   )r3   rÃ   rk   r&   rÄ   s           r<   rÅ   z<SparseSemiStructuredTensorCUSPARSELT.prune_dense_static_sortG  s�   € ôZ ×.Ñ.Ø y¸eô
ñ	
ØØØØØ'ð —‘˜_×2Ñ2°1Ñ5°rÓ:ˆØ—=‘= ×!6Ñ!6°qÑ!9¸2Ó>ˆñ Ø×!Ñ!ØØØØØ(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Ç   r)   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   r6   r½   r\   rD   rE   r-   r¥   r&   r3   Úfloat8_e4m3fnr   Ú_cslt_sparse_mmr$   r%   rj   )rF   r˜   r—   r`   rÏ   s        r<   rš   z(SparseSemiStructuredTensorCUSPARSELT._mmˆ  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Ð   )rE   r›   rœ   r�   r   r3   rà   r   rÒ   rÓ   rÔ   r   r¦   r7   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ñ>
Ø#Ÿl™lð>
à	%ò>
ó ð>
ðB ?Cò*FØ—‘ð*FØ(-¯©°tÑ(;ð*Fà	�‰ô*Fr>   r   )F)r/   Úcollectionsr   Úcollections.abcr   Útypingr   r3   Ú)torch.sparse._semi_structured_conversionsr   r   Ú!torch.sparse._semi_structured_opsr   r	   r
   r   r   r   r   r   r   r   Ú__all__r   r7   r   r¡   r   r   r   rª   r>   r<   Ú<module>rè      s£   ðã Ý "Ý $Ý ã ÷÷÷ ÷ ò€ñ ",Ø$ØCó"Ð ôY" §¡ô Y"ð| ñA7Ø—\‘\ðA7àðA7ð  óA7ôHW(Ð(Bô W(ôtQFÐ+Eõ QFr>   