예제 #1
0
def test_basic(ctx):
    # -- prepare initial conditions on host
    A = RA([[[0.1, 0.2], [0.3, 0.4]], [[0.5, 0.6]]])
    X = RA([[3, 5]])
    Y = RA([[0.0], [2, 3]])
    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)
    assert ra_allclose(A, clA)
    assert ra_allclose(X, clX)
    assert ra_allclose(Y, clY)

    # -- 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()

    # -- ensure they match
    for i, _ in enumerate(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_elementwise_inc(inc, use_alpha, ctx, rng, allclose):
    Xsizes = [(3, 3), (32, 64), (457, 342), (1, 100)]
    Asizes = [(3, 3), (1, 1), (457, 342), (100, 1)]

    A = [rng.normal(size=size).astype(np.float32) for size in Asizes]
    X = [rng.normal(size=size).astype(np.float32) for size in Xsizes]
    AX = [a * x for a, x in zip(A, X)]

    if use_alpha:
        alpha = rng.normal(size=len(Xsizes)).astype(np.float32)
        AX = [alph * ax for alph, ax in zip(alpha, AX)]
    else:
        alpha = None

    Y = [rng.normal(size=ax.shape).astype(np.float32) for ax in AX]
    if inc:
        Yref = [y + ax for y, ax in zip(Y, AX)]
    else:
        Yref = AX

    queue = cl.CommandQueue(ctx)
    clA = CLRA(queue, RA(A))
    clX = CLRA(queue, RA(X))
    clY = CLRA(queue, RA(Y))

    # compute on device
    plan = plan_elementwise_inc(queue, clA, clX, clY, alpha=alpha, inc=inc)
    plan()

    # check result
    for k, (yref, y) in enumerate(zip(Yref, clY.to_host())):
        assert allclose(y, yref, atol=2e-7), "Array %d not close" % k
예제 #3
0
def test_lif_rate(ctx, blockify):
    """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)
    clTaus = CLRA(queue, RA([t * np.ones(n) for t, n in zip(taus, n_neurons)]))

    # simulate host
    nls = [nengo.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, dt, clJ, clR, ref, clTaus, blockify=blockify)
    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())
예제 #4
0
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())
예제 #5
0
    def _test_random(self, 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 xrange(k)]
        xx = [rng.normal(size=n) for i in xrange(k)]
        yy = [rng.normal(size=m) for i in xrange(k)]
        ajs = [rng.randint(k, size=p) for i in xrange(k)]
        xjs = [rng.randint(k, size=p) for i in xrange(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)
        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, clA_js, clX, clX_js,
                                       beta, clY)

        print '-' * 5 + ' Plans ' + '-' * 45
        for plan in prog.plans:
            print plan
        prog()

        # -- ensure they match
        for i in xrange(k):
            ref = beta * Y[i]
            for aj, xj in zip(A_js[i], X_js[i]):
                ref += alpha * np.dot(A[aj], X[xj])
            sim = clY[i]
            assert np.allclose(ref, sim, atol=1e-3, rtol=1e-3)
예제 #6
0
def check_from_shapes(
    ctx,
    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, dtype=np.int32)
    X_js = RA(X_js, dtype=np.int32)
    # -- 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 = planner(queue, alpha, clA, A_js, clX, X_js, beta, clY, gamma=gamma)

    plans = prog.plans
    # assert len(plans) == 1
    for plan in plans:
        plan()

    # -- 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
예제 #7
0
    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 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))
예제 #9
0
def test_sparse(ctx, inc, rng, allclose):
    scipy_sparse = pytest.importorskip("scipy.sparse")

    # -- prepare initial conditions on host
    if 0:  # pylint: disable=using-constant-test
        # diagonal matrix
        shape = (32, 32)
        s = min(shape[0], shape[1])
        data = list(range(s))
        ii = list(range(s))
        jj = list(range(s))[::-1]
        A = scipy_sparse.coo_matrix((data, (ii, jj)), shape=shape).tocsr()
        X = RA([np.arange(1, shape[1] + 1)])
        Y = RA([np.arange(1, shape[0] + 1)])
    else:
        # random sparse matrix
        shape = (500, 500)
        sparsity = 0.002
        mask = rng.uniform(size=shape) < sparsity
        ii, jj = mask.nonzero()
        assert len(ii) > 0
        data = rng.uniform(-1, 1, size=len(ii))
        A = scipy_sparse.coo_matrix((data, (ii, jj)), shape=shape).tocsr()
        X = RA([rng.uniform(-1, 1, size=shape[1])])
        Y = RA([rng.uniform(-1, 1, size=shape[0])])

    # -- prepare initial conditions on device
    queue = cl.CommandQueue(ctx)
    A_data = to_device(queue, A.data.astype(np.float32))
    A_indices = to_device(queue, A.indices.astype(np.int32))
    A_indptr = to_device(queue, A.indptr.astype(np.int32))
    clX = CLRA(queue, X)
    clY = CLRA(queue, Y)
    assert allclose(X, clX)
    assert allclose(Y, clY)

    # -- run cl computation
    plan = plan_sparse_dot_inc(queue,
                               A_indices,
                               A_indptr,
                               A_data,
                               clX,
                               clY,
                               inc=inc)
    plan()

    # -- ensure they match
    ref = (Y[0] if inc else 0) + A.dot(X[0])
    sim = clY[0]
    assert allclose(ref, sim, atol=1e-7)
def test_reset(ctx, 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)
예제 #11
0
def test_unit(rng):
    val = rng.randn()
    A = RA([val])

    queue = cl.CommandQueue(ctx)
    clA = CLRA(queue, A)
    assert np.allclose(val, clA[0])
예제 #12
0
def test_discontiguous_setitem(ctx, rng):
    A = make_random_ra(3, 2, rng=rng)
    A0 = np.array(A[0])
    a = A0[::3, ::2]
    v = rng.uniform(-1, 1, size=a.shape)
    assert a.size > 0

    A.add_views(
        [A.starts[0]],
        [a.shape[0]],
        [a.shape[1]],
        [a.strides[0] / a.itemsize],
        [a.strides[1] / a.itemsize],
    )

    queue = cl.CommandQueue(ctx)
    clA = CLRA(queue, A)

    a[...] = v
    A[-1] = v
    assert np.allclose(A[0], A0)
    assert np.allclose(A[-1], v)

    print(clA[0].shape)
    print(clA[-1].shape)

    clA[-1] = v
    assert ra.allclose(A, clA.to_host())
    assert np.allclose(clA[-1], v)
def test_copy(ctx, rng):
    sizes = [(10, 1), (40, 64), (457, 342), (1, 100)]
    X = RA([rng.normal(size=size) for size in sizes])
    incs = rng.randint(0, 2, size=len(sizes)).astype(np.int32)

    queue = cl.CommandQueue(ctx)
    clX = CLRA(queue, X)
    clY = CLRA(queue, RA([np.zeros_like(x) for x in X]))

    # compute on device
    plan = plan_copy(queue, clX, clY, incs)
    plan()

    # check result
    for x, y in zip(X, clY.to_host()):
        assert np.allclose(y, x)
예제 #14
0
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)
예제 #16
0
def test_elementwise_inc(ctx, 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)
예제 #17
0
def make_random_pair(n, d=1, low=20, high=40):
    """Helper to make a pair of RaggedArrays, one host and one device"""
    shapes = zip(*(np.random.randint(low=low, high=high, size=n).tolist()
                   for dd in xrange(d)))
    vals = [np.random.normal(size=shape) for shape in shapes]
    A = RA(vals)

    queue = cl.CommandQueue(ctx)
    clA = CLRA(queue, A)
    return A, clA
def test_elementwise_inc_outer(ctx, rng):
    Xsizes = [3, 9, 15, 432]
    Asizes = [9, 42, 10, 124]
    alpha = rng.normal(size=len(Xsizes)).astype(np.float32)
    A = RA([rng.normal(size=size).astype(np.float32) for size in Asizes])
    X = RA([rng.normal(size=size).astype(np.float32) for size in Xsizes])
    AX = RA([alph * np.outer(a, x) for alph, a, x in zip(alpha, A, X)])
    Y = RA([rng.normal(size=ax.shape) for ax in AX])
    Yref = RA([y + ax for y, ax in zip(Y, AX)])

    queue = cl.CommandQueue(ctx)
    clA = CLRA(queue, A)
    clX = CLRA(queue, X)
    clY = CLRA(queue, Y)

    # compute on device
    plan = plan_elementwise_inc(queue, clA, clX, clY, alpha=alpha, outer=True)
    plan()

    # check result
    for yref, y in zip(Yref, clY.to_host()):
        assert np.allclose(y, yref, atol=2e-7)
예제 #19
0
    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())
예제 #20
0
    def test_basic(self):
        # -- prepare initial conditions on host
        A = RA([[[0.1, .2], [.3, .4]], [[.5, .6]]])
        X = RA([[3, 5]])
        Y = RA([
            [0.0],
            [2, 3],
        ])
        A_js = RA([[1], [0]])
        X_js = RA([[0], [0]])
        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)
        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
        plan = plan_ragged_gather_gemv(queue, alpha, clA, clA_js, clX, clX_js,
                                       beta, clY)

        plan()

        # -- ensure they match
        for i in xrange(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)
예제 #21
0
def test_linearfilter(ctx, n_per_kind, rng):
    kinds = (
        nengo.synapses.LinearFilter((2.,), (1.,), analog=False),
        nengo.synapses.Lowpass(0.005),
        nengo.synapses.Alpha(0.005),
        )
    assert len(n_per_kind) == len(kinds)
    kinds_n = [(kind, n) for kind, n in zip(kinds, n_per_kind) if n > 0]

    dt = 0.001
    steps = [kind.make_step((n,), (n,), dt, None, dtype=np.float32)
             for kind, n in kinds_n]
    A = RA([step.den for step in steps])
    B = RA([step.num for step in steps])

    X = RA([rng.normal(size=n) for kind, n in kinds_n])
    Y = RA([np.zeros(n) for kind, n in kinds_n])
    Xbuf = RA([np.zeros(shape) for shape in zip(B.sizes, X.sizes)])
    Ybuf = RA([np.zeros(shape) for shape in zip(A.sizes, Y.sizes)])

    queue = cl.CommandQueue(ctx)
    clA = CLRA(queue, A)
    clB = CLRA(queue, B)
    clX = CLRA(queue, X)
    clY = CLRA(queue, Y)
    clXbuf = CLRA(queue, Xbuf)
    clYbuf = CLRA(queue, Ybuf)

    n_calls = 3
    plans = plan_linearfilter(queue, clX, clY, clA, clB, clXbuf, clYbuf)
    with Timer() as timer:
        for _ in range(n_calls):
            [plan() for plan in plans]

    print(timer.duration)

    for i, [kind, n] in enumerate(kinds_n):
        n = min(n, 100)
        step = kind.make_step((n, 1), (n, 1), dt, None, dtype=np.float32)

        x = X[i][:n]
        y = np.zeros_like(x)
        for _ in range(n_calls):
            y[:] = step(0, x)

        z = clY[i][:n]
        assert np.allclose(z, y, atol=1e-7, rtol=1e-5), kind
예제 #22
0
def make_random_pair(n, d, **kwargs):
    """Helper to make a pair of RaggedArrays, one host and one device"""
    A = make_random_ra(n, d, **kwargs)
    queue = cl.CommandQueue(ctx)
    clA = CLRA(queue, A)
    return A, clA
def test_linearfilter(ctx, n_per_kind, rng):
    kinds = (
        nengo.synapses.LinearFilter((2.0, ), (1.0, ), analog=False),
        nengo.synapses.Lowpass(0.005),
        nengo.synapses.Alpha(0.005),
    )
    assert len(n_per_kind) == len(kinds)
    kinds_n = [(kind, n) for kind, n in zip(kinds, n_per_kind) if n > 0]

    dt = 0.001
    steps = list()
    for kind, n in kinds_n:
        state = kind.make_state((n, ), (n, ), dt, dtype=np.float32)
        step = kind.make_step((n, ), (n, ), dt, rng=None, state=state)
        steps.append(step)

    # Nengo 3 uses state space filters. For now, convert back to transfer function.
    # Getting rid of this conversion would require a new plan_linearfilter.
    dens = list()
    nums = list()
    for f in steps:
        if type(f).__name__ == "NoX":  # special case for a feedthrough
            den = np.array([1.0])
            num = f.D
        else:
            num, den = ss2tf(f.A, f.B, f.C, f.D)

        # This preprocessing copied out of nengo2.8/synapses.LinearFilter.make_step
        num = num.flatten()

        assert den[0] == 1.0
        num = num[1:] if num[0] == 0 else num
        den = den[1:]  # drop first element (equal to 1)
        num, den = num.astype(np.float32), den.astype(np.float32)
        dens.append(den)
        nums.append(num)

    A = RA(dens)
    B = RA(nums)

    X = RA([rng.normal(size=n) for kind, n in kinds_n])
    Y = RA([np.zeros(n) for kind, n in kinds_n])
    Xbuf = RA([np.zeros(shape) for shape in zip(B.sizes, X.sizes)])
    Ybuf = RA([np.zeros(shape) for shape in zip(A.sizes, Y.sizes)])

    queue = cl.CommandQueue(ctx)
    clA = CLRA(queue, A)
    clB = CLRA(queue, B)
    clX = CLRA(queue, X)
    clY = CLRA(queue, Y)
    clXbuf = CLRA(queue, Xbuf)
    clYbuf = CLRA(queue, Ybuf)

    n_calls = 3
    plans = plan_linearfilter(queue, clX, clY, clA, clB, clXbuf, clYbuf)
    with Timer() as timer:
        for _ in range(n_calls):
            for plan in plans:
                plan()

    print(timer.duration)

    for i, [kind, n] in enumerate(kinds_n):
        n = min(n, 100)
        state = kind.make_state((n, ), (n, ), dt, dtype=np.float32)
        step = kind.make_step((n, ), (n, ), dt, rng=None, state=state)

        x = X[i][:n].T
        y = np.zeros_like(x)
        for _ in range(n_calls):
            y[:] = step(0, x)

        z = clY[i][:n].T
        assert np.allclose(z, y, atol=1e-7, rtol=1e-5), kind
예제 #24
0
    def test_lif_step(self, upsample=1, n_elements=0):
        """Test the lif nonlinearity, comparing one step with the Numpy version."""
        dt = 1e-3
        # n_neurons = [3, 3, 3]
        n_neurons = [12345, 23456, 34567]
        N = len(n_neurons)
        J = RA([np.random.normal(scale=1.2, size=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=-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
        # tau = 20e-3
        # refs = list(np.random.uniform(low=1.7e-3, high=4.2e-3, size=len(n_neurons)))
        taus = list(
            np.random.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)
        # clRef = CLRA(queue, RA(refs))
        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):
            if upsample <= 1:
                nl.step_math0(dt, J[i], V[i], W[i], OS[i])
            else:
                s = np.zeros_like(OS[i])
                for j in xrange(upsample):
                    nl.step_math0(dt / upsample, J[i], V[i], W[i], s)
                    OS[i] = (OS[i] > 0.5) | (s > 0.5)

        ### 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 xrange(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())
예제 #25
0
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
    plan = planner(queue,
                   alpha,
                   clA,
                   clA_js,
                   clX,
                   clX_js,
                   beta,
                   clY,
                   gamma=gamma)

    plan()

    # -- ensure they match
    for i in xrange(len(A_js)):
        #print 'gamma', gamma
        #print 'Y[i] * beta + gamma', Y[i] * beta + gamma
        #print A[0]
        #print X[0]
        #print 'AX', sum(
        #[np.dot(A[aj], X[xj])
        #for aj, xj in zip(A_js[i], X_js[i])])
        ref = gamma + beta * Y[i] + alpha * sum(
            [np.dot(A[aj], X[xj]) for aj, xj in zip(A_js[i], X_js[i])])
        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