|
15 | 15 | from coremltools.converters.mil import mil |
16 | 16 | from coremltools.converters.mil.mil import Builder as mb |
17 | 17 | 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 |
18 | 20 | from coremltools.converters.mil.mil.passes.defs.cleanup import topological_reorder |
19 | 21 | from coremltools.converters.mil.mil.passes.defs.cleanup.remove_redundant_ops import ( |
20 | 22 | remove_redundant_ops, |
21 | 23 | ) |
22 | 24 | from coremltools.converters.mil.mil.passes.pass_registry import PASS_REGISTRY |
| 25 | +from coremltools.converters.mil.testing_reqs import compute_units |
23 | 26 | from coremltools.converters.mil.testing_utils import ( |
24 | 27 | apply_pass_and_basic_check, |
25 | 28 | assert_model_is_valid, |
@@ -1394,6 +1397,35 @@ def prog(x): |
1394 | 1397 | assert get_op_types_in_program(prev_prog) == ["floor_div", "add"] |
1395 | 1398 | assert get_op_types_in_program(prog) == expected |
1396 | 1399 |
|
| 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 | + |
1397 | 1429 | def test_elementwise_broadcast(self): |
1398 | 1430 | @mb.program(input_specs=[mb.TensorSpec(shape=[4])]) |
1399 | 1431 | def prog(x): |
|
0 commit comments