Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple outputs in extract_submodel for >=iOS 16 #2270

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions coremltools/converters/mil/debugging_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def replace_inputs(func, input_vars):
raise ValueError(f"outputs {outputs_not_found} not found in the function.")

func.set_outputs(new_outputs)
func.set_output_types([ct.TensorType(dtype=o.dtype) for o in new_outputs])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @smpanaro !
I think this is a correct fix.
Would you mind adding one unittest to cover this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem! Added one.


# Clean up the graph
PASS_REGISTRY["common::dead_code_elimination"](prog)
Expand Down
28 changes: 27 additions & 1 deletion coremltools/converters/mil/mil/tests/test_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import coremltools as ct
from coremltools.converters.mil import Builder as mb
from coremltools.converters.mil.debugging_utils import extract_submodel
from coremltools.converters.mil.mil import get_new_symbol
from coremltools.converters.mil.mil import get_new_symbol, types
from coremltools.converters.mil.mil.types.symbolic import is_symbolic
from coremltools.converters.mil.testing_utils import get_op_types_in_program

Expand Down Expand Up @@ -94,6 +94,32 @@ def prog(x):
assert output.shape[0] == 1
assert is_symbolic(output.shape[1])

def test_extract_submodel_change_output_type_count(self):
"""
Input graph:
x -> sin -> cos -> sub -> output_1
"""
@mb.program(input_specs=[mb.TensorSpec(shape=(1, 2), dtype=types.fp16)], opset_version=ct.target.iOS16)
def prog(x):
sin = mb.sin(x=x, name="sin")
cos = mb.cos(x=sin, name="cos")
relu = mb.relu(x=cos, name="relu")
return relu

model = ct.convert(prog, convert_to="mlprogram")

# Original program has a single output_types entry.
model._mil_program.functions["main"].set_output_types([ct.TensorType(dtype=np.float16)])

submodel = extract_submodel(model, outputs=["cos", "relu"])
func = submodel._mil_program.functions["main"]

outputs = list(func.outputs)
names = [output.name for output in outputs]
assert len(outputs) == 2
assert "cos" in names and "relu" in names
assert all([o.dtype == types.fp16 for o in outputs]), "all fp16 dtypes"

def test_extract_submodel_complex(self):
"""
Input graph:
Expand Down