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

Avoid Division by Zero #11397

Merged
merged 2 commits into from
Jul 25, 2018
Merged

Avoid Division by Zero #11397

merged 2 commits into from
Jul 25, 2018

Conversation

toddsundsted
Copy link
Contributor

@toddsundsted toddsundsted commented Jun 26, 2018

Description

In Python, expressions like nd.random_uniform(shape=[5, 5, 0]) cause the runtime to crash. The underlying problem seems to be a division by zero in LaunchRNG(...) at line 50. The divisor nthread is zero because nloop is zero when N is zero due to C++ integer division rules (N is zero because the size of the NDArray above is zero).

See #11398 for the bug report.

Checklist

Essentials

  • Changes are complete
  • Code is well-documented:
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Changes

  • Added a check in LaunchRNG(...) to ensure N is not zero. Small negative values for N would also cause division by zero, but small negative numbers (induced by things like nd.random_uniform(shape=[5, 5, -1])) cause allocation failures before this code is even reached, but I check that case to be safe.
  • Simply returning if N <= 0 should be sufficient because the map operation effectively does nothing in this case.

@toddsundsted toddsundsted force-pushed the check-shape branch 2 times, most recently from f4e54b2 to a2f01e6 Compare June 28, 2018 21:12
@toddsundsted toddsundsted changed the title Check Shape Avoid Division by Zero Jun 28, 2018
*/
template<typename OP, typename xpu, typename GType, typename ...Args>
inline static void LaunchRNG(mshadow::Stream<xpu> *s,
common::random::RandGenerator<xpu, GType> *gen,
const int N, Args... args) {
// minimal check to avoid division by zero, below.
// if `N` is zero the map operation is a no-op in any case.
if (N == 0) {
Copy link
Member

Choose a reason for hiding this comment

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

to be a more stable check, use N <= 0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@zhreshold fixed in 6075fc2. cleaned up commit history with rebase.

@zhreshold zhreshold merged commit 0b8b939 into apache:master Jul 25, 2018
@toddsundsted toddsundsted deleted the check-shape branch July 25, 2018 21:50
XinYao1994 pushed a commit to XinYao1994/incubator-mxnet that referenced this pull request Aug 29, 2018
* Return if iteration counter `N` is less than or equal to zero.

* Fix spelling.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants