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

fix min max on zero-sized ndarray #14745

Merged
merged 4 commits into from
Apr 23, 2019
Merged

Conversation

arcadiaphy
Copy link
Member

@arcadiaphy arcadiaphy commented Apr 19, 2019

Description

Calling min or max operator on zero-sized ndarray will return invalid value:

a = mx.nd.zeros((5, 0))
a.min()  # or a.max()
# result
# [-3.4028235e+38]
# <NDArray 1 @cpu(0)>

In numpy, the same situation will trigger ValueError, this PR replicates this behavior by throwing MXNetError.

Edit:
This error will only be triggered in numpy mode:

mx.set_np_compat(True)
a = mx.nd.zeros((5, 0))
a.min()  # or a.max()

Checklist

Essentials

Please feel free to remove inapplicable items for your PR.

  • The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant JIRA issue created (except PRs with tiny changes)
  • Changes are complete (i.e. I finished coding on this PR)
  • All changes have test coverage:
  • Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
  • Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
  • Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
  • Code is well-documented:
  • For user-facing API changes, API doc string has been updated.
  • For new C++ functions in header files, their functionalities and arguments are documented.
  • For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
  • Check the API doc at https://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Changes

  • Feature1, tests, (and when applicable, API doc)
  • Feature2, tests, (and when applicable, API doc)

Comments

  • If this change is a backward incompatible change, why must this change be made.
  • Interesting edge cases to note here

@Roshrini Roshrini added the pr-awaiting-review PR is waiting for code review label Apr 19, 2019
Copy link
Contributor

@reminisce reminisce left a comment

Choose a reason for hiding this comment

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

Please note that a zero-size shape represents a unknown shape in mxnet. Please turn on numpy compatibility switch to use 0 as zero dim size.

@with_seed()
def test_zero_sized_min_max():
def min():
a = mx.nd.zeros(shape=(5, 0))
Copy link
Contributor

Choose a reason for hiding this comment

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

It should have already failed here in the infer-shape function of zeros. If you want to use 0 as zero dim size, please turn numpy compatibility by adding a decorator to the test function so that it will fail in ReduceMinMaxAxesShape when a.min() is called.

@with_seed()
@use_np_compat
def test_zero_size_min_max():

Copy link
Member Author

Choose a reason for hiding this comment

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

@reminisce You'are right. After #14661, the error I reported will only be triggered in numpy mode.

@@ -6990,6 +6990,20 @@ def test_float16_min_max():
assert np.finfo('float16').max == mx.nd.max(a).asscalar()


@with_seed()
def test_zero_sized_min_max():
Copy link
Contributor

Choose a reason for hiding this comment

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

test_zero_sized_min_max -> test_zero_size_min_max

@arcadiaphy
Copy link
Member Author

An unrelated custom operator error happens in CI, I'll create another PR to address it.

@arcadiaphy arcadiaphy force-pushed the pr_zero_minmax branch 2 times, most recently from 5b8e819 to 92f993f Compare April 23, 2019 06:29
@arcadiaphy arcadiaphy merged commit da7fff7 into apache:master Apr 23, 2019
@arcadiaphy arcadiaphy deleted the pr_zero_minmax branch April 23, 2019 08:40
lihaofd pushed a commit to lihaofd/incubator-mxnet that referenced this pull request Apr 23, 2019
fix min max on zero-sized ndarray (apache#14745)
haohuanw pushed a commit to haohuanw/incubator-mxnet that referenced this pull request Jun 23, 2019
* fix min max of zero-sized ndarray

* add test

* turn on numpy mode

* trigger CI
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pr-awaiting-review PR is waiting for code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants