filipemaia / afnumpy Goto Github PK
View Code? Open in Web Editor NEWA GPU-ready drop-in replacement for numpy.
License: BSD 2-Clause "Simplified" License
A GPU-ready drop-in replacement for numpy.
License: BSD 2-Clause "Simplified" License
When using slices larger than the array it raises an exception when it shouldn't.
When using arrays with elements larger than the array it returns non sensical values.
Examples:
import afnumpy
a = afnumpy.zeros((5))
a[1:8]
/Library/Python/2.7/site-packages/arrayfire-3.3.0-py2.7.egg/arrayfire/array.pyc in __getitem__(self, key)
992 return out
993 except RuntimeError as e:
--> 994 raise IndexError(str(e))
995
996
IndexError: ('In function dim_t af::calcDim(const af_seq &, const dim_t &)\nIn file src/backend/dim4.cpp:206\nInvalid dimension for argument 1\nExpected: seq.end < parentDim\n', 203)
import afnumpy
a = afnumpy.zeros((5))
print a[afnumpy.array([9])]
array([ 0.])
This example code fails on my machine (GTX 108, CUDA):
import afnumpy as af
import afnumpy.fft
import numpy as np
import numpy.fft
a = np.arange(10)
x,y = np.meshgrid(a,a)
c = afnumpy.fft.fftshift(af.array(x))
print('so far this works')
b = af.arange(10)
X,Y = af.meshgrid(b,b)
C = afnumpy.fft.fftshift(X)
so far this works
Traceback (most recent call last):
File "test_fftshift.py", line 13, in <module>
C = afnumpy.fft.fftshift(X)
File "/gpfs0/home/benedikt/software/afnumpy/afnumpy/fft.py", line 73, in fftshift
s = arrayfire.data.shift(x.d_array, *pu.c2f(shift))
File "/mnt/cbis/home/benedikt/miniconda3/envs/py3/lib/python3.6/site-packages/arrayfire/data.py", line 538, in shift
safe_call(backend.get().af_shift(c_pointer(out.arr), a.arr, d0, d1, d2, d3))
ctypes.ArgumentError: argument 3: <class 'TypeError'>: Don't know how to convert parameter 3
I don't know how difficult it could be. But I am thinking of merging your approach with a c++ ndarray library so that we could make framework that could wrap the c++ version of arrayfire like what afnumpy did.
Any comments?
Hi,
Could you overload the ndarray constructor, so that one can a pass raw pointer on device to create an instance, along with shape, dtype etc?
--dinesh
While having a afnumpy array (say xx). The command (-1)**xx, I get the following error :
TypeError Traceback (most recent call last)
in ()
----> 1 zz=(-1)**xx
/usr/local/lib/python2.7/dist-packages/afnumpy/multiarray.pyc in rpow(self, other)
207 s = arrayfire.pow(pu.raw(other), self.astype(type(other)).d_array)
208 else:
--> 209 s = arrayfire.pow(pu.raw(other), self.d_array)
210 return ndarray(self.shape, dtype=pu.typemap(s.dtype()), af_array=s)
211
/usr/local/lib/python2.7/dist-packages/arrayfire/arith.pyc in pow(lhs, rhs)
677 - If lhs
and rhs
are both af.Array, they must be of same size.
678 """
--> 679 return _arith_binary_func(lhs, rhs, backend.get().af_pow)
680
681 def pow2(a):
/usr/local/lib/python2.7/dist-packages/arrayfire/arith.pyc in _arith_binary_func(lhs, rhs, c_func)
40 lty = implicit_dtype(lhs, rhs.type())
41 other = Array()
---> 42 other.arr = constant_array(lhs, rdims[0], rdims[1], rdims[2], rdims[3], lty)
43 safe_call(c_func(ct.pointer(out.arr), other.arr, rhs.arr, _bcast_var.get()))
44
/usr/local/lib/python2.7/dist-packages/arrayfire/array.py in constant_array(val, d0, d1, d2, d3, dtype)
50 dtype = ct.c_int(dtype.value)
51 else:
---> 52 raise TypeError("Invalid dtype")
53
54 out = ct.c_void_p(0)
Is this supported yet ?
Hi,
Seeing this library made me wonder if anyone had tried using this as a numpy
drop-in for pandas
. If it works, I'd wonder why this isn't common practice yet (-> single-digit github stars), cuz it sounds pretty cool! :)
Seomthing like:
afnumpy.float32(numpy.ones((3,3)))
returns a numpy array instead of afnumpy. This is inconsistent with numpy.float32
chaging a python list input to a numpy array.
Hi,
I fetched and installed afnumpy via pip. When importing in my script I get an error message:
File "C:\winpython35\python-3.5.1.amd64\lib\site-packages\afnumpy__init__.py", line 3, in
from multiarray import ndarray, zeros, where, array, fromstring, arange, empty
ImportError: No module named 'multiarray'
As you can see, I'm on Python 3.5. Arrayfire is v3.3 and works flawless. Also I can see the multiarray.py file in the afnumpy folder. So the file is clearly not missing. Any ideas what's going wrong?
There's another question for when it's working: are arrays interchangeable between afnumpy and the arrayfire python wrapper? That means, are they the same?
Thanks in advance...
Update: When I comment out the line regarding multiarray it won't find the next import module, although all the .py files are right there.
How to convert afnumpy array to arrayfire array?
When slicing a multidimensional array like this:
a = afnumpy.ones((10,20))
print a[0]
print a[0,:]
array([ 1.])
array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1.])
wouldn't it be better if the first case would match the second case, always slicing the remaining dimensions rather than selecting the first item along all dimensions?
It would be great to have some of the functionality of numpy.pad
I am trying to do something like this:
import afnumpy as xp
num_rays=64
num_slices=64
true_obj=xp.ones((num_slices,num_rays,num_rays),dtype='float32')
pad_1D = num_rays//2
padding_array = ((0, 0), (pad_1D, pad_1D), (pad_1D, pad_1D))
true_obj = xp.pad(true_obj, padding_array, 'constant', constant_values = 0)
Hi,
I tested fft performance in comparison to vanilla numpy and reikna ( http://reikna.publicfields.net/ ). Reikna does not use clfft but has its own fft implementation. Strange thing: with complex128 dtype, afnumpy is faster than reikna as expected, with complex64 it's not. In fact, times stay the same for both dtypes with afnumpy. Could it be, that afnumpy and arrayfire use complex128 default for ffts? Speed should go up by 4x on my GPU for complex64.
Update: When I use arrayfire-python's ffts directly (not via afnumpy) speed is on par with reikna for both dtypes. So I guess somehow afnumpy's fft is complex128 under the hood...
This code example produces a segmentation fault on my GPU:
import afnumpy as af
import afnumpy.fft as fft
a = fft.fftn(af.zeros(100))
can you reproduce?
https://github.com/FilipeMaia/afnumpy/blob/master/afnumpy/linalg/linalg.py#L14
As implemented, afnumpy.dot
differs from the standard numpy definition
http://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html#numpy.dot
is this intentional?
Could you please add downloadable citations for publications?
Hi, Filipe. I want to know how to use the arrayfire function as arrayfire.matmul for matrix multiplication and other functions in arrayfire.linalg. Does it exist a easy way for this?
I tried to use af.arrayfire.matmul. However, it failed since it can not recognize the data type. To run the code, I have to copy the data from GPU to CPU, and then use numpy function instead of afnumpy.
Thanks.
as stated.
Thanks in advance for prompt reply
Hi,
Does creating a afnumpy array from numpy array moves data into device memeory? If not, is there a easy way to do it?
regards
There is an (seemingly abandoned) project that uses ctypes to port ArrayFire with similar goals:
https://github.com/arrayfire-community/arrayfire_python
Perhaps you can base your work off of it ?
Needs to be implemented.
a = afnumpy.ones((2,2))
a *= 1
raises an AttributeError
:
AttributeError Traceback (most recent call last)
<ipython-input-29-a0b68920a011> in <module>()
----> 1 b -= 0
/Library/Python/2.7/site-packages/afnumpy/decorators.pyc in wrapper(*args, **kws)
27 args = bcast_args
28
---> 29 ret = func(*args, **kws)
30 if len(ret.shape) == 0:
31 return ret[()]
/Library/Python/2.7/site-packages/afnumpy/multiarray.pyc in __isub__(self, other)
156 @iufunc
157 def __isub__(self, other):
--> 158 afnumpy.subtract(self, pu.raw(other), out=self)
159 return self
160
/Library/Python/2.7/site-packages/afnumpy/decorators.pyc in wrapper(*args, **kws)
10 if out is not None:
11 if(out.ndim):
---> 12 out[:] = ret
13 else:
14 out[()] = ret
/Library/Python/2.7/site-packages/afnumpy/multiarray.pyc in __setitem__(self, idx, value)
404 else:
405 raise NotImplementedError('values must be a afnumpy.ndarray')
--> 406 self.reshape(input_shape).d_array[idx] = value
407 # This is a hack to be able to make it look like arrays with stride[0] > 1 can still be views
408 # In practise right now it only applies to ndarray.real and ndarray.imag
/Library/Python/2.7/site-packages/afnumpy/__init__.pyc in inplace_setitem(self, key, val)
13 try:
14 n_dims = self.numdims()
---> 15 if (arrayfire.util._is_number(val)):
16 tdims = arrayfire.array._get_assign_dims(key, self.dims())
17 other_arr = arrayfire.array.constant_array(val, tdims[0], tdims[1], tdims[2], tdims[3], self.type())
AttributeError: 'module' object has no attribute '_is_number'
Same happens for +=
, -=
and /=
.
Hi,
let's start off-topic: thanks for fixing my benchmark script regarding fft speeds.
Here's an idea: yesterday I had a nice chat with pavanky. He showed me how to do fftshift with af's shift() function. This post is related. I took the liberty to put something together and compare with afnumpy. If I'm not mistaken, it's faster and more compact. Try it out...
Hm, file upload doesn't want my zip. I'll have to paste here:
import math, numpy as np, arrayfire as af, afnumpy as afnp
from afnumpy.fft import fftshift, ifftshift
from time import time
def my_fftshift(arr):
args = list(range(arr.numdims()))
for axis in args:
args[axis] = math.floor(arr.dims()[axis]/2)
return af.data.shift(arr, *args)
def my_ifftshift(arr):
args = list(range(arr.numdims()))
for axis in args:
args[axis] = math.ceil(arr.dims()[axis]/2)
return af.data.shift(arr, *args)
n = 2048 # array size
k = 100 # iterations
mydtype = np.complex64
# dimensionalities
#a = (np.random.rand(1,n)*(1+1j)).astype(mydtype) # 1D gives an error in afnumpy.fftshift()
a = (np.random.rand(n,n)*(1+1j)).astype(mydtype) # 2D
#a = (np.random.rand(n,n,2)*(1+1j)).astype(mydtype) # 3D
a_af = af.np_to_af_array(a)
a_afnp = afnp.array(a)
print( 'input dims: ',len(a.shape),'\tinput dtype: ',a_afnp.dtype)
# afnumpy block
s_afnp = fftshift(a_afnp)
af.device.sync()
t0 = time()
for i in range(k):
s_afnp = fftshift(a_afnp)
af.device.sync()
t_afnp = time()-t0
af.device.device_gc()
print( 'afnumpy:\t',t_afnp )
# af block
s_af = my_fftshift(a_af)
af.device.sync()
t0 = time()
for i in range(k):
s_af = my_fftshift(a_af)
af.device.sync()
t_af = time()-t0
af.device.device_gc()
print( 'af_shift:\t',t_af )
print( 'speedup:\t',t_afnp/t_af )
print( 'results match:\t',np.allclose(s_afnp,s_af) )
assume arrayfire array a is linked to asnumpy array b
and c = b.d_array
b*=2 will modify c
c+=123 will not modify b
but
c[:] += 123 will modify b
for details, you could refer to the code below.
import arrayfire as af
import afnumpy as afn
def ndarray_(s):
return afn.ndarray(s.shape, dtype=afn.pu.typemap(s.dtype()), af_array=s)
a = af.range(10) + 99
#b = afn.array(a) # a is copied to b
b = ndarray_(a) # a is linked to b
c = b.d_array
c[0] = af.log(c[0])
print(a)
print(b) # b is linked to c
print(c)
b *= 2
print(a)
print(b) # b is linked to c
print(c)
c[:] += 123
print(a)
print(b) # b is linked to c
print(c)
c += 123
print(a)
print(b) # b is linked to c
print(c)
As arrayfire does not support stride != 1
on the first dimension it's currently not possible to have views which are not contiguous in the last dimension, e.g. a[:,0]
or a[:,::2]
will not return a view.
Latest updates on PyPI say 2016, but there are examples that still use print-statement. Setup.py doesn't contain Python-version metadata. I'd love to know if this is supported in recent versions of Python, that would make it a great tool for teaching ML according to the mostly numpy-based examples in many Deep Learning books.
The following code works using numpy but on afnumpy it crashes with the message "Size mismatch between input and output". If mask has the same dimension as a it works fine.
a = afnumpy.ones((5, 3), dtype="float32")
mask = afnumpy.arange(5) > 2
a[mask, :] = 0.
Even for certain contiguous views flat is causing a copy:
a = afnumpy.ones((10,2))
a.d_array.device_ptr()
a.flat.d_array.device_ptr() # behaves as it should
b = a[:,:]
b.d_array.device_ptr() # behaves fine
b.flat.d_array.device_ptr() # the problem is here!
Hi, Filipe. Now I tried to use arrayfire function so that I needed to create array A in arrayfire and then change the type to afnumpy array by Aaf=afnumpy.array(A). However, it was very slow. I am wondering why it is so slow (both are in GPU). Or can you tell me how to make it faster?
Thanks.
Huibin
Currently ndarray.view()
is missing which makes it hard to use pyqtgraph
with afnumpy
arrays.
Hello again,
this is a feature request: It would be awesome to get some kind of sparse array support, most importantly spmv and spmm with a dense matrix, I realize this is a scipy function, but such basic linear algebra functionality would be nice, something along the lines of
S=csr_matrix((data, (row_ind, col_ind)), [shape=(M, N)])
vol=qtS #SpMV
where qt is a 1d vector or dense matrix with dimension (,M)
Cheers,
Stefano
Slicing an array with another boolean array does not seem to be supported (arrayfire_python branch):
a = af.random.rand(100)
a[a>0.5] = 0
/Users/benedikt/Library/Python/2.7/lib/python/site-packages/afnumpy/multiarray.pyc in __setitem__(self, idx, value)
374 else:
375 raise NotImplementedError('values must be a afnumpy.ndarray')
--> 376 self.d_array[idx] = value
377 # This is a hack to be able to make it look like arrays with stride[0] > 1 can still be views
378 # In practise right now it only applies to ndarray.real and ndarray.imag
/Users/benedikt/Library/Python/2.7/lib/python/site-packages/afnumpy/__init__.pyc in inplace_setitem(self, key, val)
29 other_arr))
30 except RuntimeError as e:
---> 31 raise IndexError(str(e))
32
33 arrayfire.Array.__setitem__ = inplace_setitem
IndexError: ('In function /Users/benedikt/phd-project/software/ptychography/arrayfire/src/api/c/assign.cpp(272):\nInvalid argument at index 3\nExpected: (idxType!=b8 )\n', 202)
This is a workaround
a = af.random.rand(100)
a[af.where(a>0.5)[0]] = 0
The current behaviour matches arrayfire instead of afnumpy.
import numpy
import afnumpy
b = numpy.random.random((2,2))
d = numpy.array([0,1])
a = afnumpy.array(b)
c = afnumpy.array(d)
print b[d,d]
[ 0.90872787 0.95795982]
print a[c,c]
array([[ 0.90872787, 0.24338165],
[ 0.07215943, 0.95795982]])
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.