Skip to content
This repository was archived by the owner on Feb 7, 2023. It is now read-only.

Commit 65797e0

Browse files
authored
adding softmax nd (#489)
1 parent b561885 commit 65797e0

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

onnx_coreml/_operators_nd.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,7 +1988,27 @@ def _convert_slice(builder, node, graph, err):
19881988
)
19891989
else:
19901990
err.unsupported_op_configuration(builder, node, graph, "CoreML does not support Dynamic Slice with unknown axes. Please provide Custom Function/Layer")
1991-
1991+
1992+
def _convert_softmax_nd(builder, node, graph, err):
1993+
'''
1994+
convert to CoreML SoftMax ND Layer:
1995+
https://github.com/apple/coremltools/blob/655b3be5cc0d42c3c4fa49f0f0e4a93a26b3e492/mlmodel/format/NeuralNetwork.proto#3547
1996+
'''
1997+
axis = node.attrs.get('axis', 1)
1998+
builder.add_softmax_nd(
1999+
name=node.name,
2000+
input_name=node.inputs[0],
2001+
output_name=node.outputs[0] + ('_softmax' if node.op_type == 'LogSoftmax' else ''),
2002+
axis=axis
2003+
)
2004+
if node.op_type == 'LogSoftmax':
2005+
builder.add_unary(
2006+
name=node.name+'_log',
2007+
input_name=node.outputs[0]+'_softmax',
2008+
output_name=node.outputs[0],
2009+
mode='log'
2010+
)
2011+
19922012
def _convert_softmax(builder, node, graph, err):
19932013
'''
19942014
convert to CoreML SoftMax ND Layer:
@@ -2053,7 +2073,7 @@ def add_softmax(output_name, rank=-1, axis=-3):
20532073
axis = node.attrs.get('axis', 1)
20542074
rank = builder._get_rank(node.inputs[0])
20552075
if rank == -1:
2056-
return err.unsupported_op_configuration(builder, node, graph, "Rank unknown for input")
2076+
return _convert_softmax_nd(builder, node, graph, err)
20572077

20582078
if node.op_type == 'LogSoftmax':
20592079
add_softmax(node.outputs[0] + '_softmax', rank=rank, axis=axis)

0 commit comments

Comments
 (0)