Trilu#

  • Domain: ai.onnx

  • Since version: 14

Given a 2-D matrix or batches of 2-D matrices, returns the upper or lower triangular part of the tensor(s). The attribute “upper” determines whether the upper or lower part is retained. If set to true, the upper triangular matrix is retained. Lower triangular matrix is retained otherwise. Default value for the “upper” attribute is true. Trilu takes one input tensor of shape [*, N, M], where * is zero or more batch dimensions. The upper triangular part consists of the elements on and above the given diagonal (k). The lower triangular part consists of elements on and below the diagonal. All other elements in the matrix are set to zero. If k = 0, the triangular part on and above/below the main diagonal is retained. If upper is set to true, a positive k retains the upper triangular matrix excluding the main diagonal and (k-1) diagonals above it. A negative k value retains the main diagonal and |k| diagonals below it. If upper is set to false, a positive k retains the lower triangular matrix including the main diagonal and k diagonals above it. A negative k value excludes the main diagonal and (|k|-1) diagonals below it.

Inputs

  • input (T): Input tensor of rank 2 or higher.

  • k (tensor(int64)): A 0-D tensor containing a single value corresponding to the number diagonals above or below the main diagonal to exclude or include. Default value is 0 if it’s not specified.

Outputs

  • output (T): Output tensor of the same type and shape as the input tensor.

Attributes

  • upper (int): Boolean. Indicates whether upper or lower part of matrix is retained. Default is true.

Type Constraints

  • T: Constrain input and output types to all tensor types. Allowed types: tensor(bfloat16), tensor(bool), tensor(complex128), tensor(complex64), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8).

Examples#

test_cc_tril_neg

Node:
  Trilu(X, K) -> (Y)
  Attributes:
    upper = 0
Inputs:
  X: shape=(4, 5), dtype=int64
    [[4, 7, 3, 7, 9],
     [1, 2, 8, 6, 9],
     [9, 4, 0, 8, 7],
     [4, 3, 4, 2, 4]]
  K: shape=(), dtype=int64
    -1

Outputs:
  Y: shape=(4, 5), dtype=int64
    [[0, 0, 0, 0, 0],
     [1, 0, 0, 0, 0],
     [9, 4, 0, 0, 0],
     [4, 3, 4, 0, 0]]

test_cc_tril_one_row_neg

Node:
  Trilu(X) -> (Y)
  Attributes:
    upper = 0
Inputs:
  X: shape=(3, 1, 5), dtype=int64
    [[[6, 2, 4, 1, 6]],

     [[8, 3, 8, 7, 0]],

     [[2, 2, 9, 5, 9]]]

Outputs:
  Y: shape=(3, 1, 5), dtype=int64
    [[[6, 0, 0, 0, 0]],

     [[8, 0, 0, 0, 0]],

     [[2, 0, 0, 0, 0]]]

test_cc_tril_out_neg

Node:
  Trilu(X, K) -> (Y)
  Attributes:
    upper = 0
Inputs:
  X: shape=(4, 5), dtype=int64
    [[4, 7, 3, 7, 9],
     [1, 2, 8, 6, 9],
     [9, 4, 0, 8, 7],
     [4, 3, 4, 2, 4]]
  K: shape=(), dtype=int64
    -7

Outputs:
  Y: shape=(4, 5), dtype=int64
    [[0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0]]

test_cc_tril_out_pos

Node:
  Trilu(X, K) -> (Y)
  Attributes:
    upper = 0
Inputs:
  X: shape=(4, 5), dtype=int64
    [[4, 7, 3, 7, 9],
     [1, 2, 8, 6, 9],
     [9, 4, 0, 8, 7],
     [4, 3, 4, 2, 4]]
  K: shape=(), dtype=int64
    6

Outputs:
  Y: shape=(4, 5), dtype=int64
    [[4, 7, 3, 7, 9],
     [1, 2, 8, 6, 9],
     [9, 4, 0, 8, 7],
     [4, 3, 4, 2, 4]]

test_cc_tril_pos

Node:
  Trilu(X, K) -> (Y)
  Attributes:
    upper = 0
Inputs:
  X: shape=(4, 5), dtype=int64
    [[4, 7, 3, 7, 9],
     [1, 2, 8, 6, 9],
     [9, 4, 0, 8, 7],
     [4, 3, 4, 2, 4]]
  K: shape=(), dtype=int64
    2

