class_docstr: 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.
  
  Currently, this class supports 2:4 sparsity for int8, float16 and bfloat16 dtypes.
  We also support 1:2 sparsity for float32 dtype.
  
  This subclass stores the dense tensor in a compressed form by only storing the specified elements and corresponding metadata.
  
  The subclass supports two backend, either CUTLASS or cuSPASRELt.
  
  The cuSPARSELt backend expects the specified elements and the metadata to be stored in a single tensor:
  
  compressed tensor = [ 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.
  
  For CUTLASS backend, elements of original tensor and metadata are kept in separate tensors.
  
  When _FORCE_CUTLASS is set, or when cuSPARSELt is not available, this subclass calls into _sparse_semi_structured_linear
  and sparse_semi_structured_from_dense for conversion to the compressed format.
  
  When PyTorch is compiled with cuSPARSELt support, this subclass will call into _cslt_sparse_mm for sparse mm and
  _cslt_compress to convert into the compressed format.
docstr: SparseSemiStructuredTensor constructor.
  
  Args:
      original_tensor: The original dense tensor, or None, if we have already compressed the tensor.
      original_shape: The shape of the original dense tensor
      compressed_tensor_cusparselt: For cuSPARSELt backend, a flattened tensor to store the specified elements and metadata.
      sparse_tensor_cutlass: For CUTLASS backend, tensor to store the speficied elements.
      meta_tensor_cutlass: For CUTLASS backend, tensor to store metadata.
      transposed: Whether the tensor is transposed or not.
  
  Returns:
      None
  
  Raises:
      RuntimeError: If original_tensor is not a supported dtype, dim, shape, or device.
docstr_tok: ["SparseSemiStructuredTensor","constructor",".","Args",":","original_tensor",":","The","original","dense","tensor",",","or","None",",","if","we","have","already","compressed","the","tensor",".","original_shape",":","The","shape","of","the","original","dense","tensor","compressed_tensor_cusparselt",":","For","cuSPARSELt","backend",",","a","flattened","tensor","to","store","the","specified","elements","and","metadata",".","sparse_tensor_cutlass",":","For","CUTLASS","backend",",","tensor","to","store","the","speficied","elements",".","meta_tensor_cutlass",":","For","CUTLASS","backend",",","tensor","to","store","metadata",".","transposed",":","Whether","the","tensor","is","transposed","or","not",".","Returns",":","None","Raises",":","RuntimeError",":","If","original_tensor","is","not","a","supported","dtype",",","dim",",","shape",",","or","device","."]
code: def __init__(
      self,
      original_tensor: Optional[torch.Tensor],
      original_shape: Optional[torch.Size] = None,
      compressed_tensor_cusparselt: Optional[torch.Tensor] = None,
      sparse_tensor_cutlass: Optional[torch.Tensor] = None,
      meta_tensor_cutlass: Optional[torch.Tensor] = None,
      transposed: bool = False,
  ) -> None:
      """SparseSemiStructuredTensor constructor.
  
      Args:
          original_tensor: The original dense tensor, or None, if we have already compressed the tensor.
          original_shape: The shape of the original dense tensor
          compressed_tensor_cusparselt: For cuSPARSELt backend, a flattened tensor to store the specified elements and metadata.
          sparse_tensor_cutlass: For CUTLASS backend, tensor to store the speficied elements.
          meta_tensor_cutlass: For CUTLASS backend, tensor to store metadata.
          transposed: Whether the tensor is transposed or not.
  
      Returns:
          None
  
      Raises:
          RuntimeError: If original_tensor is not a supported dtype, dim, shape, or device.
      """
      # if original tensor is passed in, we need to compress it and store the compressed representation.
      if original_tensor is not None:
          # TODO right now we have unified checks and constraints for cuSPARSELt and CUTLASS, these are not actually the same.
          # We should consolidate similar checks here and leave backend specific checks like shape in the op implementation.
  
          # check device
          if not original_tensor.is_cuda:
              raise RuntimeError(
                  f"Error original_tensor.device= {original_tensor.device} is not supported! "
                  "Only CUDA tensors are currently supported."
              )
  
          # check dim
          if original_tensor.dim() != 2:
              raise RuntimeError(
                  f"Error original_tensor.dim = {original_tensor.dim()} is not supported! "
                  "Only 2d tensors are currently supported."
              )
  
          # check dtype
          if (
              original_tensor.dtype
              not in _DTYPE_TO_SEMI_STRUCTURED_SPARSE_CONFIG
          ):
              raise RuntimeError(
                  f"Error original_tensor.dtype {original_tensor.dtype} is not a supported dtype! "
                  "dtype must be one of: {_DTYPE_TO_SEMI_STRUCTURED_SPARSE_CONFIG}"
              )
  
          # check shape
          m, n = original_tensor.shape
          min_rows = _DTYPE_TO_SEMI_STRUCTURED_SPARSE_CONFIG[
              original_tensor.dtype
          ].sparse_min_rows
          min_cols = _DTYPE_TO_SEMI_STRUCTURED_SPARSE_CONFIG[
              original_tensor.dtype
          ].sparse_min_cols
          if (
              m < min_rows
              or m % min_rows
              or n < min_cols
              or n % min_cols
          ):
              # TODO in the future we can add in padding to support sparse dimensions that aren't perfect multiples
              raise RuntimeError(
                  f"Error original_tensor.shape {original_tensor.shape} is not supported! "
                  f"Both dimensions must be larger or equal than and a multiple of ({min_rows}, {min_cols})"
              )
  
          compressed_tensor_cusparselt = None
          sparse_tensor_cutlass = None
          meta_tensor_cutlass = None
          if self._FORCE_CUTLASS:
              from torch.sparse._semi_structured_conversions import (
                  sparse_semi_structured_from_dense_cutlass,
              )
  
              (
                  sparse_tensor_cutlass,
                  meta_tensor_cutlass,
              ) = sparse_semi_structured_from_dense_cutlass(
                  original_tensor
              )
          else:
              # use cuSPARSELt
              compressed_tensor_cusparselt = torch._cslt_compress(
                  original_tensor
              )
  
      # set values
      self.original_tensor = None
      self.compressed_tensor_cusparselt = compressed_tensor_cusparselt
      self.sparse_tensor_cutlass = sparse_tensor_cutlass
      self.meta_tensor_cutlass = meta_tensor_cutlass
      self.transposed = transposed
      self.original_shape = original_shape
  
file_path: pytorch/torch/sparse/semi_structured.py
imports: import warnings
  from collections import namedtuple
  from typing import Any, Optional
  import torch
label_desc: Use image node_id 2 to create a new SparseSemiStructuredTensor object from inherited base classes: torch with example: obj = SparseSemiStructuredTensor(original_tensor, original_shape, compressed_tensor_cusparselt, sparse_tensor_cutlass, meta_tensor_cutlass, transposed)