Skip to content

Commit ce34dce

Browse files
authored
[Optimizer] Avoid accessing None value in _process_constant_node (#2513)
Signed-off-by: Christoph Berganski <[email protected]>
1 parent 2ff01f7 commit ce34dce

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

onnxscript/optimizer/_constant_folding.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ def _process_constant_node(node: ir.Node) -> None:
7171
if attr_value is None or not isinstance(attr_value, ir.Attr):
7272
return
7373

74+
# Even if this is an attribute, the value property might not be set, which
75+
# happens e.g. in case of attribute references, i.e., ref_attr_name is set
76+
if attr_value.value is None:
77+
# For now reject this to prevent TypeError from accessing Nones below
78+
return
79+
7480
const_value: ir.TensorProtocol
7581
if attr_name in {"value_float", "value_floats"}:
7682
const_value = ir.Tensor(

onnxscript/optimizer/_constant_folding_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,22 @@ def test_multi_graph_identity_output_preserves_output_name(self):
597597
)
598598
self.assertEqual([input.name for input in optimized.graph.inputs], ["x"])
599599

600+
# This should not be constant-foldable as the constant references an
601+
# attribute and thus the shape cannot be resolved. At the same time it
602+
# should not fail due to the attribute value being None in
603+
# _process_constant_node
604+
def test_attribute_reference(self):
605+
model = """
606+
<ir_version: 7, opset_import: ["" : 17]>
607+
agraph () => (int64[N] z) {
608+
x = Constant <value_ints: ints = @attr> ()
609+
z = Shape (x)
610+
}
611+
"""
612+
613+
optimized = self._fold(model)
614+
self.assertEqual(len(optimized.graph), 2)
615+
600616

601617
if __name__ == "__main__":
602618
unittest.main()

0 commit comments

Comments
 (0)