Outputs:
  Y: shape=(4, 5), dtype=int64
    [[4, 7, 3, 0, 0],
     [1, 2, 8, 6, 0],
     [9, 4, 0, 8, 7],
     [4, 3, 4, 2, 4]]

test_cc_tril_square

Node:
  Trilu(X) -> (Y)
  Attributes:
    upper = 0
Inputs:
  X: shape=(2, 3, 3), dtype=int64
    [[[0, 4, 3],
      [2, 0, 9],
      [8, 2, 5]],

     [[2, 7, 2],
      [2, 6, 0],
      [2, 6, 5]]]

Outputs:
  Y: shape=(2, 3, 3), dtype=int64
    [[[0, 0, 0],
      [2, 0, 0],
      [8, 2, 5]],

     [[2, 0, 0],
      [2, 6, 0],
      [2, 6, 5]]]

test_cc_tril_square_neg

Node:
  Trilu(X, K) -> (Y)
  Attributes:
    upper = 0
Inputs:
  X: shape=(2, 3, 3), dtype=int64
    [[[0, 4, 3],
      [2, 0, 9],
      [8, 2, 5]],

     [[2, 7, 2],
      [2, 6, 0],
      [2, 6, 5]]]
  K: shape=(), dtype=int64
    -1

Outputs:
  Y: shape=(2, 3, 3), dtype=int64
    [[[0, 0, 0],
      [2, 0, 0],
      [8, 2, 0]],

     [[0, 0, 0],
      [2, 0, 0],
      [2, 6, 0]]]

test_cc_tril_zero

Node:
  Trilu(X, K) -> (Y)
  Attributes:
    upper = 0
Inputs:
  X: shape=(3, 0, 5), dtype=int64
    []
  K: shape=(), dtype=int64
    6

Outputs:
  Y: shape=(3, 0, 5), dtype=int64
    []

test_cc_trilu_batched_upper

Node:
  Trilu(X) -> (Y)
Inputs:
  X: shape=(2, 2, 2), dtype=float32
    [[[1., 2.],
      [3., 4.]],

     [[5., 6.],
      [7., 8.]]]

Outputs:
  Y: shape=(2, 2, 2), dtype=float32
    [[[1., 2.],
      [0., 4.]],

     [[5., 6.],
      [0., 8.]]]

test_cc_trilu_lower

Node:
  Trilu(X) -> (Y)
  Attributes:
    upper = 0
Inputs:
  X: shape=(3, 3), dtype=float32
    [[1., 2., 3.],
     [4., 5., 6.],
     [7., 8., 9.]]

Outputs:
  Y: shape=(3, 3), dtype=float32
    [[1., 0., 0.],
     [4., 5., 0.],
     [7., 8., 9.]]

test_cc_trilu_lower_k_negative

Node:
  Trilu(X, K) -> (Y)
  Attributes:
    upper = 0
Inputs:
  X: shape=(3, 3), dtype=float32
    [[1., 2., 3.],
     [4., 5., 6.],
     [7., 8., 9.]]
  K: shape=(), dtype=int64
    -1

Outputs:
  Y: shape=(3, 3), dtype=float32
    [[0., 0., 0.],
     [4., 0., 0.],
     [7., 8., 0.]]

test_cc_trilu_upper_default

Node:
  Trilu(X) -> (Y)
Inputs:
  X: shape=(3, 3), dtype=float32
    [[1., 2., 3.],
     [4., 5., 6.],
     [7., 8., 9.]]

Outputs:
  Y: shape=(3, 3), dtype=float32
    [[1., 2., 3.],
     [0., 5., 6.],
     [0., 0., 9.]]

test_cc_trilu_upper_k_positive

Node:
  Trilu(X, K) -> (Y)
Inputs:
  X: shape=(3, 4), dtype=int64
    [[ 1,  2,  3,  4],
     [ 5,  6,  7,  8],
     [ 9, 10, 11, 12]]
  K: shape=(), dtype=int64
    1

Outputs:
  Y: shape=(3, 4), dtype=int64
    [[ 0,  2,  3,  4],
     [ 0,  0,  7,  8],
     [ 0,  0,  0, 12]]

test_cc_triu

Node:
  Trilu(X) -> (Y)
Inputs:
  X: shape=(4, 5), dtype=int64
    [[4, 7, 3, 7, 9],
     [1, 2, 8, 6, 9],
     [9, 4, 0, 8, 7],
     [4, 3, 4, 2, 4]]

