Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[ONNX] reverting broadcasting fixes #14299

Merged
merged 1 commit into from
Mar 2, 2019
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
36 changes: 32 additions & 4 deletions python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,47 @@ def sample_multinomial(attrs, inputs, proto_obj):
# Arithmetic Operations
def add(attrs, inputs, proto_obj):
"""Adding two tensors"""
return translation_utils.broadcast_arithmetic_helper(attrs, inputs, proto_obj, 'broadcast_add')
new_attr = {}

if 'broadcast' in attrs and attrs['broadcast'] == 1:
broadcast_axis = attrs['axis']
op_value = translation_utils._fix_broadcast('broadcast_add', inputs,
broadcast_axis, proto_obj)
return op_value, new_attr, inputs
return 'broadcast_add', new_attr, inputs

def subtract(attrs, inputs, proto_obj):
"""Subtracting two tensors"""
return translation_utils.broadcast_arithmetic_helper(attrs, inputs, proto_obj, 'broadcast_sub')
new_attr = {}

if 'broadcast' in attrs and attrs['broadcast'] == 1:
broadcast_axis = attrs['axis']
op_value = translation_utils._fix_broadcast('broadcast_sub', inputs,
broadcast_axis, proto_obj)
return op_value, new_attr, inputs
return 'broadcast_sub', new_attr, inputs

def multiply(attrs, inputs, proto_obj):
"""Multiply two tensors"""
return translation_utils.broadcast_arithmetic_helper(attrs, inputs, proto_obj, 'broadcast_mul')
new_attr = {}

if 'broadcast' in attrs and attrs['broadcast'] == 1:
broadcast_axis = attrs['axis']
op_value = translation_utils._fix_broadcast('broadcast_mul', inputs,
broadcast_axis, proto_obj)
return op_value, new_attr, inputs
return 'broadcast_mul', new_attr, inputs

def divide(attrs, inputs, proto_obj):
"""Divide two tensors"""
return translation_utils.broadcast_arithmetic_helper(attrs, inputs, proto_obj, 'broadcast_div')
new_attr = {}

if 'broadcast' in attrs and attrs['broadcast'] == 1:
broadcast_axis = attrs['axis']
op_value = translation_utils._fix_broadcast('broadcast_div', inputs,
broadcast_axis, proto_obj)
return op_value, new_attr, inputs
return 'broadcast_div', new_attr, inputs

def mean(attrs, inputs, proto_obj):
"""Mean of all the input tensors."""
Expand Down
14 changes: 0 additions & 14 deletions python/mxnet/contrib/onnx/onnx2mx/_translation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,3 @@ def get_input_shape(sym, proto_obj):
result = mod.get_outputs()[0].asnumpy()

return result.shape

def broadcast_arithmetic_helper(attrs, inputs, proto_obj, current_op_name):
"""Helper function for broadcast arithmetic ops."""
new_attr = {}
op_names = ['batchnorm, convolution, deconvolution']
if 'broadcast' in attrs and attrs['broadcast'] == 1:
broadcast_axis = attrs['axis']
for op_name in op_names:
# if input is bias which comes after conv, deconv, batchnorm operators
# then only reshape bias term
if inputs[0].name.startswith(op_name):
op_value = _fix_broadcast(current_op_name, inputs, broadcast_axis, proto_obj)
return op_value, new_attr, inputs
return current_op_name, new_attr, inputs