Skip to content

Commit ceeb5aa

Browse files
committed
Add end-to-end prediction test for float floor_div no-op
test_floor_div_elimination_by_dtype only checks the op graph. Add test_floor_div_float_prediction_is_floored, which converts a toy program and compares the prediction against floor(x) + 1. add(_, 1) keeps the floor_div interior so noop_elimination is free to remove it: without the dtype guard the floor_div is eliminated and the model returns x + 1, so this test fails before the fix and passes after.
1 parent 346f1db commit ceeb5aa

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

‎coremltools/converters/mil/mil/passes/tests/test_cleanup_passes.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
from coremltools.converters.mil import mil
1616
from coremltools.converters.mil.mil import Builder as mb
1717
from coremltools.converters.mil.mil import Function, Symbol, get_new_symbol, types
18+
from coremltools.converters.mil.mil.ops.tests.iOS14 import backends
19+
from coremltools.converters.mil.mil.ops.tests.testing_utils import run_compare_builder
1820
from coremltools.converters.mil.mil.passes.defs.cleanup import topological_reorder
1921
from coremltools.converters.mil.mil.passes.defs.cleanup.remove_redundant_ops import (
2022
remove_redundant_ops,
2123
)
2224
from coremltools.converters.mil.mil.passes.pass_registry import PASS_REGISTRY
25+
from coremltools.converters.mil.testing_reqs import compute_units
2326
from coremltools.converters.mil.testing_utils import (
2427
apply_pass_and_basic_check,
2528
assert_model_is_valid,
@@ -1394,6 +1397,35 @@ def prog(x):
13941397
assert get_op_types_in_program(prev_prog) == ["floor_div", "add"]
13951398
assert get_op_types_in_program(prog) == expected
13961399

1400+
@pytest.mark.parametrize(
1401+
"compute_unit, backend",
1402+
itertools.product(compute_units, backends),
1403+
)
1404+
def test_floor_div_float_prediction_is_floored(self, compute_unit, backend):
1405+
# End-to-end guard that the fp32 floor_div is not silently dropped by
1406+
# noop_elimination. floor_div(x, 1) == floor(x) for a float input, so removing
1407+
# it as a "no-op" changes the numeric output. Here add(_, 1) is a non-eliminable
1408+
# consumer that keeps the floor_div interior, so the pass is free to remove it:
1409+
# with the fix the floor survives and the model returns floor(x) + 1; without it
1410+
# the floor_div is eliminated and the model would return x + 1 instead.
1411+
x = np.array([[2.7, -1.3, 0.5, 4.9], [1.1, 3.6, -2.8, 0.0]], dtype=np.float32)
1412+
expected_outputs = np.floor(x) + 1.0
1413+
1414+
def build(x):
1415+
return mb.add(x=mb.floor_div(x=x, y=1.0), y=1.0)
1416+
1417+
input_placeholders = {"x": mb.placeholder(shape=x.shape)}
1418+
input_values = {"x": x}
1419+
run_compare_builder(
1420+
build,
1421+
input_placeholders,
1422+
input_values,
1423+
expected_output_types=(2, 4, types.fp32),
1424+
expected_outputs=expected_outputs,
1425+
compute_unit=compute_unit,
1426+
backend=backend,
1427+
)
1428+
13971429
def test_elementwise_broadcast(self):
13981430
@mb.program(input_specs=[mb.TensorSpec(shape=[4])])
13991431
def prog(x):

0 commit comments

Comments
 (0)