Outputs:
  Y: shape=(4, 5), dtype=int64
    [[4, 7, 3, 7, 9],
     [0, 2, 8, 6, 9],
     [0, 0, 0, 8, 7],
     [0, 0, 0, 2, 4]]

test_cc_triu_neg

Node:
  Trilu(X, K) -> (Y)
Inputs:
  X: shape=(4, 5), dtype=int64
    [[4, 7, 3, 7, 9],
     [1, 2, 8, 6, 9],
     [9, 4, 0, 8, 7],
     [4, 3, 4, 2, 4]]
  K: shape=(), dtype=int64
    -1

Outputs:
  Y: shape=(4, 5), dtype=int64
    [[4, 7, 3, 7, 9],
     [1, 2, 8, 6, 9],
     [0, 4, 0, 8, 7],
     [0, 0, 4, 2, 4]]

test_cc_triu_one_row

Node:
  Trilu(X, K) -> (Y)
Inputs:
  X: shape=(3, 1, 5), dtype=int64
    [[[1, 4, 9, 7, 1]],

     [[9, 2, 8, 8, 4]],

     [[3, 9, 7, 4, 2]]]
  K: shape=(), dtype=int64
    1

Outputs:
  Y: shape=(3, 1, 5), dtype=int64
    [[[0, 4, 9, 7, 1]],

     [[0, 2, 8, 8, 4]],

     [[0, 9, 7, 4, 2]]]

test_cc_triu_out_neg_out

Node:
  Trilu(X, K) -> (Y)
Inputs:
  X: shape=(4, 5), dtype=int64
    [[4, 7, 3, 7, 9],
     [1, 2, 8, 6, 9],
     [9, 4, 0, 8, 7],
     [4, 3, 4, 2, 4]]
  K: shape=(), dtype=int64
    -7

Outputs:
  Y: shape=(4, 5), dtype=int64
    [[4, 7, 3, 7, 9],
     [1, 2, 8, 6, 9],
     [9, 4, 0, 8, 7],
     [4, 3, 4, 2, 4]]

test_cc_triu_out_pos

Node:
  Trilu(X, K) -> (Y)
Inputs:
  X: shape=(4, 5), dtype=int64
    [[4, 7, 3, 7, 9],
     [1, 2, 8, 6, 9],
     [9, 4, 0, 8, 7],
     [4, 3, 4, 2, 4]]
  K: shape=(), dtype=int64
    6

Outputs:
  Y: shape=(4, 5), dtype=int64
    [[0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0]]

test_cc_triu_pos

Node:
  Trilu(X, K) -> (Y)
Inputs:
  X: shape=(4, 5), dtype=int64
    [[4, 7, 3, 7, 9],
     [1, 2, 8, 6, 9],
     [9, 4, 0, 8, 7],
     [4, 3, 4, 2, 4]]
  K: shape=(), dtype=int64
    2

Outputs:
  Y: shape=(4, 5), dtype=int64
    [[0, 0, 3, 7, 9],
     [0, 0, 0, 6, 9],
     [0, 0, 0, 0, 7],
     [0, 0, 0, 0, 0]]

test_cc_triu_square

Node:
  Trilu(X) -> (Y)
Inputs:
  X: shape=(2, 3, 3), dtype=int64
    [[[4, 6, 9],
      [7, 5, 4],
      [8, 1, 2]],

     [[1, 4, 9],
      [9, 6, 3],
      [8, 9, 8]]]

Outputs:
  Y: shape=(2, 3, 3), dtype=int64
    [[[4, 6, 9],
      [0, 5, 4],
      [0, 0, 2]],

     [[1, 4, 9],
      [0, 6, 3],
      [0, 0, 8]]]

test_cc_triu_square_neg

Node:
  Trilu(X, K) -> (Y)
Inputs:
  X: shape=(2, 3, 3), dtype=int64
    [[[4, 6, 9],
      [7, 5, 4],
      [8, 1, 2]],

     [[1, 4, 9],
      [9, 6, 3],
      [8, 9, 8]]]
  K: shape=(), dtype=int64
    -1

Outputs:
  Y: shape=(2, 3, 3), dtype=int64
    [[[4, 6, 9],
      [7, 5, 4],
      [0, 1, 2]],

     [[1, 4, 9],
      [9, 6, 3],
      [0, 9, 8]]]

test_cc_triu_zero

Node:
  Trilu(X, K) -> (Y)
Inputs:
  X: shape=(0, 5), dtype=int64
    []
  K: shape=(), dtype=int64
    6

Outputs:
  Y: shape=(0, 5), dtype=int64
    []