Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[API] Add floor_divide #20620

Merged
merged 25 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from 21 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
1 change: 1 addition & 0 deletions docs/python_docs/python/api/np/routines.math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ Arithmetic operations
fmod
modf
divmod
floor_divide


Miscellaneous
Expand Down
3 changes: 3 additions & 0 deletions python/mxnet/amp/lists/symbol_fp16.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@
'_npi_multinomial',
'_npi_multiply',
'_npi_multiply_scalar',
'_npi_floor_divide',
'_npi_floor_divide_scalar',
'_npi_rfloor_divide_scalar',
'_npi_nan_to_num',
'_npi_negative',
'_npi_normal',
Expand Down
41 changes: 40 additions & 1 deletion python/mxnet/ndarray/numpy/_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
'diff', 'ediff1d', 'resize', 'polyval', 'nan_to_num', 'isnan', 'isinf', 'isposinf', 'isneginf', 'isfinite',
'atleast_1d', 'atleast_2d', 'atleast_3d', 'fill_diagonal', 'squeeze',
'where', 'bincount', 'rollaxis', 'diagflat', 'repeat', 'prod', 'pad', 'cumsum', 'sum', 'diag', 'diagonal',
'positive']
'positive', 'floor_divide']


@set_module('mxnet.ndarray.numpy')
Expand Down Expand Up @@ -1168,6 +1168,45 @@ def true_divide(x1, x2, out=None):
return _api_internal.true_divide(x1, x2, out)


@set_module('mxnet.ndarray.numpy')
@wrap_np_binary_func
def floor_divide(x1, x2, out=None):
"""Return the largest integer smaller or equal to the division of the inputs.
It is equivalent to the Python // operator and pairs with the Python % (remainder),
function so that a = a % b + b * (a // b) up to roundoff.

Parameters
----------
x1 : ndarray or scalar
Dividend array.
x2 : ndarray or scalar
Divisor array.
out : ndarray
A location into which the result is stored. If provided, it must have a shape
that the inputs broadcast to. If not provided or None, a freshly-allocated array
is returned.

Returns
-------
out : ndarray or scalar
This is a scalar if both x1 and x2 are scalars.

.. note::

This operator now supports automatic type promotion. The resulting type will be determined
according to the following rules:

* If both inputs are of floating number types, the output is the more precise type.
* If only one of the inputs is floating number type, the result is that type.
* If both inputs are of integer types (including boolean), the output is the more
precise type

"""
if isinstance(x1, numeric_types) and isinstance(x2, numeric_types):
return _np.floor_divide(x1, x2, out=out)
return _api_internal.floor_divide(x1, x2, out)


@set_module('mxnet.ndarray.numpy')
@wrap_np_binary_func
def mod(x1, x2, out=None, **kwargs):
Expand Down
64 changes: 63 additions & 1 deletion python/mxnet/numpy/multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
'quantile', 'percentile', 'shares_memory', 'may_share_memory', 'diff', 'ediff1d', 'resize', 'matmul',
'nan_to_num', 'isnan', 'isinf', 'isposinf', 'isneginf', 'isfinite', 'polyval', 'where', 'bincount',
'atleast_1d', 'atleast_2d', 'atleast_3d', 'fill_diagonal', 'squeeze',
'diagflat', 'repeat', 'prod', 'pad', 'cumsum', 'sum', 'rollaxis', 'diag', 'diagonal', 'positive']
'diagflat', 'repeat', 'prod', 'pad', 'cumsum', 'sum', 'rollaxis', 'diag', 'diagonal',
'positive', 'floor_divide']

__all__ += fallback.__all__

Expand Down Expand Up @@ -1113,6 +1114,23 @@ def __mul__(self, other):
"""x.__mul__(y) <=> x * y"""
return multiply(self, other)

@wrap_mxnp_np_ufunc
def __floordiv__(self, other):
"""x.__floordiv__(y) <=> x // y"""
return floor_divide(self, other)

@wrap_mxnp_np_ufunc
def __ifloordiv__(self, other):
"""x.__ifloordiv__(y) <=> x //= y"""
if not self.writable:
raise ValueError('trying to divide from a readonly ndarray')
return floor_divide(self, other, out=self)

@wrap_mxnp_np_ufunc
def __rfloordiv__(self, other):
"""x.__rfloordiv__(y) <=> y // x"""
return floor_divide(other, self)

def __neg__(self):
"""x.__neg__() <=> -x"""
return negative(self)
Expand Down Expand Up @@ -3432,6 +3450,50 @@ def true_divide(x1, x2, out=None):
return _mx_nd_np.true_divide(x1, x2, out=out)


@set_module('mxnet.numpy')
@wrap_np_binary_func
def floor_divide(x1, x2, out=None):
"""Return the largest integer smaller or equal to the division of the inputs.

It is equivalent to the Python // operator and pairs with the Python % (remainder),
function so that a = a % b + b * (a // b) up to roundoff.

Parameters
----------
x1 : ndarray or scalar
Dividend array.
x2 : ndarray or scalar
Divisor array.
out : ndarray
A location into which the result is stored. If provided, it must have a shape
that the inputs broadcast to. If not provided or None, a freshly-allocated array
is returned.

Returns
-------
out : ndarray or scalar
This is a scalar if both x1 and x2 are scalars.

.. note::

This operator now supports automatic type promotion. The resulting type will be determined
according to the following rules:

* If both inputs are of floating number types, the output is the more precise type.
* If only one of the inputs is floating number type, the result is that type.
* If both inputs are of integer types (including boolean), the output is the more
precise type

Examples
--------
>>> np.floor_divide(7,3)
2
>>> np.floor_divide([1., 2., 3., 4.], 2.5)
array([ 0., 0., 1., 1.])
"""
return _mx_nd_np.floor_divide(x1, x2, out=out)


@set_module('mxnet.numpy')
@wrap_np_binary_func
def mod(x1, x2, out=None, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions python/mxnet/numpy_dispatch_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def _register_array_function():
# 'ldexp',
'subtract',
'multiply',
'floor_divide',
'true_divide',
'negative',
'power',
Expand Down
9 changes: 9 additions & 0 deletions src/api/operator/numpy/np_elemwise_broadcast_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ MXNET_REGISTER_API("_npi.true_divide")
UFuncHelper(args, ret, op, op_scalar, op_rscalar);
});

MXNET_REGISTER_API("_npi.floor_divide")
.set_body([](runtime::MXNetArgs args, runtime::MXNetRetValue* ret) {
using namespace runtime;
const nnvm::Op* op = Op::Get("_npi_floor_divide");
const nnvm::Op* op_scalar = Op::Get("_npi_floor_divide_scalar");
const nnvm::Op* op_rscalar = Op::Get("_npi_rfloor_divide_scalar");
UFuncHelper(args, ret, op, op_scalar, op_rscalar);
});

