
    Pi              	       |    d dl Z d dlmZ g dZd Zd Z G d de          Z	 	 dd
e j        dedede j        fdZ	dS )    N)UniformQuantizationObserverBase)create_block_sparse_tensorcreate_semi_structured_tensorPerChannelNormObservermask_creatorc                 d   |dk    r|dk    s
J d            t          j        t          j        | |z  ||z  fd|z
  |                    }t          j        ||d          }t          j        ||d          }|                    |                                                                          S )N      ?g        z*sparsity should be a value between 0 and 1   )dtyper   dim)torch	bernoullifullrepeat_interleaveto
contiguouscuda)MN	blocksizesparsityr   As         j/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchao/sparsity/utils.pyr   r      s    s??x34  /. 	
ANAN3Q\OOO	 	A 	9!444A9!444A44;;!!##((***    c                    t          j        dd| |z  dz  f                                          }t           j        j                            |d                              | |                                                              t           j	                  }|t          j
        | |                                          |z  z   }|                    |          S )z
    This function returns a 1:2 sparse matrix of size (r, c).
    Note that this means this matrix will also be 2:4 and 4:8 sparse as well.
    r      )num_classes)r   randintr   nn
functionalone_hotreshaper   r   int32rand)rcr   choice_indicesmasksparse_weights         r   r   r      s     ]1a!a%1*77<<>>N##N#BB	A		EK	 	 EJq!,,1133d:;ME"""r   c                   0     e Zd ZdZd fdZd Zd Z xZS )r   z`
    A custom observer that computes the L2 norm of each channel and stores it in a buffer.
    returnNc                     t                      j        dt          j        t          j        dd d t          j        t          j                  j        d| d| _        | 	                    dt          j
        g                      d S )NF)r   qschemereduce_range	quant_min	quant_maxepsr	   norm )super__init__r   quint8per_channel_affinefinfofloat32r2   averaging_constantregister_buffertensor)selfkwargs	__class__s     r   r6   zPerChannelNormObserver.__init__8   s     	
,,EM**.	
 	
 	
 	
 	
 #&VU\"%5%566666r   c                 R   |                                 dk    r|S |                                }d t          |                                          D             }|d         |d         c|d<   |d<   |                    |          }t          j        |d          }t
          j                            |d          dz  }| j	                                         dk    r:| j	        
                    |j                   | j	                            |           n| xj	        |z  c_	        |S )Nr   c                     g | ]}|S r4   r4   ).0is     r   
<listcomp>z2PerChannelNormObserver.forward.<locals>.<listcomp>O   s    333q333r   r
   )	start_dimr   r   )numeldetachranger   permuter   flattenlinalgvector_normr3   resize_shapecopy_)r>   x_origxnew_axis_listyr3   s         r   forwardzPerChannelNormObserver.forwardI   s	   <<>>QMMMOO 43E!%%''NN333.;B.?qAQ+a-+IIm$$M!q)))|''q'11Q69??!!Idj)))IOOD!!!!IIIIr   c                      t          d          )Nz>PerChannelNormObserver is designed to store activations only. )NotImplementedError)r>   s    r   calculate_qparamsz(PerChannelNormObserver.calculate_qparams_   s    !L
 
 	
r   )r,   N)__name__
__module____qualname____doc__r6   rV   rY   __classcell__)r@   s   @r   r   r   3   se         7 7 7 7 7 7"  ,
 
 
 
 
 
 
r   r   r      r=   r   r   r,   c                    d}|                                  |z  dk    rt          d| j         d| d          |                                  |z  }|                                                                                     ||          }t          j        |d          dddt          ||z
            f         }t          j	        |j        |j
                  }|                    d|d	                              | j                  }|S )
a  
    Class for creating N:M sparsity masks.
    Masks will be created using the N:M ratio, where for every block of
    M weights, N will be pruned based on ranked weight value. Each mask
    will correspond to the given tensor.
    :param tensor: The input tensor to create a mask for
    :param N: The number of weights in a group to keep
    :param M: The size of a weight group
    :return: A mask tensor with the same shape as the input tensor
    Nr   zTensor of size z can't be evenly divided into z groupsr
   r   )device)r   indexvalue)rH   
ValueErrorrP   rI   absr#   r   argsortintonesra   scatter_)r=   r   r   r)   
num_groupstensor_temprb   w_bs           r   r   r   e   s     D||~~QTflTT!TTT
 
 	
 1$J --//%%''//
A>>KM+1---aaa3q1u::o>E
*[&{/A
B
B
BC<<AU!<44<<V\JJDKr   )r   r_   )
r   torch.ao.quantization.observerr   __all__r   r   r   Tensorrg   r   r4   r   r   <module>rp      s     J J J J J J  	+ 	+ 	+# # #,/
 /
 /
 /
 /
< /
 /
 /
h  L
  \	     r   