torch.tril_indices — PyTorch 2.7 documentation (original) (raw)

torch.tril_indices(row, col, offset=0, *, dtype=torch.long, device='cpu', layout=torch.strided) → Tensor

Returns the indices of the lower triangular part of a row-by-col matrix in a 2-by-N Tensor, where the first row contains row coordinates of all indices and the second row contains column coordinates. Indices are ordered based on rows and then columns.

The lower triangular part of the matrix is defined as the elements on and below the diagonal.

The argument offset controls which diagonal to consider. Ifoffset = 0, all elements on and below the main diagonal are retained. A positive value includes just as many diagonals above the main diagonal, and similarly a negative value excludes just as many diagonals below the main diagonal. The main diagonal are the set of indices{(i,i)}\lbrace (i, i) \rbrace for i∈[0,min⁡{d1,d2}−1]i \in [0, \min\{d_{1}, d_{2}\} - 1]where d1,d2d_{1}, d_{2} are the dimensions of the matrix.

Note

When running on CUDA, row * col must be less than 2592^{59} to prevent overflow during calculation.

Parameters

Keyword Arguments

Example:

a = torch.tril_indices(3, 3) a tensor([[0, 1, 1, 2, 2, 2], [0, 0, 1, 0, 1, 2]])

a = torch.tril_indices(4, 3, -1) a tensor([[1, 2, 2, 3, 3, 3], [0, 0, 1, 0, 1, 2]])

a = torch.tril_indices(4, 3, 1) a tensor([[0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3], [0, 1, 0, 1, 2, 0, 1, 2, 0, 1, 2]])