def test_lif_speed(self, heterogeneous=True): """Test the speed of the lif nonlinearity heterogeneous: if true, use a wide range of population sizes. """ dt = 1e-3 ref = 2e-3 tau = 20e-3 if heterogeneous: n_neurons = [1.0e5] * 5 + [1e3] * 50 else: n_neurons = [1.1e5] * 5 J = RA([np.random.randn(n) for n in n_neurons]) V = RA([np.random.uniform(low=0, high=1, size=n) for n in n_neurons]) W = RA([ np.random.uniform(low=-10 * dt, high=10 * dt, size=n) for n in n_neurons ]) OS = RA([np.zeros(n) for n in n_neurons]) queue = cl.CommandQueue( ctx, properties=cl.command_queue_properties.PROFILING_ENABLE) clJ = CLRA(queue, J) clV = CLRA(queue, V) clW = CLRA(queue, W) clOS = CLRA(queue, OS) n_elements = [0, 2, 5, 10] for i, nel in enumerate(n_elements): plan = plan_lif(queue, clJ, clV, clW, clV, clW, clOS, ref, tau, dt, n_elements=nel) for j in range(1000): plan(profiling=True) print "plan %d: n_elements = %d" % (i, nel) print 'n_calls ', plan.n_calls print 'queued -> submit', plan.atime print 'submit -> start ', plan.btime print 'start -> end ', plan.ctime
def check_from_shapes( planner, alpha, beta, gamma, A_shapes, X_shapes, A_js, X_js, ): rng = np.random.RandomState(1234) A = RA([0.1 + rng.rand(*shp) for shp in A_shapes]) X = RA([0.1 + rng.rand(*shp) for shp in X_shapes]) Y = RA([0.1 + rng.rand( A_shapes[A_js[ii][0]][0], X_shapes[X_js[ii][0]][1]) for ii in range(len(A_js))]) A_js = RA(A_js) X_js = RA(X_js) # -- prepare initial conditions on device queue = cl.CommandQueue(ctx) clA = CLRA(queue, A) clX = CLRA(queue, X) clY = CLRA(queue, Y) clA_js = CLRA(queue, A_js) clX_js = CLRA(queue, X_js) assert allclose(A, clA) assert allclose(X, clX) assert allclose(Y, clY) assert allclose(A_js, clA_js) assert allclose(X_js, clX_js) # -- run cl computation prog = planner( queue, alpha, clA, clA_js, clX, clX_js, beta, clY, gamma=gamma) plans = prog.plans assert len(plans) == 1 plans[0]() # -- ensure they match for i in range(len(A_js)): ref = gamma + beta * Y[i] + alpha * sum( [np.dot(A[aj], X[xj]) for aj, xj in zip(A_js[i].ravel(), X_js[i].ravel())]) sim = clY[i] if not np.allclose(ref, sim, atol=1e-3, rtol=1e-3): print('A_shapes', A_shapes) print('X_shapes', X_shapes) if len(ref) > 20: print('ref', ref[:10], '...', ref[-10:]) print('sim', sim[:10], '...', sim[-10:]) else: print('ref', ref) print('sim', sim) assert 0
def _test_random(k=4, p=1, m=10, n=10): """ Parameters ---------- k : number of operations (length of A_js) p : number of dots per operation (width of A_js) m : output dimensions n : input dimensions """ rng = np.random.RandomState(3294) aa = [rng.normal(size=(m, n)) for i in range(k)] xx = [rng.normal(size=n) for i in range(k)] yy = [rng.normal(size=m) for i in range(k)] ajs = [rng.randint(k, size=p) for i in range(k)] xjs = [rng.randint(k, size=p) for i in range(k)] A = RA(aa) X = RA(xx) Y = RA(yy) A_js = RA(ajs) X_js = RA(xjs) alpha = 0.5 beta = 0.1 # -- prepare initial conditions on device queue = cl.CommandQueue(ctx) clA = CLRA(queue, A) clX = CLRA(queue, X) clY = CLRA(queue, Y) assert allclose(A, clA) assert allclose(X, clX) assert allclose(Y, clY) # -- run cl computation prog = plan_ragged_gather_gemv( queue, alpha, clA, A_js, clX, X_js, beta, clY) print('-' * 5 + ' Plans ' + '-' * 45) for plan in prog.plans: print(plan) plan() # -- ensure they match for i in range(k): ref = beta * Y[i] for aj, xj in zip(A_js[i].ravel(), X_js[i].ravel()): ref += alpha * np.dot(A[aj], X[xj]) sim = clY[i] assert np.allclose(ref, sim, atol=1e-3, rtol=1e-3)
def test_lif_speed(rng, heterogeneous): """Test the speed of the lif nonlinearity heterogeneous: if true, use a wide range of population sizes. """ dt = 1e-3 ref = 2e-3 tau = 20e-3 n_iters = 10 if heterogeneous: n_neurons = [1.0e5] * 50 + [1e3] * 5000 else: n_neurons = [1.1e5] * 50 n_neurons = list(map(int, n_neurons)) J = RA([rng.randn(n) for n in n_neurons]) V = RA([rng.uniform(low=0, high=1, size=n) for n in n_neurons]) W = RA( [rng.uniform(low=-10 * dt, high=10 * dt, size=n) for n in n_neurons]) OS = RA([np.zeros(n) for n in n_neurons]) queue = cl.CommandQueue( ctx, properties=cl.command_queue_properties.PROFILING_ENABLE) clJ = CLRA(queue, J) clV = CLRA(queue, V) clW = CLRA(queue, W) clOS = CLRA(queue, OS) for i, blockify in enumerate([False, True]): plan = plan_lif(queue, clJ, clV, clW, clV, clW, clOS, ref, tau, dt, blockify=blockify) with Timer() as timer: for j in range(n_iters): plan() print("plan %d: blockify = %s, dur = %0.3f" % (i, blockify, timer.duration))
def test_unit(rng): val = rng.randn() A = RA([val]) queue = cl.CommandQueue(ctx) clA = CLRA(queue, A) assert np.allclose(val, clA[0])
def make_random_ra(n, d, low=20, high=40, rng=None): """Helper to make a random RaggedArray on the host""" shapes = zip( *(rng.randint(low=low, high=high + 1, size=n).tolist() for dd in range(d)) ) vals = [rng.normal(size=shape).astype(np.float32) for shape in shapes] return RA(vals)
def test_reset(rng): # Yshapes = [(100,), (10, 17), (3, 3)] Yshapes = [(1000000, ), (1000, 1700), (3, 3)] values = rng.uniform(size=len(Yshapes)).astype(np.float32) queue = cl.CommandQueue(ctx) clY = CLRA(queue, RA([np.zeros(shape) for shape in Yshapes])) clvalues = to_device(queue, values) plan = plan_reset(queue, clY, clvalues) with Timer() as t: plan() print(t.duration) # with Timer() as t: # for i in range(len(clY)): # cl.enqueue_fill_buffer( # queue, clY.cl_buf.data, values[i], # clY.starts[i], clY.shape0s[i] * clY.shape1s[i]) # queue.finish() # print(t.duration) for y, v in zip(clY, values): assert np.all(y == v)
def test_small(rng): sizes = [3] * 3 vals = [rng.normal(size=size) for size in sizes] A = RA(vals) queue = cl.CommandQueue(ctx) clA = CLRA(queue, A) assert ra.allclose(A, clA.to_host())
def test_slicedcopy(rng): sizes = rng.randint(20, 200, size=10) A = RA([rng.normal(size=size) for size in sizes]) B = RA([rng.normal(size=size) for size in sizes]) incs = RA([rng.randint(0, 2) for _ in sizes]) Ainds = [] Binds = [] for size in sizes: r = np.arange(size, dtype=np.int32) u = rng.choice([0, 1, 2]) if u == 0: Ainds.append(r) Binds.append(r) elif u == 1: Ainds.append(r[:10]) Binds.append(r[-10:]) elif u == 2: n = rng.randint(2, size - 2) Ainds.append(rng.permutation(size)[:n]) Binds.append(rng.permutation(size)[:n]) Ainds = RA(Ainds) Binds = RA(Binds) queue = cl.CommandQueue(ctx) clA = CLRA(queue, A) clB = CLRA(queue, B) clAinds = CLRA(queue, Ainds) clBinds = CLRA(queue, Binds) clincs = CLRA(queue, incs) # compute on host for i in range(len(sizes)): if incs[i]: B[i][Binds[i]] += A[i][Ainds[i]] else: B[i][Binds[i]] = A[i][Ainds[i]] # compute on device plan = plan_slicedcopy(queue, clA, clB, clAinds, clBinds, clincs) plan() # check result for y, yy in zip(B, clB.to_host()): assert np.allclose(y, yy)
def test_elementwise_inc(rng): Xsizes = [(3, 3), (32, 64), (457, 342), (1, 100)] Asizes = [(3, 3), (1, 1), (457, 342), (100, 1)] A = RA([rng.normal(size=size) for size in Asizes]) X = RA([rng.normal(size=size) for size in Xsizes]) Y = RA([a * x for a, x in zip(A, X)]) queue = cl.CommandQueue(ctx) clA = CLRA(queue, A) clX = CLRA(queue, X) clY = CLRA(queue, RA([np.zeros_like(y) for y in Y])) # compute on device plan = plan_elementwise_inc(queue, clA, clX, clY) plan() # check result for y, yy in zip(Y, clY.to_host()): assert np.allclose(y, yy)
def test_basic(): # -- prepare initial conditions on host A = RA([[[0.1, .2], [.3, .4]], [[.5, .6]]], dtype=np.float32) X = RA([[3, 5]], dtype=np.float32) Y = RA([[0.0], [2, 3], ], dtype=np.float32) A_js = RA([[1], [0]], dtype=np.int32) X_js = RA([[0], [0]], dtype=np.int32) # alpha = 0.5 alpha = 1.0 # beta = 0.1 beta = 1.0 # -- prepare initial conditions on device queue = cl.CommandQueue(ctx) clA = CLRA(queue, A) clX = CLRA(queue, X) clY = CLRA(queue, Y) clA_js = CLRA(queue, A_js) clX_js = CLRA(queue, X_js) assert allclose(A, clA) assert allclose(X, clX) assert allclose(Y, clY) assert allclose(A_js, clA_js) assert allclose(X_js, clX_js) # -- run cl computation prog = plan_ragged_gather_gemv( queue, alpha, clA, A_js, clX, X_js, beta, clY) # plans = prog.choose_plans() # assert len(plans) == 1 for plan in prog.plans: plan() print(prog.plans[0].Ybuf.get()) # -- ensure they match for i in range(len(A_js)): aj, xj = int(A_js[i]), int(X_js[i]) ref = alpha * np.dot(A[aj], X[xj]) + beta * Y[i] sim = clY[i] assert np.allclose(ref, sim)
def test_lif_rate(n_elements): """Test the `lif_rate` nonlinearity""" rng = np.random dt = 1e-3 n_neurons = [123459, 23456, 34567] J = RA([rng.normal(loc=1, scale=10, size=n) for n in n_neurons]) R = RA([np.zeros(n) for n in n_neurons]) ref = 2e-3 taus = list(rng.uniform(low=15e-3, high=80e-3, size=len(n_neurons))) queue = cl.CommandQueue(ctx) clJ = CLRA(queue, J) clR = CLRA(queue, R) clTau = CLRA(queue, RA(taus)) # simulate host nls = [ LIFRate(tau_ref=ref, tau_rc=taus[i]) for i, n in enumerate(n_neurons) ] for i, nl in enumerate(nls): nl.step_math(dt, J[i], R[i]) # simulate device plan = plan_lif_rate(queue, clJ, clR, ref, clTau, dt=dt, n_elements=n_elements) plan() rate_sum = np.sum([np.sum(r) for r in R]) if rate_sum < 1.0: logger.warn("LIF rate was not tested above the firing threshold!") assert ra.allclose(J, clJ.to_host()) assert ra.allclose(R, clR.to_host())
def test_lif_rate(self, n_elements=0): """Test the `lif_rate` nonlinearity""" # n_neurons = [3, 3, 3] n_neurons = [123459, 23456, 34567] N = len(n_neurons) J = RA([np.random.normal(loc=1, scale=10, size=n) for n in n_neurons]) R = RA([np.zeros(n) for n in n_neurons]) ref = 2e-3 taus = list( np.random.uniform(low=15e-3, high=80e-3, size=len(n_neurons))) queue = cl.CommandQueue(ctx) clJ = CLRA(queue, J) clR = CLRA(queue, R) clTau = CLRA(queue, RA(taus)) ### simulate host nls = [ LIF(n, tau_ref=ref, tau_rc=taus[i]) for i, n in enumerate(n_neurons) ] for i, nl in enumerate(nls): R[i] = nl.rates(J[i].flatten()).reshape((-1, 1)) ### simulate device plan = plan_lif_rate(queue, clJ, clR, ref, clTau, n_elements=n_elements) plan() rate_sum = np.sum([np.sum(r) for r in R]) if rate_sum < 1.0: logger.warn("LIF rate was not tested above the firing threshold!") assert ra.allclose(J, clJ.to_host()) assert ra.allclose(R, clR.to_host())
def test_shape_zeros(): A = RA([[]]) assert A[0].shape == (0, 1)
def test_shape_zeros(): A = RA([[]], dtype=np.float32) assert A[0].shape == (0, 1)
def test_lif_step(upsample, n_elements): """Test the lif nonlinearity, comparing one step with the Numpy version.""" rng = np.random dt = 1e-3 n_neurons = [12345, 23456, 34567] J = RA([rng.normal(scale=1.2, size=n) for n in n_neurons]) V = RA([rng.uniform(low=0, high=1, size=n) for n in n_neurons]) W = RA([rng.uniform(low=-5 * dt, high=5 * dt, size=n) for n in n_neurons]) OS = RA([np.zeros(n) for n in n_neurons]) ref = 2e-3 taus = list(rng.uniform(low=15e-3, high=80e-3, size=len(n_neurons))) queue = cl.CommandQueue(ctx) clJ = CLRA(queue, J) clV = CLRA(queue, V) clW = CLRA(queue, W) clOS = CLRA(queue, OS) clTau = CLRA(queue, RA(taus)) # simulate host nls = [LIF(tau_ref=ref, tau_rc=taus[i]) for i, n in enumerate(n_neurons)] for i, nl in enumerate(nls): if upsample <= 1: nl.step_math(dt, J[i], OS[i], V[i], W[i]) else: s = np.zeros_like(OS[i]) for j in range(upsample): nl.step_math(dt / upsample, J[i], s, V[i], W[i]) OS[i] = (1. / dt) * ((OS[i] > 0) | (s > 0)) # simulate device plan = plan_lif(queue, clJ, clV, clW, clV, clW, clOS, ref, clTau, dt, n_elements=n_elements, upsample=upsample) plan() if 1: a, b = V, clV for i in range(len(a)): nc, _ = not_close(a[i], b[i]).nonzero() if len(nc) > 0: j = nc[0] print("i", i, "j", j) print("J", J[i][j], clJ[i][j]) print("V", V[i][j], clV[i][j]) print("W", W[i][j], clW[i][j]) print("...", len(nc) - 1, "more") n_spikes = np.sum([np.sum(os) for os in OS]) if n_spikes < 1.0: logger.warn("LIF spiking mechanism was not tested!") assert ra.allclose(J, clJ.to_host()) assert ra.allclose(V, clV.to_host()) assert ra.allclose(W, clW.to_host()) assert ra.allclose(OS, clOS.to_host())