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
20 changes: 18 additions & 2 deletions coremltools/converters/mil/frontend/tensorflow/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1894,7 +1894,15 @@ def MirrorPad(context, node):

mode = node.attr.get("mode", "reflect").lower()
if mode == "symmetric":
mode = "reflect"
# MIL pad supports only `constant`, `reflect`, and `replicate`. TF's
# `symmetric` mode reflects including the edge values, which differs
# from `reflect` (excludes the edge). Mapping it to `reflect` would
# silently produce wrong outputs, so error out instead.
raise NotImplementedError(
"Padding mode 'symmetric' (SYMMETRIC) in the MirrorPad op is not "
"supported by Core ML. Only 'reflect' (REFLECT) is supported for "
"mirror padding."
)
in_rank = len(x.sym_type.get_shape())

if in_rank > 5 or in_rank < 2:
Expand Down Expand Up @@ -1950,7 +1958,15 @@ def Pad(context, node):

mode = node.attr.get("mode", "constant").lower()
if mode == "symmetric":
mode = "reflect"
# MIL pad supports only `constant`, `reflect`, and `replicate`. TF's
# `symmetric` mode reflects including the edge values, which differs
# from `reflect` (excludes the edge). Mapping it to `reflect` would
# silently produce wrong outputs, so error out instead.
raise NotImplementedError(
"Padding mode 'symmetric' (SYMMETRIC) in the Pad op is not "
"supported by Core ML. Only 'constant' (CONSTANT) and 'reflect' "
"(REFLECT) are supported."
)
constant_val = node.attr.get("constant_val", 0.0)
constant_val = mb.const(val=constant_val)
in_rank = len(x.sym_type.get_shape())
Expand Down
23 changes: 23 additions & 0 deletions coremltools/converters/mil/frontend/tensorflow/test/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -5402,6 +5402,29 @@ def build_model(x):
backend=backend
)

@pytest.mark.parametrize(
"backend",
backends,
)
def test_symmetric_unsupported(self, backend):
# MIL has no `symmetric` pad mode. TF `symmetric` reflects including the
# edge value, unlike MIL `reflect`, so it must error out rather than
# being silently mapped to `reflect`. See issue #2280.
input_shape = (1, 4)

@make_tf_graph([input_shape])
def build_model(x):
return tf.pad(x, paddings=[[0, 0], [2, 2]], mode="SYMMETRIC")

model, inputs, outputs = build_model

with pytest.raises(NotImplementedError, match="symmetric"):
ct.convert(
model,
source="tensorflow",
convert_to="milinternal",
)


class TestPadV2(TensorFlowBaseTest):
@pytest.mark.parametrize(
Expand Down