o
    i                  
   @   s   d dl mZ d dlZd dlmZmZmZ eje eje edg dZ	G dd dej
jZG dd	 d	ej
jZejj	
	ddejdededefddZejje	jfdejdede	defddZdS )    )EnumN)SparseSemiStructuredTensor$SparseSemiStructuredTensorCUSPARSELT!SparseSemiStructuredTensorCUTLASSGRADIENT_TYPE)DENSESPARSESTEc                   @   s<   e Zd ZedejdedefddZedejfddZ	d	S )
_SparsifyFuncxalgobackendc              
   C   sb   |dk}t |ts+tj|||d\}}}}}	|rtnt}
|
|j|||||	ddd}|S | }|S )Ncutlass)	algorithmuse_cutlassFT)packedmetapacked_tmeta_tcompressed_swizzled_bitmaskrequires_gradfuse_transpose_cusparselt)
isinstancer   torch_sparse_semi_structured_tiler   r   shapedetach)ctxr   r   r   r   r   r   r   r   bitmaskclsout r!   V/home/ubuntu/.local/lib/python3.10/site-packages/torchao/sparsity/training/autograd.pyforward   s.   
z_SparsifyFunc.forwardgrad_outc                 C   s
   |d d fS )Nr!   )r   r$   r!   r!   r"   backward4   s   
z_SparsifyFunc.backwardN)
__name__
__module____qualname__staticmethodr   Tensorstrr   r#   r%   r!   r!   r!   r"   r
      s
    r
   c                   @   s>   e Zd ZeejfdejdefddZ	edejfddZ
dS )	_SparsifyLikeFuncr   patternc              	   C   s   t |tsJ t |tstd|j stdt||j\}}|j| _|j	| _	|j| _
|| _|j|j||j||j	|j|jdS )NzC`sparsify_like(x, pattern)` is only implemented for CUTLASS backendzK`sparsify_like(x, pattern)` is not implemented when `bitmask` is transposedr   )r   r   r   NotImplementedErrorr   is_contiguousr   _sparse_semi_structured_applyr   r   r   gradient	__class__r   r   )r   r   r-   r2   r   r   r!   r!   r"   r#   ;   s2   

z_SparsifyLikeFunc.forwardr$   c              	   C   s   | j tjkst|tr|d d d fS t|trJ |j| jks J | j tjkr8| j s-J t	
|| jd d d fS | j tjks@J t	j|| jdd\}}}}t|j|| j|| j| j|jdd d d fS )Nr   )r   r.   )r2   r   r	   r   r   dtyper   threads_masksr0   r   #_sparse_semi_structured_apply_denser   r   r   r   r   r   r   r   )r   r$   r   _r   r!   r!   r"   r%   a   s<   	z_SparsifyLikeFunc.backwardN)r&   r'   r(   r)   r   r   r   r*   r   r#   r%   r!   r!   r!   r"   r,   :   s    %r,    r   r   r   r   returnc                 C      t | ||S )zl
    Sparsifies a dense tensor into a semi-structured tensor, according to the algo and backend passed.
    )r
   apply)r   r   r   r!   r!   r"   semi_structured_sparsify      	r<   r-   r2   c                 C   r:   )zj
    Sparsifies a dense tensor into a semi-structured tensor, using the mask of the provided pattern.
    )r,   r;   )r   r-   r2   r!   r!   r"   semi_structured_sparsify_like   r=   r>   )r8   r   )enumr   r   torch.sparser   r   r   _dynamoallow_in_graphr   autogradFunctionr
   r,   r*   r+   r<   r   r>   r!   r!   r!   r"   <module>   s>   $N