Skip to content

Commit

Permalink
[onnx][importer] Sanitize '-' characters in TensorProto names (#3901)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vinayakdsci authored Dec 9, 2024
1 parent c253512 commit 1994983
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/torch_mlir/extras/onnx_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 1994983

Please sign in to comment.