Skip to content
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

Implement aten::{all,any}.dims | feat(torchlib) #1084

Merged
merged 8 commits into from
Oct 13, 2023
Merged
Changes from all 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
56 changes: 56 additions & 0 deletions onnxscript/function_libs/torch_lib/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,34 @@ def aten_all_dim(self: TTensor, dim: int, keepdim: bool = False) -> BOOL:
return result


@torch_op("aten::all.dims", trace_only=True)
def aten_all_dims(self: TTensor, dim: Sequence[int] = (), keepdim: bool = False) -> BOOL:
"""all.dims(Tensor self, int[]? dim=None, bool keepdim=False) -> Tensor"""

if not dim:
return aten_all_dims_no_dim(self, keepdim)
for d in dim:
self = aten_all_dim(self, d, keepdim)
return self


@torch_op("aten::all.dims")
def aten_all_dims_no_dim(self: TTensor, keepdims: bool) -> BOOL:
"""all.dims(Tensor self, int[]? dim=None, bool keepdim=False) -> Tensor"""

# dim is None and thus not supplied

self_rank = op.Size(op.Shape(self))
if self_rank == 0:
result = op.Cast(self, to=BOOL.dtype)
else:
self_bool = op.Cast(self, to=BOOL.dtype)
self_int = op.Cast(self_bool, to=INT64.dtype)
all_true = op.ReduceMin(self_int, keepdims=keepdims)
result = op.Cast(all_true, to=BOOL.dtype)
return result


@torch_op("aten::allclose")
def aten_allclose(
self: TReal,
Expand Down Expand Up @@ -445,6 +473,34 @@ def aten_any_dim(self: TTensor, dim: int, keepdim: bool = False) -> BOOL:
return result


@torch_op("aten::any.dims", trace_only=True)
def aten_any_dims(self: TTensor, dim: Sequence[int] = (), keepdim: bool = False) -> BOOL:
"""any.dims(Tensor self, int[1]? dim=None, bool keepdim=False) -> Tensor"""

if not dim:
return aten_any_dims_no_dim(self, keepdim)
for d in dim:
self = aten_any_dim(self, d, keepdim)
return self


@torch_op("aten::any.dims")
def aten_any_dims_no_dim(self: TTensor, keepdims: bool) -> BOOL:
"""any.dims(Tensor self, int[1]? dim=None, bool keepdim=False) -> Tensor"""

# dim is None and thus not supplied

self_rank = op.Size(op.Shape(self))
if self_rank == 0:
result = op.Cast(self, to=BOOL.dtype)
else:
self_bool = op.Cast(self, to=BOOL.dtype)
self_int = op.Cast(self_bool, to=INT64.dtype)
any_true = op.ReduceMax(self_int, keepdims=keepdims)
result = op.Cast(any_true, to=BOOL.dtype)
return result


def _range_supported(dtype: int) -> bool:
"""Returns true if the dtype is supported by the ONNX Range op."""
return dtype in {
Expand Down