Beispiel #1
0
def main():
    config = None
    with open('./session.json') as json_file:
        config = from_json(json_file.read())
    net = OpenRE(config)
    net.deploy()
    net.run()
Beispiel #2
0
def _test_iobase_device(device_type):
    from openre import OpenRE
    config = {
        'layers': [
            {
                'name': 'V1',
                'width': 16,
                'height': 10,
                'relaxation': 0,
            },
        ],
        'domains': [
            {
                'name'        : 'D1',
                'device'    : {
                    'type': device_type,
                    'width': 16,
                    'height': 10,
                    'output': [
                        # [c1 c2]
                        # [c3 c4]
                        {'name': 'c1', 'shape': [0, 0, 8, 5]},
                        {'name': 'c2', 'shape': [8, 0, 8, 5]},
                        {'name': 'c3', 'shape': [0, 5, 8, 5]},
                        {'name': 'c4', 'shape': [8, 5, 8, 5]},
                    ],
                },
            },
            {
                'name'        : 'D2',
                'device'    : {
                    'type': 'OpenCL',
                },
                'layers'    : [
                    {'name': 'V1', 'input': 'c1', 'shape': [0, 0, 8, 5]},
                    {'name': 'V1', 'input': 'c2', 'shape': [8, 0, 8, 5]},
                    {'name': 'V1', 'input': 'c3', 'shape': [0, 5, 8, 5]},
                    {'name': 'V1', 'input': 'c4', 'shape': [8, 5, 8, 5]},
                ],
            },
        ],
    }
    ore = OpenRE(config)
    ore.deploy()
    device1 = ore.domains[0].device
    device2 = ore.domains[1].device
    D1 = ore.domains[0]
    D2 = ore.domains[1]
    D2.neurons.level.data[:] = 0
    D2.neurons.level.to_device(device2)
    ore.tick()

    D1.neurons.from_device(device1)
    D2.neurons.from_device(device2)
    for layer_index, layer in enumerate(D2.layers):
        for x in xrange(8):
            for y in xrange(5):
                assert layer.neurons_metadata.level[x, y] == layer_index
Beispiel #3
0
def test_output_index():
    index = OutputIndex()

    from openre import OpenRE
    from openre import neurons
    config = {
        'layers': [
            {
                'name': 'V1',
                'width': 16,
                'height': 10,
                'threshold': 255,
                'relaxation': 0,
            },
        ],
        'domains': [
            {
                'name'        : 'D1',
                'device'    : {
                    'type': 'OpenCL',
                    'threshold_inc': 0,
                    'threshold_dec': 0
                },
                'layers'    : [
                    {'name': 'V1', 'output': 'o1', 'shape': [0, 0, 8, 5]},
                    {'name': 'V1', 'shape': [8, 0, 8, 5]},
                    {'name': 'V1', 'output': 'o3', 'shape': [0, 5, 8, 5]},
                    {'name': 'V1', 'shape': [8, 5, 8, 5]},
                ],
            },
        ],
    }
    ore = OpenRE(config)
    ore.deploy()
    D1 = ore.domains[0]
    assert len(index.address.data) == 0
    D1.neurons.level.data[:] = 0
    device1 = ore.domains[0].device
    for layer in D1.layers:
        index.add(layer)
    index.shrink()
    assert len(index.address.data) == 80
    assert len(index.data.data) == len(index.address.data)
    assert len(index.tick.data) == len(index.address.data)
    assert index.data.data.dtype.type == types.output
    assert list(index.address.data) \
            == range(40) + [80 + x for x in range(40)]
    for i in xrange(len(index.data.data)):
        index.data.data[i] = i
    assert index.data_to_send()
    was = {}
    for source_id, data in index.data_to_send():
        if source_id == 'o1':
            assert list(data) == range(40)
        if source_id == 'o3':
            assert list(data) == [40 + x for x in range(40)]
        was[source_id] = 1
    assert was == {'o1': 1, 'o3':1}
    assert list(D1.output_index.address.data) == list(index.address.data)
    assert len(D1.output_index.address.data) == len(index.address.data)
    for _ in xrange(255):
        num = -1
        for layer in D1.layers:
            for i in xrange(len(layer.neurons_metadata.level)):
                num += 1
                layer.neurons_metadata.level[i] += num
        D1.neurons.level.to_device(device1)
        ore.tick()
        D1.neurons.level.from_device(device1)
        D1.neurons.flags.from_device(device1)
        oi = D1.output_index
        oi.data.from_device(device1)
        oi.tick.from_device(device1)
        for index, neuron_address in enumerate(oi.address.data):
            if D1.neurons.flags[neuron_address] & neurons.IS_SPIKED:
                assert oi.tick[index] == D1.ticks
        assert oi.tick[0] == 0
    assert oi.tick.data[1] == 255
    assert oi.data.data[1] == 1
    assert oi.tick.data[2] == 255
    assert oi.data.data[2] == 129
