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
This commit is contained in:
Scott McKay 2022-03-15 15:52:12 +10:00 committed by GitHub
parent 0d8d44d035
commit f385c73058
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 3 deletions

View file

@ -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 $<TARGET_FILE_DIR:${build_output_target}>/onnxruntime/datasets
COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:${build_output_target}>/onnxruntime/tools
COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:${build_output_target}>/onnxruntime/tools/mobile_helpers
COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:${build_output_target}>/onnxruntime/tools/qdq_helpers
COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:${build_output_target}>/onnxruntime/tools/ort_format_model
COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:${build_output_target}>/onnxruntime/tools/ort_format_model/ort_flatbuffers_py
COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:${build_output_target}>/onnxruntime/transformers
@ -461,7 +465,17 @@ add_custom_command(
COMMAND ${CMAKE_COMMAND} -E copy
${onnxruntime_mobile_util_srcs}
$<TARGET_FILE_DIR:${build_output_target}>/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 >>
$<TARGET_FILE_DIR:${build_output_target}>/onnxruntime/tools/__init__.py
COMMAND ${CMAKE_COMMAND} -E copy
${onnxruntime_qdq_helper_srcs}
$<TARGET_FILE_DIR:${build_output_target}>/onnxruntime/tools/qdq_helpers/
COMMAND ${CMAKE_COMMAND} -E copy
${onnxruntime_mobile_helpers_srcs}
$<TARGET_FILE_DIR:${build_output_target}>/onnxruntime/tools/mobile_helpers/
COMMAND ${CMAKE_COMMAND} -E copy

View file

@ -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

View file

@ -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

View file

@ -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"):