From 980999e64f3df3c602c5972548bad67bc170f3cb Mon Sep 17 00:00:00 2001 From: Evgeni Burovski Date: Sat, 11 Mar 2023 11:38:39 +0300 Subject: [PATCH 1/2] API: remove ndarray.base Base is tracked via `self._tensor._base`. Use `a.get()._base is b.get()` instead of numpy's `a.base is b`. --- torch_np/_dtypes.py | 1 - torch_np/_helpers.py | 1 - torch_np/_ndarray.py | 3 +++ 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/torch_np/_dtypes.py b/torch_np/_dtypes.py index 09a3dfcc..4b6e5732 100644 --- a/torch_np/_dtypes.py +++ b/torch_np/_dtypes.py @@ -51,7 +51,6 @@ def __new__(self, value): # return _ndarray.ndarray(tensor) - ##### these are abstract types diff --git a/torch_np/_helpers.py b/torch_np/_helpers.py index 788a748b..1082e512 100644 --- a/torch_np/_helpers.py +++ b/torch_np/_helpers.py @@ -86,7 +86,6 @@ def result_or_out(result_tensor, out_array=None, promote_scalar=False): def array_from(tensor, base=None): from ._ndarray import ndarray - return ndarray(tensor) diff --git a/torch_np/_ndarray.py b/torch_np/_ndarray.py index 0bb8d265..3ff63f1e 100644 --- a/torch_np/_ndarray.py +++ b/torch_np/_ndarray.py @@ -93,6 +93,7 @@ def strides(self): def itemsize(self): return self.tensor.element_size() + @property def flags(self): # Note contiguous in torch is assumed C-style @@ -423,6 +424,7 @@ def array(obj, dtype=None, *, copy=True, order="K", subok=False, ndmin=0, like=N if isinstance(obj, ndarray): obj = obj.tensor + # is a specific dtype requrested? torch_dtype = None if dtype is not None: @@ -432,6 +434,7 @@ def array(obj, dtype=None, *, copy=True, order="K", subok=False, ndmin=0, like=N return ndarray(tensor) + def asarray(a, dtype=None, order=None, *, like=None): if order is None: order = "K" From d923fc8595c398d24cac6fb14d5f1a9d47e6d642 Mon Sep 17 00:00:00 2001 From: Evgeni Burovski Date: Sat, 11 Mar 2023 12:36:28 +0300 Subject: [PATCH 2/2] MAINT: make _detail a package, hide individual implementation files and import from _detail --- torch_np/_detail/__init__.py | 6 ++++ torch_np/_detail/_reductions.py | 2 +- torch_np/_detail/_util.py | 1 + torch_np/_dtypes.py | 1 + torch_np/_funcs.py | 51 ++++++++++++++------------------- torch_np/_helpers.py | 1 + torch_np/_ndarray.py | 5 +--- torch_np/_wrapper.py | 24 +++++++--------- 8 files changed, 44 insertions(+), 47 deletions(-) diff --git a/torch_np/_detail/__init__.py b/torch_np/_detail/__init__.py index e69de29b..dc5f022b 100644 --- a/torch_np/_detail/__init__.py +++ b/torch_np/_detail/__init__.py @@ -0,0 +1,6 @@ +from ._flips import * +from ._reductions import * + +# leading underscore (ndarray.flatten yes, np.flatten no) +from .implementations import * +from .implementations import _flatten diff --git a/torch_np/_detail/_reductions.py b/torch_np/_detail/_reductions.py index 549f20e0..429b6c15 100644 --- a/torch_np/_detail/_reductions.py +++ b/torch_np/_detail/_reductions.py @@ -10,7 +10,7 @@ from . import _dtypes_impl, _util -NoValue = None +NoValue = _util.NoValue import functools diff --git a/torch_np/_detail/_util.py b/torch_np/_detail/_util.py index 47d4ae40..4845fe3e 100644 --- a/torch_np/_detail/_util.py +++ b/torch_np/_detail/_util.py @@ -7,6 +7,7 @@ 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): diff --git a/torch_np/_dtypes.py b/torch_np/_dtypes.py index 4b6e5732..09a3dfcc 100644 --- a/torch_np/_dtypes.py +++ b/torch_np/_dtypes.py @@ -51,6 +51,7 @@ def __new__(self, value): # return _ndarray.ndarray(tensor) + ##### these are abstract types diff --git a/torch_np/_funcs.py b/torch_np/_funcs.py index 59dd594f..ff693e93 100644 --- a/torch_np/_funcs.py +++ b/torch_np/_funcs.py @@ -2,9 +2,9 @@ import torch +from . import _detail as _impl from . import _helpers -from ._detail import _flips, _reductions, _util -from ._detail import implementations as _impl +from ._detail import _util from ._normalizations import ( ArrayLike, AxisLike, @@ -14,6 +14,8 @@ normalizer, ) +NoValue = _util.NoValue + @normalizer def nonzero(a: ArrayLike): @@ -158,13 +160,13 @@ def moveaxis(a: ArrayLike, source, destination): @normalizer def swapaxes(a: ArrayLike, axis1, axis2): - result = _flips.swapaxes(a, axis1, axis2) + result = _impl.swapaxes(a, axis1, axis2) return _helpers.array_from(result) @normalizer def rollaxis(a: ArrayLike, axis, start=0): - result = _flips.rollaxis(a, axis, start) + result = _impl.rollaxis(a, axis, start) return _helpers.array_from(result) @@ -230,9 +232,6 @@ def round_(a: ArrayLike, decimals=0, out: Optional[NDArray] = None): # ### reductions ### -NoValue = None # FIXME - - @normalizer def sum( a: ArrayLike, @@ -243,7 +242,7 @@ def sum( initial=NoValue, where=NoValue, ): - result = _reductions.sum( + result = _impl.sum( a, axis=axis, dtype=dtype, initial=initial, where=where, keepdims=keepdims ) return _helpers.result_or_out(result, out) @@ -259,7 +258,7 @@ def prod( initial=NoValue, where=NoValue, ): - result = _reductions.prod( + result = _impl.prod( a, axis=axis, dtype=dtype, initial=initial, where=where, keepdims=keepdims ) return _helpers.result_or_out(result, out) @@ -278,9 +277,7 @@ def mean( *, where=NoValue, ): - result = _reductions.mean( - a, axis=axis, dtype=dtype, where=NoValue, keepdims=keepdims - ) + result = _impl.mean(a, axis=axis, dtype=dtype, where=NoValue, keepdims=keepdims) return _helpers.result_or_out(result, out) @@ -295,7 +292,7 @@ def var( *, where=NoValue, ): - result = _reductions.var( + result = _impl.var( a, axis=axis, dtype=dtype, ddof=ddof, where=where, keepdims=keepdims ) return _helpers.result_or_out(result, out) @@ -312,7 +309,7 @@ def std( *, where=NoValue, ): - result = _reductions.std( + result = _impl.std( a, axis=axis, dtype=dtype, ddof=ddof, where=where, keepdims=keepdims ) return _helpers.result_or_out(result, out) @@ -326,7 +323,7 @@ def argmin( *, keepdims=NoValue, ): - result = _reductions.argmin(a, axis=axis, keepdims=keepdims) + result = _impl.argmin(a, axis=axis, keepdims=keepdims) return _helpers.result_or_out(result, out) @@ -338,7 +335,7 @@ def argmax( *, keepdims=NoValue, ): - result = _reductions.argmax(a, axis=axis, keepdims=keepdims) + result = _impl.argmax(a, axis=axis, keepdims=keepdims) return _helpers.result_or_out(result, out) @@ -351,9 +348,7 @@ def amax( initial=NoValue, where=NoValue, ): - result = _reductions.max( - a, axis=axis, initial=initial, where=where, keepdims=keepdims - ) + result = _impl.max(a, axis=axis, initial=initial, where=where, keepdims=keepdims) return _helpers.result_or_out(result, out) @@ -369,9 +364,7 @@ def amin( initial=NoValue, where=NoValue, ): - result = _reductions.min( - a, axis=axis, initial=initial, where=where, keepdims=keepdims - ) + result = _impl.min(a, axis=axis, initial=initial, where=where, keepdims=keepdims) return _helpers.result_or_out(result, out) @@ -382,7 +375,7 @@ def amin( def ptp( a: ArrayLike, axis: AxisLike = None, out: Optional[NDArray] = None, keepdims=NoValue ): - result = _reductions.ptp(a, axis=axis, keepdims=keepdims) + result = _impl.ptp(a, axis=axis, keepdims=keepdims) return _helpers.result_or_out(result, out) @@ -395,7 +388,7 @@ def all( *, where=NoValue, ): - result = _reductions.all(a, axis=axis, where=where, keepdims=keepdims) + result = _impl.all(a, axis=axis, where=where, keepdims=keepdims) return _helpers.result_or_out(result, out) @@ -408,13 +401,13 @@ def any( *, where=NoValue, ): - result = _reductions.any(a, axis=axis, where=where, keepdims=keepdims) + result = _impl.any(a, axis=axis, where=where, keepdims=keepdims) return _helpers.result_or_out(result, out) @normalizer def count_nonzero(a: ArrayLike, axis: AxisLike = None, *, keepdims=False): - result = _reductions.count_nonzero(a, axis=axis, keepdims=keepdims) + result = _impl.count_nonzero(a, axis=axis, keepdims=keepdims) return _helpers.array_from(result) @@ -425,7 +418,7 @@ def cumsum( dtype: DTypeLike = None, out: Optional[NDArray] = None, ): - result = _reductions.cumsum(a, axis=axis, dtype=dtype) + result = _impl.cumsum(a, axis=axis, dtype=dtype) return _helpers.result_or_out(result, out) @@ -436,7 +429,7 @@ def cumprod( dtype: DTypeLike = None, out: Optional[NDArray] = None, ): - result = _reductions.cumprod(a, axis=axis, dtype=dtype) + result = _impl.cumprod(a, axis=axis, dtype=dtype) return _helpers.result_or_out(result, out) @@ -458,5 +451,5 @@ def quantile( if interpolation is not None: raise ValueError("'interpolation' argument is deprecated; use 'method' instead") - result = _reductions.quantile(a, q, axis, method=method, keepdims=keepdims) + result = _impl.quantile(a, q, axis, method=method, keepdims=keepdims) return _helpers.result_or_out(result, out, promote_scalar=True) diff --git a/torch_np/_helpers.py b/torch_np/_helpers.py index 1082e512..788a748b 100644 --- a/torch_np/_helpers.py +++ b/torch_np/_helpers.py @@ -86,6 +86,7 @@ def result_or_out(result_tensor, out_array=None, promote_scalar=False): def array_from(tensor, base=None): from ._ndarray import ndarray + return ndarray(tensor) diff --git a/torch_np/_ndarray.py b/torch_np/_ndarray.py index 3ff63f1e..f01f9c20 100644 --- a/torch_np/_ndarray.py +++ b/torch_np/_ndarray.py @@ -3,7 +3,7 @@ import torch from . import _binary_ufuncs, _dtypes, _funcs, _helpers, _unary_ufuncs -from ._detail import _dtypes_impl, _flips, _reductions, _util +from ._detail import _dtypes_impl, _util from ._detail import implementations as _impl newaxis = None @@ -93,7 +93,6 @@ def strides(self): def itemsize(self): return self.tensor.element_size() - @property def flags(self): # Note contiguous in torch is assumed C-style @@ -424,7 +423,6 @@ def array(obj, dtype=None, *, copy=True, order="K", subok=False, ndmin=0, like=N if isinstance(obj, ndarray): obj = obj.tensor - # is a specific dtype requrested? torch_dtype = None if dtype is not None: @@ -434,7 +432,6 @@ def array(obj, dtype=None, *, copy=True, order="K", subok=False, ndmin=0, like=N return ndarray(tensor) - def asarray(a, dtype=None, order=None, *, like=None): if order is None: order = "K" diff --git a/torch_np/_wrapper.py b/torch_np/_wrapper.py index 2b7d1abf..dbd536a2 100644 --- a/torch_np/_wrapper.py +++ b/torch_np/_wrapper.py @@ -8,12 +8,15 @@ import torch -from . import _funcs, _helpers -from ._detail import _dtypes_impl, _flips, _reductions, _util -from ._detail import implementations as _impl +from . import _decorators +from . import _detail as _impl +from . import _dtypes, _funcs, _helpers +from ._detail import _dtypes_impl, _util from ._ndarray import asarray from ._normalizations import ArrayLike, DTypeLike, NDArray, SubokLike, normalizer +NoValue = _util.NoValue + # Things to decide on (punt for now) # # 1. Q: What are the return types of wrapper functions: plain torch.Tensors or @@ -49,9 +52,6 @@ # - optional out arg -NoValue = None - - ###### array creation routines @@ -492,25 +492,25 @@ def expand_dims(a: ArrayLike, axis): @normalizer def flip(m: ArrayLike, axis=None): - result = _flips.flip(m, axis) + result = _impl.flip(m, axis) return _helpers.array_from(result) @normalizer def flipud(m: ArrayLike): - result = _flips.flipud(m) + result = _impl.flipud(m) return _helpers.array_from(result) @normalizer def fliplr(m: ArrayLike): - result = _flips.fliplr(m) + result = _impl.fliplr(m) return _helpers.array_from(result) @normalizer def rot90(m: ArrayLike, k=1, axes=(0, 1)): - result = _flips.rot90(m, k, axes) + result = _impl.rot90(m, k, axes) return _helpers.array_from(result) @@ -631,9 +631,7 @@ def average( *, keepdims=NoValue, ): - result, wsum = _reductions.average( - a, axis, weights, returned=returned, keepdims=keepdims - ) + result, wsum = _impl.average(a, axis, weights, returned=returned, keepdims=keepdims) if returned: return _helpers.tuple_arrays_from((result, wsum)) else: