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

[MXNET-257] Do not copy when casting as same type #10347

Merged
merged 3 commits into from
Apr 1, 2018

Conversation

ThomasDelteil
Copy link
Contributor

@ThomasDelteil ThomasDelteil commented Mar 30, 2018

MXNET-257

Description

Currently there is a copy even when the data has already the correct type when calling 'astype'.
I am not sure why, so if it's on purpose please enlighten me 😄

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 http://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

Comments

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

@ThomasDelteil ThomasDelteil changed the title [MXNET-???] Do not copy when casting as same type [MXNET-257] Do not copy when casting as same type Mar 30, 2018
@piiswrong
Copy link
Contributor

piiswrong commented Mar 30, 2018

Numpy always copies. We should stay consistent with numpy

>>> import numpy as np
>>> a = np.zeros((10,))
>>> c = a.astype(a.dtype)
>>> a[0] = 1
>>> a
array([ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
>>> c
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

@ThomasDelteil
Copy link
Contributor Author

@piiswrong should then we introduce a flag copy=True to follow the numpy API?
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ndarray.astype.html
I am not sure I agree with the default value, but at least that should keep things backward compatible

@piiswrong
Copy link
Contributor

Sure we can add copy flag

@piiswrong piiswrong merged commit 0291f95 into apache:master Apr 1, 2018
copy : bool
Default `True`. By default, astype always returns a newly
allocated ndarray on the same context. If this is set to
False, and the dtype requirement is satisfied,
Copy link
Member

Choose a reason for hiding this comment

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

It's unclear what the dtype requirement here is from the doc. Please fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. However I used the exact same wording as the numpy docs: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ndarray.astype.html

copy : bool, optional: By default, astype always returns a newly allocated array. If this is set to false, and the dtype, order, and subok requirements are satisfied, the input array is returned instead of a copy.

I suggest switching to:

If this is set to False, and the dtype requested is the same as the array's dtype,

Copy link
Member

Choose a reason for hiding this comment

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

Looks good

lanking520 pushed a commit to lanking520/incubator-mxnet that referenced this pull request Apr 2, 2018
* do not copy on same type when casting

* update to have a copy flag

* adding sparse array
haojin2 pushed a commit to haojin2/incubator-mxnet that referenced this pull request Apr 2, 2018
* do not copy on same type when casting

* update to have a copy flag

* adding sparse array
rahul003 pushed a commit to rahul003/mxnet that referenced this pull request Jun 4, 2018
* do not copy on same type when casting

* update to have a copy flag

* adding sparse array
zheng-da pushed a commit to zheng-da/incubator-mxnet that referenced this pull request Jun 28, 2018
* do not copy on same type when casting

* update to have a copy flag

* adding sparse array
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.

3 participants