
    Vh                     B    d dl Z ddlmZ  G d dej                        Zy)    N   )base_sparsifierc                   0     e Zd ZdZddef fdZd Z xZS )NearlyDiagonalSparsifiera  Nearly Diagonal Sparsifier

    This sparsifier creates a nearly diagonal mask to be applied to the weight matrix.
    Nearly Diagonal Matrix is a matrix that contains non-zero elements near the diagonal and the rest are zero.
    An example of a nearly diagonal matrix with degree (or nearliness) 3 and 5 are follows respectively.
    1 1 0 0       1 1 1 0
    1 1 1 0       1 1 1 1
    0 1 1 1       1 1 1 1
    0 0 1 1       0 1 1 1
    Note that a nearly diagonal matrix with degree 1 is just a matrix with main diagonal populated

    This sparsifier is controlled by one variable:
    1. `nearliness` defines the number of non-zero diagonal lines that are closest to the main diagonal.
        Currently - supports only odd number

    Note:
        This can be accelerated (vectorized) once the Spdiagonal feature (PR: #78439) is landed or the banded matrix
        feature is landed: https://stackoverflow.com/questions/52463972/generating-banded-matrices-using-numpy

    Args:
        nearliness: The degree of nearliness (default = 1)

    
nearlinessc                 .    d|i}t         |   |       y )Nr   )defaults)super__init__)selfr   r	   	__class__s      f/home/dcms/DCMS/lib/python3.12/site-packages/torch/ao/pruning/sparsifier/nearly_diagonal_sparsifier.pyr   z!NearlyDiagonalSparsifier.__init__    s     *-(+    c                    t        |j                  |      d   j                  }t        j                  |      |_        |dk  ry t        ||      }|j                  \  }}|dz  dk(  rt        d      |dz  }	|	t        ||      k\  rt        d      t        d|      D ];  }
t        d|
|	z
        }t        ||
|	z   dz         }||
||f   j                  d       = y )Nr      z$nearliness can only be an odd numberz:nearliness cannot be larger than the dimensions of tensor.r   )getattrparametrizationsmasktorch
zeros_likedatashape
ValueErrorminrangemaxfill_)r   moduletensor_namer   kwargsr   tensorheightwidthdist_to_diagonalrowlowhighs                r   update_maskz$NearlyDiagonalSparsifier.update_mask$   s     v..<Q?DD$$T*	?->QCDD%?s6511L  F# 	)Ca//0Cuc$44q89Dc$h%%a(		)r   )r   )__name__
__module____qualname____doc__intr   r(   __classcell__)r   s   @r   r   r      s    0,3 ,)r   r   )r    r   BaseSparsifierr    r   r   <module>r2      s     5)== 5)r   