Skip to content

Trilu layer #1264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added testdata/dnn/onnx/data/bias_mha_rope.pb
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_mha_rope.pb
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_mha_rope_0.pb
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_mha_rope_2.pb
Binary file not shown.
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_trilu_tril_0.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_trilu_tril_1.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_trilu_tril_neg_0.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_trilu_tril_neg_1.npy
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_trilu_tril_pos_0.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_trilu_tril_pos_1.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_trilu_tril_square_0.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_trilu_tril_square_1.npy
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_trilu_tril_zero_0.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_trilu_tril_zero_1.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_trilu_triu_0.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_trilu_triu_1.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_trilu_triu_neg_0.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_trilu_triu_neg_1.npy
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_trilu_triu_pos_0.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_trilu_triu_pos_1.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_trilu_triu_square_0.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_trilu_triu_square_1.npy
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions testdata/dnn/onnx/data/iput_mha_rope_1.pb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Biput_mha_rope_1J�^)2?J��=QA9?d��>�|?e��>`n�>�On?�m�>8:B?���>e:m?[?#&e?=?�>�*?��)?�,?3�P?̴c?�˓>�s?�%?^0?s��>�[m?��M>�c>W=ы�=�p�>��^>��x?#�j?>� ?�>s>z��>⥫>�5?ʪ0?���>��=�I�>��>��>HS?�o`?bn?�n >+�*>Ϫ ?�
�<k-�>[)?�V?�<�� ?x�4?�D%?�q�<��M>4-�<V�^>�\J?�{?5�=?�2?��G;���>�N.?Jl>�L�>sa=��>߭�>$4a?�9r?�>�Gg?> v>ؕ�>���> �>�^?�!�>�'?�K�>�#�>D��<cc?���=�=� ?d�?_?X�{?��=$�>#u>��1?$P,?��l?7�>6��=�^=?�4?� �>v��>ggb>ue�>.o�>B�O>~^�>�&�>)�>�f?�W�>��O?8.?��>V>���>/�=.�?� +?�pr?��I?��>��?~�?=`?�}�=�j9?��2>WD?%b�>U�>��>��l?��w>3?9?2�;>�a?&N�>�+?>?'?B�7?�3C?��>��=���>c�(?u�
?_�?8�/?t�>��.?�`?2��>+RL?�E�>�r<^�<>T^ ?�Wa?�mG?'�=��E?�?�&�>դ#?�T? �?��Z<�Jl?��?@�?M�T?3��>�7?| �=:�L=79K?���>�s�>)?�� ?rD->m�A?Ҡ`?8�>�@�>
Expand Down
2 changes: 2 additions & 0 deletions testdata/dnn/onnx/data/output_mha_rope.pb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Boutput_mha_ropeJ�
�<@��J@`4�@AM@9�@Ό�@�;@X�@�k=@�lK@`ȅ@��Q@|�@�ȧ@e�;@&@i<@.J@���@E�L@ �@>ۦ@��:@4�@�=@7 K@.�@�Q@� @Dè@��;@Ej @��=@�L@,�@d�U@�#@$-�@ _<@ȶ#@
1 change: 1 addition & 0 deletions testdata/dnn/onnx/data/output_tensor.pb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Boutput_tensorJ�G�?��b@��@ȩ7@��@�� @�&5@��@-�?Q�c@��@�7@��@� @G6@1�@�-�?��_@�]@�4@o�@�� @}%5@��@���?�Wd@(o@�6@��@O� @��6@2�@���?��a@��@�f7@��@.� @��4@�@
Expand Down
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_trilu_tril.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_trilu_tril_neg.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_trilu_tril_one_row.npy
Binary file not shown.
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_trilu_tril_out_neg.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_trilu_tril_out_pos.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_trilu_tril_pos.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_trilu_tril_square.npy
Binary file not shown.
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_trilu_tril_zero.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_trilu_triu.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_trilu_triu_neg.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_trilu_triu_one_row.npy
Binary file not shown.
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_trilu_triu_out_pos.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_trilu_triu_pos.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_trilu_triu_square.npy
Binary file not shown.
Binary file not shown.
Binary file added testdata/dnn/onnx/data/weights_mha_rope.pb
Binary file not shown.
297 changes: 297 additions & 0 deletions testdata/dnn/onnx/generate_data_onnx_trilu_layer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
from onnx import TensorProto, version_converter
import onnx
from onnx.helper import (
make_model, make_node, make_graph, make_tensor_value_info,
)
import numpy as np
import onnxruntime as ort
import os

testcases = [
{
"lab":"tril",
"X":\
[[4, 7, 3, 7, 9],
[1, 2, 8, 6, 9],
[9, 4, 1, 8, 7],
[4, 3, 4, 2, 4]],
"Y":\
[[4, 0, 0, 0, 0],
[1, 2, 0, 0, 0],
[9, 4, 1, 0, 0],
[4, 3, 4, 2, 0]],
"upper": 0,
"k": 0
},
{
"lab":"tril_neg",
"X":\
[[4, 7, 3, 7, 9],
[1, 2, 8, 6, 9],
[9, 4, 1, 8, 7],
[4, 3, 4, 2, 4]],
"Y":\
[[0, 0, 0, 0, 0],
[1, 0, 0, 0, 0],
[9, 4, 0, 0, 0],
[4, 3, 4, 0, 0]],
"upper": 0,
"k": -1
},
{
"lab":"tril_one_row",
"X":\
[[[6, 2, 4, 1, 6]],
[[8, 3, 8, 7, 0]],
[[2, 2, 9, 5, 9]]],
"Y":\
[[[6, 0, 0, 0, 0]],
[[8, 0, 0, 0, 0]],
[[2, 0, 0, 0, 0]]],
"upper": 0,
},
{
"lab":"tril_one_row1D",
"X":\
[6, 2, 4, 1, 6],
"Y":\
[6, 0, 0, 0, 0],
"upper": 0,
},
{
"lab":"tril_out_neg",
"X":\
[[4, 7, 3, 7, 9],
[1, 2, 8, 6, 9],
[9, 4, 1, 8, 7],
[4, 3, 4, 2, 4]],
"Y":\
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
"upper": 0,
"k": -7
},
{
"lab":"tril_out_pos",
"X":\
[[4, 7, 3, 7, 9],
[1, 2, 8, 6, 9],
[9, 4, 1, 8, 7],
[4, 3, 4, 2, 4]],
"Y":\
[[4, 7, 3, 7, 9],
[1, 2, 8, 6, 9],
[9, 4, 1, 8, 7],
[4, 3, 4, 2, 4]],
"upper": 0,
"k": 6
},
{
"lab":"tril_pos",
"X":\
[[4, 7, 3, 7, 9],
[1, 2, 8, 6, 9],
[9, 4, 1, 8, 7],
[4, 3, 4, 2, 4]],
"Y":\
[[4, 7, 3, 0, 0],
[1, 2, 8, 6, 0],
[9, 4, 1, 8, 7],
[4, 3, 4, 2, 4]],
"upper": 0,
"k": 2
},
{
"lab":"tril_square",
"X":\
[[[0, 4, 3],
[2, 0, 9],
[8, 2, 5]],
[[2, 7, 2],
[2, 6, 0],
[2, 6, 5]]],
"Y":\
[[[0, 0, 0],
[2, 0, 0],
[8, 2, 5]],
[[2, 0, 0],
[2, 6, 0],
[2, 6, 5]]],
"upper": 0,
},
{
"lab":"tril_square_neg",
"X":\
[[[0, 4, 3],
[2, 0, 9],
[8, 2, 5]],
[[2, 7, 2],
[2, 6, 0],
[2, 6, 5]]],
"Y":\
[[[0, 0, 0],
[2, 0, 0],
[8, 2, 0]],
[[0, 0, 0],
[2, 0, 0],
[2, 6, 0]]],
"upper": 0,
"k": -1
},
{
"lab":"triu",
"X":\
[[4, 7, 3, 7, 9],
[1, 2, 8, 6, 9],
[9, 4, 0, 8, 7],
[4, 3, 4, 2, 4]],
"Y":\
[[4, 7, 3, 7, 9],
[0, 2, 8, 6, 9],
[0, 0, 0, 8, 7],
[0, 0, 0, 2, 4]],
},
{
"lab":"triu_neg",
"X":\
[[4, 7, 3, 7, 9],
[1, 2, 8, 6, 9],
[9, 4, 0, 8, 7],
[4, 3, 4, 2, 4]],
"Y":\
[[4, 7, 3, 7, 9],
[1, 2, 8, 6, 9],
[0, 4, 0, 8, 7],
[0, 0, 4, 2, 4]],
"k": -1
},
{
"lab":"triu_one_row",
"X":\
[[[1, 4, 9, 7, 1]],
[[9, 2, 8, 8, 4]],
[[3, 9, 7, 4, 2]]],
"Y":\
[[[0, 4, 9, 7, 1]],
[[0, 2, 8, 8, 4]],
[[0, 9, 7, 4, 2]]],
"k": 1,
},
{
"lab":"triu_out_neg_out",
"X":\
[[4, 7, 3, 7, 9],
[1, 2, 8, 6, 9],
[9, 4, 0, 8, 7],
[4, 3, 4, 2, 4]],
"Y":\
[[4, 7, 3, 7, 9],
[1, 2, 8, 6, 9],
[9, 4, 0, 8, 7],
[4, 3, 4, 2, 4]],
"k":-7
},
{
"lab":"triu_out_pos",
"X":
[[4, 7, 3, 7, 9],
[1, 2, 8, 6, 9],
[9, 4, 0, 8, 7],
[4, 3, 4, 2, 4]],
"Y":
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
"k": 6
},
{
"lab":"triu_pos",
"X":
[[4, 7, 3, 7, 9],
[1, 2, 8, 6, 9],
[9, 4, 0, 8, 7],
[4, 3, 4, 2, 4]],
"Y":
[[0, 0, 3, 7, 9],
[0, 0, 0, 6, 9],
[0, 0, 0, 0, 7],
[0, 0, 0, 0, 0]],
"k":2
},
{
"lab": "triu_square",
"X":
[[[4, 6, 9],
[7, 5, 4],
[8, 1, 2]],
[[1, 4, 9],
[9, 6, 3],
[8, 9, 8]]],
"Y":
[[[4, 6, 9],
[0, 5, 4],
[0, 0, 2]],
[[1, 4, 9],
[0, 6, 3],
[0, 0, 8]]]
},
{
"lab": "triu_square_neg",
"X":
[[[4, 6, 9],
[7, 5, 4],
[8, 1, 2]],
[[1, 4, 9],
[9, 6, 3],
[8, 9, 8]]],
"Y":
[[[4, 6, 9],
[7, 5, 4],
[0, 1, 2]],
[[1, 4, 9],
[9, 6, 3],
[0, 9, 8]]],
"k": -1
}
]

for i in range(len(testcases)):
data_case = testcases[i]
k = data_case.get("k", 0)
upper = data_case.get("upper", 1)
lab= data_case.get("lab", "tril")

input= np.array(data_case["X"], dtype=np.float32)
output= np.array(data_case["Y"], dtype=np.float32)

x = make_tensor_value_info("x", TensorProto.FLOAT, input.shape)
y = make_tensor_value_info("y", TensorProto.FLOAT, output.shape)

k_ = make_tensor_value_info("k", TensorProto.INT64, [1])

node = onnx.helper.make_node(
"Trilu",
inputs=["x","k"],
outputs=["y"],
upper=upper,
)


graph = make_graph(
[node],
"trilu_graph",
[x, k_],
[y],
)

model = make_model(graph, producer_name="trilu_tril", ir_version=10)
onnx.save_model(model, os.path.join("models",f"trilu_{lab}.onnx"))

# save x and y as npy
np.save(os.path.join("data",f"input_trilu_{lab}_0.npy"), input.data)
np.save(os.path.join("data",f"input_trilu_{lab}_1.npy"), np.array([k], dtype=np.int64).data)

np.save(os.path.join("data",f"output_trilu_{lab}.npy"), output.data)
Binary file added testdata/dnn/onnx/models/mha_rope.onnx
Binary file not shown.
Binary file added testdata/dnn/onnx/models/trilu_tril.onnx
Binary file not shown.
Binary file added testdata/dnn/onnx/models/trilu_tril_neg.onnx
Binary file not shown.
Binary file added testdata/dnn/onnx/models/trilu_tril_one_row.onnx
Binary file not shown.
Binary file added testdata/dnn/onnx/models/trilu_tril_one_row1D.onnx
Binary file not shown.
Binary file added testdata/dnn/onnx/models/trilu_tril_out_neg.onnx
Binary file not shown.
Binary file added testdata/dnn/onnx/models/trilu_tril_out_pos.onnx
Binary file not shown.
Binary file added testdata/dnn/onnx/models/trilu_tril_pos.onnx
Binary file not shown.
Binary file added testdata/dnn/onnx/models/trilu_tril_square.onnx
Binary file not shown.
Binary file added testdata/dnn/onnx/models/trilu_tril_square_neg.onnx
Binary file not shown.
Binary file added testdata/dnn/onnx/models/trilu_tril_zero.onnx
Binary file not shown.
19 changes: 19 additions & 0 deletions testdata/dnn/onnx/models/trilu_triu.onnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@


trilu_tril:h

x
ky"Trilu*
upper� trilu_graphZ
x


Z
k


b
y


B
19 changes: 19 additions & 0 deletions testdata/dnn/onnx/models/trilu_triu_neg.onnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@


trilu_tril:h

x
ky"Trilu*
upper� trilu_graphZ
x


Z
k


b
y


B
21 changes: 21 additions & 0 deletions testdata/dnn/onnx/models/trilu_triu_one_row.onnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@


trilu_tril:p

x
ky"Trilu*
upper� trilu_graphZ
x



Z
k


b
y



B
19 changes: 19 additions & 0 deletions testdata/dnn/onnx/models/trilu_triu_out_neg_out.onnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@


trilu_tril:h

x
ky"Trilu*
upper� trilu_graphZ
x


Z
k


b
y


B
Loading