MXNET_REGISTER_API("_npi.mod").set_body([](runtime::MXNetArgs args, runtime::MXNetRetValue* ret) {
using namespace runtime;
const nnvm::Op* op = Op::Get("_npi_mod");
Expand Down
22 changes: 22 additions & 0 deletions src/common/cuda/rtc/forward_functions-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,28 @@ rsub(const DType a, const DType2 b) {
return b - a;
}

template <typename DType, typename DType2>
__device__ inline mixed_type<DType, DType2>
floor_divide(const DType a, const DType2 b) {
mixed_type<DType, DType2> c = ::floor(a / b);
if ((c * b != a) && (a < 0) != (b < 0)) {
return mixed_type<DType, DType2>(c - 1);
} else {
return c;
}
}

template <typename DType, typename DType2>
__device__ inline mixed_type<DType, DType2>
rfloor_divide(const DType a, const DType2 b) {
mixed_type<DType, DType2> c = ::floor(b / a);
if ((c * a != b) && (a < 0) != (b < 0)) {
return mixed_type<DType, DType2>(c - 1);
} else {
return c;
}
}

template <typename DType, typename DType2>
__device__ inline mixed_type<DType, DType2>
mul(const DType a, const DType2 b) {
Expand Down
120 changes: 120 additions & 0 deletions src/operator/mshadow_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,126 @@ struct rtrue_divide : public mxnet_op::tunable {
}
};

/***** floor_divide ******/

struct floor_divide : public mxnet_op::tunable {
template <typename DType,
typename std::enable_if<!std::is_same<DType, bool>::value, int>::type = 0>
MSHADOW_XINLINE static DType Map(DType a, DType b) {
DType c = static_cast<DType>(::floor(a / b));
if ((c * b != a) && ((a < 0) != (b < 0))) {
return DType(c - 1);
} else {
return c;
}
}

MSHADOW_XINLINE static bool Map(bool a, bool b) {
return static_cast<bool>(::floor(a / b));
}
};

struct rfloor_divide : public mxnet_op::tunable {
template <typename DType,
typename std::enable_if<!std::is_same<DType, bool>::value, int>::type = 0>
MSHADOW_XINLINE static DType Map(DType a, DType b) {
DType c = static_cast<DType>(::floor(b / a));
if ((c * a != b) && ((a < 0) != (b < 0))) {
return DType(c - 1);
} else {
return c;
}
}

MSHADOW_XINLINE static bool Map(bool a, bool b) {
return static_cast<bool>(::floor(b / a));
}
};

struct mixed_floor_divide {
template <typename DType, typename std::enable_if<std::is_integral<DType>::value, int>::type = 0>
MSHADOW_XINLINE static mshadow::half::half_t Map(DType a, mshadow::half::half_t b) {
mshadow::half::half_t a_half = static_cast<mshadow::half::half_t>(a);
mshadow::half::half_t c = static_cast<mshadow::half::half_t>(::floor(a_half / b));
if ((c * b != a_half) && ((a_half < 0) != (b < 0))) {
return mshadow::half::half_t(c - 1);
} else {
return c;
}
}

template <typename DType,
typename std::enable_if<std::is_same<DType, mshadow::half::half_t>::value ||
std::is_integral<DType>::value,
int>::type = 0>
MSHADOW_XINLINE static float Map(DType a, float b) {
float a_float = static_cast<float>(a);
float c = ::floorf(a_float / b);
if ((c * b != a_float) && ((a_float < 0) != (b < 0))) {
return c - 1.0f;
} else {
return c;
}
}

template <typename DType,
typename std::enable_if<std::is_same<DType, mshadow::half::half_t>::value ||
std::is_same<DType, float>::value ||
std::is_integral<DType>::value,
int>::type = 0>
MSHADOW_XINLINE static double Map(DType a, double b) {
double a_double = static_cast<double>(a);
double c = ::floor(a_double / b);
if ((c * b != a_double) && ((a_double < 0) != (b < 0))) {
return c - 1.0;
} else {
return c;
}
}
};

struct mixed_rfloor_divide {
template <typename DType, typename std::enable_if<std::is_integral<DType>::value, int>::type = 0>
MSHADOW_XINLINE static mshadow::half::half_t Map(DType a, mshadow::half::half_t b) {
mshadow::half::half_t a_half = static_cast<mshadow::half::half_t>(a);
mshadow::half::half_t c = static_cast<mshadow::half::half_t>(::floor(b / a_half));
if ((c * a_half != b) && ((a_half < 0) != (b < 0))) {
return mshadow::half::half_t(c - 1);
} else {
return c;
}
}

template <typename DType,
typename std::enable_if<std::is_same<DType, mshadow::half::half_t>::value ||
std::is_integral<DType>::value,
int>::type = 0>
MSHADOW_XINLINE static float Map(DType a, float b) {
float a_float = static_cast<float>(a);
float c = ::floorf(b / a_float);
if ((c * a_float != b) && ((a_float < 0) != (b < 0))) {
return c - 1.0f;
} else {
return c;
}
}

template <typename DType,
typename std::enable_if<std::is_same<DType, mshadow::half::half_t>::value ||
std::is_same<DType, float>::value ||
std::is_integral<DType>::value,
int>::type = 0>
MSHADOW_XINLINE static double Map(DType a, double b) {
double a_double = static_cast<double>(a);
double c = ::floor(b / a_double);
if ((c * a_double != b) && ((a_double < 0) != (b < 0))) {
return c - 1.0;
} else {
return c;
}
}
};

MXNET_BINARY_MATH_OP_NC(left, a);

MXNET_BINARY_MATH_OP_NC(right, b);
Expand Down
9 changes: 9 additions & 0 deletions src/operator/numpy/np_elemwise_broadcast_op_scalar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,14 @@ MXNET_OPERATOR_REGISTER_NP_BINARY_SCALAR(_npi_rpower_scalar)
.set_attr<FCompute>("FCompute<cpu>", BinaryScalarOp::Compute<cpu, mshadow_op::rpower>)
.set_attr<nnvm::FGradient>("FGradient", ElemwiseGradUseOut{"_backward_rpower_scalar"});

MXNET_OPERATOR_REGISTER_NP_BINARY_SCALAR(_npi_floor_divide_scalar)
.set_attr<FCompute>("FCompute<cpu>", BinaryScalarOp::Compute<cpu, op::mshadow_op::floor_divide>)
.set_attr<nnvm::FGradient>("FGradient", MakeZeroGradNodes);

MXNET_OPERATOR_REGISTER_NP_BINARY_SCALAR(_npi_rfloor_divide_scalar)
.set_attr<FCompute>("FCompute<cpu>",
BinaryScalarOp::Compute<cpu, op::mshadow_op::rfloor_divide>)
.set_attr<nnvm::FGradient>("FGradient", MakeZeroGradNodes);

} // namespace op
} // namespace mxnet
6 changes: 6 additions & 0 deletions src/operator/numpy/np_elemwise_broadcast_op_scalar.cu
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,11 @@ NNVM_REGISTER_OP(_npi_power_scalar)
NNVM_REGISTER_OP(_npi_rpower_scalar)
.set_attr<FCompute>("FCompute<gpu>", BinaryScalarRTCCompute{"rpow"});

NNVM_REGISTER_OP(_npi_floor_divide_scalar)
.set_attr<FCompute>("FCompute<gpu>", BinaryScalarRTCCompute{"floor_divide"});

NNVM_REGISTER_OP(_npi_rfloor_divide_scalar)
.set_attr<FCompute>("FCompute<gpu>", BinaryScalarRTCCompute{"rfloor_divide"});

} // namespace op
} // namespace mxnet
Loading