def _plan_AdaptiveLIF(self, ops): dt = self.model.dt J = self.all_data[[self.sidx[op.J] for op in ops]] V = self.all_data[[self.sidx[op.states[0]] for op in ops]] W = self.all_data[[self.sidx[op.states[1]] for op in ops]] N = self.all_data[[self.sidx[op.states[2]] for op in ops]] S = self.all_data[[self.sidx[op.output] for op in ops]] ref = self.RaggedArray( [op.neurons.tau_ref * np.ones(op.J.size) for op in ops], dtype=J.dtype) tau = self.RaggedArray( [op.neurons.tau_rc * np.ones(op.J.size) for op in ops], dtype=J.dtype) tau_n = self.RaggedArray( [op.neurons.tau_n * np.ones(op.J.size) for op in ops], dtype=J.dtype) inc_n = self.RaggedArray( [op.neurons.inc_n * np.ones(op.J.size) for op in ops], dtype=J.dtype) return [ plan_lif(self.queue, dt, J, V, W, S, ref, tau, N=N, tau_n=tau_n, inc_n=inc_n) ]
def test_lif_step(ctx, upsample): """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 = rng.uniform(low=15e-3, high=80e-3, size=len(n_neurons)) amp = 1. queue = cl.CommandQueue(ctx) clJ = CLRA(queue, J) clV = CLRA(queue, V) clW = CLRA(queue, W) clOS = CLRA(queue, OS) clTaus = CLRA(queue, RA([t * np.ones(n) for t, n in zip(taus, n_neurons)])) # simulate host nls = [nengo.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, dt, clJ, clV, clW, clOS, ref, clTaus, amp, 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())
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())
def plan_SimLIF(self, ops): J = self.all_data[[self.sidx[op.J] for op in ops]] V = self.all_data[[self.sidx[op.voltage] for op in ops]] W = self.all_data[[self.sidx[op.refractory_time] for op in ops]] S = self.all_data[[self.sidx[op.output] for op in ops]] ref = self.RaggedArray([op.nl.tau_ref for op in ops]) tau = self.RaggedArray([op.nl.tau_rc for op in ops]) dt = self.model.dt return [plan_lif(self.queue, J, V, W, V, W, S, ref, tau, dt, tag="lif", upsample=1)]
def test_lif_speed(ctx, 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 amp = 1.0 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], dtype=np.float32) V = RA([rng.uniform(low=0, high=1, size=n) for n in n_neurons], dtype=np.float32) W = RA( [rng.uniform(low=-10 * dt, high=10 * dt, size=n) for n in n_neurons], dtype=np.float32, ) OS = RA([np.zeros(n) for n in n_neurons], dtype=np.float32) 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, dt, clJ, clV, clW, clOS, ref, tau, amp, blockify=blockify) with Timer() as timer: for _ in range(n_iters): plan() print("plan %d: blockify = %s, dur = %0.3f" % (i, blockify, timer.duration))
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 = 1000 if heterogeneous: n_neurons = [1.0e5] * 50 + [1e3] * 500 else: n_neurons = [1.1e5] * 50 n_neurons = list(map(int, n_neurons)) J = RA([rng.randn(n) for n in n_neurons], dtype=np.float32) V = RA([rng.uniform(low=0, high=1, size=n) for n in n_neurons], dtype=np.float32) W = RA([rng.uniform(low=-10 * dt, high=10 * dt, size=n) for n in n_neurons], dtype=np.float32) OS = RA([np.zeros(n) for n in n_neurons], dtype=np.float32) 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, dt, clJ, clV, clW, clOS, ref, tau, 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)) print "Original LIF impl" for i, blockify in enumerate([False, True]): plan = plan_lif_old(queue, dt, clJ, clV, clW, clOS, ref, tau, 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 _plan_LIF(self, ops): if not all(op.neurons.min_voltage == 0 for op in ops): raise NotImplementedError("LIF min voltage") J = self.all_data[[self.sidx[op.J] for op in ops]] V = self.all_data[[self.sidx[op.states[0]] for op in ops]] W = self.all_data[[self.sidx[op.states[1]] for op in ops]] S = self.all_data[[self.sidx[op.output] for op in ops]] ref = self.RaggedArray([op.neurons.tau_ref * np.ones(op.J.size) for op in ops], dtype=J.dtype) tau = self.RaggedArray([op.neurons.tau_rc * np.ones(op.J.size) for op in ops], dtype=J.dtype) dt = self.model.dt return [plan_lif(self.queue, J, V, W, V, W, S, ref, tau, dt)]
def _plan_LIF(self, ops): if not all(op.neurons.min_voltage == 0 for op in ops): raise NotImplementedError("LIF min voltage") J = self.all_data[[self.sidx[op.J] for op in ops]] V = self.all_data[[self.sidx[op.states[0]] for op in ops]] W = self.all_data[[self.sidx[op.states[1]] for op in ops]] S = self.all_data[[self.sidx[op.output] for op in ops]] ref = self.RaggedArray( [op.neurons.tau_ref * np.ones(op.J.size) for op in ops], dtype=J.dtype) tau = self.RaggedArray( [op.neurons.tau_rc * np.ones(op.J.size) for op in ops], dtype=J.dtype) dt = self.model.dt return [plan_lif(self.queue, J, V, W, V, W, S, ref, tau, dt)]
def _plan_AdaptiveLIF(self, ops): dt = self.model.dt J = self.all_data[[self.sidx[op.J] for op in ops]] V = self.all_data[[self.sidx[op.states[0]] for op in ops]] W = self.all_data[[self.sidx[op.states[1]] for op in ops]] N = self.all_data[[self.sidx[op.states[2]] for op in ops]] S = self.all_data[[self.sidx[op.output] for op in ops]] ref = self.RaggedArray( [np.array(op.neurons.tau_ref, dtype=J.dtype) for op in ops]) tau = self.RaggedArray( [np.array(op.neurons.tau_rc, dtype=J.dtype) for op in ops]) tau_n = self.RaggedArray( [np.array(op.neurons.tau_n, dtype=J.dtype) for op in ops]) inc_n = self.RaggedArray( [np.array(op.neurons.inc_n, dtype=J.dtype) for op in ops]) return [plan_lif(self.queue, dt, J, V, W, S, ref, tau, N=N, tau_n=tau_n, inc_n=inc_n, n_elements=2)]
def _plan_AdaptiveLIF(self, ops): dt = self.model.dt J = self.all_data[[self.sidx[op.J] for op in ops]] V = self.all_data[[self.sidx[op.states[0]] for op in ops]] W = self.all_data[[self.sidx[op.states[1]] for op in ops]] N = self.all_data[[self.sidx[op.states[2]] for op in ops]] S = self.all_data[[self.sidx[op.output] for op in ops]] ref = self.RaggedArray([op.neurons.tau_ref * np.ones(op.J.size) for op in ops], dtype=J.dtype) tau = self.RaggedArray([op.neurons.tau_rc * np.ones(op.J.size) for op in ops], dtype=J.dtype) amp = self.RaggedArray([op.neurons.amplitude * np.ones(op.J.size) for op in ops], dtype=J.dtype) tau_n = self.RaggedArray([op.neurons.tau_n * np.ones(op.J.size) for op in ops], dtype=J.dtype) inc_n = self.RaggedArray([op.neurons.inc_n * np.ones(op.J.size) for op in ops], dtype=J.dtype) return [plan_lif(self.queue, dt, J, V, W, S, ref, tau, amp, N=N, tau_n=tau_n, inc_n=inc_n)]
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 if heterogeneous: n_neurons = [1.0e5] * 5 + [1e3] * 50 else: n_neurons = [1.1e5] * 5 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) # n_elements = [0, 1, 2, 5, 10, 50] n_elements = [0, 1, 2, 5] for i, nel in enumerate(n_elements): plan = plan_lif(queue, clJ, clV, clW, clV, clW, clOS, ref, tau, dt, n_elements=nel) with Timer() as timer: for j in range(1000): plan() print("plan %d: n_elements = %d, dur = %0.3f" % (i, nel, timer.duration))