Skip to content

Commit 5693ca7

Browse files
authored
[ONNX] Fix import of boolean tensor constants (#4064)
Currently the shape and element type of boolean tensor attributes are incorrect, since they use the shape/dtype of the packed numpy array rather than of the original tensor value. Specifying the shape and element type explicitly resolves this.
1 parent eaad883 commit 5693ca7

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

python/torch_mlir/extras/onnx_importer.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,8 @@ def get_operator_function(
11371137
axis=None,
11381138
bitorder="little",
11391139
),
1140-
signless=False,
1140+
shape=tp.dims,
1141+
type=IntegerType.get_signless(1),
11411142
),
11421143
onnx.TensorProto.DataType.UINT8: lambda tp: DenseElementsAttr.get(
11431144
np.asarray(tp.int32_data, dtype=np.uint8).reshape(tp.dims), signless=False
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Licensed under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
# RUN: %PYTHON %s %t.onnx
6+
# RUN: %PYTHON -m torch_mlir.tools.import_onnx %t.onnx > %t.mlir
7+
# RUN: FileCheck %s < %t.mlir
8+
9+
import onnx
10+
from onnx.helper import make_graph, make_tensor, make_tensor_value_info
11+
12+
graph = make_graph(
13+
name="graph",
14+
inputs=[],
15+
nodes=[],
16+
outputs=[],
17+
initializer=[
18+
# CHECK{LITERAL}: torch.operator "onnx.Constant"() {torch.onnx.value = dense<[[true, false], [false, true]]> : tensor<2x2xi1>} : () -> !torch.vtensor<[2,2],i1>
19+
make_tensor(
20+
"bool_tensor",
21+
onnx.TensorProto.BOOL,
22+
dims=[2, 2],
23+
vals=[True, False, False, True],
24+
)
25+
],
26+
)
27+
model = onnx.helper.make_model(graph)
28+
29+
import sys
30+
31+
out_file_path = sys.argv[1]
32+
onnx.save(model, out_file_path)

0 commit comments

Comments
 (0)