Skip to content

Add the order kwarg to cupy.reshape and the underlying ndarray method - #1843

Merged
niboshi merged 11 commits into
cupy:masterfrom
grlee77:reshape_w_order
Jan 8, 2019
Merged

niboshi merged 11 commits into
cupy:masterfrom
grlee77:reshape_w_order

Conversation

@grlee77

@grlee77 grlee77 commented Nov 29, 2018

Copy link
Copy Markdown
Member

This PR implements a simple means to support the order kwarg in ndarray's reshape method.

namely, the trick used here is that:

x.reshape(newshape, order='F')

is equivalent to:

x.transpose().reshape(newshape[::-1]).transpose()

Various reshape test cases were duplicated for Fortran order to verify that this is indeed the case.

This transpose-based implementation probably has slight overhead vs. a lower-level implementation, but is considerably simpler and seems to have fairly small overhead. This is probably because transpose is fast as it returns a view rather than a copy.

@grlee77

grlee77 commented Nov 29, 2018

Copy link
Copy Markdown
Member Author

Here is a quick benchmarks in an Ipython indicating that the overhead is likely only ~1 microsecond when applying order='F' to a fortran-ordered array vs. applying order='C' to a C-ordered array. This overhead is substantially less than the overhead of making a copy (at least for the moderate array size tested here).

import cupy
from cupy import testing

x = testing.shaped_arange((200, 3, 400), cupy)
xf = cupy.asfortranarray(a)

# C-order reshape of C-ordered array
%timeit x.reshape(3, -1, order='C')
2.23 µs ± 192 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

# F-order reshape of F-ordered array
%timeit xf.reshape(3, -1, order='F')
3.3 µs ± 39.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

# two cases where a copy is made
%timeit xf.reshape(3, -1, order='C')
47.5 µs ± 199 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

%timeit x.reshape(3, -1, order='F')
49 µs ± 663 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

@niboshi niboshi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for the PR! Please check the review.

Comment thread cupy/core/core.pyx Resolved
Comment thread cupy/core/core.pyx Resolved
Comment thread cupy/core/core.pyx Resolved
Comment thread cupy/core/core.pyx Resolved
Comment thread cupy/manipulation/shape.py Resolved
self.assertFalse(a.flags.c_contiguous)
self.assertTrue(a.flags.f_contiguous)

a = a.reshape(1, 6, 1, order='F')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Shouldn't it be b =? Or is it intended?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I have replaced these test cases with a more thorough set of tests.

A few of the new test cases with order = 'A' required the following change to get the same behavior as numpy:

if order_char == 'A':
    if self._f_contiguous:
        order_char = 'F'
   else:
        order_char = 'C'

to

if order_char == 'A':
    if self._f_contiguous and not self._c_contiguous:
        order_char = 'F'
   else:
        order_char = 'C'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks.

@okuta okuta added the cat:feature New features/APIs label Dec 24, 2018
@niboshi

niboshi commented Dec 25, 2018

Copy link
Copy Markdown
Contributor

Jenkins, test this please.

@niboshi niboshi added the st:test-and-merge (deprecated) Ready to merge after test pass. label Dec 25, 2018
@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit 791ad15, target branch master) failed with status FAILURE.
(For contributors, please wait until the reviewer confirms the details of the error.)

@niboshi

niboshi commented Dec 25, 2018

Copy link
Copy Markdown
Contributor

Could you check test failure?

    tests.cupy_tests.manipulation_tests.test_shape.TestReshapeOrder_param_3.test_reshape_contiguity
    tests.cupy_tests.manipulation_tests.test_shape.TestReshapeOrder_param_33.test_reshape_contiguity
    tests.cupy_tests.manipulation_tests.test_shape.TestReshapeOrder_param_12.test_reshape_contiguity
    tests.cupy_tests.manipulation_tests.test_shape.TestReshapeOrder_param_18.test_reshape_contiguity
    tests.cupy_tests.manipulation_tests.test_shape.TestReshapeOrder_param_0.test_reshape_contiguity
    tests.cupy_tests.manipulation_tests.test_shape.TestReshapeOrder_param_24.test_reshape_contiguity
    tests.cupy_tests.manipulation_tests.test_shape.TestReshapeOrder_param_15.test_reshape_contiguity
    tests.cupy_tests.manipulation_tests.test_shape.TestReshapeOrder_param_27.test_reshape_contiguity
    tests.cupy_tests.manipulation_tests.test_shape.TestReshapeOrder_param_6.test_reshape_contiguity
    tests.cupy_tests.manipulation_tests.test_shape.TestReshapeOrder_param_30.test_reshape_contiguity
    tests.cupy_tests.manipulation_tests.test_shape.TestReshapeOrder_param_21.test_reshape_contiguity
    tests.cupy_tests.manipulation_tests.test_shape.TestReshapeOrder_param_9.test_reshape_contiguity