Beispiel #4
0
def test_camera():
    from openre import OpenRE
    import os
    config = {
        'layers': [
            {
                'name': 'V1',
                'relaxation': 0,
                'width': 16,
                'height': 10,
            },
        ],
        'domains': [
            {
                'name'        : 'Camera',
                'device'    : {
                    'type': 'GrayVideo',
                    'device': os.path.join(
                        os.path.dirname(__file__),
                        './templates/device/camera-test.avi'
                    ),
                    'width': 16,
                    'height': 10,
                    #'window': 'Cam input',
                    'output': [
                        # [c1 c2]
                        # [c3 c4]
                        {'name': 'c1', 'shape': [0, 0, 8, 5]},
                        {'name': 'c2', 'shape': [8, 0, 8, 5]},
                        {'name': 'c3', 'shape': [0, 5, 8, 5]},
                        {'name': 'c4', 'shape': [8, 5, 8, 5]},
                    ],
                },
            },
            {
                'name'        : 'D2',
                'device'    : {
                    'type': 'OpenCL',
                },
                'layers'    : [
                    {'name': 'V1', 'input': 'c1', 'shape': [0, 0, 8, 5]},
                    {'name': 'V1', 'input': 'c2', 'shape': [8, 0, 8, 5]},
                    {'name': 'V1', 'input': 'c3', 'shape': [0, 5, 8, 5]},
                    {'name': 'V1', 'input': 'c4', 'shape': [8, 5, 8, 5]},
                ],
            },
        ],
    }
    ore = OpenRE(config)
    ore.deploy()
    device1 = ore.domains[0].device
    device2 = ore.domains[1].device
    D1 = ore.domains[0]
    D2 = ore.domains[1]
    D2.neurons.level.data[:] = 0
    assert sum(D2.neurons.level.data) == 0
    D2.neurons.level.to_device(device2)
    ore.tick()
    D1.neurons.from_device(device1)
    D2.neurons.from_device(device2)
    arr = D2.neurons.level.data
    a1 = arr[0:40]
    a2 = arr[40:80]
    a3 = arr[80:120]
    a4 = arr[120:160]
    check = numpy.array(
        [[  0,   0,   0,   0,   0,   0,   0,   0,  20,  20,  20,  20,  20,  20,  20,  20],
         [  0,   0,   0,   0,   0,   0,   0,   0,  20,  20,  20,  20,  20,  20,  20,  20],
         [  2,   2,   2,   2,   2,   2,   2,   2,  21,  21,  21,  21,  21,  21,  21,  21],
         [  2,   2,   2,   2,   2,   2,   2,   2,  21,  21,  21,  21,  21,  21,  21,  21],
         [ 19,  19,  19,  19,  19,  19,  19,  19,  19,  19,  19,  19,  19,  19,  19,  19],
         [115, 115, 115, 115, 115, 115, 115, 115, 245, 245, 245, 245, 245, 245, 245, 245],
         [116, 116, 116, 116, 116, 116, 116, 116, 254, 254, 254, 254, 254, 254, 254, 254],
         [116, 116, 116, 116, 116, 116, 116, 116, 254, 254, 254, 254, 254, 254, 254, 254],
         [114, 114, 114, 114, 114, 114, 114, 114, 255, 255, 255, 255, 255, 255, 255, 255],
         [114, 114, 114, 114, 114, 114, 114, 114, 255, 255, 255, 255, 255, 255, 255, 255],],
        dtype=numpy.uint8
    )
    assert list(numpy.ravel(check[0:5, 0:8])) == list(a1)
    assert list(numpy.ravel(check[0:5, 8:16])) == list(a2)
    assert list(numpy.ravel(check[5:10, 0:8])) == list(a3)
    assert list(numpy.ravel(check[5:10, 8:16])) == list(a4)
    assert sum(D2.neurons.level.data) > 0
    ore.clean()
