From f385c73058176ed81493319d3e49b5cc9382f63f Mon Sep 17 00:00:00 2001 From: Scott McKay Date: Tue, 15 Mar 2022 15:52:12 +1000 Subject: [PATCH] Fix a couple of issues with the python package tools (#10858) * Tweaks to the model utils * Add handling for a dim_value of -1 when replacing the entire input shape. This occurs in models exported from PaddlePaddle * make pytorch helpers accessible in package * make QDQ helpers accessible in package --- cmake/onnxruntime_python.cmake | 16 +++++++++++++- tools/python/util/__init__.py | 8 +++++-- tools/python/util/__init__append.py | 5 +++++ tools/python/util/onnx_model_utils.py | 31 +++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 tools/python/util/__init__append.py diff --git a/cmake/onnxruntime_python.cmake b/cmake/onnxruntime_python.cmake index fa9c641001..002d8a05e5 100644 --- a/cmake/onnxruntime_python.cmake +++ b/cmake/onnxruntime_python.cmake @@ -398,6 +398,9 @@ file(GLOB onnxruntime_mobile_helpers_srcs CONFIGURE_DEPENDS ${REPO_ROOT}/tools/ci_build/github/android/nnapi_supported_ops.md ${REPO_ROOT}/tools/ci_build/github/apple/coreml_supported_ops.md ) +file(GLOB onnxruntime_qdq_helper_srcs CONFIGURE_DEPENDS + ${REPO_ROOT}/tools/python/util/qdq_helpers/*.py +) set(build_output_target onnxruntime_common) if(NOT onnxruntime_ENABLE_STATIC_ANALYSIS) @@ -409,6 +412,7 @@ add_custom_command( COMMAND ${CMAKE_COMMAND} -E make_directory $/onnxruntime/datasets COMMAND ${CMAKE_COMMAND} -E make_directory $/onnxruntime/tools COMMAND ${CMAKE_COMMAND} -E make_directory $/onnxruntime/tools/mobile_helpers + COMMAND ${CMAKE_COMMAND} -E make_directory $/onnxruntime/tools/qdq_helpers COMMAND ${CMAKE_COMMAND} -E make_directory $/onnxruntime/tools/ort_format_model COMMAND ${CMAKE_COMMAND} -E make_directory $/onnxruntime/tools/ort_format_model/ort_flatbuffers_py COMMAND ${CMAKE_COMMAND} -E make_directory $/onnxruntime/transformers @@ -461,7 +465,17 @@ add_custom_command( COMMAND ${CMAKE_COMMAND} -E copy ${onnxruntime_mobile_util_srcs} $/onnxruntime/tools/ - COMMAND ${CMAKE_COMMAND} -E copy + # append the /tools/python/utils imports to the __init__.py that came from /onnxruntime/tools. + # we're aggregating scripts from two different locations, and only include selected functionality from + # /tools/python/util. due to that we take the full __init__.py from /onnxruntime/tools and append + # the required content from /tools/python/util/__init__append.py. + COMMAND ${CMAKE_COMMAND} -E cat + ${REPO_ROOT}/tools/python/util/__init__append.py >> + $/onnxruntime/tools/__init__.py + COMMAND ${CMAKE_COMMAND} -E copy + ${onnxruntime_qdq_helper_srcs} + $/onnxruntime/tools/qdq_helpers/ + COMMAND ${CMAKE_COMMAND} -E copy ${onnxruntime_mobile_helpers_srcs} $/onnxruntime/tools/mobile_helpers/ COMMAND ${CMAKE_COMMAND} -E copy diff --git a/tools/python/util/__init__.py b/tools/python/util/__init__.py index 759871c22f..5dcc1bdd9f 100644 --- a/tools/python/util/__init__.py +++ b/tools/python/util/__init__.py @@ -4,8 +4,6 @@ from .get_azcopy import get_azcopy from .logger import get_logger from .platform_helpers import (is_windows, is_macOS, is_linux) -# Test what is needed here to use in a script -# from .pytorch_export_helpers import infer_input_info from .run import run try: @@ -13,3 +11,9 @@ try: from .reduced_build_config_parser import parse_config except ImportError: get_logger('tools_python_utils').info('flatbuffers module is not installed. parse_config will not be available') + +# see if we can make the pytorch helpers available. +import importlib.util # noqa +have_torch = importlib.util.find_spec("torch") +if have_torch: + from .pytorch_export_helpers import infer_input_info diff --git a/tools/python/util/__init__append.py b/tools/python/util/__init__append.py new file mode 100644 index 0000000000..f1d318335a --- /dev/null +++ b/tools/python/util/__init__append.py @@ -0,0 +1,5 @@ +# appended to the __init__.py in the onnxruntime module's 'tools' folder from /tools/python/util/__init__append.py +import importlib.util +have_torch = importlib.util.find_spec("torch") +if have_torch: + from .pytorch_export_helpers import infer_input_info # noqa diff --git a/tools/python/util/onnx_model_utils.py b/tools/python/util/onnx_model_utils.py index d232324d04..6d51d0b5da 100644 --- a/tools/python/util/onnx_model_utils.py +++ b/tools/python/util/onnx_model_utils.py @@ -124,6 +124,34 @@ def _replace_symbolic_dim_value(graph: onnx.GraphProto, **kwargs): update_dim_values(graph.value_info) +def _remove_invalid_dim_values_impl(graph: onnx.GraphProto): + def clear_invalid_values(value): + if value.type.HasField("tensor_type"): + shape = value.type.tensor_type.shape + if shape: + for dim in shape.dim: + if dim.HasField('dim_value') and dim.dim_value < 1: + dim.Clear() + + for i in graph.input: + clear_invalid_values(i) + + for o in graph.output: + clear_invalid_values(o) + + for vi in graph.value_info: + clear_invalid_values(vi) + + +def remove_invalid_dim_values(graph: onnx.GraphProto): + ''' + Iterate the graph and subgraphs, unsetting any dim_value entries that have a value of less than 1. + These are typically erroneously inserted by a converter to represent a dynamic dimension. + :param graph: GraphProto to update + ''' + iterate_graph_per_graph_func(graph, _remove_invalid_dim_values_impl) + + def make_dim_param_fixed(graph: onnx.GraphProto, param_name: str, value: int): ''' Iterate all values in the graph, replacing dim_param in a tensor shape with the provided value. @@ -144,6 +172,9 @@ def make_input_shape_fixed(graph: onnx.GraphProto, input_name: str, fixed_shape: :param fixed_shape: Shape to use. ''' + # remove any invalid dim values first. typically this is a dim_value of -1. + remove_invalid_dim_values(graph) + for i in graph.input: if i.name == input_name: if not i.type.HasField("tensor_type"):