Skip to content
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
57 changes: 55 additions & 2 deletions python/numpy2x/_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,56 @@ def asarray(obj, dtype=None, order=None, *, copy=None, like=None, **kwargs):
return np.asarray(obj, dtype, order, copy=copy, like=like, **kwargs)
if isinstance(obj, ndarray) and (dtype is None or obj.dtype == np.dtype(dtype)) and not copy:
return obj
if (type(obj) is np.ndarray and order in (None, "K", "A") and copy is not True
and (dtype is None or obj.dtype == np.dtype(dtype))):
r = _import_strided(obj)
if r is not None:
return r
return array(obj, dtype, copy=True, order=order or "K")


def _c_strides(a):
st, step = [], a.itemsize
for n in reversed(a.shape):
st.append(step)
step *= n
return tuple(reversed(st))


def _import_strided(obj):
"""asarray of an ndarray: NumPy returns the array itself, numpy2x a copy
with its strides where they matter (NumPy's loops take other paths on
other strides, and their results can differ in the last bit; later
results take their layouts from them): an empty array keeps them all
(no memory), a dense one (any axis order, also reversed axes and
extent-1 axes of any stride) all of them. A strided one (a stepped
view) is copied densely, its gaps closed, keeping reversed axes
reversed. None: the plain copy of array() has the same strides."""
if obj.dtype not in _CODES:
return None
st = obj.strides
if obj.size == 0:
r = np.ndarray(obj.shape, obj.dtype, np.empty(0, obj.dtype), strides=st)
return _adopt(r)
if obj.flags.c_contiguous and st == _c_strides(obj):
return None # the common case: array() copies it as it is
lo = hi = 0
for n, s in zip(obj.shape, st):
if s < 0:
lo += (n - 1) * s
else:
hi += (n - 1) * s
if hi - lo + obj.itemsize == obj.nbytes: # dense: the same strides
buf = np.empty(obj.nbytes, np.uint8)
r = np.ndarray(obj.shape, obj.dtype, buf, offset=-lo, strides=st)
r[...] = obj
return _adopt(r)
if not any(s < 0 for s in st):
return None
flip = tuple(slice(None, None, -1) if s < 0 else slice(None) for s in st)
return _adopt(obj[flip].copy(order="K")[flip])


