From 19949835400392b71fe8f04760d1fa4855beab70 Mon Sep 17 00:00:00 2001 From: Vinayak Dev <104419489+vinayakdsci@users.noreply.github.com> Date: Mon, 9 Dec 2024 23:28:51 +0530 Subject: [PATCH] [onnx][importer] Sanitize '-' characters in TensorProto names (#3901) Dense Resources cannot have `-` characters as part of the resource keys. Many ONNX models, however, do have these characters in `TensorProto` or initializer names. This patch adds an unconditional replace function in the sanitization of initializer names that replaces all `-` characters with `_` (underscores) when the dense resources are created, which allows successful parsing of the IR. In case the name was legal before sanitization, the function has no effect. Unnecessary additional time complexity is avoided by omitting an `if` condition to check containment. --- python/torch_mlir/extras/onnx_importer.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/python/torch_mlir/extras/onnx_importer.py b/python/torch_mlir/extras/onnx_importer.py index 9fe29212386a..ab9b6de3cd5b 100644 --- a/python/torch_mlir/extras/onnx_importer.py +++ b/python/torch_mlir/extras/onnx_importer.py @@ -739,6 +739,13 @@ def type_proto_to_type(self, tp: onnx.TypeProto) -> IrType: def _sanitize_name(self, name): if not name.isidentifier(): name = "_" + name + + # The presence of '-' characters in the initializer names can cause + # unintended side-effects when the IR is parsed during compilation. + # Simply replace all the occurrences of '-' in the name string when the + # dense resource is created. + name = name.replace("-", "_") + return re.sub("[:/]", "_", name) def tensor_proto_to_attr(self, tp: onnx.TensorProto) -> Attribute: