From 5168d7ddc46ee92dc355988347caab5248cb5093 Mon Sep 17 00:00:00 2001 From: Dmitry Kurtaev Date: Fri, 27 Oct 2023 15:42:04 +0300 Subject: [PATCH 1/2] Test data for two resizes with shared subgraphs --- ...ut_two_resizes_with_shared_subgraphs_0.npy | Bin 0 -> 208 bytes ...ut_two_resizes_with_shared_subgraphs_1.npy | Bin 0 -> 152 bytes ...ut_two_resizes_with_shared_subgraphs_2.npy | Bin 0 -> 136 bytes ...tput_two_resizes_with_shared_subgraphs.npy | Bin 0 -> 208 bytes .../generate_onnx_models_with_onnxscript.py | 55 +++++++++++++++++- .../two_resizes_with_shared_subgraphs.onnx | Bin 0 -> 2905 bytes 6 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 testdata/dnn/onnx/data/input_two_resizes_with_shared_subgraphs_0.npy create mode 100644 testdata/dnn/onnx/data/input_two_resizes_with_shared_subgraphs_1.npy create mode 100644 testdata/dnn/onnx/data/input_two_resizes_with_shared_subgraphs_2.npy create mode 100644 testdata/dnn/onnx/data/output_two_resizes_with_shared_subgraphs.npy create mode 100644 testdata/dnn/onnx/models/two_resizes_with_shared_subgraphs.onnx diff --git a/testdata/dnn/onnx/data/input_two_resizes_with_shared_subgraphs_0.npy b/testdata/dnn/onnx/data/input_two_resizes_with_shared_subgraphs_0.npy new file mode 100644 index 0000000000000000000000000000000000000000..b4059848a149a5259e3c57d28ff39c3f204497ed GIT binary patch literal 208 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlV+l>qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= zXCxM+0{I$-ItoB!qN8A{siRP5)Pwl@nl+43%2YBO0a)ZW9?-Ck~I GoIL=s5;^(+ literal 0 HcmV?d00001 diff --git a/testdata/dnn/onnx/data/input_two_resizes_with_shared_subgraphs_1.npy b/testdata/dnn/onnx/data/input_two_resizes_with_shared_subgraphs_1.npy new file mode 100644 index 0000000000000000000000000000000000000000..1435d978c97bb156909470d05be7e68b0d2bcd0b GIT binary patch literal 152 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlV+l>qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= zXCxM+0{I$-ItoB!tfOG0siRPqoAIaUsO_*m=~X4l#&V(cT3DEP6dh= jXCxM+0{I$-ItoB!1SBqoAIaUsO_*m=~X4l#&V(cT3DEP6dh= zXCxM+0{I$-ItoB!qN8A{siRPost.BOOL[27, 9]: y = op.Greater(x, op.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.INT64, [], np.array([61], dtype=np.int64)))) return y 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) + +from onnxscript import opset11 + +@ost.script() +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"]: + shape_src_1 = opset11.Shape(x) + shape_src_2 = opset11.Shape(x) + 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) + 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) + unsqueeze_w_1 = opset11.Unsqueeze(gather_w, axes=[0]) + unsqueeze_w_2 = opset11.Unsqueeze(gather_w, axes=[0]) + unsqueeze_h_1 = opset11.Unsqueeze(gather_h, axes=[0]) + unsqueeze_h_2 = opset11.Unsqueeze(gather_h, axes=[0]) + concat_1 = opset11.Cast(opset11.Concat(unsqueeze_h_1, unsqueeze_w_1, axis=0), to=ost.INT64.dtype) + concat_2 = opset11.Cast(opset11.Concat(unsqueeze_h_2, unsqueeze_w_2, axis=0), to=ost.INT64.dtype) + + # First branch + sliced = opset11.Slice(opset11.Shape(y), + starts=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.INT64, [1], np.array([0], dtype=np.int64))), + ends=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.INT64, [1], np.array([2], dtype=np.int64))), + axes=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.INT64, [1], np.array([0], dtype=np.int64))), + ) + concat_y = opset11.Concat(sliced, concat_1, axis=0) + resized_y = opset11.Resize(y, + roi=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.FLOAT, [0], np.empty([0]))), + scales=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.FLOAT, [0], np.empty([0]))), + sizes=concat_y, + coordinate_transformation_mode='pytorch_half_pixel', + cubic_coeff_a=-0.75, + mode='linear', + nearest_mode='floor' + ) + + # Second branch + sliced = opset11.Slice(opset11.Shape(z), + starts=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.INT64, [1], np.array([0], dtype=np.int64))), + ends=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.INT64, [1], np.array([2], dtype=np.int64))), + axes=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.INT64, [1], np.array([0], dtype=np.int64))), + ) + concat_z = opset11.Concat(sliced, concat_2, axis=0) + resized_z = opset11.Resize(z, + roi=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.FLOAT, [0], np.empty([0]))), + scales=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.FLOAT, [0], np.empty([0]))), + sizes=concat_z, + coordinate_transformation_mode='pytorch_half_pixel', + cubic_coeff_a=-0.75, + mode='linear', + nearest_mode='floor' + ) + + return opset11.Add(resized_y, resized_z) + +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)) diff --git a/testdata/dnn/onnx/models/two_resizes_with_shared_subgraphs.onnx b/testdata/dnn/onnx/models/two_resizes_with_shared_subgraphs.onnx new file mode 100644 index 0000000000000000000000000000000000000000..58315bd3101156b23734ac8e2f4dc191de35656c GIT binary patch literal 2905 zcmd5;-E!kJ6t*2Faa1}KvD+=X0|S{I2C~Z%+ew_1oBg@q9)_E}(YTIMJDb|Uc1Yu= z=|eEw@&-H;N0ueaZIkYB#YIN)cYeO3qfb(-&iOy5=u4zu1^VWGU$_S05u5BnY1(!kGe@5lGNsW!HP(hkLfyWKb6`B^^bAtO)Dx_s+yu{ljP zhqE~>+Admq~w(sw+i%TkVNo&Lz-5rfByh8_#4_AH%=RTC&(0av(se zy9$`F0PjG6WT>R+@@9_sFy_Mqcd}I!&^gOCg=Mf0(AgAKIFhZGZP_v3*}g7TXUGF5 zXQ-5iMFTpPcA|^bSKd?RZ9nM!rw)DRg?oIM%KCP=m(naM=$x2~6rmL@+fwhZH6SGv&RVp{lrQw5!p^QRr#UR|-$#EZ6+FziZqFTYI8ppE6qE-(p(zX9T>Uo7~Vb{|F zqm<^GG$!x@vWlI4>&<+AuHkR;Z9@1_$jCOzLjrGZ3dbVbTwJF9I?Ud`U{CS4=((li zuTdAbFZ?_Rkzwh`!kv&tmtoG}Z8Xor_s`f}cZwc?^GWH${0yrFKZ6;IdyEEiyci*+ z#*gJ&exW|AHb%&@y4Zw-X4B*}Ovn70?;sPrwI!AhY={N+DVH^zhdWMNi9KsR94f!Q zyk%4?xMhU*PATt>u^9<}W6E1Vi7=L@Qk!ZfV_7+?8C2&cUOBr;W>aJsDWERE@qOT= z0X`B>gMz9R>=ay*c_pZq`=|-BX#<42Ltfzc6?zGJ`;O^x6MrCNC}*~uIdbNTjI#ze zRLXBx1=W`VRohylYX9`*6XiZrlBmmtgQzG}BlG;Pu0WLq7k3ssl8b{&s%pYS M&b+rK(oT;41uiVH(f|Me literal 0 HcmV?d00001 From b907d6d70231285a0bd214d88d4aaf6e4114fc78 Mon Sep 17 00:00:00 2001 From: Dmitry Kurtaev Date: Tue, 31 Oct 2023 13:44:55 +0300 Subject: [PATCH 2/2] Add extra node --- ...tput_two_resizes_with_shared_subgraphs.npy | Bin 208 -> 208 bytes .../generate_onnx_models_with_onnxscript.py | 3 +++ .../two_resizes_with_shared_subgraphs.onnx | Bin 2905 -> 3048 bytes 3 files changed, 3 insertions(+) diff --git a/testdata/dnn/onnx/data/output_two_resizes_with_shared_subgraphs.npy b/testdata/dnn/onnx/data/output_two_resizes_with_shared_subgraphs.npy index 10f1552304ecb8e205d3ce1b74e45befea46a0f8..50f517b80ed7b1e6e4ed317907a397ce3de782ac 100644 GIT binary patch delta 85 zcmV-b0IL7c0nh=Ef=D4kzP3v|#r?AaDFXhYrX;+`G;{FLCKVK@$Q%>}dr+%tj}R delta 85 zcmV-b0IL7c0nh=Ef=-V=wor{ffFOiFk>GVdbx3hPM#O(V2$O+7n^uTFE*g(N6w8o5 r{oaW`X6=qY4kx2OwP3eDAaA@shYpB8+`EoHFL9|qf+oa2*we~C6|W^t diff --git a/testdata/dnn/onnx/generate_onnx_models_with_onnxscript.py b/testdata/dnn/onnx/generate_onnx_models_with_onnxscript.py index 561231071..c8c9e6122 100644 --- a/testdata/dnn/onnx/generate_onnx_models_with_onnxscript.py +++ b/testdata/dnn/onnx/generate_onnx_models_with_onnxscript.py @@ -84,6 +84,9 @@ def two_resizes_with_shared_subgraphs(x: ost.FLOAT["batch", 1, "height", "width" concat_1 = opset11.Cast(opset11.Concat(unsqueeze_h_1, unsqueeze_w_1, axis=0), to=ost.INT64.dtype) concat_2 = opset11.Cast(opset11.Concat(unsqueeze_h_2, unsqueeze_w_2, axis=0), to=ost.INT64.dtype) + # This op is required to test double node removal + y = opset11.Add(y, opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.FLOAT, [1], np.array([0.5], dtype=np.float32)))) + # First branch sliced = opset11.Slice(opset11.Shape(y), starts=opset11.Constant(value=onnx.helper.make_tensor("", onnx.TensorProto.INT64, [1], np.array([0], dtype=np.int64))), diff --git a/testdata/dnn/onnx/models/two_resizes_with_shared_subgraphs.onnx b/testdata/dnn/onnx/models/two_resizes_with_shared_subgraphs.onnx index 58315bd3101156b23734ac8e2f4dc191de35656c..14c89af15ba5e6e2e92a950ecd597f0501d6d98b 100644 GIT binary patch delta 763 zcmZuuJ#Q015WT&pqF^TylBFoVy)R!15J5BuiGmjCuTPh zQ+F#?;}TU;RfgP|9UhE}iM%TY@Rs&X5kS}Mz%MD7Au8ZE?Oxg84(=EX5DF1$eIk`j zC6qDUhewuAA~>~Pb$mt^nie1})?2^}JBIr*Kuk=emEjZns)Hd2^uHa{a)Fi=x`w(& zyrF#O0R|*8_+hX5G4sYp<#;sD1+TvrvK_*8(_gND(S^ZZTVZlxwdn3gB3OBa_XdeRJaM~bNw~#Kk4!hzFKn?DkMJf!`k}I#(`zcVm^0>i zZpE=`Fu4Gd8chEO4&ifSznxVrHQ1Q0XwL)i*}cvB6kMQN%R2&IvQ-jR?+D(p4vFCl P+i5EY|0<@yE2#lyOt{^TBQ&%CZLH*Fw&!?Et+T?92{iu2M_`zLJ*0t&_!HLG%?@g zFK{Nh`wMV)arQ5mIN0NgI5@rSd*AooJ@04Y-M}g-y%{4smNt_V4RqYU=vE3Vk&bC) z@Y#8_+bGS^U13|?wqb}jpc_%J&41w zV$ZKiS}G~JOeIxrEs1AZI;E;AOf)s^|I8ws(j&u}9HB|b+;rZBCwj6LCVGFC<<|1F j23XMI@I_;g(kNoEuO(3dZlVcjYX-95fyR*yk6QT$_|=kL