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

feat: added torch.all #355

Merged
merged 17 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
63 changes: 63 additions & 0 deletions thunder/tests/opinfos.py
Original file line number Diff line number Diff line change
Expand Up @@ -4641,6 +4641,69 @@ def unsqueeze_sample_generator(op, device, dtype, requires_grad, **kwargs):
reduction_ops = []


def all_tensor_sample_generator(op, device, dtype, requires_grad, **kwargs):
make = partial(make_tensor, device=device, dtype=dtype, requires_grad=requires_grad)

# input shape, dim, keepdim
dim_cases = (
((4, 4), None, False),
((4, 4), None, True),
((2, 3), 0, True),
((2, 3, 4), (1, 2), False),
((2, 3, 4), (1, 2), True),
((2, 3, 4), (-1, 1), False),
((2, 3, 4), (-1, 1), True),
)

for input_shape, dim, keepdim in dim_cases:
yield SampleInput(make(input_shape), dim, keepdim)


def all_tensor_error_generator(op, device, dtype=torch.float32, **kwargs):
make = partial(make_tensor, device=device, dtype=dtype)
err_msg = r"Dimension out of range \(expected to be in range of \[.*?\], but got .*\)"
yield (
SampleInput(make(5, 1, 2, 3), 4),
IndexError,
err_msg,
)


all_tensor_opinfo = OpInfo(
ltorch.all_tensor,
sample_input_generator=all_tensor_sample_generator,
error_input_generator=all_tensor_error_generator,
torch_reference=torch.all,
)

reduction_ops.append(all_tensor_opinfo)


def any_tensor_sample_generator(op, device, dtype, requires_grad, **kwargs):
make = partial(make_tensor, device=device, dtype=dtype, requires_grad=requires_grad)

# input shape, dim, keepdim
dim_cases = (
((4, 4), None, False),
((4, 4), None, True),
((2, 3), 0, True),
((2, 3, 4), (1, 2), False),
((2, 3, 4), (1, 2), True),
((2, 3, 4), (-1, 1), False),
((2, 3, 4), (-1, 1), True),
)

for input_shape, dim, keepdim in dim_cases:
yield SampleInput(make(input_shape), dim, keepdim)


any_tensor_opinfo = OpInfo(
k223kim marked this conversation as resolved.
Show resolved Hide resolved
ltorch.any_tensor,
sample_input_generator=any_tensor_sample_generator,
k223kim marked this conversation as resolved.
Show resolved Hide resolved
torch_reference=torch.any,
)
k223kim marked this conversation as resolved.
Show resolved Hide resolved


# TODO: increase reduction samples and refacort amax and sum generators
def amax_amin_sample_generator(op, device, dtype, requires_grad, **kwargs):
# For grad test stability it's better to use wider range of values
Expand Down
26 changes: 26 additions & 0 deletions thunder/torch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1786,6 +1786,32 @@ def _reduction(
return result


@torchsymbol(torch.all, is_method=True, id="torch.all")
def all_tensor(
k223kim marked this conversation as resolved.
Show resolved Hide resolved
k223kim marked this conversation as resolved.
Show resolved Hide resolved
a: TensorLike, /, dim: None | int | Sequence[int] = None, keepdim: bool = False, *, out: None | TensorLike = None
) -> TensorLike | None:
k223kim marked this conversation as resolved.
Show resolved Hide resolved
utils.check(out is None, lambda: "out is not None which is currently unsupported", NotImplementedError)
result = logical_not(a)
result = logical_not(any_tensor(logical_not(a), dim=dim, keepdim=keepdim))

if a.dtype is dtypes.uint8:
k223kim marked this conversation as resolved.
Show resolved Hide resolved
result = to(result, dtype=dtypes.uint8)
return result


@torchsymbol(torch.any, is_method=True, id="torch.any")
def any_tensor(a: TensorLike, /, dim: None | int | Sequence[int] = None, keepdim: bool = False):
k223kim marked this conversation as resolved.
Show resolved Hide resolved
a_ = clang.maybe_convert_to_dtype(a, dtypes.bool8)
if isinstance(dim, Sequence) and len(dim) == 0:
# PyTorch returns a_.clone()
result = a_ | a_
k223kim marked this conversation as resolved.
Show resolved Hide resolved
else:
result = ne(sum(a_, dim=dim, keepdim=keepdim), False)
if a.dtype is dtypes.uint8:
k223kim marked this conversation as resolved.
Show resolved Hide resolved
return prims.convert_element_type(result, dtypes.uint8)
return result


@torchsymbol(torch.amax, is_method=True)
def amax(a, /, dim=None, keepdim: bool = False):
return _reduction(
Expand Down
Loading