Skip to content

feat(atenlib):add ops(isnan,issamesize,isneginf,isposinf,isnonzero) #530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions onnxscript/function_libs/torch_aten/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2850,10 +2850,16 @@ def aten_is_neg(self: TensorType) -> bool:
raise NotImplementedError()


def aten_is_nonzero(self: TensorType) -> bool:
@torch_op("aten::is_nonzero")
def aten_is_nonzero(self: Union[TReal, BOOL]) -> BOOL:
"""is_nonzero(Tensor self) -> bool"""

raise NotImplementedError()
# if size != 1, return False
# else [0],[True],[0.0] return True, others return False
result = op.Not(op.Size(self) != 1)
if result:
result = op.Cast(self, to=BOOL.dtype)
return result


def aten_is_pinned(self: TensorType, device: Optional[str] = None) -> bool:
Expand All @@ -2862,10 +2868,23 @@ def aten_is_pinned(self: TensorType, device: Optional[str] = None) -> bool:
raise NotImplementedError()


def aten_is_same_size(self: TensorType, other: TensorType) -> bool:
@torch_op("aten::is_same_size")
def aten_is_same_size(self: TTensor, other: TTensor) -> BOOL:
"""is_same_size(Tensor self, Tensor other) -> bool"""

raise NotImplementedError()
# shape should be the same, but different shape cannot be use op.Equal()
# so need compare the rank first, if rank is same, then compare shape
self_rank = op.Size(op.Shape(self))
other_rank = op.Size(op.Shape(other))
result = op.Equal(self_rank, other_rank)
if result:
self_shape = op.Shape(self)
other_shape = op.Shape(other)
result_bool = op.Equal(self_shape, other_shape)
result_int = op.Cast(result_bool, to=INT8.dtype)
result = op.Cast(op.ReduceMin(result_int, keepdims=0), to=BOOL.dtype)

return result


def aten_is_set_to(self: TensorType, tensor: TensorType) -> bool:
Expand Down Expand Up @@ -2905,7 +2924,7 @@ def aten_isclose(


@torch_op("aten::isfinite")
def aten_isfinite(self: TensorType) -> TensorType:
def aten_isfinite(self: TFloatOrBFloat16) -> BOOL:
"""isfinite(Tensor self) -> Tensor"""

not_inf = op.Not(op.IsInf(self))
Expand All @@ -2920,22 +2939,25 @@ def aten_isinf(self: Union[FLOAT, DOUBLE]) -> BOOL:
return op.IsInf(self)


def aten_isnan(self: TensorType) -> TensorType:
@torch_op("aten::isnan")
def aten_isnan(self: TFloatOrBFloat16) -> BOOL:
"""isnan(Tensor self) -> Tensor"""

raise NotImplementedError()
return op.IsNaN(self)


def aten_isneginf(self: TensorType) -> TensorType:
@torch_op("aten::isneginf")
def aten_isneginf(self: TReal) -> BOOL:
"""isneginf(Tensor self) -> Tensor"""

raise NotImplementedError()
return op.And(op.Less(self, 0), op.IsInf(self))


def aten_isposinf(self: TensorType) -> TensorType:
@torch_op("aten::isposinf")
def aten_isposinf(self: TReal) -> BOOL:
"""isposinf(Tensor self) -> Tensor"""

raise NotImplementedError()
return op.And(op.Greater(self, 0), op.IsInf(self))


def aten_isreal(self: TensorType) -> TensorType:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,14 @@ def _where_input_wrangler(
"full_like": core_ops.aten_full_like,
"ge": core_ops.aten_ge,
"gt": core_ops.aten_gt,
# "is_same_size": core_ops.aten_is_same_size, # no test case in OPS_DB
# "is_nonzero": core_ops.aten_is_nonzero, # no test case in OPS_DB
"isclose": core_ops.aten_isclose,
"isfinite": core_ops.aten_isfinite,
"isinf": core_ops.aten_isinf,
"isnan": core_ops.aten_isnan,
"isneginf": core_ops.aten_isneginf,
"isposinf": core_ops.aten_isposinf,
"log": core_ops.aten_log,
"le": core_ops.aten_le,
"log10": core_ops.aten_log10,
Expand Down