Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[numpy 1.8.x] Array slices broken? #52

Closed
a1k0n opened this issue Oct 15, 2015 · 7 comments
Closed

[numpy 1.8.x] Array slices broken? #52

a1k0n opened this issue Oct 15, 2015 · 7 comments

Comments

@a1k0n
Copy link

a1k0n commented Oct 15, 2015

See the following test case:

import autograd.numpy as np
import autograd

def fn1(x):
    a = np.array([x[0], x[1]])
    b = np.array([x[2], x[3]])
    return np.dot(a, b)

f = autograd.value_and_grad(fn1)
print f(np.ones(4))

produces (2.0, array([ 1., 1., 1., 1.])), naturally.

The identical code using array slices for a and b:

def fn2(x):
    a = x[0:2]
    b = x[2:4]
    return np.dot(a, b)

f = autograd.value_and_grad(fn2)
print f(np.ones(4))

throws an exception in autograd/numpy/numpy_extra.pyc on line 123 in primitive_sum_arrays(*arrays), trying to call np.add.at. I haven't figured out which argument is the slice here.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-15a72619fe84> in <module>()
     16 
     17 f = autograd.value_and_grad(fn2)
---> 18 print f(np.ones(4))

/Library/Python/2.7/site-packages/autograd/convenience_wrappers.pyc in value_and_grad_fun(*args, **kwargs)
     45 
     46     def value_and_grad_fun(*args, **kwargs):
---> 47         gradval, val = gradval_and_val(*args, **kwargs)
     48         return val, gradval
     49 

/Library/Python/2.7/site-packages/autograd/convenience_wrappers.pyc in grad_and_aux_fun(*args, **kwargs)
     31             saved_aux.append(aux)
     32             return val
---> 33         gradval = grad(return_val_save_aux, argnum)(*args, **kwargs)
     34         return gradval, saved_aux[0]
     35 

/Library/Python/2.7/site-packages/autograd/core.pyc in gradfun(*args, **kwargs)
     15     the same type as the argument."""
     16     def gradfun(*args,**kwargs):
---> 17         return backward_pass(*forward_pass(fun,args,kwargs,argnum))
     18 
     19     try:

/Library/Python/2.7/site-packages/autograd/core.pyc in backward_pass(start_node, end_node, tape)
     52         node = tape.pop()
     53         if node.outgrads:
---> 54             cur_outgrad = node.sum_outgrads()
     55             assert type(new_node(getval(cur_outgrad))) == node.node_type, \
     56                 "Types are {0} and {1}".format(type(new_node(getval(cur_outgrad))), node.node_type)

/Library/Python/2.7/site-packages/autograd/core.pyc in sum_outgrads(self)
    154 
    155     def sum_outgrads(self):
--> 156         return self.node_type.sum_outgrads(self.outgrads)
    157 
    158 class Node(object):

/Library/Python/2.7/site-packages/autograd/numpy/numpy_extra.pyc in sum_outgrads(outgrads)
     46             return outgrads[0]
     47         else:
---> 48             return primitive_sum_arrays(*outgrads)
     49 
     50     @staticmethod

/Library/Python/2.7/site-packages/autograd/core.pyc in __call__(self, *args, **kwargs)
    111                         tapes.add(tape)
    112 
--> 113         result = self.fun(*argvals, **kwargs)
    114         if result is NotImplemented: return result
    115         if ops:

/Library/Python/2.7/site-packages/autograd/numpy/numpy_extra.pyc in primitive_sum_arrays(*arrays)
    121     for array in arrays:
    122         if isinstance(array, SparseArray):
--> 123             np.add.at(new_array, array.idx, array.val)
    124         else:
    125             new_array += array

TypeError: long() argument must be a string or a number, not 'slice'
@dougalm
Copy link
Contributor

dougalm commented Oct 15, 2015

Hi Andy, thanks for the bug report and for the nice minimal test case!

I think the issue is with numpy versions. Your test case works fine on my system, which has numpy 1.9.1. Is it possible that you're using an older version of numpy? Numpy's ufunc.add.at is supposed to support slices but maybe older versions don't? We ought to figure out which versions work and either list them as dependencies or implement a fallback option.

I added a version of your test to our test suite. Can you confirm that this test fails for you?

@a1k0n
Copy link
Author

a1k0n commented Oct 16, 2015

Hmm! It seems to be a Mac system numpy issue on my end, with both Yosemite and El Capitan.

I have two computers (both Macs), one Yosemite with numpy 1.10.0.post2 and one El Capitan with 1.8.0rc1, and both fail the test. Both also segfault trying to run nosetests.

I have an AWS node running Ubuntu with numpy 1.10.1 and that works just fine. And a Raspberry Pi 2 with numpy 1.10.1, also works fine.

El Capitan prevents me from upgrading the system numpy. If I upgrade to numpy 1.10.1 in a virtualenv, it works fine. And running nosetests no longer segfaults (though there are other unit test failures... I can open separate issues on those if you wish?)

@a1k0n
Copy link
Author

a1k0n commented Oct 16, 2015

...and on Yosemite I just upgraded to numpy 1.10.1, and I still get the same error, so it's definitely the mac system python that is unhappy about this, somehow. Weird. I'll dig a little more.

@a1k0n
Copy link
Author

a1k0n commented Oct 16, 2015

Ok, yeah, this is definitely a mac system numpy issue, nothing to do with autograd. Minimal test case is

np.add.at(np.zeros(2), slice(0, 2), 1)

Closing.

It turns out that the system Python includes numpy 1.8.0rc1, and attempting to upgrade it with pip actually doesn't work.

Thanks @dougalm for a really great library!

@a1k0n a1k0n closed this as completed Oct 16, 2015
@a1k0n a1k0n changed the title Array slices broken? [OS X] Array slices broken? Oct 16, 2015
@a1k0n
Copy link
Author

a1k0n commented Oct 16, 2015

Ah, hang on.

I was just checking the version dependency you brought up in your original comment, numpy==1.8.0 also fails the test, as does 1.8.1 and 1.8.2! 1.9.0 is actually the first version which passes the test.

So... I guess maybe the minimum requirement should be bumped up after all.

@a1k0n a1k0n reopened this Oct 16, 2015
@a1k0n a1k0n changed the title [OS X] Array slices broken? [numpy 1.8.x] Array slices broken? Oct 16, 2015
@mattjj mattjj closed this as completed in b2e7707 Oct 16, 2015
@mattjj
Copy link
Contributor

mattjj commented Oct 16, 2015

Thanks for the thorough testing! I bumped the numpy version requirement in setup.py to 1.9, hopefully that's the fix you had in mind.

@a1k0n
Copy link
Author

a1k0n commented Oct 16, 2015

Indeed it was, thanks!

I suppose it would be possible to support 1.8 by expanding the slice object but it's definitely not worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants