8000 Extend function inliner to support schema-defined functions by gramalingam · Pull Request #6931 · onnx/onnx · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Extend function inliner to support schema-defined functions #6931

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
14 changes: 13 additions & 1 deletion onnx/cpp2py_export.cc
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,19 @@ PYBIND11_MODULE(onnx_cpp2py_export, onnx_cpp2py_export) {
ModelProto model{};
ParseProtoFromPyBytes(&model, bytes);
auto function_id_set = inliner::FunctionIdSet::Create(std::move(function_ids), exclude);
inliner::InlineSelectedFunctions(model, *function_id_set);
inliner::InlineSelectedLocalFunctions(model, *function_id_set);
std::string out;
model.SerializeToString(&out);
return py::bytes(out);
});

inliner.def(
"inline_selected_functions2",
[](const py::bytes& bytes, std::vector<std::pair<std::string, std::string>> function_ids, bool exclude) {
ModelProto model{};
ParseProtoFromPyBytes(&model, bytes);
auto function_id_set = inliner::FunctionIdSet::Create(std::move(function_ids), exclude);
inliner::InlineSelectedFunctions(model, *function_id_set, nullptr);
std::string out;
model.SerializeToString(&out);
return py::bytes(out);
Expand Down
20 changes: 15 additions & 5 deletions onnx/inliner.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,33 @@ def inline_local_functions(


def inline_selected_functions(
model: onnx.ModelProto, function_ids: list[tuple[str, str]], exclude: bool = False
model: onnx.ModelProto,
function_ids: list[tuple[str, str]],
exclude: bool = False,
inline_schema_functions: bool = False,
) -> onnx.ModelProto:
"""Inline selected model-local functions in given model.
"""Inline selected functions in given model.

Arguments:
model: an ONNX ModelProto
function_ids: list of functions to include/exclude when inlining. Each
element is a tuple of (function domain, function name).
exclude: if true, inlines all functions except those specified in function_ids.
if false, inlines all functions specified in function_ids.
inline_schema_functions: if true, inlines schema-defined functions as well
as model-local functions. Otherwise, only model-local functions are inlined.

Returns:
ModelProto with all calls to model-local functions inlined (recursively)
"""
result = C.inline_selected_functions(
model.SerializeToString(), function_ids, exclude
)
if inline_schema_functions:
result = C.inline_selected_functions2(
model.SerializeToString(), function_ids, exclude
)
else:
result = C.inline_selected_functions(
model.SerializeToString(), function_ids, exclude
)
inlined_model = onnx.ModelProto()
inlined_model.ParseFromString(result)
return inlined_model
Loading
Loading
0