def test_init_bad(): MockSimulator.setup() neuron = MockNeuron() with pytest.raises(KeyError): neuron.get_initial_value("badvariable") with pytest.raises(KeyError): assert 1 == neuron.initialize("anotherbad", "junk")
def test_connector( clist, column_names, weights, delays, expected_clist, expected_weights, expected_delays, expected_extra_parameters, expected_extra_parameter_names): MockSimulator.setup() connector = FromListConnector(clist, column_names=column_names) if expected_clist is not None: assert(numpy.array_equal(connector.conn_list, expected_clist)) else: assert(numpy.array_equal(connector.conn_list, clist)) # Check extra parameters are as expected extra_params = connector.get_extra_parameters() extra_param_names = connector.get_extra_parameter_names() assert(numpy.array_equal(extra_params, expected_extra_parameters)) assert(numpy.array_equal( extra_param_names, expected_extra_parameter_names)) if extra_params is not None: assert(len(extra_params.shape) == 2) assert(extra_params.shape[1] == len(extra_param_names)) for i in range(len(extra_param_names)): assert(extra_params[:, i].shape == (len(clist), )) # Check weights and delays are used or ignored as expected block = connector.create_synaptic_block( weights, delays, [], 0, [], 0, Slice(0, 10), Slice(0, 10), 1) assert(numpy.array_equal(block["weight"], numpy.array(expected_weights))) assert(numpy.array_equal(block["delay"], numpy.array(expected_delays)))
def test_csa_block_connector(): MockSimulator.setup() try: # This creates a block of size (2, 5) with a probability of 0.5; then # within the block an individual connection has a probability of 0.3 connector = CSAConnector( csa.block(2, 5) * csa.random(0.5) * csa.random(0.3)) weight = 1.0 delay = 2.0 mock_synapse_info = MockSynapseInfo(MockPopulation(10, "pre"), MockPopulation(10, "post"), weight, delay) connector.set_projection_information(1000.0, mock_synapse_info) pre_vertex_slice = Slice(0, 10) post_vertex_slice = Slice(0, 10) block = connector.create_synaptic_block([pre_vertex_slice], 0, [post_vertex_slice], 0, pre_vertex_slice, post_vertex_slice, 0, mock_synapse_info) assert (len(block) >= 0) assert (all(item["weight"] == 1.0 for item in block)) assert (all(item["delay"] == 2.0 for item in block)) except TypeError: raise SkipTest("https://github.com/INCF/csa/issues/17") except RuntimeError: if sys.version_info >= (3, 7): raise SkipTest("https://github.com/INCF/csa/issues/16") raise
def test_connector(clist, column_names, weights, delays, expected_clist, expected_weights, expected_delays, expected_extra_parameters, expected_extra_parameter_names): MockSimulator.setup() connector = FromListConnector(clist, column_names=column_names) if expected_clist is not None: assert (numpy.array_equal(connector.conn_list, expected_clist)) else: assert (numpy.array_equal(connector.conn_list, clist)) # Check extra parameters are as expected extra_params = connector.get_extra_parameters() extra_param_names = connector.get_extra_parameter_names() assert (numpy.array_equal(extra_params, expected_extra_parameters)) assert (numpy.array_equal(extra_param_names, expected_extra_parameter_names)) if extra_params is not None: assert (len(extra_params.shape) == 2) assert (extra_params.shape[1] == len(extra_param_names)) for i in range(len(extra_param_names)): assert (extra_params[:, i].shape == (len(clist), )) # Check weights and delays are used or ignored as expected pre_slice = Slice(0, 10) post_slice = Slice(0, 10) mock_synapse_info = MockSynapseInfo(MockPopulation(10, "Pre"), MockPopulation(10, "Post"), weights, delays) block = connector.create_synaptic_block([pre_slice], 0, [post_slice], 0, pre_slice, post_slice, 1, mock_synapse_info) assert (numpy.array_equal(block["weight"], numpy.array(expected_weights))) assert (numpy.array_equal(block["delay"], numpy.array(expected_delays)))
def test_initializable(): MockSimulator.setup() neuron = MockNeuron() assert [1, 1, 1, 1, 1] == neuron.get_initial_value("foo") neuron.initialize("foo", 2) assert [2, 2, 2, 2, 2] == neuron.get_initial_value("foo_init") assert [11, 11, 11, 11, 11] == neuron.get_initial_value("bar_init") assert [11, 11, 11, 11, 11] == neuron.get_initial_value("bar")
def test_initializable(): MockSimulator.setup() neuron = MockNeuron(5, FooBar()) assert 1 == neuron.get_initial_value("foo") neuron.initialize("foo", 2) assert 2 == neuron.get_initial_value("foo_init") assert 11 == neuron.get_initial_value("bar_init") assert 11 == neuron.get_initial_value("bar")
def test_initial_values(): MockSimulator.setup() neuron = MockNeuron() initial_values = neuron.initial_values assert "foo" in initial_values assert "bar" in initial_values initial_values = neuron.get_initial_values(selector=3) assert {"foo": [1], "bar": [11]} == initial_values
def test_init_by_in(): MockSimulator.setup() neuron = MockNeuron() assert [1, 1, 1, 1, 1] == neuron.get_initial_value("foo") neuron.set_initial_value(variable="foo", value=11, selector=1) assert [1, 11, 1, 1, 1] == neuron.get_initial_value("foo") neuron.set_initial_value(variable="foo", value=12, selector=2) assert [1, 11, 12, 1, 1] == neuron.get_initial_value("foo") assert [11] == neuron.get_initial_value("bar", selector=1) assert [12] == neuron.get_initial_value("foo", selector=2)
def test_csa_random_connector(): MockSimulator.setup() connector = CSAConnector(csa.random(0.05)) connector.set_projection_information( MockPopulation(10, "pre"), MockPopulation(10, "post"), MockRNG(), 1000.0) pre_vertex_slice = Slice(0, 10) post_vertex_slice = Slice(0, 10) block = connector.create_synaptic_block( 1.0, 2.0, [pre_vertex_slice], 0, [post_vertex_slice], 0, pre_vertex_slice, post_vertex_slice, 0) assert(len(block) >= 0) assert(all(item["weight"] == 1.0 for item in block)) assert(all(item["delay"] == 2.0 for item in block))
def test_connector_split(): MockSimulator.setup() n_sources = 1000 n_targets = 1000 n_connections = 10000 pre_neurons_per_core = 57 post_neurons_per_core = 59 sources = numpy.random.randint(0, n_sources, n_connections) targets = numpy.random.randint(0, n_targets, n_connections) pre_slices = [ Slice(i, i + pre_neurons_per_core - 1) for i in range(0, n_sources, pre_neurons_per_core) ] post_slices = [ Slice(i, i + post_neurons_per_core - 1) for i in range(0, n_targets, post_neurons_per_core) ] connection_list = numpy.dstack((sources, targets))[0] connector = MockFromListConnector(connection_list) weight = 1.0 delay = 1.0 mock_synapse_info = MockSynapseInfo(MockPopulation(n_sources, "Pre"), MockPopulation(n_targets, "Post"), weight, delay) has_block = set() try: # Check each connection is in the right place for i, pre_slice in enumerate(pre_slices): for j, post_slice in enumerate(post_slices): block = connector.create_synaptic_block( pre_slices, i, post_slices, j, pre_slice, post_slice, 1, mock_synapse_info) for source in block["source"]: assert (pre_slice.lo_atom <= source <= pre_slice.hi_atom) for target in block["target"]: assert (post_slice.lo_atom <= target <= post_slice.hi_atom) for item in block: has_block.add((item["source"], item["target"])) # Check each connection has a place for source, target in zip(sources, targets): assert (source, target) in has_block # Check the split only happens once assert connector._split_count == 1 except AssertionError: print(connection_list) reraise(*sys.exc_info())
def test_range_list_as_list(): MockSimulator.setup() spec_writer = FileDataWriter("test.dat") spec = DataSpecificationGenerator(spec_writer, None) try: value = SpynnakerRangedList(size=10, value=_generator(10), key="test") param = NeuronParameter(value, DataType.S1615) iterator = param.iterator_by_slice(0, 5, spec) values = _iterate_parameter_values(iterator, DataType.S1615) assert list(value[0:5]) == values assert isinstance(iterator, _Range_Iterator) finally: spec.end_specification() os.remove("test.dat")
def test_real_list(): MockSimulator.setup() spec_writer = FileDataWriter("test.dat") spec = DataSpecificationGenerator(spec_writer, None) try: value = range(10) param = NeuronParameter(value, DataType.S1615) iterator = param.iterator_by_slice(0, 5, spec) values = _iterate_parameter_values(iterator, DataType.S1615) assert list(value[0:5]) == values assert isinstance(iterator, _Get_Iterator) finally: spec.end_specification() os.remove("test.dat")
def test_single_value(): MockSimulator.setup() spec_writer = FileDataWriter("test.dat") spec = DataSpecificationGenerator(spec_writer, None) try: value = 1.0 param = NeuronParameter(value, DataType.S1615) iterator = param.iterator_by_slice(0, 5, spec) values = _iterate_parameter_values(iterator, DataType.S1615) assert [value] * 5 == values assert isinstance(iterator, _SingleValue_Iterator) finally: spec.end_specification() os.remove("test.dat")
def test_simple_record(): simulator = MockSimulator() globals_variables.set_failed_state(SpynnakerFailedState()) globals_variables.set_simulator(simulator) recordables = ["v", "gsyn_exc", "gsyn_inh"] data_types = { "v": DataType.S1615, "gsyn_exc": DataType.S1615, "gsyn_inh": DataType.S1615 } nr = NeuronRecorder(recordables, data_types, [], 100) assert(frozenset(["v", "gsyn_exc", "gsyn_inh"]) == frozenset(nr.get_recordable_variables())) assert([] == nr.recording_variables) nr.set_recording("v", True) assert(["v"] == nr.recording_variables) _slice = Slice(0, 50) gps = nr.get_global_parameters(_slice) # 3 rates (index "0" is v) assert (gps[0].get_value() == 1) # 3 n_neurons (index "3" is v) assert (gps[3].get_value() == _slice.n_atoms)
def test_csa_block_connector(): MockSimulator.setup() # This creates a block of size (2, 5) with a probability of 0.5; then # within the block an individual connection has a probability of 0.3 connector = CSAConnector( csa.block(2, 5) * csa.random(0.5) * csa.random(0.3)) connector.set_projection_information( MockPopulation(10, "pre"), MockPopulation(10, "post"), MockRNG(), 1000.0) pre_vertex_slice = Slice(0, 10) post_vertex_slice = Slice(0, 10) block = connector.create_synaptic_block( 1.0, 2.0, [pre_vertex_slice], 0, [post_vertex_slice], 0, pre_vertex_slice, post_vertex_slice, 0) assert(len(block) >= 0) assert(all(item["weight"] == 1.0 for item in block)) assert(all(item["delay"] == 2.0 for item in block))
def run_spec_check(method): MockSimulator.setup() if platform.system() == "Windows": spec_writer = FileDataWriter("test.dat") spec = DataSpecificationGenerator(spec_writer, None) try: method(spec) finally: spec.end_specification() os.remove("test.dat") else: with tempfile.NamedTemporaryFile() as temp: spec = DataSpecificationGenerator(FileDataWriter(temp.name), None) try: method(spec) finally: spec.end_specification()
def test_connector(clist, column_names, weights, delays, expected_clist, expected_weights, expected_delays, expected_extra_parameters, expected_extra_parameter_names): MockSimulator.setup() temp = tempfile.NamedTemporaryFile(delete=False) with temp as f: header = '' if column_names is not None: columns = ["i", "j"] columns.extend(column_names) header = 'columns = {}'.format(columns) if clist is not None and len(clist): numpy.savetxt(f, clist, header=header) elif len(header): f.write("# {}\n".format(header)) connector = FromFileConnector(temp.name) if expected_clist is not None: assert (numpy.array_equal(connector.conn_list, expected_clist)) else: assert (numpy.array_equal(connector.conn_list, clist)) # Check extra parameters are as expected extra_params = connector.get_extra_parameters() extra_param_names = connector.get_extra_parameter_names() assert (numpy.array_equal(extra_params, expected_extra_parameters)) assert (numpy.array_equal(extra_param_names, expected_extra_parameter_names)) if extra_params is not None: assert (len(extra_params.shape) == 2) assert (extra_params.shape[1] == len(extra_param_names)) for i in range(len(extra_param_names)): assert (extra_params[:, i].shape == (len(clist), )) # Check weights and delays are used or ignored as expected pre_slice = Slice(0, 10) post_slice = Slice(0, 10) mock_synapse_info = MockSynapseInfo(MockPopulation(10, "Pre"), MockPopulation(10, "Post"), weights, delays) block = connector.create_synaptic_block([pre_slice], 0, [post_slice], 0, pre_slice, post_slice, 1, mock_synapse_info) assert (numpy.array_equal(block["weight"], numpy.array(expected_weights))) assert (numpy.array_equal(block["delay"], numpy.array(expected_delays)))
def test_csa_from_list_connector(): MockSimulator.setup() conn_list = [(i, i + 1 % 10) for i in range(10)] connector = CSAConnector(conn_list) connector.set_projection_information( MockPopulation(10, "pre"), MockPopulation(10, "post"), MockRNG(), 1000.0) pre_vertex_slice = Slice(0, 10) post_vertex_slice = Slice(0, 10) block = connector.create_synaptic_block( 1.0, 2.0, [pre_vertex_slice], 0, [post_vertex_slice], 0, pre_vertex_slice, post_vertex_slice, 0) assert(len(block) > 0) assert(all(item["source"] == conn[0] for item, conn in zip(block, conn_list))) assert(all(item["target"] == conn[1] for item, conn in zip(block, conn_list))) assert(all(item["weight"] == 1.0 for item in block)) assert(all(item["delay"] == 2.0 for item in block))
def test_recording_variables(): simulator = MockSimulator() globals_variables.set_failed_state(SpynnakerFailedState()) globals_variables.set_simulator(simulator) nr = NeuronRecorder(["spikes", "v", "gsyn_exc", "gsyn_inh"], 100) assert ([] == nr.recording_variables) nr.set_recording("v", True) nr.set_recording("gsyn_inh", True) assert (["v", "gsyn_inh"] == nr.recording_variables) assert ([1, 3] == nr.recorded_region_ids)
def test_csa_one_to_one_connector(): MockSimulator.setup() connector = CSAConnector(csa.oneToOne) weight = 1.0 delay = 2.0 mock_synapse_info = MockSynapseInfo(MockPopulation(10, "pre"), MockPopulation(10, "post"), weight, delay) connector.set_projection_information(1000.0, mock_synapse_info) pre_vertex_slice = Slice(0, 10) post_vertex_slice = Slice(0, 10) block = connector.create_synaptic_block([pre_vertex_slice], 0, [post_vertex_slice], 0, pre_vertex_slice, post_vertex_slice, 0, mock_synapse_info) assert (len(block) > 0) assert (all(item["source"] == item["target"] for item in block)) assert (all(item["weight"] == 1.0 for item in block)) assert (all(item["delay"] == 2.0 for item in block))
def test_get_max_row_length(dynamics_class, timing, weight, size, exception, max_size): MockSimulator.setup() if timing is not None and weight is not None: dynamics = dynamics_class(timing(), weight()) else: dynamics = dynamics_class() io = SynapseIORowBased() population_table = MasterPopTableAsBinarySearch() synapse_information = SynapseInformation(None, None, None, None, None, None, dynamics, 0) in_edge = ProjectionApplicationEdge(None, None, synapse_information) if exception is not None: with pytest.raises(exception) as exc_info: io._get_max_row_length(size, dynamics, population_table, in_edge, size) assert exc_info.value.max_size == max_size else: actual_size = io._get_max_row_length(size, dynamics, population_table, in_edge, size) assert actual_size == max_size
def test_recording_variables(): simulator = MockSimulator() globals_variables.set_failed_state(SpynnakerFailedState()) globals_variables.set_simulator(simulator) recordables = ["v", "gsyn_exc", "gsyn_inh"] data_types = { "v": DataType.S1615, "gsyn_exc": DataType.S1615, "gsyn_inh": DataType.S1615 } nr = NeuronRecorder(recordables, data_types, [], 100) assert([] == nr.recording_variables) nr.set_recording("v", True) nr.set_recording("gsyn_inh", True) assert(["v", "gsyn_inh"] == nr.recording_variables) assert([0, 2] == nr.recorded_region_ids)
def test_simple_record(): simulator = MockSimulator() globals_variables.set_failed_state(SpynnakerFailedState()) globals_variables.set_simulator(simulator) recordables = ["v", "gsyn_exc", "gsyn_inh"] data_types = { "v": DataType.S1615, "gsyn_exc": DataType.S1615, "gsyn_inh": DataType.S1615 } nr = NeuronRecorder(recordables, data_types, [], 100, [], []) assert(frozenset(["v", "gsyn_exc", "gsyn_inh"]) == frozenset(nr.get_recordable_variables())) assert([] == nr.recording_variables) nr.set_recording("v", True) assert(["v"] == nr.recording_variables)
def test_selector(): simulator = MockSimulator.setup() model = IFCurrExpBase() pop_1 = PyNNPopulationCommon(spinnaker_control=simulator, size=5, label="Test", constraints=None, model=model, structure=None, initial_values=None) pop_1.set("tau_m", 2) values = pop_1.get("tau_m") assert [2, 2, 2, 2, 2] == values values = pop_1.get_by_selector(slice(1, 3), "tau_m") assert [2, 2] == values pop_1.set_by_selector(slice(1, 3), "tau_m", 3) values = pop_1.get("tau_m") assert [2, 3, 3, 2, 2] == values values = pop_1.get(["cm", "v_thresh"]) assert [1.0, 1.0, 1.0, 1.0, 1.0] == values['cm'] assert [-50.0, -50.0, -50.0, -50.0, -50.0] == values["v_thresh"] values = pop_1.get_by_selector([1, 3, 4], ["cm", "v_thresh"]) assert [1.0, 1.0, 1.0] == values['cm'] assert [-50.0, -50.0, -50.0] == values["v_thresh"]
def test_write_synaptic_matrix_and_master_population_table(self): MockSimulator.setup() default_config_paths = os.path.join( os.path.dirname(abstract_spinnaker_common.__file__), AbstractSpiNNakerCommon.CONFIG_FILE_NAME) config = conf_loader.load_config( AbstractSpiNNakerCommon.CONFIG_FILE_NAME, default_config_paths) config.set("Simulation", "one_to_one_connection_dtcm_max_bytes", 40) machine_time_step = 1000.0 pre_app_vertex = SimpleApplicationVertex(10) pre_vertex = SimpleMachineVertex(resources=None) pre_vertex_slice = Slice(0, 9) post_app_vertex = SimpleApplicationVertex(10) post_vertex = SimpleMachineVertex(resources=None) post_vertex_slice = Slice(0, 9) post_slice_index = 0 one_to_one_connector_1 = OneToOneConnector(None) one_to_one_connector_1.set_projection_information( pre_app_vertex, post_app_vertex, None, machine_time_step) one_to_one_connector_1.set_weights_and_delays(1.5, 1.0) one_to_one_connector_2 = OneToOneConnector(None) one_to_one_connector_2.set_projection_information( pre_app_vertex, post_app_vertex, None, machine_time_step) one_to_one_connector_2.set_weights_and_delays(2.5, 2.0) all_to_all_connector = AllToAllConnector(None) all_to_all_connector.set_projection_information( pre_app_vertex, post_app_vertex, None, machine_time_step) all_to_all_connector.set_weights_and_delays(4.5, 4.0) direct_synapse_information_1 = SynapseInformation( one_to_one_connector_1, SynapseDynamicsStatic(), 0) direct_synapse_information_2 = SynapseInformation( one_to_one_connector_2, SynapseDynamicsStatic(), 1) all_to_all_synapse_information = SynapseInformation( all_to_all_connector, SynapseDynamicsStatic(), 0) app_edge = ProjectionApplicationEdge( pre_app_vertex, post_app_vertex, direct_synapse_information_1) app_edge.add_synapse_information(direct_synapse_information_2) app_edge.add_synapse_information(all_to_all_synapse_information) machine_edge = ProjectionMachineEdge( app_edge.synapse_information, pre_vertex, post_vertex) partition_name = "TestPartition" graph = MachineGraph("Test") graph.add_vertex(pre_vertex) graph.add_vertex(post_vertex) graph.add_edge(machine_edge, partition_name) graph_mapper = GraphMapper() graph_mapper.add_vertex_mapping( pre_vertex, pre_vertex_slice, pre_app_vertex) graph_mapper.add_vertex_mapping( post_vertex, post_vertex_slice, post_app_vertex) graph_mapper.add_edge_mapping(machine_edge, app_edge) weight_scales = [4096.0, 4096.0] key = 0 routing_info = RoutingInfo() routing_info.add_partition_info(PartitionRoutingInfo( [BaseKeyAndMask(key, 0xFFFFFFF0)], graph.get_outgoing_edge_partition_starting_at_vertex( pre_vertex, partition_name))) temp_spec = tempfile.mktemp() spec_writer = FileDataWriter(temp_spec) spec = DataSpecificationGenerator(spec_writer, None) master_pop_sz = 1000 master_pop_region = 0 all_syn_block_sz = 2000 synapse_region = 1 spec.reserve_memory_region(master_pop_region, master_pop_sz) spec.reserve_memory_region(synapse_region, all_syn_block_sz) synapse_type = MockSynapseType() synaptic_manager = SynapticManager( synapse_type=synapse_type, ring_buffer_sigma=5.0, spikes_per_second=100.0, config=config) synaptic_manager._write_synaptic_matrix_and_master_population_table( spec, [post_vertex_slice], post_slice_index, post_vertex, post_vertex_slice, all_syn_block_sz, weight_scales, master_pop_region, synapse_region, routing_info, graph_mapper, graph, machine_time_step) spec.end_specification() spec_writer.close() spec_reader = FileDataReader(temp_spec) executor = DataSpecificationExecutor( spec_reader, master_pop_sz + all_syn_block_sz) executor.execute() master_pop_table = executor.get_region(0) synaptic_matrix = executor.get_region(1) all_data = bytearray() all_data.extend(master_pop_table.region_data[ :master_pop_table.max_write_pointer]) all_data.extend(synaptic_matrix.region_data[ :synaptic_matrix.max_write_pointer]) master_pop_table_address = 0 synaptic_matrix_address = master_pop_table.max_write_pointer direct_synapses_address = struct.unpack_from( "<I", synaptic_matrix.region_data)[0] direct_synapses_address += synaptic_matrix_address + 8 indirect_synapses_address = synaptic_matrix_address + 4 placement = Placement(None, 0, 0, 1) transceiver = MockTransceiverRawData(all_data) # Get the master population table details items = synaptic_manager._poptable_type\ .extract_synaptic_matrix_data_location( key, master_pop_table_address, transceiver, placement.x, placement.y) # The first entry should be direct, but the rest should be indirect; # the second is potentially direct, but has been restricted by the # restriction on the size of the direct matrix assert len(items) == 3 # TODO: This has been changed because direct matrices are disabled! assert not items[0][2] assert not items[1][2] assert not items[2][2] data_1, row_len_1 = synaptic_manager._retrieve_synaptic_block( transceiver=transceiver, placement=placement, master_pop_table_address=master_pop_table_address, indirect_synapses_address=indirect_synapses_address, direct_synapses_address=direct_synapses_address, key=key, n_rows=pre_vertex_slice.n_atoms, index=0, using_extra_monitor_cores=False) connections_1 = synaptic_manager._synapse_io.read_synapses( direct_synapse_information_1, pre_vertex_slice, post_vertex_slice, row_len_1, 0, 2, weight_scales, data_1, None, app_edge.n_delay_stages, machine_time_step) # The first matrix is a 1-1 matrix, so row length is 1 assert row_len_1 == 1 # Check that all the connections have the right weight and delay assert len(connections_1) == post_vertex_slice.n_atoms assert all([conn["weight"] == 1.5 for conn in connections_1]) assert all([conn["delay"] == 1.0 for conn in connections_1]) data_2, row_len_2 = synaptic_manager._retrieve_synaptic_block( transceiver=transceiver, placement=placement, master_pop_table_address=master_pop_table_address, indirect_synapses_address=indirect_synapses_address, direct_synapses_address=direct_synapses_address, key=key, n_rows=pre_vertex_slice.n_atoms, index=1, using_extra_monitor_cores=False) connections_2 = synaptic_manager._synapse_io.read_synapses( direct_synapse_information_2, pre_vertex_slice, post_vertex_slice, row_len_2, 0, 2, weight_scales, data_2, None, app_edge.n_delay_stages, machine_time_step) # The second matrix is a 1-1 matrix, so row length is 1 assert row_len_2 == 1 # Check that all the connections have the right weight and delay assert len(connections_2) == post_vertex_slice.n_atoms assert all([conn["weight"] == 2.5 for conn in connections_2]) assert all([conn["delay"] == 2.0 for conn in connections_2]) data_3, row_len_3 = synaptic_manager._retrieve_synaptic_block( transceiver=transceiver, placement=placement, master_pop_table_address=master_pop_table_address, indirect_synapses_address=indirect_synapses_address, direct_synapses_address=direct_synapses_address, key=key, n_rows=pre_vertex_slice.n_atoms, index=2, using_extra_monitor_cores=False) connections_3 = synaptic_manager._synapse_io.read_synapses( all_to_all_synapse_information, pre_vertex_slice, post_vertex_slice, row_len_3, 0, 2, weight_scales, data_3, None, app_edge.n_delay_stages, machine_time_step) # The third matrix is an all-to-all matrix, so length is n_atoms assert row_len_3 == post_vertex_slice.n_atoms # Check that all the connections have the right weight and delay assert len(connections_3) == \ post_vertex_slice.n_atoms * pre_vertex_slice.n_atoms assert all([conn["weight"] == 4.5 for conn in connections_3]) assert all([conn["delay"] == 4.0 for conn in connections_3])
def test_get_max_synapses(): MockSimulator.setup() d = SynapseDynamicsSTDP(timing_dependence=TimingDependenceSpikePair(), weight_dependence=WeightDependenceAdditive(), pad_to_length=258) assert d.get_max_synapses(256) <= 256
def test_write_synaptic_matrix_and_master_population_table(self): MockSimulator.setup() # Add an sdram so maxsdram is high enough SDRAM(10000) default_config_paths = os.path.join( os.path.dirname(abstract_spinnaker_common.__file__), AbstractSpiNNakerCommon.CONFIG_FILE_NAME) config = conf_loader.load_config( AbstractSpiNNakerCommon.CONFIG_FILE_NAME, default_config_paths) config.set("Simulation", "one_to_one_connection_dtcm_max_bytes", 40) machine_time_step = 1000.0 pre_app_vertex = SimpleApplicationVertex(10) pre_vertex = SimpleMachineVertex(resources=None) pre_vertex_slice = Slice(0, 9) post_app_vertex = SimpleApplicationVertex(10) post_vertex = SimpleMachineVertex(resources=None) post_vertex_slice = Slice(0, 9) post_slice_index = 0 one_to_one_connector_1 = OneToOneConnector(None) one_to_one_connector_1.set_projection_information( pre_app_vertex, post_app_vertex, None, machine_time_step) one_to_one_connector_2 = OneToOneConnector(None) one_to_one_connector_2.set_projection_information( pre_app_vertex, post_app_vertex, None, machine_time_step) all_to_all_connector = AllToAllConnector(None) all_to_all_connector.set_projection_information( pre_app_vertex, post_app_vertex, None, machine_time_step) direct_synapse_information_1 = SynapseInformation( one_to_one_connector_1, SynapseDynamicsStatic(), 0, 1.5, 1.0) direct_synapse_information_2 = SynapseInformation( one_to_one_connector_2, SynapseDynamicsStatic(), 1, 2.5, 2.0) all_to_all_synapse_information = SynapseInformation( all_to_all_connector, SynapseDynamicsStatic(), 0, 4.5, 4.0) app_edge = ProjectionApplicationEdge( pre_app_vertex, post_app_vertex, direct_synapse_information_1) app_edge.add_synapse_information(direct_synapse_information_2) app_edge.add_synapse_information(all_to_all_synapse_information) machine_edge = ProjectionMachineEdge( app_edge.synapse_information, pre_vertex, post_vertex) partition_name = "TestPartition" graph = MachineGraph("Test") graph.add_vertex(pre_vertex) graph.add_vertex(post_vertex) graph.add_edge(machine_edge, partition_name) graph_mapper = GraphMapper() graph_mapper.add_vertex_mapping( pre_vertex, pre_vertex_slice, pre_app_vertex) graph_mapper.add_vertex_mapping( post_vertex, post_vertex_slice, post_app_vertex) graph_mapper.add_edge_mapping(machine_edge, app_edge) weight_scales = [4096.0, 4096.0] key = 0 routing_info = RoutingInfo() routing_info.add_partition_info(PartitionRoutingInfo( [BaseKeyAndMask(key, 0xFFFFFFF0)], graph.get_outgoing_edge_partition_starting_at_vertex( pre_vertex, partition_name))) temp_spec = tempfile.mktemp() spec_writer = FileDataWriter(temp_spec) spec = DataSpecificationGenerator(spec_writer, None) master_pop_sz = 1000 master_pop_region = 0 all_syn_block_sz = 2000 synapse_region = 1 direct_region = 2 spec.reserve_memory_region(master_pop_region, master_pop_sz) spec.reserve_memory_region(synapse_region, all_syn_block_sz) synaptic_manager = SynapticManager( n_synapse_types=2, ring_buffer_sigma=5.0, spikes_per_second=100.0, config=config) # UGLY but the mock transceiver NEED generate_on_machine be False abstract_generate_connector_on_machine.IS_PYNN_8 = False synaptic_manager._write_synaptic_matrix_and_master_population_table( spec, [post_vertex_slice], post_slice_index, post_vertex, post_vertex_slice, all_syn_block_sz, weight_scales, master_pop_region, synapse_region, direct_region, routing_info, graph_mapper, graph, machine_time_step) spec.end_specification() spec_writer.close() spec_reader = FileDataReader(temp_spec) executor = DataSpecificationExecutor( spec_reader, master_pop_sz + all_syn_block_sz) executor.execute() master_pop_table = executor.get_region(0) synaptic_matrix = executor.get_region(1) direct_matrix = executor.get_region(2) all_data = bytearray() all_data.extend(master_pop_table.region_data[ :master_pop_table.max_write_pointer]) all_data.extend(synaptic_matrix.region_data[ :synaptic_matrix.max_write_pointer]) all_data.extend(direct_matrix.region_data[ :direct_matrix.max_write_pointer]) master_pop_table_address = 0 synaptic_matrix_address = master_pop_table.max_write_pointer direct_synapses_address = ( synaptic_matrix_address + synaptic_matrix.max_write_pointer) direct_synapses_address += 4 indirect_synapses_address = synaptic_matrix_address placement = Placement(None, 0, 0, 1) transceiver = MockTransceiverRawData(all_data) # Get the master population table details items = synaptic_manager._poptable_type\ .extract_synaptic_matrix_data_location( key, master_pop_table_address, transceiver, placement.x, placement.y) # The first entry should be direct, but the rest should be indirect; # the second is potentially direct, but has been restricted by the # restriction on the size of the direct matrix assert len(items) == 3 assert items[0][2] assert not items[1][2] assert not items[2][2] data_1, row_len_1 = synaptic_manager._retrieve_synaptic_block( transceiver=transceiver, placement=placement, master_pop_table_address=master_pop_table_address, indirect_synapses_address=indirect_synapses_address, direct_synapses_address=direct_synapses_address, key=key, n_rows=pre_vertex_slice.n_atoms, index=0, using_extra_monitor_cores=False) connections_1 = synaptic_manager._synapse_io.read_synapses( direct_synapse_information_1, pre_vertex_slice, post_vertex_slice, row_len_1, 0, 2, weight_scales, data_1, None, app_edge.n_delay_stages, machine_time_step) # The first matrix is a 1-1 matrix, so row length is 1 assert row_len_1 == 1 # Check that all the connections have the right weight and delay assert len(connections_1) == post_vertex_slice.n_atoms assert all([conn["weight"] == 1.5 for conn in connections_1]) assert all([conn["delay"] == 1.0 for conn in connections_1]) data_2, row_len_2 = synaptic_manager._retrieve_synaptic_block( transceiver=transceiver, placement=placement, master_pop_table_address=master_pop_table_address, indirect_synapses_address=indirect_synapses_address, direct_synapses_address=direct_synapses_address, key=key, n_rows=pre_vertex_slice.n_atoms, index=1, using_extra_monitor_cores=False) connections_2 = synaptic_manager._synapse_io.read_synapses( direct_synapse_information_2, pre_vertex_slice, post_vertex_slice, row_len_2, 0, 2, weight_scales, data_2, None, app_edge.n_delay_stages, machine_time_step) # The second matrix is a 1-1 matrix, so row length is 1 assert row_len_2 == 1 # Check that all the connections have the right weight and delay assert len(connections_2) == post_vertex_slice.n_atoms assert all([conn["weight"] == 2.5 for conn in connections_2]) assert all([conn["delay"] == 2.0 for conn in connections_2]) data_3, row_len_3 = synaptic_manager._retrieve_synaptic_block( transceiver=transceiver, placement=placement, master_pop_table_address=master_pop_table_address, indirect_synapses_address=indirect_synapses_address, direct_synapses_address=direct_synapses_address, key=key, n_rows=pre_vertex_slice.n_atoms, index=2, using_extra_monitor_cores=False) connections_3 = synaptic_manager._synapse_io.read_synapses( all_to_all_synapse_information, pre_vertex_slice, post_vertex_slice, row_len_3, 0, 2, weight_scales, data_3, None, app_edge.n_delay_stages, machine_time_step) # The third matrix is an all-to-all matrix, so length is n_atoms assert row_len_3 == post_vertex_slice.n_atoms # Check that all the connections have the right weight and delay assert len(connections_3) == \ post_vertex_slice.n_atoms * pre_vertex_slice.n_atoms assert all([conn["weight"] == 4.5 for conn in connections_3]) assert all([conn["delay"] == 4.0 for conn in connections_3])
def test_set_synapse_dynamics(self): MockSimulator.setup() default_config_paths = os.path.join( os.path.dirname(abstract_spinnaker_common.__file__), AbstractSpiNNakerCommon.CONFIG_FILE_NAME) config = conf_loader.load_config( AbstractSpiNNakerCommon.CONFIG_FILE_NAME, default_config_paths) synaptic_manager = SynapticManager( n_synapse_types=2, ring_buffer_sigma=5.0, spikes_per_second=100.0, config=config) static = SynapseDynamicsStatic() stdp = SynapseDynamicsSTDP( timing_dependence=TimingDependenceSpikePair(), weight_dependence=WeightDependenceAdditive()) alt_stdp = SynapseDynamicsSTDP( timing_dependence=TimingDependenceSpikePair(), weight_dependence=WeightDependenceMultiplicative()) static_struct = SynapseDynamicsStructuralStatic( partner_selection=LastNeuronSelection(), formation=DistanceDependentFormation(), elimination=RandomByWeightElimination(0.5)) alt_static_struct = SynapseDynamicsStructuralStatic( partner_selection=RandomSelection(), formation=DistanceDependentFormation(), elimination=RandomByWeightElimination(0.5)) stdp_struct = SynapseDynamicsStructuralSTDP( partner_selection=LastNeuronSelection(), formation=DistanceDependentFormation(), elimination=RandomByWeightElimination(0.5), timing_dependence=TimingDependenceSpikePair(), weight_dependence=WeightDependenceAdditive()) alt_stdp_struct = SynapseDynamicsStructuralSTDP( partner_selection=RandomSelection(), formation=DistanceDependentFormation(), elimination=RandomByWeightElimination(0.5), timing_dependence=TimingDependenceSpikePair(), weight_dependence=WeightDependenceAdditive()) alt_stdp_struct_2 = SynapseDynamicsStructuralSTDP( partner_selection=LastNeuronSelection(), formation=DistanceDependentFormation(), elimination=RandomByWeightElimination(0.5), timing_dependence=TimingDependenceSpikePair(), weight_dependence=WeightDependenceMultiplicative()) # This should be fine as it is the first call synaptic_manager.synapse_dynamics = static # This should be fine as STDP overrides static synaptic_manager.synapse_dynamics = stdp # This should fail because STDP dependences are difference with self.assertRaises(SynapticConfigurationException): synaptic_manager.synapse_dynamics = alt_stdp # This should work because STDP dependences are the same synaptic_manager.synapse_dynamics = stdp # This should work because static always works, but the type should # still be STDP synaptic_manager.synapse_dynamics = static self.assertIsInstance( synaptic_manager.synapse_dynamics, SynapseDynamicsSTDP) # This should work but should merge with the STDP rule synaptic_manager.synapse_dynamics = static_struct self.assertIsInstance( synaptic_manager.synapse_dynamics, SynapseDynamicsStructuralSTDP) # These should work as static / the STDP is the same but neither should # change anything synaptic_manager.synapse_dynamics = static self.assertIsInstance( synaptic_manager.synapse_dynamics, SynapseDynamicsStructuralSTDP) synaptic_manager.synapse_dynamics = stdp self.assertIsInstance( synaptic_manager.synapse_dynamics, SynapseDynamicsStructuralSTDP) synaptic_manager.synapse_dynamics = static_struct self.assertIsInstance( synaptic_manager.synapse_dynamics, SynapseDynamicsStructuralSTDP) # These should fail as things are different with self.assertRaises(SynapticConfigurationException): synaptic_manager.synapse_dynamics = alt_static_struct with self.assertRaises(SynapticConfigurationException): synaptic_manager.synapse_dynamics = alt_stdp # This should pass as same structural STDP synaptic_manager.synapse_dynamics = stdp_struct self.assertIsInstance( synaptic_manager.synapse_dynamics, SynapseDynamicsStructuralSTDP) # These should fail as both different with self.assertRaises(SynapticConfigurationException): synaptic_manager.synapse_dynamics = alt_stdp_struct with self.assertRaises(SynapticConfigurationException): synaptic_manager.synapse_dynamics = alt_stdp_struct_2 # Try starting again to get a couple more combinations synaptic_manager = SynapticManager( n_synapse_types=2, ring_buffer_sigma=5.0, spikes_per_second=100.0, config=config) # STDP followed by structural STDP should result in Structural STDP synaptic_manager.synapse_dynamics = stdp synaptic_manager.synapse_dynamics = stdp_struct self.assertIsInstance( synaptic_manager.synapse_dynamics, SynapseDynamicsStructuralSTDP) # ... and should fail here because of differences with self.assertRaises(SynapticConfigurationException): synaptic_manager.synapse_dynamics = alt_stdp with self.assertRaises(SynapticConfigurationException): synaptic_manager.synapse_dynamics = alt_static_struct with self.assertRaises(SynapticConfigurationException): synaptic_manager.synapse_dynamics = alt_stdp_struct with self.assertRaises(SynapticConfigurationException): synaptic_manager.synapse_dynamics = alt_stdp_struct_2 # One more time! synaptic_manager = SynapticManager( n_synapse_types=2, ring_buffer_sigma=5.0, spikes_per_second=100.0, config=config) # Static followed by static structural should result in static # structural synaptic_manager.synapse_dynamics = static synaptic_manager.synapse_dynamics = static_struct self.assertIsInstance( synaptic_manager.synapse_dynamics, SynapseDynamicsStructuralStatic) # ... and should fail here because of differences with self.assertRaises(SynapticConfigurationException): synaptic_manager.synapse_dynamics = alt_static_struct with self.assertRaises(SynapticConfigurationException): synaptic_manager.synapse_dynamics = alt_stdp_struct # This should be fine synaptic_manager.synapse_dynamics = static # This should be OK, but should merge with STDP (opposite of above) synaptic_manager.synapse_dynamics = stdp self.assertIsInstance( synaptic_manager.synapse_dynamics, SynapseDynamicsStructuralSTDP) # ... and now these should fail with self.assertRaises(SynapticConfigurationException): synaptic_manager.synapse_dynamics = alt_stdp with self.assertRaises(SynapticConfigurationException): synaptic_manager.synapse_dynamics = alt_static_struct with self.assertRaises(SynapticConfigurationException): synaptic_manager.synapse_dynamics = alt_stdp_struct with self.assertRaises(SynapticConfigurationException): synaptic_manager.synapse_dynamics = alt_stdp_struct_2 # OK, just one more, honest synaptic_manager = SynapticManager( n_synapse_types=2, ring_buffer_sigma=5.0, spikes_per_second=100.0, config=config) synaptic_manager.synapse_dynamics = static_struct synaptic_manager.synapse_dynamics = stdp_struct
def test_connectors( n_pre, n_post, n_in_slice, create_connector, weight, delay): MockSimulator.setup() max_target = 0 max_source = 0 max_row_length = None max_col_length = None for seed in range(10): numpy.random.seed(random.randint(0, 1000)) connector = create_connector() mock_synapse_info = MockSynapseInfo(MockPopulation(n_pre, "Pre"), MockPopulation(n_post, "Post"), weight, delay) connector.set_projection_information( machine_time_step=1000, synapse_info=mock_synapse_info) pre_slices = [ Slice(i, i + n_in_slice - 1) for i in range(0, n_pre, n_in_slice)] post_slices = [ Slice(i, i + n_in_slice - 1) for i in range(0, n_post, n_in_slice)] pre_slice_index = 0 post_slice_index = 0 pre_vertex_slice = pre_slices[pre_slice_index] post_vertex_slice = post_slices[post_slice_index] synapse_type = 0 pre_slice = pre_slices[pre_slice_index] post_slice = post_slices[post_slice_index] pre_range = numpy.arange(pre_slice.lo_atom, pre_slice.hi_atom + 2) post_range = numpy.arange(post_slice.lo_atom, post_slice.hi_atom + 2) max_delay = connector.get_delay_maximum(mock_synapse_info) max_weight = connector.get_weight_maximum(mock_synapse_info) if max_row_length is None: max_row_length = connector.\ get_n_connections_from_pre_vertex_maximum( post_vertex_slice, mock_synapse_info) else: assert(max_row_length == connector. get_n_connections_from_pre_vertex_maximum( post_vertex_slice, mock_synapse_info)) if max_col_length is None: max_col_length = connector.\ get_n_connections_to_post_vertex_maximum(mock_synapse_info) else: assert(max_col_length == connector. get_n_connections_to_post_vertex_maximum(mock_synapse_info)) synaptic_block = connector.create_synaptic_block( pre_slices, pre_slice_index, post_slices, post_slice_index, pre_vertex_slice, post_vertex_slice, synapse_type, mock_synapse_info) source_histogram = numpy.histogram( synaptic_block["source"], pre_range)[0] target_histogram = numpy.histogram( synaptic_block["target"], post_range)[0] matrix_max_weight = ( max(synaptic_block["weight"]) if len(synaptic_block) > 0 else 0) matrix_max_delay = ( max(synaptic_block["delay"]) if len(synaptic_block) > 0 else 0) max_source = max((max(source_histogram), max_source)) max_target = max((max(target_histogram), max_target)) if len(post_slices) > post_slice_index + 1: test_post_slice = post_slices[post_slice_index + 1] test_synaptic_block = connector.create_synaptic_block( pre_slices, pre_slice_index, post_slices, post_slice_index + 1, pre_vertex_slice, test_post_slice, synapse_type, mock_synapse_info) if len(test_synaptic_block) > 0: assert not numpy.array_equal( test_synaptic_block, synaptic_block) if len(pre_slices) > pre_slice_index + 1: test_pre_slice = pre_slices[pre_slice_index + 1] test_synaptic_block = connector.create_synaptic_block( pre_slices, pre_slice_index + 1, post_slices, post_slice_index, test_pre_slice, post_vertex_slice, synapse_type, mock_synapse_info) if len(test_synaptic_block) > 0: assert not numpy.array_equal( test_synaptic_block, synaptic_block) try: assert max(source_histogram) <= max_row_length assert max(target_histogram) <= max_col_length assert matrix_max_weight <= max_weight assert matrix_max_delay <= max_delay except Exception: print(connector, n_pre, n_post, n_in_slice) print(max_row_length, max(source_histogram), source_histogram) print(max_col_length, max(target_histogram), target_histogram) print(max_weight, matrix_max_weight, synaptic_block["weight"]) print(max_delay, matrix_max_delay, synaptic_block["delay"]) print(connector, n_pre, n_post, n_in_slice, max_row_length, max_source, max_col_length, max_target)
def test_connectors( n_pre, n_post, n_in_slice, create_connector, weight, delay): MockSimulator.setup() max_target = 0 max_source = 0 max_row_length = None max_col_length = None for seed in range(1000): numpy.random.seed(seed) connector = create_connector() connector.set_projection_information( pre_population=MockPopulation(n_pre, "Pre"), post_population=MockPopulation(n_post, "Post"), rng=None, machine_time_step=1000) pre_slices = [ Slice(i, i + n_in_slice - 1) for i in range(0, n_pre, n_in_slice)] post_slices = [ Slice(i, i + n_in_slice - 1) for i in range(0, n_post, n_in_slice)] pre_slice_index = 0 post_slice_index = 0 pre_vertex_slice = pre_slices[pre_slice_index] post_vertex_slice = post_slices[post_slice_index] synapse_type = 0 pre_slice = pre_slices[pre_slice_index] post_slice = post_slices[post_slice_index] pre_range = numpy.arange(pre_slice.lo_atom, pre_slice.hi_atom + 2) post_range = numpy.arange(post_slice.lo_atom, post_slice.hi_atom + 2) max_delay = connector.get_delay_maximum(delay) max_weight = connector.get_weight_maximum(weight) if max_row_length is None: max_row_length = connector.\ get_n_connections_from_pre_vertex_maximum( delay, post_vertex_slice) else: assert(max_row_length == connector. get_n_connections_from_pre_vertex_maximum( delay, post_vertex_slice)) if max_col_length is None: max_col_length = connector.\ get_n_connections_to_post_vertex_maximum() else: assert(max_col_length == connector. get_n_connections_to_post_vertex_maximum()) synaptic_block = connector.create_synaptic_block( weight, delay, pre_slices, pre_slice_index, post_slices, post_slice_index, pre_vertex_slice, post_vertex_slice, synapse_type) source_histogram = numpy.histogram( synaptic_block["source"], pre_range)[0] target_histogram = numpy.histogram( synaptic_block["target"], post_range)[0] matrix_max_weight = ( max(synaptic_block["weight"]) if len(synaptic_block) > 0 else 0) matrix_max_delay = ( max(synaptic_block["delay"]) if len(synaptic_block) > 0 else 0) max_source = max((max(source_histogram), max_source)) max_target = max((max(target_histogram), max_target)) if len(post_slices) > post_slice_index + 1: test_post_slice = post_slices[post_slice_index + 1] test_synaptic_block = connector.create_synaptic_block( weight, delay, pre_slices, pre_slice_index, post_slices, post_slice_index + 1, pre_vertex_slice, test_post_slice, synapse_type) if len(test_synaptic_block) > 0: assert not numpy.array_equal( test_synaptic_block, synaptic_block) if len(pre_slices) > pre_slice_index + 1: test_pre_slice = pre_slices[pre_slice_index + 1] test_synaptic_block = connector.create_synaptic_block( weight, delay, pre_slices, pre_slice_index + 1, post_slices, post_slice_index, test_pre_slice, post_vertex_slice, synapse_type) if len(test_synaptic_block) > 0: assert not numpy.array_equal( test_synaptic_block, synaptic_block) try: assert max(source_histogram) <= max_row_length assert max(target_histogram) <= max_col_length assert matrix_max_weight <= max_weight assert matrix_max_delay <= max_delay except Exception: print(connector, n_pre, n_post, n_in_slice) print(max_row_length, max(source_histogram), source_histogram) print(max_col_length, max(target_histogram), target_histogram) print(max_weight, matrix_max_weight, synaptic_block["weight"]) print(max_delay, matrix_max_delay, synaptic_block["delay"]) raise SkipTest( "https://github.com/SpiNNakerManchester/sPyNNaker/issues/587") print(connector, n_pre, n_post, n_in_slice, max_row_length, max_source, max_col_length, max_target)
def test_write_synaptic_matrix_and_master_population_table(self): MockSimulator.setup() # Add an sdram so max SDRAM is high enough SDRAM(10000) # UGLY but the mock transceiver NEED generate_on_machine to be False AbstractGenerateConnectorOnMachine.generate_on_machine = self.say_false default_config_paths = os.path.join( os.path.dirname(abstract_spinnaker_common.__file__), AbstractSpiNNakerCommon.CONFIG_FILE_NAME) config = conf_loader.load_config( AbstractSpiNNakerCommon.CONFIG_FILE_NAME, default_config_paths) config.set("Simulation", "one_to_one_connection_dtcm_max_bytes", 40) machine_time_step = 1000.0 pre_app_vertex = SimpleApplicationVertex(10) pre_vertex_slice = Slice(0, 9) pre_vertex = pre_app_vertex.create_machine_vertex( pre_vertex_slice, None) post_app_vertex = SimpleApplicationVertex(10) post_vertex_slice = Slice(0, 9) post_vertex = post_app_vertex.create_machine_vertex( post_vertex_slice, None) post_slice_index = 0 one_to_one_connector_1 = OneToOneConnector(None) direct_synapse_information_1 = SynapseInformation( one_to_one_connector_1, pre_app_vertex, post_app_vertex, False, False, None, SynapseDynamicsStatic(), 0, 1.5, 1.0) one_to_one_connector_1.set_projection_information( machine_time_step, direct_synapse_information_1) one_to_one_connector_2 = OneToOneConnector(None) direct_synapse_information_2 = SynapseInformation( one_to_one_connector_2, pre_app_vertex, post_app_vertex, False, False, None, SynapseDynamicsStatic(), 1, 2.5, 2.0) one_to_one_connector_2.set_projection_information( machine_time_step, direct_synapse_information_2) all_to_all_connector = AllToAllConnector(None) all_to_all_synapse_information = SynapseInformation( all_to_all_connector, pre_app_vertex, post_app_vertex, False, False, None, SynapseDynamicsStatic(), 0, 4.5, 4.0) all_to_all_connector.set_projection_information( machine_time_step, all_to_all_synapse_information) app_edge = ProjectionApplicationEdge(pre_app_vertex, post_app_vertex, direct_synapse_information_1) app_edge.add_synapse_information(direct_synapse_information_2) app_edge.add_synapse_information(all_to_all_synapse_information) machine_edge = app_edge.create_machine_edge(pre_vertex, post_vertex, label=None) partition_name = "TestPartition" graph = MachineGraph("Test") graph.add_vertex(pre_vertex) graph.add_vertex(post_vertex) graph.add_edge(machine_edge, partition_name) weight_scales = [4096.0, 4096.0] key = 0 routing_info = RoutingInfo() routing_info.add_partition_info( PartitionRoutingInfo( [BaseKeyAndMask(key, 0xFFFFFFF0)], graph.get_outgoing_edge_partition_starting_at_vertex( pre_vertex, partition_name))) temp_spec = tempfile.mktemp() spec_writer = FileDataWriter(temp_spec) spec = DataSpecificationGenerator(spec_writer, None) master_pop_sz = 1000 all_syn_block_sz = 2000 master_pop_region = 0 synapse_region = 1 direct_region = 2 spec.reserve_memory_region(master_pop_region, master_pop_sz) spec.reserve_memory_region(synapse_region, all_syn_block_sz) synaptic_manager = SynapticManager(n_synapse_types=2, ring_buffer_sigma=5.0, spikes_per_second=100.0, config=config) # Poke in our testing region IDs synaptic_manager._pop_table_region = master_pop_region synaptic_manager._synaptic_matrix_region = synapse_region synaptic_manager._direct_matrix_region = direct_region synaptic_manager._write_synaptic_matrix_and_master_population_table( spec, [post_vertex_slice], post_slice_index, post_vertex, post_vertex_slice, all_syn_block_sz, weight_scales, routing_info, graph, machine_time_step) spec.end_specification() spec_writer.close() spec_reader = FileDataReader(temp_spec) executor = DataSpecificationExecutor(spec_reader, master_pop_sz + all_syn_block_sz) executor.execute() master_pop_table = executor.get_region(0) synaptic_matrix = executor.get_region(1) direct_matrix = executor.get_region(2) all_data = bytearray() all_data.extend( master_pop_table.region_data[:master_pop_table.max_write_pointer]) all_data.extend( synaptic_matrix.region_data[:synaptic_matrix.max_write_pointer]) all_data.extend( direct_matrix.region_data[:direct_matrix.max_write_pointer]) master_pop_table_address = 0 synaptic_matrix_address = master_pop_table.max_write_pointer direct_synapses_address = (synaptic_matrix_address + synaptic_matrix.max_write_pointer) direct_synapses_address += 4 indirect_synapses_address = synaptic_matrix_address placement = Placement(None, 0, 0, 1) transceiver = MockTransceiverRawData(all_data) # Get the master population table details items = synaptic_manager._extract_synaptic_matrix_data_location( key, master_pop_table_address, transceiver, placement) # The first entry should be direct, but the rest should be indirect; # the second is potentially direct, but has been restricted by the # restriction on the size of the direct matrix assert len(items) == 3 assert items[0][2] assert not items[1][2] assert not items[2][2] data_1, row_len_1 = synaptic_manager._retrieve_synaptic_block( txrx=transceiver, placement=placement, master_pop_table_address=master_pop_table_address, indirect_synapses_address=indirect_synapses_address, direct_synapses_address=direct_synapses_address, key=key, n_rows=pre_vertex_slice.n_atoms, index=0, using_monitors=False) connections_1 = synaptic_manager._read_synapses( direct_synapse_information_1, pre_vertex_slice, post_vertex_slice, row_len_1, 0, weight_scales, data_1, None, machine_time_step) # The first matrix is a 1-1 matrix, so row length is 1 assert row_len_1 == 1 # Check that all the connections have the right weight and delay assert len(connections_1) == post_vertex_slice.n_atoms assert all([conn["weight"] == 1.5 for conn in connections_1]) assert all([conn["delay"] == 1.0 for conn in connections_1]) data_2, row_len_2 = synaptic_manager._retrieve_synaptic_block( txrx=transceiver, placement=placement, master_pop_table_address=master_pop_table_address, indirect_synapses_address=indirect_synapses_address, direct_synapses_address=direct_synapses_address, key=key, n_rows=pre_vertex_slice.n_atoms, index=1, using_monitors=False) connections_2 = synaptic_manager._read_synapses( direct_synapse_information_2, pre_vertex_slice, post_vertex_slice, row_len_2, 0, weight_scales, data_2, None, machine_time_step) # The second matrix is a 1-1 matrix, so row length is 1 assert row_len_2 == 1 # Check that all the connections have the right weight and delay assert len(connections_2) == post_vertex_slice.n_atoms assert all([conn["weight"] == 2.5 for conn in connections_2]) assert all([conn["delay"] == 2.0 for conn in connections_2]) data_3, row_len_3 = synaptic_manager._retrieve_synaptic_block( txrx=transceiver, placement=placement, master_pop_table_address=master_pop_table_address, indirect_synapses_address=indirect_synapses_address, direct_synapses_address=direct_synapses_address, key=key, n_rows=pre_vertex_slice.n_atoms, index=2, using_monitors=False) connections_3 = synaptic_manager._read_synapses( all_to_all_synapse_information, pre_vertex_slice, post_vertex_slice, row_len_3, 0, weight_scales, data_3, None, machine_time_step) # The third matrix is an all-to-all matrix, so length is n_atoms assert row_len_3 == post_vertex_slice.n_atoms # Check that all the connections have the right weight and delay assert len(connections_3) == \ post_vertex_slice.n_atoms * pre_vertex_slice.n_atoms assert all([conn["weight"] == 4.5 for conn in connections_3]) assert all([conn["delay"] == 4.0 for conn in connections_3])
def test_connectors(n_pre, n_post, n_in_slice, create_connector, weight, delay): MockSimulator.setup() max_target = 0 max_source = 0 max_row_length = None max_col_length = None for seed in range(1000): numpy.random.seed(seed) connector = create_connector() connector.set_projection_information( pre_population=MockPopulation(n_pre, "Pre"), post_population=MockPopulation(n_post, "Post"), rng=None, machine_time_step=1000) pre_slices = [ Slice(i, i + n_in_slice - 1) for i in range(0, n_pre, n_in_slice) ] post_slices = [ Slice(i, i + n_in_slice - 1) for i in range(0, n_post, n_in_slice) ] pre_slice_index = 0 post_slice_index = 0 pre_vertex_slice = pre_slices[pre_slice_index] post_vertex_slice = post_slices[post_slice_index] synapse_type = 0 pre_slice = pre_slices[pre_slice_index] post_slice = post_slices[post_slice_index] pre_range = numpy.arange(pre_slice.lo_atom, pre_slice.hi_atom + 2) post_range = numpy.arange(post_slice.lo_atom, post_slice.hi_atom + 2) max_delay = connector.get_delay_maximum(delay) max_weight = connector.get_weight_maximum(weight) if max_row_length is None: max_row_length = connector.\ get_n_connections_from_pre_vertex_maximum( delay, post_vertex_slice) else: assert (max_row_length == connector.get_n_connections_from_pre_vertex_maximum( delay, post_vertex_slice)) if max_col_length is None: max_col_length = connector.\ get_n_connections_to_post_vertex_maximum() else: assert (max_col_length == connector.get_n_connections_to_post_vertex_maximum()) synaptic_block = connector.create_synaptic_block( weight, delay, pre_slices, pre_slice_index, post_slices, post_slice_index, pre_vertex_slice, post_vertex_slice, synapse_type) source_histogram = numpy.histogram(synaptic_block["source"], pre_range)[0] target_histogram = numpy.histogram(synaptic_block["target"], post_range)[0] matrix_max_weight = (max(synaptic_block["weight"]) if len(synaptic_block) > 0 else 0) matrix_max_delay = (max(synaptic_block["delay"]) if len(synaptic_block) > 0 else 0) max_source = max((max(source_histogram), max_source)) max_target = max((max(target_histogram), max_target)) if len(post_slices) > post_slice_index + 1: test_post_slice = post_slices[post_slice_index + 1] test_synaptic_block = connector.create_synaptic_block( weight, delay, pre_slices, pre_slice_index, post_slices, post_slice_index + 1, pre_vertex_slice, test_post_slice, synapse_type) if len(test_synaptic_block) > 0: assert not numpy.array_equal(test_synaptic_block, synaptic_block) if len(pre_slices) > pre_slice_index + 1: test_pre_slice = pre_slices[pre_slice_index + 1] test_synaptic_block = connector.create_synaptic_block( weight, delay, pre_slices, pre_slice_index + 1, post_slices, post_slice_index, test_pre_slice, post_vertex_slice, synapse_type) if len(test_synaptic_block) > 0: assert not numpy.array_equal(test_synaptic_block, synaptic_block) try: assert max(source_histogram) <= max_row_length assert max(target_histogram) <= max_col_length assert matrix_max_weight <= max_weight assert matrix_max_delay <= max_delay except Exception: print(connector, n_pre, n_post, n_in_slice) print(max_row_length, max(source_histogram), source_histogram) print(max_col_length, max(target_histogram), target_histogram) print(max_weight, matrix_max_weight, synaptic_block["weight"]) print(max_delay, matrix_max_delay, synaptic_block["delay"]) raise SkipTest( "https://github.com/SpiNNakerManchester/sPyNNaker/issues/587") print(connector, n_pre, n_post, n_in_slice, max_row_length, max_source, max_col_length, max_target)