Beispiel #5
0
def test_output():
    from openre import OpenRE
    config = {
        'layers': [
            {
                'name': 'V1',
                'relaxation': 0,
                'width': 16,
                'height': 10,
            },
            {
                'name': 'V2',
                'relaxation': 0,
                'width': 16,
                'height': 10,
            },
        ],
        'domains': [
            {
                'name'        : 'D1',
                'device'    : {
                    'type': 'OpenCL',
                },
                'layers'    : [
                    {'name': 'V2', 'output': 'o1', 'shape': [0, 0, 8, 5]},
                    {'name': 'V2', 'output': 'o2', 'shape': [8, 0, 8, 5]},
                    {'name': 'V2', 'output': 'o3', 'shape': [0, 5, 8, 5]},
                    {'name': 'V2', 'output': 'o4', 'shape': [8, 5, 8, 5]},
                ],
            },
            {
                'name'        : 'D2',
                'device'    : {
                    'type': 'OpenCL',
                },
                'layers'    : [
                    {'name': 'V1', 'input': 'o1', 'shape': [0, 0, 8, 5]},
                    {'name': 'V1', 'input': 'o2', 'shape': [8, 0, 8, 5]},
                    {'name': 'V1', 'input': 'o3', 'shape': [0, 5, 8, 5]},
                    {'name': 'V1', 'input': 'o4', 'shape': [8, 5, 8, 5]},
                ],
            },
        ],
    }
    ore = OpenRE(config)
    ore.deploy()
    device1 = ore.domains[0].device
    device2 = ore.domains[1].device
    D1 = ore.domains[0]
    D2 = ore.domains[1]
    D1.neurons.level.data[0] = 35000
    D2.neurons.level.data[:] = 0
    D1.neurons.level.to_device(device1)
    D2.neurons.level.to_device(device2)
    ore.tick()
    D1.neurons.from_device(device1)
    D2.neurons.from_device(device2)
    assert D2.neurons.level.data[0] == 255
    D1.neurons.level.data[1] = 35000
    D1.neurons.level.to_device(device1)
    ore.tick()
    D1.neurons.from_device(device1)
    D2.neurons.from_device(device2)
    assert D2.neurons.level.data[0] == 255 + 254
    assert D2.neurons.level.data[1] == 254
Beispiel #6
0
def check_input(expire):
    import numpy
    from openre import OpenRE
    from openre.vector import StandaloneVector
    from openre import neurons
    import logging
    # remote domains
    config = {
        'layers': [
            {
                'name': 'Input',
                'width': 10,
                'height': 10,
                'is_inhibitory': False,
                'threshold': 128,
                'relaxation': 0,
                'connect': [
                    {
                        'name': 'V2',
                        'radius': 1,
                        'shift': [0, 0],
                    },
                ],
            },
            {
                'name': 'V2',
                'width': 10,
                'height': 10,
                'relaxation': 0,
            },
        ],
        'domains': [
            {
                'name'        : 'D1',
                'device'    : {
                    'type': 'OpenCL',
                    'threshold_inc': 0,
                    'threshold_dec': 0
                },
                'layers'    : [
                    {'name': 'Input', 'shape': [0, 0, 5, 5], 'expire': expire},
                    {'name': 'Input', 'shape': [0, 5, 5, 5], 'expire': expire},
                    {'name': 'Input', 'shape': [5, 0, 5, 5], 'expire': expire},
                    {'name': 'Input', 'shape': [5, 5, 5, 5], 'expire': expire},
                ],
            },
            {
                'name'        : 'D2',
                'device'    : {
                    'type': 'OpenCL',
                    'threshold_inc': 0,
                    'threshold_dec': 0
                },
                'layers'    : [
                    {'name': 'V2'},
                ],
            },
        ],
    }
    ore = OpenRE(config)
    ore.deploy()
    device1 = ore.domains[0].device
    device2 = ore.domains[1].device
    D1 = ore.domains[0]
    D2 = ore.domains[1]
    arr = numpy.arange(0, 200, 2, dtype=numpy.uint8)

    #arr = StandaloneVector(numpy.reshape(arr, (10, 10)))
    arr = numpy.reshape(arr, (10, 10))
    l0 = arr[0:5, 0:5]
    l1 = arr[5:10, 0:5]
    l2 = arr[0:5, 5:10]
    l3 = arr[5:10, 5:10]
    # somewhere here we will send packet to the input device
    # and in input device will receive it:
    layer_data = [l0, l1, l2, l3]
    for layer_index, data in enumerate(layer_data):
        if layer_index % 2:
            D1.register_input_layer_data(
                layer_index,
                StandaloneVector(data).bytes()
            )
        else:
            D1.register_input_layer_data(
                layer_index,
                data
            )
    neurons_length = 0
    for layer_index, data in enumerate(layer_data):
        layer = D1.layers[layer_index]
        vector = StandaloneVector()
        if isinstance(layer.input_data, basestring):
            vector.from_bytes(layer.input_data)
        else:
            vector.set_data(layer.input_data)
        assert list(vector.data) == list(numpy.ravel(data))
        assert len(vector.data) == len(numpy.ravel(data))
        neurons_length += len(vector.data)
    assert len(D1.neurons.level) == neurons_length
    D1.neurons.level.data[:] = 0
    D1.neurons.level.to_device(device1)
    D2.neurons.level.data[:] = 0
    D2.neurons.level.to_device(device2)
    ore.tick()
    D1.neurons.from_device(device1)
    D2.neurons.from_device(device2)
    level_check \
            = numpy.ravel(numpy.concatenate(layer_data)).astype(numpy.uint32)
    level_check[level_check >= 128] -= 128
    assert list(D1.neurons.level.data) == list(level_check)
    flags_check = numpy.ravel(numpy.concatenate(layer_data))
    flags_check[flags_check < 128] = neurons.IS_TRANSMITTER
    flags_check[flags_check >= 128] = neurons.IS_TRANSMITTER | neurons.IS_SPIKED
    assert list(flags_check) == list(D1.neurons.flags.data)
    # check D2 neuron level
    flags2_check = numpy.ravel(numpy.concatenate(layer_data))
    level2_check = numpy.copy(D2.synapses.level.data)
    level2_check[flags2_check < 128] = 0
    neurons2_level = numpy.reshape(
        D2.neurons.level.data[0:len(level2_check)],
        (10, 10)
    )
    l20 = neurons2_level[0:5, 0:5]
    l21 = neurons2_level[5:10, 0:5]
    l22 = neurons2_level[0:5, 5:10]
    l23 = neurons2_level[5:10, 5:10]
    layer2_data = [l20, l21, l22, l23]
    # D2 neuron level should be eq to synapses levels
    assert list(numpy.ravel(numpy.concatenate(layer2_data))) \
            == list(level2_check)
    try:
        for pass_num in range(2, 4):
            ore.tick()
            D1.neurons.from_device(device1)
            D2.neurons.from_device(device2)
            if expire and expire >= pass_num:
                level_check = level_check \
                        + numpy.ravel(numpy.concatenate(layer_data))
            level_check[level_check >= 128] -= 128
            assert list(D1.neurons.level.data) == list(level_check)
            if not expire or expire < pass_num:
                for layer_index, data in enumerate(layer_data):
                    assert layer.input_data_cache is None
                    assert layer.input_data is None
    except AssertionError:
        logging.warn('Expire #%s, pass #%s', expire, pass_num)
        raise