def fresh(r, args=(), kwargs=None):
"""Result `r` of a NumPy function as numpy2x array: fresh memory is adopted
without a copy, a view of other memory is copied first. Returned as is:
Expand Down Expand Up @@ -433,12 +480,18 @@ def handler(a, axis=None, out=None, keepdims=_NV, *, where=_NV):


_INTP = np.intp
_WIDE = (np.dtype(np.float64), np.dtype(np.complex128))


def _divide_by_count(r, n):
"""NumPy's true_divide(r, intp(n), out=r, casting='unsafe') (and, for
0-d results, r.dtype.type(r / n)): divided in the promoted dtype of r
and intp (float64, complex128), cast back to r's dtype."""
and intp (float64, complex128), cast back to r's dtype. In place, as
NumPy: the result keeps r's layout (a reduction's extent-1 axes)."""
if r.ndim:
if r.dtype in _WIDE: # the division's own dtype: no cast
return np.true_divide(r, _INTP(n), out=r)
return np.true_divide(r, _INTP(n), out=r, casting="unsafe")
q = np.true_divide(r, _INTP(n))
return q if q.dtype == r.dtype else q.astype(r.dtype)

Expand Down Expand Up @@ -1082,7 +1135,7 @@ def _gather_fast(a, key):
return None
idx = asarray(idx if idx.dtype in _INDEX_DTYPES else idx.astype(np.intp))
if (idx.dtype not in _INDEX_DTYPES or not a.ndim or not a.size or not idx.size
or not a._c_contiguous):
or not a._c_ordered): # extent-1 axes too: NumPy orders by their strides
return None
nb = idx.ndim
res = list(range(p)) + [-1 - d for d in range(nb)] + list(range(p + 1, a.ndim))
Expand Down
81 changes: 60 additions & 21 deletions python/numpy2x/_numpy2x.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ static int Array_set_warn_on_write(ArrayObject* self, PyObject* v, void* c) {
}

static int c_contiguous(const nx_array* a);
static int c_ordered(const nx_array* a);

static PyObject* Array_get_size(ArrayObject* self, void* c) {
(void)c;
Expand All @@ -370,6 +371,11 @@ static PyObject* Array_get_c_contiguous(ArrayObject* self, void* c) {
return PyBool_FromLong(c_contiguous(self->a));
}

static PyObject* Array_get_c_ordered(ArrayObject* self, void* c) {
(void)c;
return PyBool_FromLong(c_ordered(self->a));
}

static PyObject* Array_get_base(ArrayObject* self, void* c) {
(void)c;
return Py_NewRef(self->base != NULL ? self->base : Py_None);
Expand Down Expand Up @@ -776,6 +782,8 @@ static PyGetSetDef Array_getset[] = {
"NumPy's flags object of the layout (no sync): a new snapshot per access.", NULL},
{"_c_contiguous", (getter)Array_get_c_contiguous, NULL,
"C-contiguous, as NumPy's flag (no sync).", NULL},
{"_c_ordered", (getter)Array_get_c_ordered, NULL,
"C order with C strides on every axis, also of extent 1 (no sync).", NULL},
{"_scalar", (getter)Array_get_scalar, (setter)Array_set_scalar,
"0-d result NumPy would return as a scalar: in-place operators rebind, as on NumPy's "
"scalars",
Expand Down Expand Up @@ -1402,17 +1410,33 @@ static int c_contiguous(const nx_array* a) {
const int64_t* strides = nx_array_strides(a);
static const int64_t itemsizes[] = {1, 4, 8, 4, 8, 8, 16}; /* nx_dtype order */
int64_t step = itemsizes[nx_array_dtype(a)];
int contiguous = 1;
for (int i = nd; i-- > 0;) {
if (shape[i] == 0) {
return 1;
return 1; /* no elements: contiguous whatever the strides */
}
if (shape[i] != 1) {
if (strides[i] != step) {
return 0;
}
contiguous &= strides[i] == step;
step *= shape[i];
}
}
return contiguous;
}

/* C order with every stride NumPy's C order gives, also along extent-1 axes
* (NumPy orders a result's axes by them). */
static int c_ordered(const nx_array* a) {
const int nd = nx_array_ndim(a);
const int64_t* shape = nx_array_shape(a);
const int64_t* strides = nx_array_strides(a);
static const int64_t itemsizes[] = {1, 4, 8, 4, 8, 8, 16}; /* nx_dtype order */
int64_t step = itemsizes[nx_array_dtype(a)];
for (int i = nd; i-- > 0;) {
if (strides[i] != step) {
return 0;
}
step *= shape[i];
}
return 1;
}

Expand Down Expand Up @@ -1456,15 +1480,30 @@ static PyObject* elision_candidate(PyObject* const* inputs, nx_array* const* ins

/* --- scalar shortcut ------------------------------------------------------ */

/* An op on scalars only (ready 0-d numpy2x arrays, Python and NumPy scalars)
* has nothing to run asynchronously: NumPy computes it right here, on NumPy
/* An op on scalars only (ready 0-d numpy2x arrays standing for NumPy scalars,
* `_scalar`; Python and NumPy scalars) has nothing to run asynchronously: NumPy computes it right here, on NumPy
* scalars, as the NumPy program would (its 0-d values are NumPy scalars:
* reductions and scalar indexing return them, and ufuncs on 0-d arrays do
* too). Its result is NumPy's: a NumPy scalar. Any n-d operand (even of one
* element) keeps the array path, so NumPy's output shapes stay as they
* element) and a plain 0-d array (NumPy's 0-d array) keep the array path, so NumPy's output shapes stay as they
* are. Fills `out` (capacity `out_len` entries, at least `n`) with new
* references and returns 1, or 0 if an operand is not a ready scalar. Call it
* through SCALARS_OF, which passes the length of the `out` array. */
static int end_borrow(nx_array* a, PyObject* view);

/* Releases what scalars_of() filled in: borrowed views end their borrow. */
static void release_scalars(PyObject* const* inputs, PyObject** out, Py_ssize_t n) {
for (Py_ssize_t i = 0; i < n; ++i) {
if (is_array(inputs[i]) && !((ArrayObject*)inputs[i])->scalar) {
if (end_borrow(((ArrayObject*)inputs[i])->a, out[i]) < 0) {
PyErr_WriteUnraisable(inputs[i]);
}
} else {
Py_DECREF(out[i]);
}
}
}

static int scalars_of(PyObject* const* inputs, Py_ssize_t n, PyObject** out, Py_ssize_t out_len) {
if (n > MAXIN) {
return 0;
Expand All @@ -1489,11 +1528,15 @@ static int scalars_of(PyObject* const* inputs, Py_ssize_t n, PyObject** out, Py_
}
for (Py_ssize_t i = 0; i < n; ++i) {
PyObject* x = inputs[i];
out[i] = is_array(x) ? nx_array_scalar(((ArrayObject*)x)->a) : Py_NewRef(x);
/* A stand-in for a NumPy scalar (`_scalar`) becomes that scalar; a
* plain 0-d array stays one (a borrowed view): NumPy computes on
* 0-d arrays with its array loops, on scalars partly with other
* code (complex64 square differs in the last bit). */
out[i] = !is_array(x) ? Py_NewRef(x)
: ((ArrayObject*)x)->scalar ? nx_array_scalar(((ArrayObject*)x)->a)
: nx_array_borrow(((ArrayObject*)x)->a);
if (out[i] == NULL) {
for (Py_ssize_t j = 0; j < i; ++j) {
Py_DECREF(out[j]);
}
release_scalars(inputs, out, i);
return -1;
}
}
Expand All @@ -1504,12 +1547,6 @@ static int scalars_of(PyObject* const* inputs, Py_ssize_t n, PyObject** out, Py_
#define SCALARS_OF(inputs, n, out) \
scalars_of((inputs), (n), (out), (Py_ssize_t)(sizeof(out) / sizeof((out)[0])))

static void release_all(PyObject** objs, Py_ssize_t n) {
for (Py_ssize_t i = 0; i < n; ++i) {
Py_DECREF(objs[i]);
}
}

/* --- the ufunc fast path -------------------------------------------------- */

/* Input kinds in the dtype-resolution cache key. Arrays and NumPy scalars use
Expand Down Expand Up @@ -1845,7 +1882,7 @@ static PyObject* Array_array_ufunc(PyObject* self, PyObject* const* args, Py_ssi
}
if (k > 0) {
PyObject* r = PyObject_Vectorcall(args[0], sc, (size_t)n, NULL);
release_all(sc, n);
release_scalars(inputs, sc, n);
return r;
}
}
Expand Down Expand Up @@ -1932,7 +1969,7 @@ static PyObject* binop(const char* name, binaryfunc scalar_op, PyObject* a, PyOb
if (k != 0) {
PyObject* r = k > 0 ? scalar_op(sc[0], sc[1]) : NULL;
if (k > 0) {
release_all(sc, 2);
release_scalars(in2, sc, 2);
}
return r;
}
Expand Down Expand Up @@ -1980,7 +2017,9 @@ static PyObject* unop(const char* name, unaryfunc scalar_op, PyObject* a) {
const int k = SCALARS_OF(&a, 1, sc);
if (k != 0) {
PyObject* r = k > 0 ? scalar_op(sc[0]) : NULL;
Py_XDECREF(k > 0 ? sc[0] : NULL);
if (k > 0) {
release_scalars(&a, sc, 1);
}
return r;
}
PyObject* ufunc = op_ufunc(name);
Expand Down Expand Up @@ -4883,7 +4922,7 @@ static PyObject* UfuncProxy_vectorcall(PyObject* self, PyObject* const* args, si
}
if (k > 0) {
PyObject* r = PyObject_Vectorcall(uf, sc, (size_t)n, NULL);
release_all(sc, n);
release_scalars(args, sc, n);
return r;
}
PyObject* result = NULL;
Expand Down
Loading
Loading