-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create the Rank and IsScalar shared functions | feat(torchlib) (#1105)
This change introduces two shared operators `Rank` and `IsScalar`. They are used to replace the `Size(Shape())` pattern for code reuse and readability. I used a hack to always include these shared functions in the model proto because without #834 we cannot dynamically add these functions to the model as they are used. I added a TODO for this. The first usage is in `aten_all`. I will update the rest of the functions in a separate PR. #1095
- Loading branch information
1 parent
0035390
commit 9fb0a7d
Showing
6 changed files
with
54 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"""Shared constants for the library.""" | ||
|
||
DOMAIN = "pkg.onnxscript.torch_lib" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""Common operators shared in the torchlib library.""" | ||
|
||
import onnxscript | ||
import onnxscript.values | ||
from onnxscript import BOOL, INT64 | ||
from onnxscript import opset18 as op | ||
from onnxscript.function_libs.torch_lib import _constants, tensor_typing | ||
|
||
DOMAIN = f"{_constants.DOMAIN}.common" | ||
|
||
common_opset = onnxscript.values.Opset(domain=DOMAIN, version=1) | ||
|
||
|
||
@onnxscript.script(common_opset) | ||
def Rank(input: tensor_typing.TTensor) -> INT64: | ||
"""Take the rank of the input tensor.""" | ||
|
||
return op.Size(op.Shape(input)) | ||
|
||
|
||
@onnxscript.script(common_opset) | ||
def IsScalar(input: tensor_typing.TTensor) -> BOOL: | ||
"""Return whether the input has rank 0, or is a scalar.""" | ||
|
||
return op.Equal(op.Size(op.Shape(input)), op.Constant(value_int=0)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters