Skip to content
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

fix unsqueeze bug #65

Merged
merged 1 commit into from
May 29, 2022
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
10 changes: 10 additions & 0 deletions examples/oneflow2onnx/models/GPU/test_mobilenet_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ def test_mobilenetv3():
flow.save(mobilenetv3.state_dict(), tmpdirname)
convert_to_onnx_and_check(mobilenetv3_graph, flow_weight_dir=tmpdirname, onnx_model_path=".", device="gpu")

def test_mobilenetv3_opset14():

mobilenetv3_graph = MobileNetV3()
mobilenetv3_graph._compile(flow.randn(1, 3, 224, 224).to("cuda"))

with tempfile.TemporaryDirectory() as tmpdirname:
flow.save(mobilenetv3.state_dict(), tmpdirname)
convert_to_onnx_and_check(mobilenetv3_graph, flow_weight_dir=tmpdirname, onnx_model_path=".", device="gpu", opset=14)

test_mobilenetv3()
test_mobilenetv3_opset14()
11 changes: 11 additions & 0 deletions examples/oneflow2onnx/nodes/CPU/test_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,15 @@ def test_conv2d():
flow.save(conv_module.state_dict(), tmpdirname)
convert_to_onnx_and_check(conv_graph, flow_weight_dir=tmpdirname, onnx_model_path="/tmp")

def test_conv2d_opset14():

conv_graph = Conv2dOpGraph()
conv_graph._compile(flow.randn(1, 3, 224, 224))

with tempfile.TemporaryDirectory() as tmpdirname:
flow.save(conv_module.state_dict(), tmpdirname)
convert_to_onnx_and_check(conv_graph, flow_weight_dir=tmpdirname, onnx_model_path="/tmp", opset=14)


test_conv2d()
test_conv2d_opset14()
20 changes: 20 additions & 0 deletions oneflow_onnx/oneflow2onnx/handlers/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ def Version_6(cls, ctx, node, **kwargs):
ctx.set_dtype(node.input_tensor_names[1], unsqueeze_dtype)
super().Version_6(ctx, node, **kwargs)

@classmethod
def Version_13(cls, ctx, node, **kwargs):
axis = node.attrs["axis"]
unsqueeze_axes = []
x_rank = len(ctx.get_shape(node.input_tensor_names[0]))
for i in range(x_rank):
if axis != i:
unsqueeze_axes.append(i)
assert len(ctx.get_shape(node.input_tensor_names[1])) == 1
shape_node = ctx.MakeConst(
oneflow._oneflow_internal.UniqueStr("shape"), np.array(unsqueeze_axes)
)
ctx.InsertNewNodeOnInput(
node, "Unsqueeze", [node.input_tensor_names[1], shape_node.output_tensor_names[0]]
)
super().Version_6(ctx, node, **kwargs)

@flow_op(["leaky_relu", "softplus"], onnx_op=["LeakyRelu", "Softplus"])
class DirectOpSinceOpset1:
Expand Down Expand Up @@ -280,6 +296,10 @@ def Version_1(cls, ctx, node, **kwargs):
ctx.MakeNode(
"Mul", [node.input_tensor_names[0], node1.output_tensor_names[0]], outputs=[node.output_tensor_names[0]], op_name_scope=node.name, name="mul"
)

@classmethod
def Version_14(cls, ctx, node, **kwargs):
pass

@flow_op("hardsigmoid", onnx_op="HardSigmoid")
class HardSigmoid:
Expand Down
5 changes: 4 additions & 1 deletion oneflow_onnx/oneflow2onnx/optimizer/const_fold_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ def _FoldUnsqueeze(node, graph):
numpy expand_dims only supports to unsqueeze one dim one time, so reshape is used to simplify the logic
"""
const_val = node.input_nodes[0].get_tensor_value(as_list=False)
axes = node.attrs["axes"]
if "axes" in node.attrs:
axes = node.attrs["axes"]
else:
axes = node.input_nodes[1].get_tensor_value(as_list=False)
util.MakeSure(
all(axis >= 0 for axis in axes),
"onnx spec says it only supports positive axis",
Expand Down