Skip to content

Commit 457383d

Browse files
Change _const prefix to _val | feat(torchlib) (#886)
Use `_val` for all intermediate values to avoid ambiguity based on comment: #881 (review) Co-authored-by: Bowen Bao <[email protected]>
1 parent b2c19a7 commit 457383d

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

onnxscript/function_libs/torch_lib/graph_building.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,18 @@
6666

6767

6868
def _rename_intermediate_value(name: str) -> str:
69-
if name.isdigit():
70-
return f"_val_{name}"
71-
return name
69+
"""Prepend `_val_` to a numeric tensor name make it valid in ONNX.
7270
71+
The TorchScript graph creates numeric value names by default. e.g. `0`, `1`.
72+
These are not legal ONNX tensor names, since ONNX requires the names to be valid
73+
C variable names.
7374
74-
def _rename_intermediate_constant(name: str) -> str:
75+
It also improves readability by making the names less likely to be confused
76+
with shape values.
77+
"""
7578
if name.isdigit():
76-
return f"_const_{name}"
79+
# Prefix with `_` to avoid name collision
80+
return f"_val_{name}"
7781
return name
7882

7983

@@ -458,7 +462,7 @@ def _add_constant_to_graph(self, constant) -> torch.Value:
458462
self._torch_graph, "prim::Constant", inputs=(), attributes={}
459463
)[0]
460464
value.setType(torch.OptionalType.ofTensor())
461-
value.setDebugName(_rename_intermediate_constant(value.debugName()))
465+
value.setDebugName(_rename_intermediate_value(value.debugName()))
462466
return value
463467

464468
if isinstance(constant, bool):
@@ -486,7 +490,7 @@ def _add_constant_to_graph(self, constant) -> torch.Value:
486490
inputs=(),
487491
attributes=dict(value=constant_tensor),
488492
)[0]
489-
value.setDebugName(_rename_intermediate_constant(value.debugName()))
493+
value.setDebugName(_rename_intermediate_value(value.debugName()))
490494
return value
491495

492496
@runtime_typing.checked

0 commit comments

Comments
 (0)