Beispiel #7
0
def test_device():
    if cl is None:
        # skip test
        return
    from openre import OpenRE
    import numpy as np
    from openre import neurons
    synapse_max_level = 30000
    config = {
        'synapse': {
            'max_level': synapse_max_level,
            'spike_learn_threshold': 2,
        },
        'layers': [
            {
                'name': 'V1',
                'threshold': synapse_max_level,
                'relaxation': 1000,
                'width': 20,
                'height': 20,
                'is_inhibitory': True,
                'connect': [
                    {
                        'name': 'V2',
                        'radius': 1,
                        'shift': [0, 0],
                    },
                ],
            },
            {
                'name': 'V2',
                'threshold': synapse_max_level,
                'relaxation': 1000,
                'width': 20,
                'height': 20,
            },
        ],
        'domains': [
            {
                'name'        : 'D1',
                'device'    : {
                    'type': 'OpenCL',
                    'threshold_inc': 0,
                    'threshold_dec': 0
                },
                'stat_size': 1,
                'layers'    : [
                    {'name': 'V1'},
                    {'name': 'V2'},
                ],
            },
        ],
    }
    ore = OpenRE(config)
    ore.deploy()
    assert isinstance(ore.domains[0].device, OpenCL)
    assert ore.domains[0].neurons.level.device_data_pointer
    assert ore.domains[0].layers_vector.threshold.device_data_pointer
    domain = ore.domains[0]
    layer = domain.layers[0]
    layer2 = domain.layers[1]
    device = ore.domains[0].device
    max_vitality = types.max(types.vitality)

    # check lengths
    assert len(domain.synapses.level) == 400
    assert len(domain.synapses) == 400
    assert domain.neurons.length == 800
    assert layer.neurons_metadata.threshold[0] == synapse_max_level
    assert layer.neurons_metadata.level.length == 400
    assert layer2.neurons_metadata.level.length == 400
    for field, field_type in domain.synapses_metadata.__class__.fields:
        assert getattr(domain.synapses_metadata, field).length == 400
        assert len(getattr(domain.synapses, field).data) == 400
    assert domain.pre_synapse_index.key.length == 800
    assert domain.pre_synapse_index.value.length == 400

    assert domain.post_synapse_index.key.length == 800
    assert domain.post_synapse_index.value.length == 400

    # prepare neurons
    layer.neurons_metadata.level[0, 0] = synapse_max_level
    assert not layer.neurons_metadata.flags[0, 0] & neurons.IS_SPIKED

    layer.neurons_metadata.level[0, 1] = layer.relaxation + 1
    layer.neurons_metadata.flags[0, 1] |= neurons.IS_SPIKED
    assert layer.neurons_metadata.flags[0, 1] | neurons.IS_SPIKED

    layer.neurons_metadata.level[0, 2] = synapse_max_level
    layer.neurons_metadata.flags[0, 2] |= neurons.IS_DEAD
    layer.neurons_metadata.flags[0, 2] |= neurons.IS_SPIKED

    layer.neurons_metadata.level[0, 3] = synapse_max_level
    layer.neurons_metadata.flags[0, 3] |= neurons.IS_RECEIVER
    layer.neurons_metadata.flags[0, 3] |= neurons.IS_SPIKED

    layer.neurons_metadata.level[0, 4] = -1

    layer.neurons_metadata.level[0, 5] = -1

    layer.neurons_metadata.level[0, 6] = synapse_max_level
    layer.neurons_metadata.vitality[0, 6] = layer.spike_cost

    layer.neurons_metadata.level[0, 7] = synapse_max_level
    layer2.neurons_metadata.level[0, 7] = synapse_max_level

    # synapses
    before = layer2.neurons_metadata.level[0, 0]
    synapse_address = domain.pre_synapse_index.key[0]
    synapse_level = domain.synapses.level[synapse_address]
    layer.neurons_metadata.level[1, 0] = synapse_max_level
    layer2.neurons_metadata.flags[1, 0] |= neurons.IS_DEAD
    layer2.neurons_metadata.level[1, 1] = synapse_max_level
    layer.neurons_metadata.flags[1, 2] |= neurons.IS_DEAD
    layer2.neurons_metadata.level[1, 2] = synapse_max_level

    l2_n_level_before = layer2.neurons_metadata.level[0, 0]

    layer2.neurons_metadata.level[0, 6] = synapse_max_level
    s_level_before_7 = domain.synapses.level[domain.post_synapse_index.key[
        layer2.neurons_metadata.level.to_address(0, 7)
    ]]
    domain.synapses.learn[domain.post_synapse_index.key[
        layer2.neurons_metadata.level.to_address(0, 7)
    ]] = domain.learn_threshold

    domain.neurons.to_device(device)
    domain.synapses.to_device(device)
    domain.tick()
    domain.neurons.from_device(device)
    domain.synapses.from_device(device)
    s_level = domain.synapses.level[domain.post_synapse_index.key[
        layer2.neurons_metadata.level.to_address(0, 0)
    ]]
    if l2_n_level_before - layer2.relaxation < 0:
        res = np.int32(0) - s_level
        assert res == layer2.neurons_metadata.level[0, 0]
    else:
        assert l2_n_level_before - s_level - layer2.relaxation \
                == layer2.neurons_metadata.level[0, 0]

    # check neurons (layer.neurons_metadata.level[x, y])
    assert layer.neurons_metadata.level[0, 0] == 0
    assert layer.neurons_metadata.flags[0, 0] & neurons.IS_SPIKED
    assert layer.neurons_metadata.spike_tick[0, 0] == 1
    assert layer.neurons_metadata.vitality[0, 0] \
            == max_vitality - layer.spike_cost + 1

    assert layer.neurons_metadata.level[0, 1] == 1
    assert not layer.neurons_metadata.flags[0, 1] & neurons.IS_SPIKED
    assert layer.neurons_metadata.spike_tick[0, 1] == 0
    assert layer.neurons_metadata.vitality[0, 1] \
            == max_vitality

    assert layer.neurons_metadata.level[0, 2] == synapse_max_level
    assert layer.neurons_metadata.flags[0, 2] & neurons.IS_DEAD
    assert layer.neurons_metadata.flags[0, 2] & neurons.IS_SPIKED

    assert layer.neurons_metadata.level[0, 3] == synapse_max_level
    assert layer.neurons_metadata.flags[0, 3] & neurons.IS_RECEIVER
    assert not layer.neurons_metadata.flags[0, 3] & neurons.IS_SPIKED

    assert layer.neurons_metadata.level[0, 4] == 0

    assert layer.neurons_metadata.level[0, 5] == 0

    # spike and dies (low neuron.vitality)
    assert not layer.neurons_metadata.flags[0, 6] & neurons.IS_SPIKED
    assert layer.neurons_metadata.flags[0, 6] & neurons.IS_DEAD
    assert layer.neurons_metadata.vitality[0, 6] \
            == max_vitality

    # check synapses
    s_level_after_7 = domain.synapses.level[domain.post_synapse_index.key[
        layer2.neurons_metadata.level.to_address(0, 7)
    ]]
    s_learn_after_7 = domain.synapses.learn[domain.post_synapse_index.key[
        layer2.neurons_metadata.level.to_address(0, 7)
    ]]
    #assert s_level_before_7 == s_level_after_7
    assert domain.synapses.flags[domain.post_synapse_index.key[
        layer2.neurons_metadata.level.to_address(0, 7)
    ]] & synapses.IS_STRENGTHENED
    assert not domain.synapses.flags[domain.post_synapse_index.key[
        layer2.neurons_metadata.level.to_address(0, 0)
    ]] & synapses.IS_STRENGTHENED

    before = before - 1000
    if before < 0:
        before = 0
    # layer 1 is inhibitory
    assert layer2.neurons_metadata.level[0, 0] == before - synapse_level
    # dead post-neuron so synapse level should be 0
    assert domain.synapses.level[domain.pre_synapse_index.key[
        layer.neurons_metadata.level.to_address(1, 0)
    ]] == 0
    assert layer2.neurons_metadata.flags[1, 1] & neurons.IS_SPIKED
    # dead pre-neuron so synapse level should be 0
    assert domain.synapses.level[domain.post_synapse_index.key[
        layer2.neurons_metadata.level.to_address(1, 2)
    ]] == 0
    assert domain.synapses.learn[domain.pre_synapse_index.key[
        layer.neurons_metadata.level.to_address(0, 0)
    ]] == 0
    # check stats
    for field_num in range(0, domain.stat_fields):
        if field_num not in [2, 4]:
            assert domain.stat_vector[0 + field_num] \
                == domain.layers_stat[0 + field_num] \
                + domain.layers_stat[ \
                    0 + field_num + len(domain.stat_vector)]

    assert domain.config['stat_size'] == domain.stat('stat_size')
    # field 0 - total spikes
    assert domain.stat_vector[0] >= 4
    assert domain.stat_vector[0] == domain.stat('total_spikes')
    assert domain.layers_stat[0] >= 2
    assert domain.layers_stat[0 + len(domain.stat_vector)] >= 2
    # field 1 - number of the dead neurons
    assert domain.layers_stat[1] == 3
    assert domain.stat_vector[1] == domain.stat('dead_neurons')
    assert domain.layers_stat[1 + len(domain.stat_vector)] == 1
    # field 2 - number of synapses with IS_STRENGTHENED flag
    assert domain.stat_vector[2] == 1
    assert domain.stat_vector[2] == domain.stat('strengthened_synapses')
    # field 3 - tiredness
    assert domain.layers_stat[3] \
            == (layer.spike_cost - 1) * domain.layers_stat[0]
    assert domain.layers_stat[3 + len(domain.stat_vector)] \
            == (layer2.spike_cost - 1) \
                * domain.layers_stat[0 + len(domain.stat_vector)]
    assert domain.stat_vector[3] == domain.stat('neurons_tiredness')
    # field 4 - sum(synapse.learn)
    assert domain.stat_vector[4] >= (domain.learn_rate - 1)
    assert domain.stat_vector[4] == domain.stat('synapse_learn_level')

    # test kernel
    test_length = 16
    test_kernel = np.zeros((test_length,)).astype(np.uint32)
    test_kernel_buf = cl.Buffer(
            device.ctx,
            cl.mem_flags.READ_WRITE | cl.mem_flags.COPY_HOST_PTR,
            hostbuf=test_kernel
        )

    device.program.test_kernel(
        device.queue, (test_length,), None,
        test_kernel_buf,
        np.int32(test_length),
        np.uint32(types.max(types.vitality))
    )
    cl.enqueue_copy(device.queue, test_kernel, test_kernel_buf)
    assert 8 | (3 & neurons.IS_SPIKED) == 10
    assert 8 | (1 & neurons.IS_SPIKED) == 8
    assert 7 & ~neurons.IS_SPIKED == 5
    assert 8 & ~neurons.IS_SPIKED == 8
    assert list(test_kernel) == [
        neurons.IS_INHIBITORY,
        neurons.IS_SPIKED,
        neurons.IS_DEAD,
        neurons.IS_TRANSMITTER,
        neurons.IS_RECEIVER,
        test_length,
        1, # 3 & IS_INHIBITORY
        2, # 3 & IS_SPIKED
        0, # 3 & IS_DEAD
        test_length,
        null,
        synapses.IS_STRENGTHENED,
        3,
        8 | (3 & neurons.IS_SPIKED),
        7 & ~neurons.IS_SPIKED,
        types.max(types.vitality),
    ]


    # remote domains
    config = {
        'synapse': {
            'max_level': synapse_max_level,
            'spike_learn_threshold': 2,
        },
        'layers': [
            {
                'name': 'V1',
                'threshold': synapse_max_level,
                'relaxation': 1000,
                'width': 20,
                'height': 20,
                'is_inhibitory': True,
                'connect': [
                    {
                        'name': 'V2',
                        'radius': 1,
                        'shift': [0, 0],
                    },
                ],
            },
            {
                'name': 'V2',
                'threshold': synapse_max_level,
                'relaxation': 1000,
                'width': 20,
                'height': 20,
            },
        ],
        'domains': [
            {
                'name'        : 'D1',
                'device'    : {
                    'type': 'OpenCL',
                },
                'layers'    : [
                    {'name': 'V1'},
                ],
            },
            {
                'name'        : 'D2',
                'device'    : {
                    'type': 'OpenCL',
                },
                'layers'    : [
                    {'name': 'V2'},
                ],
            },
        ],
    }
    ore = OpenRE(config)
    ore.deploy()
    d1 = ore.domains[0]
    d2 = ore.domains[1]
    v1 = d1.layers[0]
    v1.neurons_metadata.level[0, 0] = synapse_max_level
    d1.neurons.to_device(d1.device)
    d1.synapses.to_device(d1.device)
    d1.tick()
    d1.neurons.from_device(d1.device)
    d1.synapses.from_device(d1.device)
    d1.transmitter_index.is_spiked.from_device(d1.device)
    assert v1.neurons_metadata.flags[0, 0] & neurons.IS_SPIKED
    assert v1.neurons_metadata.flags[0, 0] & neurons.IS_INHIBITORY
    assert d1.transmitter_index.is_spiked[0]
    #assert d1.transmitter_index.flags[0] & neurons.IS_INHIBITORY

    assert d2.receiver_index.is_spiked[0]
    local_address = d2.receiver_index.local_address[0]
    assert local_address == 400
    assert not d2.neurons.flags[local_address] & neurons.IS_SPIKED
    assert d2.neurons.flags[local_address] & neurons.IS_INHIBITORY
    assert d2.neurons.flags[local_address] & neurons.IS_RECEIVER
    d2.tick()
    d2.neurons.from_device(d2.device)
    assert not d2.receiver_index.is_spiked[0]
    assert d2.neurons.flags[local_address] & neurons.IS_SPIKED
    assert d2.neurons.flags[local_address] & neurons.IS_INHIBITORY
    assert d2.neurons.flags[local_address] & neurons.IS_RECEIVER
