@@ -43,103 +43,103 @@ def make_model_and_data(model, *args, **kwargs):
4343 output_files = os .path .join ("data" , "output_" + name )
4444 np .save (output_files , output )
4545
46- # '''
47- # It builds a model with two Gather ops sharing a single same indices:
48-
49- # [Input] -> Gather(indices=0) -> Gather(indices=0) -> [Output]
50-
51- # , where the two indices constants have the same name.
52- # '''
53- # @ost.script()
54- # def gather_shared_indices(x: ost.FLOAT[2, 1, 3, 4]) -> ost.FLOAT[3, 4]:
55- # indices = op.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.INT64, [], np.array([0], dtype=np.int64)))
56- # y0 = op.Gather(x, indices, axis=0)
57- # y1 = op.Gather(y0, indices, axis=0)
58- # return y1
59- # make_model_and_data(gather_shared_indices, np.random.rand(2, 1, 3, 4).astype(np.float32))
60-
61- # '''
62- # [Input] -> Greater(B=61) -> [Output]
63- # \
64- # dtype=np.int64
65- # '''
66- # @ost.script()
67- # def greater_input_dtype_int64(x: ost.FLOAT[27, 9]) ->ost.BOOL[27, 9]:
68- # y = op.Greater(x, op.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.INT64, [], np.array([61], dtype=np.int64))))
69- # return y
70- # make_model_and_data(greater_input_dtype_int64, np.random.randint(0, 100, size=[27, 9], dtype=np.int64), force_saving_input_as_dtype_float32=True, force_saving_output_as_dtype_float32=True)
71-
72- # from onnxscript import opset11
73-
74- # @ost.script()
75- # def two_resizes_with_shared_subgraphs(x: ost.FLOAT["batch", 1, "height", "width"], y: ost.FLOAT[1, 1, 3, 2], z: ost.FLOAT[1, 1, 2, 1]) ->ost.FLOAT["batch", 1, "height", "width"]:
76- # shape_src_1 = opset11.Shape(x)
77- # shape_src_2 = opset11.Shape(x)
78- # gather_h = opset11.Gather(shape_src_1, opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.INT64, [], np.array([2], dtype=np.int64))), axis=0)
79- # gather_w = opset11.Gather(shape_src_2, opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.INT64, [], np.array([3], dtype=np.int64))), axis=0)
80- # unsqueeze_w_1 = opset11.Unsqueeze(gather_w, axes=[0])
81- # unsqueeze_w_2 = opset11.Unsqueeze(gather_w, axes=[0])
82- # unsqueeze_h_1 = opset11.Unsqueeze(gather_h, axes=[0])
83- # unsqueeze_h_2 = opset11.Unsqueeze(gather_h, axes=[0])
84- # concat_1 = opset11.Cast(opset11.Concat(unsqueeze_h_1, unsqueeze_w_1, axis=0), to=ost.INT64.dtype)
85- # concat_2 = opset11.Cast(opset11.Concat(unsqueeze_h_2, unsqueeze_w_2, axis=0), to=ost.INT64.dtype)
86-
87- # # This op is required to test double node removal
88- # y = opset11.Add(y, opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.FLOAT, [1], np.array([0.5], dtype=np.float32))))
89-
90- # # First branch
91- # sliced = opset11.Slice(opset11.Shape(y),
92- # starts=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.INT64, [1], np.array([0], dtype=np.int64))),
93- # ends=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.INT64, [1], np.array([2], dtype=np.int64))),
94- # axes=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.INT64, [1], np.array([0], dtype=np.int64))),
95- # )
96- # concat_y = opset11.Concat(sliced, concat_1, axis=0)
97- # resized_y = opset11.Resize(y,
98- # roi=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.FLOAT, [0], np.empty([0]))),
99- # scales=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.FLOAT, [0], np.empty([0]))),
100- # sizes=concat_y,
101- # coordinate_transformation_mode='pytorch_half_pixel',
102- # cubic_coeff_a=-0.75,
103- # mode='linear',
104- # nearest_mode='floor'
105- # )
106-
107- # # Second branch
108- # sliced = opset11.Slice(opset11.Shape(z),
109- # starts=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.INT64, [1], np.array([0], dtype=np.int64))),
110- # ends=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.INT64, [1], np.array([2], dtype=np.int64))),
111- # axes=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.INT64, [1], np.array([0], dtype=np.int64))),
112- # )
113- # concat_z = opset11.Concat(sliced, concat_2, axis=0)
114- # resized_z = opset11.Resize(z,
115- # roi=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.FLOAT, [0], np.empty([0]))),
116- # scales=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.FLOAT, [0], np.empty([0]))),
117- # sizes=concat_z,
118- # coordinate_transformation_mode='pytorch_half_pixel',
119- # cubic_coeff_a=-0.75,
120- # mode='linear',
121- # nearest_mode='floor'
122- # )
123-
124- # return opset11.Add(resized_y, resized_z)
125-
126- # make_model_and_data(two_resizes_with_shared_subgraphs, np.random.rand(1, 1, 4, 5).astype(np.float32), np.random.rand(1, 1, 3, 2).astype(np.float32), np.random.rand(1, 1, 2, 1).astype(np.float32))
127-
128-
129- # @ost.script()
130- # def bias_gelu(x: ost.FLOAT[1, 2, 3]) -> ost.FLOAT[1, 2, 3]:
131- # bias = op.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.FLOAT, [3], np.array([0.1, 0.3, 0.2], dtype=np.float32)))
132- # add1 = op.Add(x, bias)
133- # tmp = op.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.FLOAT, [], np.array([np.sqrt(2)], dtype=np.float32)))
134- # div = op.Div(add1, tmp)
135- # erf = op.Erf(div)
136- # tmp_0 = op.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.FLOAT, [], np.array([1], dtype=np.float32)))
137- # add2 = op.Add(erf, tmp_0)
138- # mul = op.Mul(add1, add2)
139- # tmp_1 = op.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.FLOAT, [], np.array([0.5], dtype=np.float32)))
140- # return op.Mul(mul, tmp_1)
141-
142- # make_model_and_data(bias_gelu, np.random.rand(1, 2, 3).astype(np.float32))
46+ '''
47+ It builds a model with two Gather ops sharing a single same indices:
48+
49+ [Input] -> Gather(indices=0) -> Gather(indices=0) -> [Output]
50+
51+ , where the two indices constants have the same name.
52+ '''
53+ @ost .script ()
54+ def gather_shared_indices (x : ost .FLOAT [2 , 1 , 3 , 4 ]) -> ost .FLOAT [3 , 4 ]:
55+ indices = op .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .INT64 , [], np .array ([0 ], dtype = np .int64 )))
56+ y0 = op .Gather (x , indices , axis = 0 )
57+ y1 = op .Gather (y0 , indices , axis = 0 )
58+ return y1
59+ make_model_and_data (gather_shared_indices , np .random .rand (2 , 1 , 3 , 4 ).astype (np .float32 ))
60+
61+ '''
62+ [Input] -> Greater(B=61) -> [Output]
63+ \
64+ dtype=np.int64
65+ '''
66+ @ost .script ()
67+ def greater_input_dtype_int64 (x : ost .FLOAT [27 , 9 ]) -> ost .BOOL [27 , 9 ]:
68+ y = op .Greater (x , op .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .INT64 , [], np .array ([61 ], dtype = np .int64 ))))
69+ return y
70+ make_model_and_data (greater_input_dtype_int64 , np .random .randint (0 , 100 , size = [27 , 9 ], dtype = np .int64 ), force_saving_input_as_dtype_float32 = True , force_saving_output_as_dtype_float32 = True )
71+
72+ from onnxscript import opset11
73+
74+ @ost .script ()
75+ def two_resizes_with_shared_subgraphs (x : ost .FLOAT ["batch" , 1 , "height" , "width" ], y : ost .FLOAT [1 , 1 , 3 , 2 ], z : ost .FLOAT [1 , 1 , 2 , 1 ]) -> ost .FLOAT ["batch" , 1 , "height" , "width" ]:
76+ shape_src_1 = opset11 .Shape (x )
77+ shape_src_2 = opset11 .Shape (x )
78+ gather_h = opset11 .Gather (shape_src_1 , opset11 .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .INT64 , [], np .array ([2 ], dtype = np .int64 ))), axis = 0 )
79+ gather_w = opset11 .Gather (shape_src_2 , opset11 .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .INT64 , [], np .array ([3 ], dtype = np .int64 ))), axis = 0 )
80+ unsqueeze_w_1 = opset11 .Unsqueeze (gather_w , axes = [0 ])
81+ unsqueeze_w_2 = opset11 .Unsqueeze (gather_w , axes = [0 ])
82+ unsqueeze_h_1 = opset11 .Unsqueeze (gather_h , axes = [0 ])
83+ unsqueeze_h_2 = opset11 .Unsqueeze (gather_h , axes = [0 ])
84+ concat_1 = opset11 .Cast (opset11 .Concat (unsqueeze_h_1 , unsqueeze_w_1 , axis = 0 ), to = ost .INT64 .dtype )
85+ concat_2 = opset11 .Cast (opset11 .Concat (unsqueeze_h_2 , unsqueeze_w_2 , axis = 0 ), to = ost .INT64 .dtype )
86+
87+ # This op is required to test double node removal
88+ y = opset11 .Add (y , opset11 .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .FLOAT , [1 ], np .array ([0.5 ], dtype = np .float32 ))))
89+
90+ # First branch
91+ sliced = opset11 .Slice (opset11 .Shape (y ),
92+ starts = opset11 .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .INT64 , [1 ], np .array ([0 ], dtype = np .int64 ))),
93+ ends = opset11 .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .INT64 , [1 ], np .array ([2 ], dtype = np .int64 ))),
94+ axes = opset11 .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .INT64 , [1 ], np .array ([0 ], dtype = np .int64 ))),
95+ )
96+ concat_y = opset11 .Concat (sliced , concat_1 , axis = 0 )
97+ resized_y = opset11 .Resize (y ,
98+ roi = opset11 .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .FLOAT , [0 ], np .empty ([0 ]))),
99+ scales = opset11 .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .FLOAT , [0 ], np .empty ([0 ]))),
100+ sizes = concat_y ,
101+ coordinate_transformation_mode = 'pytorch_half_pixel' ,
102+ cubic_coeff_a = - 0.75 ,
103+ mode = 'linear' ,
104+ nearest_mode = 'floor'
105+ )
106+
107+ # Second branch
108+ sliced = opset11 .Slice (opset11 .Shape (z ),
109+ starts = opset11 .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .INT64 , [1 ], np .array ([0 ], dtype = np .int64 ))),
110+ ends = opset11 .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .INT64 , [1 ], np .array ([2 ], dtype = np .int64 ))),
111+ axes = opset11 .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .INT64 , [1 ], np .array ([0 ], dtype = np .int64 ))),
112+ )
113+ concat_z = opset11 .Concat (sliced , concat_2 , axis = 0 )
114+ resized_z = opset11 .Resize (z ,
115+ roi = opset11 .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .FLOAT , [0 ], np .empty ([0 ]))),
116+ scales = opset11 .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .FLOAT , [0 ], np .empty ([0 ]))),
117+ sizes = concat_z ,
118+ coordinate_transformation_mode = 'pytorch_half_pixel' ,
119+ cubic_coeff_a = - 0.75 ,
120+ mode = 'linear' ,
121+ nearest_mode = 'floor'
122+ )
123+
124+ return opset11 .Add (resized_y , resized_z )
125+
126+ make_model_and_data (two_resizes_with_shared_subgraphs , np .random .rand (1 , 1 , 4 , 5 ).astype (np .float32 ), np .random .rand (1 , 1 , 3 , 2 ).astype (np .float32 ), np .random .rand (1 , 1 , 2 , 1 ).astype (np .float32 ))
127+
128+
129+ @ost .script ()
130+ def bias_gelu (x : ost .FLOAT [1 , 2 , 3 ]) -> ost .FLOAT [1 , 2 , 3 ]:
131+ bias = op .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .FLOAT , [3 ], np .array ([0.1 , 0.3 , 0.2 ], dtype = np .float32 )))
132+ add1 = op .Add (x , bias )
133+ tmp = op .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .FLOAT , [], np .array ([np .sqrt (2 )], dtype = np .float32 )))
134+ div = op .Div (add1 , tmp )
135+ erf = op .Erf (div )
136+ tmp_0 = op .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .FLOAT , [], np .array ([1 ], dtype = np .float32 )))
137+ add2 = op .Add (erf , tmp_0 )
138+ mul = op .Mul (add1 , add2 )
139+ tmp_1 = op .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .FLOAT , [], np .array ([0.5 ], dtype = np .float32 )))
140+ return op .Mul (mul , tmp_1 )
141+
142+ make_model_and_data (bias_gelu , np .random .rand (1 , 2 , 3 ).astype (np .float32 ))
143143
144144batch_size = 1
145145sequence_length = 320
@@ -298,3 +298,15 @@ def attention_single_head(x: ost.FLOAT[batch_size, sequence_length, input_hidden
298298 return reshape
299299
300300make_model_and_data (attention_single_head , np .random .rand (batch_size , sequence_length , input_hidden_size ).astype (np .float32 ))
301+
302+ # Einsum_const_inputs
303+
304+ input_0_data = np .random .rand (3 , 2 , 2 , 4 ).astype (np .float32 )
305+ input_1_data = np .random .rand (2 , 2 , 4 ).astype (np .float32 )
306+
307+ @ost .script ()
308+ def einsum_const_inputs (input_0 : ost .FLOAT [3 , 2 , 2 , 4 ]) -> ost .FLOAT [3 , 2 , 2 , 2 ]:
309+ input_1 = op .Constant (value = onnx .helper .make_tensor ("" , onnx .TensorProto .FLOAT , input_1_data .shape , input_1_data ))
310+ return op .Einsum (input_0 , input_1 , equation = "bhwc, hkc -> bhwk" )
311+
312+ make_model_and_data (einsum_const_inputs , input_0_data )
0 commit comments