File tree Expand file tree Collapse file tree 1 file changed +11
-7
lines changed
onnxscript/function_libs/torch_lib Expand file tree Collapse file tree 1 file changed +11
-7
lines changed Original file line number Diff line number Diff line change 66
66
67
67
68
68
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.
72
70
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.
73
74
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
+ """
75
78
if name .isdigit ():
76
- return f"_const_{ name } "
79
+ # Prefix with `_` to avoid name collision
80
+ return f"_val_{ name } "
77
81
return name
78
82
79
83
@@ -458,7 +462,7 @@ def _add_constant_to_graph(self, constant) -> torch.Value:
458
462
self ._torch_graph , "prim::Constant" , inputs = (), attributes = {}
459
463
)[0 ]
460
464
value .setType (torch .OptionalType .ofTensor ())
461
- value .setDebugName (_rename_intermediate_constant (value .debugName ()))
465
+ value .setDebugName (_rename_intermediate_value (value .debugName ()))
462
466
return value
463
467
464
468
if isinstance (constant , bool ):
@@ -486,7 +490,7 @@ def _add_constant_to_graph(self, constant) -> torch.Value:
486
490
inputs = (),
487
491
attributes = dict (value = constant_tensor ),
488
492
)[0 ]
489
- value .setDebugName (_rename_intermediate_constant (value .debugName ()))
493
+ value .setDebugName (_rename_intermediate_value (value .debugName ()))
490
494
return value
491
495
492
496
@runtime_typing .checked
You can’t perform that action at this time.
0 commit comments