Пример #1
0
 def _tick_input_data(self, domain):
     """
     Process data from other domains
     """
     ticks = domain.ticks
     ret = []
     for layer in self._input:
         if layer.input_data is None and layer.input_data_cache is None:
             continue
         data = layer.input_data
         layer.input_data = None
         input_data_vector = layer.input_data_cache
         if input_data_vector is None:
             input_data_vector = StandaloneVector()
             if isinstance(data, basestring):
                 input_data_vector.from_bytes(data)
             else:
                 input_data_vector.set_data(data)
             assert len(input_data_vector) == layer.length, \
                 "Domain '%s': len(input_data_vector) = %s, " \
                 "layer.length = %s" \
                 % (domain.name, len(input_data_vector), layer.length)
             layer.input_data_cache = input_data_vector
         length = len(input_data_vector)
         if not length:
             return
         ret.append([
             layer.config['name'],
             np.reshape(input_data_vector.data, (layer.height, layer.width))
         ])
         if layer.input_expire <= ticks:
             layer.input_data = None
             layer.input_data_cache = None
     if ret:
         self.receive_data(domain, ret)
Пример #2
0
 def tick_layers_input_data(self, domain):
     import numpy as np
     for layer_index, layer in enumerate(domain.layers):
         if not layer.input_data:
             continue
         input_data_vector = StandaloneVector()
         data = layer.input_data
         if isinstance(data, basestring):
             input_data_vector.from_bytes(data)
         else:
             input_data_vector.set_data(data)
         check = np.zeros(40, dtype=np.uint8)
         check.fill(layer_index)
         assert list(input_data_vector.data) == list(check)
         domain.stat_inc('test_io_input')
     ret = super(IOBaseTesterSimpleCheck,
                  self).tick_layers_input_data(domain)
     assert layer.input_data is None
     return ret
Пример #3
0
 def tick_layers_input_data(self, domain):
     """
     Add value from layer.input_data to neurons.level
     """
     ticks = domain.ticks
     for layer in domain.layers:
         if layer.input_data is None and layer.input_data_cache is None:
             continue
         data = layer.input_data
         layer.input_data = None
         input_data_vector = layer.input_data_cache
         if input_data_vector is None:
             input_data_vector = StandaloneVector()
             if isinstance(data, basestring):
                 input_data_vector.from_bytes(data)
             else:
                 input_data_vector.set_data(data)
             assert len(input_data_vector) == layer.length, \
                 "Domain '%s': len(input_data_vector)=%s, layer.length=%s" \
                 % (domain.name, len(input_data_vector), layer.length)
             layer.input_data_cache = input_data_vector
         length = len(input_data_vector)
         if not length:
             return
         input_data_vector.to_device(self)
         # only one layer with the same dims as input data
         assert length == len(layer.neurons_metadata.level)
         self.program.tick_numpy_input_data_uint8(
             self.queue, (length,), None,
             # data
             input_data_vector.device_data_pointer,
             # layer
             types.address(layer.neurons_metadata.address),
             # neurons
             domain.neurons.level.device_data_pointer
         ).wait()
         if layer.input_expire <= ticks:
             layer.input_data = None
             layer.input_data_cache = None
Пример #4
0
 def register_input_layer_data(self, layer_index, data):
     """
     Send data over network
     """
     vector = StandaloneVector().set_data(data)
     self.pub_data('NP', str(layer_index), vector.bytes())
Пример #5
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