Skip to content

Commit d3a02fb

Browse files
authored
[ONNX] Fix initializer prefix stripping (#20323)
The Relax ONNX importer used `str.strip("onnx::")` when `keep_params_in_input=True`. Since `strip` treats its argument as a set of characters, initializer names that do not have the prefix can still lose leading or trailing `o`, `n`, `x`, or `:` characters. For example, `neck...` became `eck...`. This change uses `str.removeprefix("onnx::")` so only the exact PyTorch-generated prefix is removed. The regression test covers both a prefixed initializer and an ordinary initializer beginning with `n`. Fixes #20290 Tests: - `python -m pytest tests/python/relax/test_frontend_onnx.py -k 'params_names_start_with_onnx or initializer_name_only_removes_onnx_prefix or concat_with_param or multi_ops_with_same_params' -q` - `python -m ruff check python/tvm/relax/frontend/onnx/onnx_frontend.py tests/python/relax/test_frontend_onnx.py` - `python -m ruff format --check python/tvm/relax/frontend/onnx/onnx_frontend.py tests/python/relax/test_frontend_onnx.py` Local full-file note: the complete ONNX test file reached 28 passing tests before hitting the unrelated Windows JIT export assertion `Target triple should not be empty`.
1 parent d01d441 commit d3a02fb

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

python/tvm/relax/frontend/onnx/onnx_frontend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6354,7 +6354,7 @@ def _parse_graph_initializers(self, graph: onnx.onnx_ml_pb2.GraphProto):
63546354
# Create variables for constants.
63556355
if self._keep_params_in_input:
63566356
# Pytorch sometimes inserts silly weight prefix. Remove it.
6357-
var_name = init_tensor.name.strip("onnx::")
6357+
var_name = init_tensor.name.removeprefix("onnx::")
63586358
init_var = self._new_var(var_name, shape=array.shape, dtype=array.dtype)
63596359
self._nodes[init_tensor.name] = init_var
63606360
# We need to keep track of both the real value and variable for this variable.

tests/python/relax/test_frontend_onnx.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11797,6 +11797,36 @@ def main(
1179711797
tvm.ir.assert_structural_equal(tvm_model, Expected)
1179811798

1179911799

11800+
@pytest.mark.parametrize(
11801+
("initializer_name", "expected_name"),
11802+
[
11803+
("onnx::weight", "weight"),
11804+
(
11805+
"neck.lateral_convs.2.conv2.weight_quantized",
11806+
"neck.lateral_convs.2.conv2.weight_quantized",
11807+
),
11808+
],
11809+
)
11810+
def test_initializer_name_only_removes_onnx_prefix(initializer_name, expected_name):
11811+
graph = helper.make_graph(
11812+
[helper.make_node("Add", ["input", initializer_name], ["output"])],
11813+
"test_initializer_name_only_removes_onnx_prefix",
11814+
inputs=[helper.make_tensor_value_info("input", TensorProto.FLOAT, [1])],
11815+
initializer=[numpy_helper.from_array(np.ones([1], dtype="float32"), initializer_name)],
11816+
outputs=[helper.make_tensor_value_info("output", TensorProto.FLOAT, [1])],
11817+
)
11818+
model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 14)])
11819+
model.ir_version = 8
11820+
11821+
tvm_model = from_onnx(
11822+
model,
11823+
keep_params_in_input=True,
11824+
sanitize_input_names=False,
11825+
)
11826+
11827+
assert tvm_model["main"].params[-1].name == expected_name
11828+
11829+
1180011830
def test_shape_dim_string_expression_graph_add():
1180111831
identity_node = helper.make_node("Identity", ["x"], ["y"])
1180211832

0 commit comments

Comments
 (0)