Skip to content

Commit 10a656d

Browse files
authored
Merge pull request #1462 from IntelPython/reuse_floor_divide_func
Reuse dpctl.tensor.floor_divide() function.
2 parents 457fb55 + 9349223 commit 10a656d

File tree

7 files changed

+75
-104
lines changed

7 files changed

+75
-104
lines changed

dpnp/dpnp_algo/dpnp_algo.pxd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
140140
DPNP_FN_FLATTEN_EXT
141141
DPNP_FN_FLOOR
142142
DPNP_FN_FLOOR_EXT
143-
DPNP_FN_FLOOR_DIVIDE
144-
DPNP_FN_FLOOR_DIVIDE_EXT
145143
DPNP_FN_FMOD
146144
DPNP_FN_FMOD_EXT
147145
DPNP_FN_FULL

dpnp/dpnp_algo/dpnp_algo_mathematical.pxi

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ __all__ += [
4949
"dpnp_ediff1d",
5050
"dpnp_fabs",
5151
"dpnp_floor",
52-
"dpnp_floor_divide",
5352
"dpnp_fmod",
5453
"dpnp_gradient",
5554
'dpnp_hypot',
@@ -301,14 +300,6 @@ cpdef utils.dpnp_descriptor dpnp_floor(utils.dpnp_descriptor x1, utils.dpnp_desc
301300
return call_fptr_1in_1out_strides(DPNP_FN_FLOOR_EXT, x1, dtype=None, out=out, where=True, func_name='floor')
302301

303302

304-
cpdef utils.dpnp_descriptor dpnp_floor_divide(utils.dpnp_descriptor x1_obj,
305-
utils.dpnp_descriptor x2_obj,
306-
object dtype=None,
307-
utils.dpnp_descriptor out=None,
308-
object where=True):
309-
return call_fptr_2in_1out(DPNP_FN_FLOOR_DIVIDE_EXT, x1_obj, x2_obj, dtype, out, where)
310-
311-
312303
cpdef utils.dpnp_descriptor dpnp_fmod(utils.dpnp_descriptor x1_obj,
313304
utils.dpnp_descriptor x2_obj,
314305
object dtype=None,

dpnp/dpnp_algo/dpnp_elementwise_common.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"dpnp_cos",
4747
"dpnp_divide",
4848
"dpnp_equal",
49+
"dpnp_floor_divide",
4950
"dpnp_greater",
5051
"dpnp_greater_equal",
5152
"dpnp_less",
@@ -342,6 +343,48 @@ def dpnp_equal(x1, x2, out=None, order="K"):
342343
return dpnp_array._create_from_usm_ndarray(res_usm)
343344

344345

346+
_floor_divide_docstring_ = """
347+
floor_divide(x1, x2, out=None, order="K")
348+
Calculates the ratio for each element `x1_i` of the input array `x1` with
349+
the respective element `x2_i` of the input array `x2` to the greatest
350+
integer-value number that is not greater than the division result.
351+
Args:
352+
x1 (dpnp.ndarray):
353+
First input array, expected to have numeric data type.
354+
x2 (dpnp.ndarray):
355+
Second input array, also expected to have numeric data type.
356+
out ({None, dpnp.ndarray}, optional):
357+
Output array to populate.
358+
Array have the correct shape and the expected data type.
359+
order ("C","F","A","K", None, optional):
360+
Memory layout of the newly output array, if parameter `out` is `None`.
361+
Default: "K".
362+
Returns:
363+
dpnp.ndarray:
364+
an array containing the result of element-wise floor division.
365+
The data type of the returned array is determined by the Type
366+
Promotion Rules
367+
"""
368+
369+
370+
def dpnp_floor_divide(x1, x2, out=None, order="K"):
371+
"""Invokes floor_divide() from dpctl.tensor implementation for floor_divide() function."""
372+
373+
# dpctl.tensor only works with usm_ndarray or scalar
374+
x1_usm_or_scalar = dpnp.get_usm_ndarray_or_scalar(x1)
375+
x2_usm_or_scalar = dpnp.get_usm_ndarray_or_scalar(x2)
376+
out_usm = None if out is None else dpnp.get_usm_ndarray(out)
377+
378+
func = BinaryElementwiseFunc(
379+
"floor_divide",
380+
ti._floor_divide_result_type,
381+
ti._floor_divide,
382+
_floor_divide_docstring_,
383+
)
384+
res_usm = func(x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order)
385+
return dpnp_array._create_from_usm_ndarray(res_usm)
386+
387+
345388
_greater_docstring_ = """
346389
greater(x1, x2, out=None, order="K")
347390

dpnp/dpnp_iface_mathematical.py

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
from .dpnp_algo.dpnp_elementwise_common import (
5151
dpnp_add,
5252
dpnp_divide,
53+
dpnp_floor_divide,
5354
dpnp_multiply,
5455
dpnp_subtract,
5556
)
@@ -821,7 +822,18 @@ def floor(x1, out=None, **kwargs):
821822
return call_origin(numpy.floor, x1, out=out, **kwargs)
822823

823824

824-
def floor_divide(x1, x2, dtype=None, out=None, where=True, **kwargs):
825+
def floor_divide(
826+
x1,
827+
x2,
828+
/,
829+
out=None,
830+
*,
831+
where=True,
832+
order="K",
833+
dtype=None,
834+
subok=True,
835+
**kwargs,
836+
):
825837
"""
826838
Compute the largest integer smaller or equal to the division of the inputs.
827839
@@ -830,7 +842,7 @@ def floor_divide(x1, x2, dtype=None, out=None, where=True, **kwargs):
830842
Limitations
831843
-----------
832844
Parameters ``x1`` and ``x2`` are supported as either :obj:`dpnp.ndarray` or scalar.
833-
Parameters ``dtype``, ``out`` and ``where`` are supported with their default values.
845+
Parameters ``where``, ``dtype`` and ``subok`` are supported with their default values.
834846
Keyword arguments ``kwargs`` are currently unsupported.
835847
Otherwise the functions will be executed sequentially on CPU.
836848
Input array data types are limited by supported DPNP :ref:`Data types`.
@@ -845,55 +857,22 @@ def floor_divide(x1, x2, dtype=None, out=None, where=True, **kwargs):
845857
Examples
846858
--------
847859
>>> import dpnp as np
848-
>>> result = np.floor_divide(np.array([1, -1, -2, -9]), np.array([-2, -2, -2, -2]))
849-
>>> [x for x in result]
850-
[-1, 0, 1, 4]
860+
>>> np.floor_divide(np.array([1, -1, -2, -9]), np.array([-2, -2, -2, -2]))
861+
array([-1, 0, 1, 4])
851862
852863
"""
853864

854-
x1_is_scalar = dpnp.isscalar(x1)
855-
x2_is_scalar = dpnp.isscalar(x2)
856-
x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False)
857-
x2_desc = dpnp.get_dpnp_descriptor(x2, copy_when_nondefault_queue=False)
858-
859-
if x1_desc and x2_desc and not kwargs:
860-
if not x1_desc and not x1_is_scalar:
861-
pass
862-
elif not x2_desc and not x2_is_scalar:
863-
pass
864-
elif x1_is_scalar and x2_is_scalar:
865-
pass
866-
elif x1_desc and x1_desc.ndim == 0:
867-
pass
868-
elif x2_desc and x2_desc.ndim == 0:
869-
pass
870-
elif x2_is_scalar and not x2_desc:
871-
pass
872-
elif x1_desc and x2_desc and x1_desc.size != x2_desc.size:
873-
# TODO: enable broadcasting
874-
pass
875-
elif x1_desc and x2_desc and x1_desc.shape != x2_desc.shape:
876-
pass
877-
elif dtype is not None:
878-
pass
879-
elif out is not None:
880-
pass
881-
elif not where:
882-
pass
883-
elif x1_is_scalar and x2_desc.ndim > 1:
884-
pass
885-
else:
886-
out_desc = (
887-
dpnp.get_dpnp_descriptor(out, copy_when_nondefault_queue=False)
888-
if out is not None
889-
else None
890-
)
891-
return dpnp_floor_divide(
892-
x1_desc, x2_desc, dtype, out_desc, where
893-
).get_pyobj()
894-
895-
return call_origin(
896-
numpy.floor_divide, x1, x2, out=out, where=where, dtype=dtype, **kwargs
865+
return _check_nd_call(
866+
numpy.floor_divide,
867+
dpnp_floor_divide,
868+
x1,
869+
x2,
870+
out=out,
871+
where=where,
872+
order=order,
873+
dtype=dtype,
874+
subok=subok,
875+
**kwargs,
897876
)
898877

899878

tests/skipped_tests.tbl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ tests/test_random.py::TestPermutationsTestShuffle::test_shuffle1[lambda x: dpnp.
77

88
tests/test_sycl_queue.py::test_1in_1out[opencl:gpu:0-trapz-data19]
99
tests/test_sycl_queue.py::test_1in_1out[opencl:cpu:0-trapz-data19]
10-
tests/test_sycl_queue.py::test_broadcasting[opencl:gpu:0-floor_divide-data12-data22]
1110
tests/test_sycl_queue.py::test_broadcasting[opencl:gpu:0-remainder-data15-data25]
12-
tests/test_sycl_queue.py::test_broadcasting[opencl:cpu:0-floor_divide-data12-data22]
1311
tests/test_sycl_queue.py::test_broadcasting[opencl:cpu:0-remainder-data15-data25]
1412

1513
tests/third_party/cupy/fft_tests/test_fft.py::TestFft2_param_1_{axes=None, norm=None, s=(1, None), shape=(3, 4)}::test_fft2
@@ -693,19 +691,15 @@ tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_2_{reps
693691
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_3_{reps=(0, 1)}::test_array_tile
694692
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_4_{reps=(2, 3)}::test_array_tile
695693
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_5_{reps=(2, 3, 4, 5)}::test_array_tile
696-
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_455_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), dtype=float64, name='floor_divide', use_dtype=False}::test_binary
697694
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_457_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), dtype=float64, name='fmod', use_dtype=False}::test_binary
698695
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_459_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), dtype=float64, name='remainder', use_dtype=False}::test_binary
699696
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_461_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), dtype=float64, name='mod', use_dtype=False}::test_binary
700-
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_463_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=array([[0, 1, 2], [3, 4, 5]]), dtype=float64, name='floor_divide', use_dtype=False}::test_binary
701697
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_465_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=array([[0, 1, 2], [3, 4, 5]]), dtype=float64, name='fmod', use_dtype=False}::test_binary
702698
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_467_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=array([[0, 1, 2], [3, 4, 5]]), dtype=float64, name='remainder', use_dtype=False}::test_binary
703699
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_469_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=array([[0, 1, 2], [3, 4, 5]]), dtype=float64, name='mod', use_dtype=False}::test_binary
704-
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_535_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), dtype=float64, name='floor_divide', use_dtype=False}::test_binary
705700
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_537_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), dtype=float64, name='fmod', use_dtype=False}::test_binary
706701
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_539_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), dtype=float64, name='remainder', use_dtype=False}::test_binary
707702
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_541_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), dtype=float64, name='mod', use_dtype=False}::test_binary
708-
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_543_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=array([[0, 1, 2], [3, 4, 5]]), dtype=float64, name='floor_divide', use_dtype=False}::test_binary
709703
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_545_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=array([[0, 1, 2], [3, 4, 5]]), dtype=float64, name='fmod', use_dtype=False}::test_binary
710704
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_547_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=array([[0, 1, 2], [3, 4, 5]]), dtype=float64, name='remainder', use_dtype=False}::test_binary
711705
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_549_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=array([[0, 1, 2], [3, 4, 5]]), dtype=float64, name='mod', use_dtype=False}::test_binary
@@ -714,7 +708,6 @@ tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticModf::test_m
714708
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticRaisesWithNumpyInput_param_10_{name='remainder', nargs=2}::test_raises_with_numpy_input
715709
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticRaisesWithNumpyInput_param_11_{name='mod', nargs=2}::test_raises_with_numpy_input
716710
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticRaisesWithNumpyInput_param_1_{name='angle', nargs=1}::test_raises_with_numpy_input
717-
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticRaisesWithNumpyInput_param_8_{name='floor_divide', nargs=2}::test_raises_with_numpy_input
718711

719712
tests/third_party/cupy/math_tests/test_explog.py::TestExplog::test_logaddexp
720713
tests/third_party/cupy/math_tests/test_explog.py::TestExplog::test_logaddexp2

0 commit comments

Comments
 (0)