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

Reuse dpctl.tensor.floor_divide() function. #1427

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 0 additions & 2 deletions dpnp/dpnp_algo/dpnp_algo.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
DPNP_FN_FLATTEN_EXT
DPNP_FN_FLOOR
DPNP_FN_FLOOR_EXT
DPNP_FN_FLOOR_DIVIDE
DPNP_FN_FLOOR_DIVIDE_EXT
DPNP_FN_FMOD
DPNP_FN_FMOD_EXT
DPNP_FN_FULL
Expand Down
9 changes: 0 additions & 9 deletions dpnp/dpnp_algo/dpnp_algo_mathematical.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ __all__ += [
"dpnp_ediff1d",
"dpnp_fabs",
"dpnp_floor",
"dpnp_floor_divide",
"dpnp_fmod",
"dpnp_gradient",
'dpnp_hypot',
Expand Down Expand Up @@ -301,14 +300,6 @@ cpdef utils.dpnp_descriptor dpnp_floor(utils.dpnp_descriptor x1, utils.dpnp_desc
return call_fptr_1in_1out_strides(DPNP_FN_FLOOR_EXT, x1, dtype=None, out=out, where=True, func_name='floor')


cpdef utils.dpnp_descriptor dpnp_floor_divide(utils.dpnp_descriptor x1_obj,
utils.dpnp_descriptor x2_obj,
object dtype=None,
utils.dpnp_descriptor out=None,
object where=True):
return call_fptr_2in_1out(DPNP_FN_FLOOR_DIVIDE_EXT, x1_obj, x2_obj, dtype, out, where)


cpdef utils.dpnp_descriptor dpnp_fmod(utils.dpnp_descriptor x1_obj,
utils.dpnp_descriptor x2_obj,
object dtype=None,
Expand Down
37 changes: 37 additions & 0 deletions dpnp/dpnp_algo/dpnp_elementwise_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
__all__ = [
"dpnp_add",
"dpnp_divide",
"dpnp_floor_divide"
"dpnp_multiply",
"dpnp_subtract"
]
Expand Down Expand Up @@ -241,3 +242,39 @@ def dpnp_subtract(x1, x2, out=None, order='K'):
_subtract_docstring_, ti._subtract_inplace)
res_usm = func(x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order)
return dpnp_array._create_from_usm_ndarray(res_usm)


_floor_divide_docstring_ = """
npolina4 marked this conversation as resolved.
Show resolved Hide resolved
floor_divide(x1, x2, out=None, order='K')

Calculates the ratio for each element `x1_i` of the input array `x1` with
the respective element `x2_i` of the input array `x2` to the greatest
integer-value number that is not greater than the division result.

Args:
x1 (dpnp.ndarray):
First input array, expected to have numeric data type.
x2 (dpnp.ndarray):
Second input array, also expected to have numeric data type.
out ({None, dpnp.ndarray}, optional):
Output array to populate.
Array have the correct shape and the expected data type.
order ("C","F","A","K", None, optional):
Memory layout of the newly output array, if parameter `out` is `None`.
Default: "K".
Returns:
dpnp.ndarray:
an array containing the result of element-wise floor division.
The data type of the returned array is determined by the Type
Promotion Rules
"""

def dpnp_floor_divide(x1, x2, out=None, order='K'):
# dpctl.tensor only works with usm_ndarray or scalar
x1_usm_or_scalar = dpnp.get_usm_ndarray_or_scalar(x1)
x2_usm_or_scalar = dpnp.get_usm_ndarray_or_scalar(x2)
out_usm = None if out is None else dpnp.get_usm_ndarray(out)

func = BinaryElementwiseFunc("floor_divide", ti._floor_divide_result_type, ti._floor_divide, _floor_divide_docstring_)
res_usm = func(x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order)
return dpnp_array._create_from_usm_ndarray(res_usm)
npolina4 marked this conversation as resolved.
Show resolved Hide resolved
42 changes: 4 additions & 38 deletions dpnp/dpnp_iface_mathematical.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from .dpnp_algo.dpnp_elementwise_common import (
dpnp_add,
dpnp_divide,
dpnp_floor_divide,
dpnp_multiply,
dpnp_subtract
)
Expand Down Expand Up @@ -737,7 +738,7 @@ def floor(x1, out=None, **kwargs):
return call_origin(numpy.floor, x1, out=out, **kwargs)


def floor_divide(x1, x2, dtype=None, out=None, where=True, **kwargs):
def floor_divide(x1, x2, /, out=None, *, where=True, order='K', dtype=None, subok=True, **kwargs):
"""
Compute the largest integer smaller or equal to the division of the inputs.

Expand All @@ -746,7 +747,7 @@ def floor_divide(x1, x2, dtype=None, out=None, where=True, **kwargs):
Limitations
-----------
Parameters ``x1`` and ``x2`` are supported as either :obj:`dpnp.ndarray` or scalar.
Parameters ``dtype``, ``out`` and ``where`` are supported with their default values.
Parameters ``where``, ``dtype``, and ``subok`` are supported with their default values.
Keyword arguments ``kwargs`` are currently unsupported.
Otherwise the functions will be executed sequentially on CPU.
Input array data types are limited by supported DPNP :ref:`Data types`.
npolina4 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -767,42 +768,7 @@ def floor_divide(x1, x2, dtype=None, out=None, where=True, **kwargs):

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please update an example, since the output has different format now

"""

x1_is_scalar = dpnp.isscalar(x1)
x2_is_scalar = dpnp.isscalar(x2)
x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False)
x2_desc = dpnp.get_dpnp_descriptor(x2, copy_when_nondefault_queue=False)

if x1_desc and x2_desc and not kwargs:
if not x1_desc and not x1_is_scalar:
pass
elif not x2_desc and not x2_is_scalar:
pass
elif x1_is_scalar and x2_is_scalar:
pass
elif x1_desc and x1_desc.ndim == 0:
pass
elif x2_desc and x2_desc.ndim == 0:
pass
elif x2_is_scalar and not x2_desc:
pass
elif x1_desc and x2_desc and x1_desc.size != x2_desc.size:
# TODO: enable broadcasting
pass
elif x1_desc and x2_desc and x1_desc.shape != x2_desc.shape:
pass
elif dtype is not None:
pass
elif out is not None:
pass
elif not where:
pass
elif x1_is_scalar and x2_desc.ndim > 1:
pass
else:
out_desc = dpnp.get_dpnp_descriptor(out, copy_when_nondefault_queue=False) if out is not None else None
return dpnp_floor_divide(x1_desc, x2_desc, dtype, out_desc, where).get_pyobj()

return call_origin(numpy.floor_divide, x1, x2, out=out, where=where, dtype=dtype, **kwargs)
return _check_nd_call(numpy.floor_divide, dpnp_floor_divide, x1, x2, out=out, where=where, order=order, dtype=dtype, subok=subok, **kwargs)


def fmax(*args, **kwargs):
Expand Down
5 changes: 3 additions & 2 deletions tests/test_mathematical.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ def test_divide(self, dtype, lhs, rhs):
def test_fmod(self, dtype, lhs, rhs):
self._test_mathematical('fmod', dtype, lhs, rhs)

@pytest.mark.usefixtures("allow_fall_back_on_numpy")
@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True))
@pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we unmute tests from tests/third_party/cupy/math_tests/test_arithmetic.py ?

def test_floor_divide(self, dtype, lhs, rhs):
antonwolfy marked this conversation as resolved.
Show resolved Hide resolved
if dtype == dpnp.float32 and rhs == 0.3:
pytest.skip("In this case, a different result, but similar to xp.floor(xp.divide(lhs, rhs).")
self._test_mathematical('floor_divide', dtype, lhs, rhs)

@pytest.mark.usefixtures("allow_fall_back_on_numpy")
Expand Down