Beispiel #8
0
def test_remote_domain():
    from openre import OpenRE
    from openre.domain import create_domain_factory, Domain
    class RemoteDomainTest(StatsMixin):
        """
        Тестовый прокси к удаленному домену.
        """
        def __init__(self, config, net, domain_index):
            super(RemoteDomainTest, self).__init__()
            self.config = config
            self.net = net
            self.name = self.config['name']
            logging.debug('Create remote domain (name: %s)', config['name'])
            self.index = domain_index

        def __setattr__(self, name, value):
            super(RemoteDomainTest, self).__setattr__(name, value)

        def __getattr__(self, name):
            def api_call(*args, **kwargs):
                self.stat_inc(name)
                return
            return api_call

    config = {
        'layers': [
            {
                'name': 'V1',
                'threshold': 30000,
                'relaxation': 1000,
                'width': 30,
                'height': 30,
                'connect': [
                    {
                        'name': 'V2',
                        'radius': 3,
                    },
                ],
            },
            {
                'name': 'V2',
                'threshold': 30000,
                'width': 10,
                'height': 10,
                'connect': [
                    {
                        'name': 'V2',
                        'radius': 3,
                    },
                ],
            },
        ],
        'domains': [
            {
                'name'        : 'D1',
                'layers'    : [
                    {'name': 'V1'},
                ],
            },
            {
                'name'        : 'D2',
                'layers'    : [
                    {'name': 'V2'},
                ],
            },
        ],
    }
    ore = OpenRE(config)
    ore.deploy(create_domain_factory(Domain, RemoteDomainTest, ['D1']))
    local = ore.domains[0]
    remote = ore.domains[1]
    assert local.name == 'D1'
    assert local.index == 0
    assert isinstance(local, Domain)

    assert remote.name == 'D2'
    assert remote.index == 1
    assert isinstance(remote, RemoteDomainTest)
    assert remote.stat('send_synapse') == 17424
    assert remote.stat('deploy_layers') == 1
    assert remote.stat('deploy_neurons') == 1
    assert remote.stat('pre_deploy_synapses') == 1
    assert remote.stat('deploy_indexes') == 1
    assert remote.stat('deploy_device') == 1

    config = {
        'layers': [
            {
                'name': 'V1',
                'threshold': 30000,
                'relaxation': 1000,
                'width': 2,
                'height': 2,
                'connect': [
                    {
                        'name': 'V2',
                        'radius': 2,
                    },
                ],
            },
            {
                'name': 'V2',
                'threshold': 30000,
                'width': 2,
                'height': 2,
            },
        ],
        'domains': [
            {
                'name'        : 'D1',
                'layers'    : [
                    {'name': 'V1'},
                ],
            },
            {
                'name'        : 'D2',
                'layers'    : [
                    {'name': 'V2'},
                ],
            },
        ],
    }
    ore = OpenRE(config)
    ore.deploy(create_domain_factory(Domain, RemoteDomainTest, ['D1']))
    local = ore.domains[0]
    remote = ore.domains[1]
    # 4 neurons in V1 connects to 4 neurons in V2 with radius 2
    assert remote.stat('send_synapse') == 4*4

    dummy = RemoteDomainDummy({'name':'D'}, None, 0)
    assert dummy.deploy_layers() is None
