-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into xiaowu/fixBug(embedding_bag)
- Loading branch information
Showing
33 changed files
with
1,144 additions
and
279 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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Experimental flags. | ||
NOTE: These flags are experimental only. Any flag here can be removed at any | ||
time without notice. | ||
""" | ||
|
||
import logging | ||
import os | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def _load_boolean_flag( | ||
name: str, | ||
*, | ||
this_will: str, | ||
deprecated: bool = False, | ||
) -> bool: | ||
"""Load a boolean flag from environment variable. | ||
Args: | ||
name: The name of the environment variable. | ||
this_will: A string that describes what this flag will do. | ||
deprecated: Whether this flag is deprecated. | ||
""" | ||
state = os.getenv(name) == "1" | ||
if state: | ||
if deprecated: | ||
logger.error( | ||
"Experimental flag %s is deprecated. Please remove it from your environment.", | ||
name, | ||
) | ||
else: | ||
logger.warning("Experimental flag %s is enabled. This will %s.", name, this_will) | ||
return state | ||
|
||
|
||
EXPERIMENTAL_INITIALIZERS_AS_INPUTS: bool = _load_boolean_flag( | ||
"TORCHLIB_EXPERIMENTAL_INITIALIZERS_AS_INPUTS", | ||
this_will="make initializers as inputs to the model graph", | ||
) |
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
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,56 @@ | ||
"""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 | ||
from onnxscript.function_libs.torch_lib.tensor_typing import RealType | ||
from onnxscript.onnx_types import COMPLEX64, COMPLEX128, DOUBLE, FLOAT | ||
|
||
COMPLEX64_TYPE = COMPLEX64.dtype | ||
COMPLEX128_TYPE = COMPLEX128.dtype | ||
|
||
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)) | ||
|
||
|
||
def cast_to(a: RealType, dtype: int) -> RealType: | ||
"""Cast input to dtype while handling complex types.""" | ||
|
||
# Traced function because different if branches return different dtypes | ||
# which is not supported in an ONNX function | ||
if dtype == COMPLEX128_TYPE: | ||
# Cast to the real representation of the complex type | ||
casted = op.Cast(a, to=DOUBLE.dtype) | ||
# Create a complex number | ||
real_part = op.Unsqueeze(casted, axes=[-1]) | ||
imag_part = op.Expand(op.Cast(0.0, to=DOUBLE.dtype), op.Shape(real_part)) | ||
result = op.Concat(real_part, imag_part, axis=-1) | ||
elif dtype == COMPLEX64_TYPE: | ||
# Cast to the real representation of the complex type | ||
casted = op.Cast(a, to=FLOAT.dtype) | ||
# Create a complex number | ||
real_part = op.Unsqueeze(casted, axes=[-1]) | ||
imag_part = op.Expand(0.0, op.Shape(real_part)) | ||
result = op.Concat(real_part, imag_part, axis=-1) | ||
else: | ||
# Cast to real numbers | ||
result = op.Cast(a, to=dtype) | ||
|
||
return result |
Oops, something went wrong.