1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
| """Generate minimal ONNX test models for Go bindings testing."""
|
| import os
|
| from onnx import TensorProto, checker, helper, save_model
|
| OUT_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
| def make_add_f32():
| """Add op: A[2,3] + B[2,3] = C[2,3], all float32."""
| input_a = helper.make_tensor_value_info("A", TensorProto.FLOAT, [2, 3])
| input_b = helper.make_tensor_value_info("B", TensorProto.FLOAT, [2, 3])
| output_c = helper.make_tensor_value_info("C", TensorProto.FLOAT, [2, 3])
|
| node = helper.make_node("Add", inputs=["A", "B"], outputs=["C"])
| graph = helper.make_graph([node], "add_graph", [input_a, input_b], [output_c])
| model = helper.make_model(
| graph,
| opset_imports=[helper.make_opsetid("", 13)],
| )
| model.ir_version = 8
| checker.check_model(model)
| path = os.path.join(OUT_DIR, "add_f32.onnx")
| save_model(model, path)
| return path
|
|
| def make_matmul_dynamic():
| """MatMul with dynamic batch: A[batch,4] @ B[4,2] = C[batch,2]."""
| input_a = helper.make_tensor_value_info("A", TensorProto.FLOAT, ["batch", 4])
| input_b = helper.make_tensor_value_info("B", TensorProto.FLOAT, [4, 2])
| output_c = helper.make_tensor_value_info("C", TensorProto.FLOAT, ["batch", 2])
|
| node = helper.make_node("MatMul", inputs=["A", "B"], outputs=["C"])
| graph = helper.make_graph([node], "matmul_graph", [input_a, input_b], [output_c])
| model = helper.make_model(
| graph,
| opset_imports=[helper.make_opsetid("", 13)],
| )
| model.ir_version = 8
| checker.check_model(model)
| path = os.path.join(OUT_DIR, "matmul_dynamic.onnx")
| save_model(model, path)
| return path
|
|
| def make_kvconcat():
| """Concat on axis=2: past[1,2,seq,4] + new_val[1,2,1,4] = out[1,2,seq+1,4].
|
| Tests zero-length dim path when seq=0.
| """
| past = helper.make_tensor_value_info("past", TensorProto.FLOAT, [1, 2, "seq", 4])
| new_val = helper.make_tensor_value_info("new_val", TensorProto.FLOAT, [1, 2, 1, 4])
| out = helper.make_tensor_value_info("out", TensorProto.FLOAT, [1, 2, "seq_plus_1", 4])
|
| node = helper.make_node("Concat", inputs=["past", "new_val"], outputs=["out"], axis=2)
| graph = helper.make_graph([node], "kvconcat_graph", [past, new_val], [out])
| model = helper.make_model(
| graph,
| opset_imports=[helper.make_opsetid("", 13)],
| )
| model.ir_version = 8
| checker.check_model(model)
| path = os.path.join(OUT_DIR, "kvconcat.onnx")
| save_model(model, path)
| return path
|
|
| if __name__ == "__main__":
| for gen in [make_add_f32, make_matmul_dynamic, make_kvconcat]:
| path = gen()
| size = os.path.getsize(path)
| print(f"{os.path.basename(path):25s} {size:4d} bytes")
|
|