Skip to content

MAINT: handle not implemented arguments centrally, via a dedicated annotation #114

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 4 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
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
65 changes: 14 additions & 51 deletions torch_np/_detail/_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
Anything here only deals with torch objects, e.g. "dtype" is a torch.dtype instance etc
"""

import functools
import typing

import torch

from . import _dtypes_impl, _util

NoValue = _util.NoValue


import functools

############# XXX
### From _util.axis_expand_func

Expand Down Expand Up @@ -51,7 +47,7 @@ def wrapped(tensor, axis, *args, **kwds):

def emulate_keepdims(func):
@functools.wraps(func)
def wrapped(tensor, axis=None, keepdims=NoValue, *args, **kwds):
def wrapped(tensor, axis=None, keepdims=None, *args, **kwds):
result = func(tensor, axis=axis, *args, **kwds)
if keepdims:
result = _util.apply_keepdims(result, axis, tensor.ndim)
Expand Down Expand Up @@ -133,10 +129,7 @@ def argmin(tensor, axis=None):

@emulate_keepdims
@deco_axis_expand
def any(tensor, axis=None, *, where=NoValue):
if where is not NoValue:
raise NotImplementedError

def any(tensor, axis=None, *, where=None):
axis = _util.allow_only_single_axis(axis)

if axis is None:
Expand All @@ -148,10 +141,7 @@ def any(tensor, axis=None, *, where=NoValue):

@emulate_keepdims
@deco_axis_expand
def all(tensor, axis=None, *, where=NoValue):
if where is not NoValue:
raise NotImplementedError

def all(tensor, axis=None, *, where=None):
axis = _util.allow_only_single_axis(axis)

if axis is None:
Expand All @@ -163,37 +153,25 @@ def all(tensor, axis=None, *, where=NoValue):

@emulate_keepdims
@deco_axis_expand
def max(tensor, axis=None, initial=NoValue, where=NoValue):
if initial is not NoValue or where is not NoValue:
raise NotImplementedError

result = tensor.amax(axis)
return result
def max(tensor, axis=None, initial=None, where=None):
return tensor.amax(axis)


@emulate_keepdims
@deco_axis_expand
def min(tensor, axis=None, initial=NoValue, where=NoValue):
if initial is not NoValue or where is not NoValue:
raise NotImplementedError

result = tensor.amin(axis)
return result
def min(tensor, axis=None, initial=None, where=None):
return tensor.amin(axis)


@emulate_keepdims
@deco_axis_expand
def ptp(tensor, axis=None):
result = tensor.amax(axis) - tensor.amin(axis)
return result
return tensor.amax(axis) - tensor.amin(axis)


@emulate_keepdims
@deco_axis_expand
def sum(tensor, axis=None, dtype=None, initial=NoValue, where=NoValue):
if initial is not NoValue or where is not NoValue:
raise NotImplementedError

def sum(tensor, axis=None, dtype=None, initial=None, where=None):
assert dtype is None or isinstance(dtype, torch.dtype)

if dtype == torch.bool:
Expand All @@ -209,10 +187,7 @@ def sum(tensor, axis=None, dtype=None, initial=NoValue, where=NoValue):

@emulate_keepdims
@deco_axis_expand
def prod(tensor, axis=None, dtype=None, initial=NoValue, where=NoValue):
if initial is not NoValue or where is not NoValue:
raise NotImplementedError

def prod(tensor, axis=None, dtype=None, initial=None, where=None):
axis = _util.allow_only_single_axis(axis)

if dtype == torch.bool:
Expand All @@ -228,10 +203,7 @@ def prod(tensor, axis=None, dtype=None, initial=NoValue, where=NoValue):

@emulate_keepdims
@deco_axis_expand
def mean(tensor, axis=None, dtype=None, *, where=NoValue):
if where is not NoValue:
raise NotImplementedError

def mean(tensor, axis=None, dtype=None, *, where=None):
dtype = _atleast_float(dtype, tensor.dtype)

is_half = dtype == torch.float16
Expand All @@ -252,10 +224,7 @@ def mean(tensor, axis=None, dtype=None, *, where=NoValue):

@emulate_keepdims
@deco_axis_expand
def std(tensor, axis=None, dtype=None, ddof=0, *, where=NoValue):
if where is not NoValue:
raise NotImplementedError

def std(tensor, axis=None, dtype=None, ddof=0, *, where=None):
dtype = _atleast_float(dtype, tensor.dtype)
tensor = _util.cast_if_needed(tensor, dtype)
result = tensor.std(dim=axis, correction=ddof)
Expand All @@ -265,10 +234,7 @@ def std(tensor, axis=None, dtype=None, ddof=0, *, where=NoValue):

@emulate_keepdims
@deco_axis_expand
def var(tensor, axis=None, dtype=None, ddof=0, *, where=NoValue):
if where is not NoValue:
raise NotImplementedError

def var(tensor, axis=None, dtype=None, ddof=0, *, where=None):
dtype = _atleast_float(dtype, tensor.dtype)
tensor = _util.cast_if_needed(tensor, dtype)
result = tensor.var(dim=axis, correction=ddof)
Expand Down Expand Up @@ -387,9 +353,6 @@ def quantile(
# Here we choose to work out-of-place because why not.
pass

if interpolation is not None:
raise ValueError("'interpolation' argument is deprecated; use 'method' instead")

if not a.dtype.is_floating_point:
dtype = _dtypes_impl.default_float_dtype
a = a.to(dtype)
Expand Down
1 change: 0 additions & 1 deletion torch_np/_detail/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from . import _dtypes_impl

NoValue = None

# https://github.com/numpy/numpy/blob/v1.23.0/numpy/distutils/misc_util.py#L497-L504
def is_sequence(seq):
Expand Down
Loading