@@ -54,7 +54,9 @@ def _temporary_renames(**kwargs: Var):
54
54
arg ._rename (name )
55
55
56
56
57
- def build (inputs : Dict [str , Var ], outputs : Dict [str , Var ]) -> onnx .ModelProto :
57
+ def build (
58
+ inputs : Dict [str , Var ], outputs : Dict [str , Var ], * , drop_unused_inputs = False
59
+ ) -> onnx .ModelProto :
58
60
"""
59
61
Builds an ONNX Model with given model inputs and outputs.
60
62
@@ -71,6 +73,11 @@ def build(inputs: Dict[str, Var], outputs: Dict[str, Var]) -> onnx.ModelProto:
71
73
Model outputs. Keys are names, values may be any ``Var``.
72
74
Building will resolve what nodes were used in the construction
73
75
of output variables.
76
+ drop_unused_inputs
77
+ If ``False``, only inputs that are needed for the computation
78
+ of the ``outputs`` will appear as inputs of the returned
79
+ ``ModelProto``. Otherwise, all inputs are required by the
80
+ returned object (default).
74
81
75
82
Returns
76
83
-------
@@ -81,6 +88,11 @@ def build(inputs: Dict[str, Var], outputs: Dict[str, Var]) -> onnx.ModelProto:
81
88
the newest one. The minimum ``ai.onnx`` version is set to 14 to
82
89
avoid tooling issues with legacy versions.
83
90
91
+ Raises
92
+ ------
93
+ KeyError
94
+ If the ``outputs`` cannot be build from the given ``inputs``.
95
+
84
96
Examples
85
97
--------
86
98
>>> import numpy as np
@@ -113,8 +125,15 @@ def build(inputs: Dict[str, Var], outputs: Dict[str, Var]) -> onnx.ModelProto:
113
125
114
126
with _temporary_renames (** inputs ):
115
127
graph = results (** outputs )
116
- graph = graph .with_arguments (* inputs .values ())
117
- return graph .to_onnx_model ()
128
+ if not drop_unused_inputs :
129
+ graph = graph .with_arguments (* inputs .values ())
130
+ model_proto = graph .to_onnx_model ()
131
+
132
+ # Validate that no further inputs were required.
133
+ if any (inp .name not in inputs for inp in model_proto .graph .input ):
134
+ raise KeyError ("Model requires additional inputs not provided in 'inputs'." )
135
+
136
+ return model_proto
118
137
119
138
120
139
class _InlineCall (Protocol ):
0 commit comments