Beispiel #9
0
def test_random_device():
    from openre import OpenRE
    # remote domains
    config = {
        'layers': [
            {
                'name': 'R1',
                'width': 20,
                'height': 20,
                'is_inhibitory': False,
                'connect': [
                    {
                        'name': 'V2',
                        'radius': 1,
                        'shift': [0, 0],
                    },
                ],
            },
            {
                'name': 'V2',
                'width': 20,
                'height': 20,
            },
        ],
        'domains': [
            {
                'name'        : 'D1',
                'device'    : {
                    'type': 'Random',
                    'method': 'randint',
                    'args': [0, 60000],
                    'threshold': 30000,
                },
                'layers'    : [
                    {'name': 'R1'},
                ],
            },
            {
                'name'        : 'D2',
                'device'    : {
                    'type': 'OpenCL',
                },
                'layers'    : [
                    {'name': 'V2'},
                ],
            },
        ],
    }
    ore = OpenRE(config)
    ore.deploy()
    device2 = ore.domains[1].device
    D1 = ore.domains[0]
    D2 = ore.domains[1]
    ore.tick()
    D2.neurons.from_device(device2)
    # receiver neurons in D2 is spiked the same as spikes data in
    # D1.transmitter_index
    assert list((D1.transmitter_index.is_spiked.data + 1) & neurons.IS_SPIKED) \
            == list(D2.neurons.flags.data[400:] & neurons.IS_SPIKED)
    ore.tick()
    D2.neurons.from_device(device2)
    # at least one spike should happen
    assert sum(list(D2.neurons.flags.data[0:400] & neurons.IS_SPIKED))