o
    i{                     @   s   d dl mZ d dlZd dlmZ d dlmZ 	dddZejj	j
eejj	jeejj	jeejj	jeedd	ejj	jeejj	jeed
d	ejj	jeedd	ejj	jeed
d	iZdS )    )partialN)SparseSemiStructuredTensor)semi_structured_sparsify_like c                    s   d|D ]	}t |tr|qdusJ  fddfddt|D }j dd |D  j dd |D  jjS )a  
    adds pointwise op support for semi-structured tensors.

    Assumes that at least one of the arguments in arg is a SparseSemiStructuredTensor.
    The last instance of a SparseSemiStructuredTensor is used as the reference mask to sparsify the others tensors passed in args.
    sparsify_like_args_list is used to specify which arguments to sparsify like the reference tensor.
    Nc                    s   t |tjrit |ts4| v rt|}|S td j d j dt dt d|  dt| |j	d u sRj	d u sR|j	
 j	
 ksR|j	 j	 kritd j d j dt dt d	|S )Nz
Operation .z on z requires all operands to be z, but operand z is a z with the same sparsity pattern)
isinstancetorchTensorr   r   
ValueError
__module____name__typecompressed_swizzled_bitmaskdata_ptrstride)itensor)funcreference_sparse_tensorsparsify_like_args_listr   [/home/ubuntu/.local/lib/python3.10/site-packages/torchao/sparsity/training/pointwise_ops.py
handle_arg   s<   



z-_semi_sparse_pointwise_op.<locals>.handle_argc                    s   g | ]	\}} ||qS r   r   ).0r   r   )r   r   r   
<listcomp>?   s    z-_semi_sparse_pointwise_op.<locals>.<listcomp>c                 S       g | ]}t |tr|jn|qS r   )r   r   packedr   xr   r   r   r   D       c                 S   r   r   )r   r   packed_tr   r   r   r   r   K   r   )r   r   	enumerate	__class__shapemetameta_tr   )r   typesargskwargsr   r   args_updatedr   )r   r   r   r   r   _semi_sparse_pointwise_op   s0   

!r)   )r      )r   )r   )r   Nr   )	functoolsr   r   torch.sparser   "torchao.sparsity.training.autogradr   r)   opsatenrelugelusilumuladdgelu_backwardsilu_backwardthreshold_backward#CUTLASS_POINTWISE_OP_DISPATCH_TABLEr   r   r   r   <module>   s2   

I