13:51:06 _______________ TestReshapeOrder_param_3.test_reshape_contiguity _______________
13:51:06 
13:51:06 self = <cupy.testing.parameterized.TestReshapeOrder_param_3 testMethod=test_reshape_contiguity>
13:51:06 
13:51:06     def test_reshape_contiguity(self):
13:51:06         shape_init, shape_final = self.shape_in_out
13:51:06     
13:51:06         a_cupy = testing.shaped_arange(shape_init, xp=cupy)
13:51:06         a_cupy = cupy.asarray(a_cupy, order=self.order_init)
13:51:06         b_cupy = a_cupy.reshape(shape_final, order=self.order_reshape)
13:51:06     
13:51:06         a_numpy = testing.shaped_arange(shape_init, xp=numpy)
13:51:06         a_numpy = numpy.asarray(a_numpy, order=self.order_init)
13:51:06         b_numpy = a_numpy.reshape(shape_final, order=self.order_reshape)
13:51:06     
13:51:06         assert b_cupy.flags.f_contiguous == b_numpy.flags.f_contiguous
13:51:06 >       assert b_cupy.flags.c_contiguous == b_numpy.flags.c_contiguous
13:51:06 E       AssertionError: Parameterized test failed.
13:51:06 E       
13:51:06 E       Base test method: TestReshapeOrder.test_reshape_contiguity
13:51:06 E       Test parameters:
13:51:06 E         shape_in_out: ((2, 3), (1, 6, 1))
13:51:06 E         order_reshape: F
13:51:06 E         order_init: C
13:51:06 E       
13:51:06 E       AssertionError: assert True == False
13:51:06 E        +  where True =   C_CONTIGUOUS : True\n  F_CONTIGUOUS : True\n  OWNDATA : False.c_contiguous
13:51:06 E        +    where   C_CONTIGUOUS : True\n  F_CONTIGUOUS : True\n  OWNDATA : False = array([[[ 1.],\n        [ 4.],\n        [ 2.],\n        [ 5.],\n        [ 3.],\n        [ 6.]]], dtype=float32).flags
13:51:06 E        +  and   False =   C_CONTIGUOUS : False\n  F_CONTIGUOUS : True\n  OWNDATA : False\n  WRITEABLE : True\n  ALIGNED : True\n  UPDATEIFCOPY : False.c_contiguous
13:51:06 E        +    where   C_CONTIGUOUS : False\n  F_CONTIGUOUS : True\n  OWNDATA : False\n  WRITEABLE : True\n  ALIGNED : True\n  UPDATEIFCOPY : False = array([[[ 1.],\n        [ 4.],\n        [ 2.],\n        [ 5.],\n        [ 3.],\n        [ 6.]]], dtype=float32).flags
13:51:06 
13:51:06 a_cupy     = array([[ 1.,  2.,  3.],
13:51:06        [ 4.,  5.,  6.]], dtype=float32)
13:51:06 a_numpy    = array([[ 1.,  2.,  3.],
13:51:06        [ 4.,  5.,  6.]], dtype=float32)
13:51:06 b_cupy     = array([[[ 1.],
13:51:06         [ 4.],
13:51:06         [ 2.],
13:51:06         [ 5.],
13:51:06         [ 3.],
13:51:06         [ 6.]]], dtype=float32)
13:51:06 b_numpy    = array([[[ 1.],
13:51:06         [ 4.],
13:51:06         [ 2.],
13:51:06         [ 5.],
13:51:06         [ 3.],
13:51:06         [ 6.]]], dtype=float32)
13:51:06 self       = <cupy.testing.parameterized.TestReshapeOrder_param_3 testMethod=test_reshape_contiguity>
13:51:06 shape_final = (1, 6, 1)
13:51:06 shape_init = (2, 3)
13:51:06 
13:51:06 tests/cupy_tests/manipulation_tests/test_shape.py:124: AssertionError

@niboshi niboshi removed the st:test-and-merge (deprecated) Ready to merge after test pass. label Dec 25, 2018
@grlee77

grlee77 commented Dec 25, 2018

Copy link
Copy Markdown
Member Author

I am traveling and don't have access to a GPU for the next couple of days, but will take a look later in the week.

@grlee77

grlee77 commented Dec 27, 2018

Copy link
Copy Markdown
Member Author

Okay, this seems to be an issue with different behavior in older numpy. If I mark the test as requiring numpy >= 1.12 it should work.

simple example:

import numpy as np
np.arange(6).reshape((1, 6, 1)).flags

This will show True for C contiguous, but False for F contiguous in NumPy 1.9-1.11, but True for both in NumPy 1.12+. Having both be True seems correct to me, so I think we just need to require 1.12+ on that test.

A C-contiguous array with shape (1, 6, 1) has True for both F-contiguous and C-contiguous
flags only in NumPy>=1.12.
@grlee77

grlee77 commented Jan 4, 2019

Copy link
Copy Markdown
Member Author

This is ready to try the CI again. (I assume a core team member needs to request from Jenkins?)

@niboshi

niboshi commented Jan 8, 2019

Copy link
Copy Markdown
Contributor

Thanks!
Jenkins, test this please

@niboshi niboshi added the st:test-and-merge (deprecated) Ready to merge after test pass. label Jan 8, 2019
@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit 04a8cb3, target branch master) failed with status FAILURE.
(For contributors, please wait until the reviewer confirms the details of the error.)

@niboshi

niboshi commented Jan 8, 2019

Copy link
Copy Markdown
Contributor

Thanks, LGTM!
Jenkins test failure is irrelevant.

@niboshi niboshi added this to the v6.0.0b2 milestone Jan 8, 2019
@niboshi niboshi changed the title add the order kwarg to cupy.reshape and the underlying ndarray method Add the order kwarg to cupy.reshape and the underlying ndarray method Jan 8, 2019
@niboshi
niboshi merged commit 6ab44f7 into cupy:master Jan 8, 2019
@grlee77
grlee77 deleted the reshape_w_order branch November 26, 2019 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat:feature New features/APIs st:test-and-merge (deprecated) Ready to merge after test pass.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants