Skip to content

Fix usage array function for CuPy modules - #2026

Merged
niboshi merged 7 commits into
cupy:masterfrom
pentschev:fix-array-function-module-usage
Feb 26, 2019
Merged

niboshi merged 7 commits into
cupy:masterfrom
pentschev:fix-array-function-module-usage

Conversation

@pentschev

Copy link
Copy Markdown
Member

This PR solves #2011

Comment thread cupy/core/core.pyx Resolved
@niboshi niboshi self-assigned this Feb 12, 2019
Comment thread cupy/core/core.pyx Resolved
@niboshi

niboshi commented Feb 13, 2019

Copy link
Copy Markdown
Contributor

Jenkins, test this please

@niboshi niboshi added st:test-and-merge (deprecated) Ready to merge after test pass. cat:bug Bugs labels Feb 13, 2019
@chainer-ci

Copy link
Copy Markdown
Member

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

@niboshi

niboshi commented Feb 13, 2019

Copy link
Copy Markdown
Contributor

As a quick report, there're two failures from Jenkins:

19:28:31 ____________________ TestArrayFunction.test_array_function _____________________
19:28:31 
19:28:31 self = <cupy_tests.core_tests.test_array_function.TestArrayFunction testMethod=test_array_function>
19:28:31 
19:28:31     def test_array_function(self):
19:28:31         a = numpy.random.randn(100, 100)
19:28:31         a_cpu = numpy.asarray(a)
19:28:31         a_gpu = cupy.asarray(a)
19:28:31     
19:28:31         # The numpy call for both CPU and GPU arrays is intentional to test the
19:28:31         # __array_function__ protocol
19:28:31         qr_cpu = numpy.linalg.qr(a_cpu)
19:28:31 >       qr_gpu = numpy.linalg.qr(a_gpu)
19:28:31 
19:28:31 a          = array([[-0.31192251,  0.54540922, -1.95350922, ..., -2.15705981,
19:28:31         -1.00669283, -0.53702112],
19:28:31        [-0.9179094...205, -0.19056921],
19:28:31        [ 0.08367027,  0.86406915,  1.26840093, ...,  0.36209913,
19:28:31          0.37135528,  0.2503814 ]])
19:28:31 a_cpu      = array([[-0.31192251,  0.54540922, -1.95350922, ..., -2.15705981,
19:28:31         -1.00669283, -0.53702112],
19:28:31        [-0.9179094...205, -0.19056921],
19:28:31        [ 0.08367027,  0.86406915,  1.26840093, ...,  0.36209913,
19:28:31          0.37135528,  0.2503814 ]])
19:28:31 a_gpu      = array([[-0.31192251,  0.54540922, -1.95350922, ..., -2.15705981,
19:28:31         -1.00669283, -0.53702112],
19:28:31        [-0.9179094...205, -0.19056921],
19:28:31        [ 0.08367027,  0.86406915,  1.26840093, ...,  0.36209913,
19:28:31          0.37135528,  0.2503814 ]])
19:28:31 qr_cpu     = (array([[-0.02894301, -0.05205252, -0.20064207, ...,  0.08465095,
19:28:31          0.20440943,  0.01568078],
19:28:31        [-0.085171... [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00, ...,
19:28:31          0.00000000e+00,  0.00000000e+00,  2.00867216e-03]]))
19:28:31 self       = <cupy_tests.core_tests.test_array_function.TestArrayFunction testMethod=test_array_function>
19:28:31 
19:28:31 tests/cupy_tests/core_tests/test_array_function.py:21: 
19:28:31 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
19:28:31 /opt/pyenv/versions/3.7.0/lib/python3.7/site-packages/numpy/linalg/linalg.py:903: in qr
19:28:31     a, wrap = _makearray(a)
19:28:31 /opt/pyenv/versions/3.7.0/lib/python3.7/site-packages/numpy/linalg/linalg.py:117: in _makearray
19:28:31     new = asarray(a)
19:28:31 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
19:28:31 
19:28:31 a = array([[-0.31192251,  0.54540922, -1.95350922, ..., -2.15705981,
19:28:31         -1.00669283, -0.53702112],
19:28:31        [-0.9179094...205, -0.19056921],
19:28:31        [ 0.08367027,  0.86406915,  1.26840093, ...,  0.36209913,
19:28:31          0.37135528,  0.2503814 ]])
19:28:31 dtype = None, order = None
19:28:31 
19:28:31     @set_module('numpy')
19:28:31     def asarray(a, dtype=None, order=None):
19:28:31         """Convert the input to an array.
19:28:31     
19:28:31         Parameters
19:28:31         ----------
19:28:31         a : array_like
19:28:31             Input data, in any form that can be converted to an array.  This
19:28:31             includes lists, lists of tuples, tuples, tuples of tuples, tuples
19:28:31             of lists and ndarrays.
19:28:31         dtype : data-type, optional
19:28:31             By default, the data-type is inferred from the input data.
19:28:31         order : {'C', 'F'}, optional
19:28:31             Whether to use row-major (C-style) or
19:28:31             column-major (Fortran-style) memory representation.
19:28:31             Defaults to 'C'.
19:28:31     
19:28:31         Returns
19:28:31         -------
19:28:31         out : ndarray
19:28:31             Array interpretation of `a`.  No copy is performed if the input
19:28:31             is already an ndarray with matching dtype and order.  If `a` is a
19:28:31             subclass of ndarray, a base class ndarray is returned.
19:28:31     
19:28:31         See Also
19:28:31         --------
19:28:31         asanyarray : Similar function which passes through subclasses.
19:28:31         ascontiguousarray : Convert input to a contiguous array.
19:28:31         asfarray : Convert input to a floating point ndarray.
19:28:31         asfortranarray : Convert input to an ndarray with column-major
19:28:31                          memory order.
19:28:31         asarray_chkfinite : Similar function which checks input for NaNs and Infs.
19:28:31         fromiter : Create an array from an iterator.
19:28:31         fromfunction : Construct an array by executing a function on grid
19:28:31                        positions.
19:28:31     
19:28:31         Examples
19:28:31         --------
19:28:31         Convert a list into an array:
19:28:31     
19:28:31         >>> a = [1, 2]
19:28:31         >>> np.asarray(a)
19:28:31         array([1, 2])
19:28:31     
19:28:31         Existing arrays are not copied:
19:28:31     
19:28:31         >>> a = np.array([1, 2])
19:28:31         >>> np.asarray(a) is a
19:28:31         True
19:28:31     
19:28:31         If `dtype` is set, array is copied only if dtype does not match:
19:28:31     
19:28:31         >>> a = np.array([1, 2], dtype=np.float32)
19:28:31         >>> np.asarray(a, dtype=np.float32) is a
19:28:31         True
19:28:31         >>> np.asarray(a, dtype=np.float64) is a
19:28:31         False
19:28:31     
19:28:31         Contrary to `asanyarray`, ndarray subclasses are not passed through:
19:28:31     
19:28:31         >>> issubclass(np.recarray, np.ndarray)
19:28:31         True
19:28:31         >>> a = np.array([(1.0, 2), (3.0, 4)], dtype='f4,i4').view(np.recarray)
19:28:31         >>> np.asarray(a) is a
19:28:31         False
19:28:31         >>> np.asanyarray(a) is a
19:28:31         True
19:28:31     
19:28:31         """
19:28:31 >       return array(a, dtype, copy=False, order=order)
19:28:31 E       ValueError: object __array__ method not producing an array
19:28:31 
19:28:31 a          = array([[-0.31192251,  0.54540922, -1.95350922, ..., -2.15705981,
19:28:31         -1.00669283, -0.53702112],
19:28:31        [-0.9179094...205, -0.19056921],
19:28:31        [ 0.08367027,  0.86406915,  1.26840093, ...,  0.36209913,
19:28:31          0.37135528,  0.2503814 ]])
19:28:31 dtype      = None
19:28:31 order      = None
19:28:31 
19:28:31 /opt/pyenv/versions/3.7.0/lib/python3.7/site-packages/numpy/core/numeric.py:538: ValueError
19:28:31 _______________________ TestDLTensorMemory.test_deleter ________________________
19:28:31 
19:28:31 self = <cupy_tests.core_tests.test_dlpack.TestDLTensorMemory testMethod=test_deleter>
19:28:31 
19:28:31     def test_deleter(self):
19:28:31         pool = cupy.get_default_memory_pool()
19:28:31         pool.free_all_blocks()
19:28:31         array = cupy.empty(10)
19:28:31         tensor = array.toDlpack()
19:28:31 >       assert pool.n_free_blocks() == 0
19:28:31 E       AssertionError: assert 2 == 0
19:28:31 E        +  where 2 = <built-in method n_free_blocks of cupy.cuda.memory.MemoryPool object at 0x7f42bac2bd30>()
19:28:31 E        +    where <built-in method n_free_blocks of cupy.cuda.memory.MemoryPool object at 0x7f42bac2bd30> = <cupy.cuda.memory.MemoryPool object at 0x7f42bac2bd30>.n_free_blocks
19:28:31 
19:28:31 array      = array([1.14937202e-001, 5.26889490e-002, 8.07467735e-001, 6.38091053e-001,
19:28:31        7.82251249e-001, 1.83094166e-001, 0.00000000e+000, 0.00000000e+000,
19:28:31        9.24424698e+081, 1.83456408e+206])
19:28:31 pool       = <cupy.cuda.memory.MemoryPool object at 0x7f42bac2bd30>
19:28:31 self       = <cupy_tests.core_tests.test_dlpack.TestDLTensorMemory testMethod=test_deleter>
19:28:31 tensor     = <capsule object "dltensor" at 0x7f4257329d20>
19:28:31 
19:28:31 tests/cupy_tests/core_tests/test_dlpack.py:40: AssertionError

@niboshi niboshi removed the st:test-and-merge (deprecated) Ready to merge after test pass. label Feb 13, 2019
@pentschev

pentschev commented Feb 13, 2019 •

Copy link
Copy Markdown
Member Author

Thanks for posting this, @niboshi. I forgot to mention before, but this PR is also related to #1650, and Jenkins will need to test with NumPy >= 1.16 and export NUMPY_EXPERIMENTAL_ARRAY_FUNCTION=1 before executing, this should fix the first error. Alternatively, I would suggest to add a pytest.importorksip() to run the test only if NumPy >= 1.17, it's still unreleased but __array_function__ is enabled by default.

The second error, from TestDLTensorMemory, seems unrelated to me and I can't reproduce it here. Is it possible that this is hardware related or a non-deterministic bug?

@pentschev

Copy link
Copy Markdown
Member Author

I fixed the conflict and took the liberty to skip the test if NumPy < 1.17. Please, let me know if that's acceptable.

@mrocklin

Copy link
Copy Markdown
Contributor

Checking in. Is there other work that remains to be done here?

@niboshi

niboshi commented Feb 26, 2019 •

Copy link
Copy Markdown
Contributor

I'm sorry, we have been considering how we should test this feature.
Current conclusion is not to test it for now.
Could you just remove the test?

We will restore the test once NumPy 1.17 will be released.

(For record) Possibilities included:

  1. Add a new CI matrix with NUMPY_EXPERIMENTAL_ARRAY_FUNCTION=1.
  2. Changing NumPy's internal flag from within the test.
  3. Use a child process to run the test (with NUMPY_EXPERIMENTAL_ARRAY_FUNCTION=1).

1. is costly in terms of effort.
2. just doesn't work; NumPy uses the flag on import.
3. Child process would have to start in spawn mode, but that mode has to be configured globally; not appropriate as something to be done in a test.

@niboshi

niboshi commented Feb 26, 2019

Copy link
Copy Markdown
Contributor

Ah, sorry, current test has @testing.with_requires('numpy>=1.17.0') so I think it's OK.

@niboshi

niboshi commented Feb 26, 2019

Copy link
Copy Markdown
Contributor

Jenkins, test this please

@chainer-ci

Copy link
Copy Markdown
Member

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

@niboshi

niboshi commented Feb 26, 2019

Copy link
Copy Markdown
Contributor

Jenkins failure is irrelevant to this PR.
TestDLTensorMemory failure is gone for some reason. I assume it was also irrelevant.

LGTM, thank you so much!

@niboshi
niboshi merged commit af7e5d4 into cupy:master Feb 26, 2019
@niboshi niboshi added this to the v6.0.0b3 milestone Feb 26, 2019
@mrocklin

Copy link
Copy Markdown
Contributor

Thank you for the review @niboshi !

I'm looking forward to seeing better CuPy and Dask integration in the future.

Thanks @pentschev for finding and fixing this.

@pentschev

Copy link
Copy Markdown
Member Author

Thanks @niboshi for taking the time to review. Thanks also @mrocklin for the help with reaching the solution.

@pentschev
pentschev deleted the fix-array-function-module-usage branch April 1, 2019 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants