Skip to content
Open
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
5 changes: 4 additions & 1 deletion coremltools/converters/mil/backend/nn/op_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -2472,7 +2472,10 @@ def softplus(const_context, builder, op):
@register_mil_to_nn_mapping
def softmax(const_context, builder, op):
rank = op.x.rank
if op.axis.val == -3 or op.axis.val > 0 and op.axis.val == rank - 3:
# SoftmaxLayer is specified as axis=-3, but for rank-3 inputs the
# NeuralNetwork runtime applies softmax along axis=-1 instead (see issue
# 1714). Only emit it when rank >= 4, matching the concat mapping.
if rank >= 4 and (op.axis.val == -3 or op.axis.val > 0 and op.axis.val == rank - 3):
builder.add_softmax(
name=op.name, input_name=op.x.name, output_name=op.outputs[0].name,
)
Expand Down
34 changes: 34 additions & 0 deletions coremltools/converters/mil/mil/ops/tests/iOS14/test_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,40 @@ def build(x):
backend=backend,
)

@pytest.mark.parametrize("compute_unit, backend", itertools.product(compute_units, backends))
def test_builder_to_backend_rank3_axis_minus_3(self, compute_unit, backend):
# Rank-3 softmax(axis=-3) must not use NeuralNetwork SoftmaxLayer
# (https://github.com/apple/coremltools/issues/1714): that layer
# applies axis=-1 when rank==3, which yields all-ones here.
t = np.array([[[0.5]], [[1.5]]], dtype=np.float32)
input_placeholders = {"x": mb.placeholder(shape=t.shape)}
input_values = {"x": t}

def build(x):
return mb.softmax(x=x, axis=-3)

expected_output_types = (2, 1, 1, types.fp32)
expected_outputs = scipy.special.softmax(t, axis=-3).astype(np.float32)
run_compare_builder(
build,
input_placeholders,
input_values,
expected_output_types,
expected_outputs,
compute_unit=compute_unit,
backend=backend,
)

def test_neuralnetwork_rank3_axis_minus_3_uses_softmax_nd(self):
@mb.program(input_specs=[mb.TensorSpec(shape=(2, 1, 1))])
def prog(x):
return mb.softmax(x=x, axis=-3, name="y")

mlmodel = ct.convert(prog, source="milinternal", convert_to="neuralnetwork")
layer = mlmodel.get_spec().neuralNetwork.layers[0]
assert layer.WhichOneof("layer") == "softmaxND"
assert layer.softmaxND.axis == -3

@ssa_fn
def test_builder_eval(self):
x_val = np.array([[-1, 2, -3], [4, -5, 6]], dtype=np.float32)
Expand Down
5 changes: 3 additions & 2 deletions mlmodel/format/NeuralNetwork.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2306,8 +2306,9 @@ message LRNLayerParams {
* Softmax Normalization Layer
*
* A layer that performs softmax normalization.
* Normalization is applied along axis = -3 or N-3 (where N is the rank of the input)
* For softmax layer that can operate on any axis, see SoftmaxNDLayer.
* For rank >= 4, normalization is applied along axis = -3 or N-3 (where N is the rank of the input).
* For rank == 3, the NeuralNetwork runtime applies softmax along axis = -1, not -3.
* For a softmax layer that can operate on any axis, see SoftmaxNDLayer.
*
*
* .. code::
Expand Down