sPyNNaker neural_modelling 7.4.2
Loading...
Searching...
No Matches
synapse_dynamics_stdp_mad_impl.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 The University of Manchester
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
20
27};
28
29extern uint32_t skipped_synapses;
30
31//---------------------------------------
43 const uint32_t time,
44 const uint32_t last_pre_time, const pre_trace_t last_pre_trace,
45 const pre_trace_t new_pre_trace, const uint32_t delay_dendritic,
46 const uint32_t delay_axonal, update_state_t current_state,
48 // Apply axonal delay to time of last presynaptic spike
49 const uint32_t delayed_last_pre_time = last_pre_time + delay_axonal;
50
51 // Get the post-synaptic window of events to be processed
52 const uint32_t window_begin_time =
53 (delayed_last_pre_time >= delay_dendritic)
54 ? (delayed_last_pre_time - delay_dendritic) : 0;
55 const uint32_t delayed_pre_time = time + delay_axonal;
56 const uint32_t window_end_time =
57 (delayed_pre_time >= delay_dendritic)
58 ? (delayed_pre_time - delay_dendritic) : 0;
60 post_event_history, window_begin_time, window_end_time);
61
62 log_debug("\tPerforming deferred synapse update at time:%u", time);
63 log_debug("\t\tbegin_time:%u, end_time:%u - prev_time:%u (valid %u), num_events:%u",
64 window_begin_time, window_end_time, post_window.prev_time,
65 post_window.prev_time_valid, post_window.num_events);
66
67#if LOG_LEVEL >= LOG_DEBUG
70 window_end_time, delay_dendritic);
71#endif
72
73 // Process events in post-synaptic window
74 while (post_window.num_events > 0) {
75 const uint32_t delayed_post_time = *post_window.next_time + delay_dendritic;
76
77 log_debug("\t\tApplying post-synaptic event at delayed time:%u, pre:%u\n",
78 delayed_post_time, delayed_last_pre_time);
79
80 // Apply spike to state
81 current_state = timing_apply_post_spike(
82 delayed_post_time, *post_window.next_trace, delayed_last_pre_time,
83 last_pre_trace, post_window.prev_time, post_window.prev_trace,
84 current_state);
85
86 // Go onto next event
87 post_window = post_events_next(post_window);
88 }
89
90 // Apply spike to state only if there has been a post spike ever
91 if (post_window.prev_time_valid) {
92 const uint32_t delayed_last_post = post_window.prev_time + delay_dendritic;
93 log_debug("\t\tApplying pre-synaptic event at time:%u last post time:%u\n",
94 delayed_pre_time, delayed_last_post);
95 current_state = timing_apply_pre_spike(
96 delayed_pre_time, new_pre_trace, delayed_last_pre_time, last_pre_trace,
97 delayed_last_post, post_window.prev_trace, current_state);
98 }
99
100 // Return final synaptic word and weight
101 return synapse_structure_get_final_state(current_state);
102}
103
105 address_t address, uint32_t n_neurons, uint32_t n_synapse_types,
106 uint32_t *ring_buffer_to_input_buffer_left_shifts) {
107
108 if (!synapse_dynamics_stdp_init(&address, &params, n_synapse_types,
109 ring_buffer_to_input_buffer_left_shifts)) {
110 return false;
111 }
112
114 if (post_event_history == NULL) {
115 return false;
116 }
117
118 return true;
119}
120
121//---------------------------------------
122// Synaptic row plastic-region implementation
123//---------------------------------------
125 synapse_row_plastic_data_t *plastic_region_data,
126 synapse_row_fixed_part_t *fixed_region,
127 uint32_t *ring_buffer_to_input_buffer_left_shifts) {
128 __use(plastic_region_data);
129 __use(fixed_region);
130 __use(ring_buffer_to_input_buffer_left_shifts);
131
132#if LOG_LEVEL >= LOG_DEBUG
133 // Extract separate arrays of weights (from plastic region),
134 // Control words (from fixed region) and number of plastic synapses
135 const plastic_synapse_t *plastic_words = plastic_region_data->synapses;
136 const control_t *control_words = synapse_row_plastic_controls(fixed_region);
137 size_t plastic_synapse = synapse_row_num_plastic_controls(fixed_region);
138
139 log_debug("Plastic region %u synapses\n", plastic_synapse);
140
141 // Loop through plastic synapses
142 for (uint32_t i = 0; i < plastic_synapse; i++) {
143 // Get next control word (auto incrementing control word)
144 uint32_t control_word = *control_words++;
145 uint32_t synapse_type = synapse_row_sparse_type(
147
148 // Get weight
150 *plastic_words++, synapse_type);
152 update_state);
153 weight_t weight = synapse_structure_get_final_weight(final_state);
154
155 log_debug("%08x [%3d: (w: %5u (=", control_word, i, weight);
157 weight, ring_buffer_to_input_buffer_left_shifts[synapse_type]);
158 log_debug("nA) d: %2u, n = %3u)] - {%08x %08x}\n",
162 }
163#endif // LOG_LEVEL >= LOG_DEBUG
164}
165
166//---------------------------------------
170static inline index_t sparse_axonal_delay(uint32_t x) {
171#if 1
172 // No axonal delay, ever
173 __use(x);
174 return 0;
175#else
176 return (x >> synapse_delay_index_type_bits) & SYNAPSE_AXONAL_DELAY_MASK;
177#endif
178}
179
180//---------------------------------------
182 uint32_t time, index_t neuron_index) {
183 log_debug("Adding post-synaptic event to trace at time:%u", time);
184
185 // Add post-event
186 post_event_history_t *history = &post_event_history[neuron_index];
187 const uint32_t last_post_time = history->times[history->count_minus_one];
188 const post_trace_t last_post_trace =
189 history->traces[history->count_minus_one];
190 post_events_add(time, history,
191 timing_add_post_spike(time, last_post_time, last_post_trace));
192}
193
194//---------------------------------------
195static inline plastic_synapse_t process_plastic_synapse(
196 uint32_t control_word, uint32_t last_pre_time, pre_trace_t last_pre_trace,
197 pre_trace_t new_pre_trace, weight_t *ring_buffers, uint32_t time,
198 uint32_t colour_delay, plastic_synapse_t synapse) {
199 fixed_stdp_synapse s = synapse_dynamics_stdp_get_fixed(control_word, time,
200 colour_delay);
201
202 // Create update state from the plastic synaptic word
204 synapse, s.type);
205
206 // Update the synapse state
207 uint32_t post_delay = s.delay_dendritic;
208 if (!params.backprop_delay) {
209 post_delay = 0;
210 }
212 time - colour_delay, last_pre_time, last_pre_trace, new_pre_trace,
213 post_delay, s.delay_axonal, current_state,
214 &post_event_history[s.index]);
215
216 // Add weight to ring-buffer entry, but only if not too late
217 if (s.delay_axonal + s.delay_dendritic > colour_delay) {
218 int32_t weight = synapse_structure_get_final_weight(final_state);
219 synapse_dynamics_stdp_update_ring_buffers(ring_buffers, s, weight);
220 } else {
222 }
223
225}
226
228 synapse_row_plastic_data_t *plastic_region_address,
229 synapse_row_fixed_part_t *fixed_region,
230 weight_t *ring_buffers, uint32_t time, uint32_t colour_delay,
231 bool *write_back) {
232 // Extract separate arrays of plastic synapses (from plastic region),
233 // Control words (from fixed region) and number of plastic synapses
234 plastic_synapse_t *plastic_words = plastic_region_address->synapses;
235 const control_t *control_words = synapse_row_plastic_controls(fixed_region);
236 size_t n_plastic_synapses = synapse_row_num_plastic_controls(fixed_region);
237
238 num_plastic_pre_synaptic_events += n_plastic_synapses;
239
240 // Get last pre-synaptic event from event history
241 const uint32_t last_pre_time = plastic_region_address->history.prev_time;
242 const pre_trace_t last_pre_trace = plastic_region_address->history.prev_trace;
243
244 // Update pre-synaptic trace
245 log_debug("Adding pre-synaptic event to trace at time:%u", time);
246 plastic_region_address->history.prev_time = time - colour_delay;
247 plastic_region_address->history.prev_trace =
248 timing_add_pre_spike(time - colour_delay, last_pre_time, last_pre_trace);
249
250 // Loop through plastic synapses
251 for (; n_plastic_synapses > 0; n_plastic_synapses--) {
252 // Get next control word (auto incrementing)
253 uint32_t control_word = *control_words++;
254
255 plastic_words[0] = process_plastic_synapse(
256 control_word, last_pre_time, last_pre_trace,
257 plastic_region_address->history.prev_trace, ring_buffers, time,
258 colour_delay, plastic_words[0]);
259 plastic_words++;
260 }
261 *write_back = true;
262 return true;
263}
264
266 uint32_t id, synaptic_row_t row, weight_t *weight, uint16_t *delay,
267 uint32_t *offset, uint32_t *synapse_type) {
269 const synapse_row_plastic_data_t *plastic_data = (void *)
271 const plastic_synapse_t *plastic_words = plastic_data->synapses;
272 const control_t *control_words = synapse_row_plastic_controls(fixed_region);
273 const size_t n_plastic_synapses = synapse_row_num_plastic_controls(fixed_region);
274
275 // Loop through plastic synapses
276 for (size_t plastic_synapse = n_plastic_synapses; plastic_synapse > 0;
277 plastic_synapse--) {
278 // Take the weight anyway as this updates the plastic words
279 *weight = synapse_structure_get_weight(*plastic_words++);
280
281 // Check if index is the one I'm looking for
282 uint32_t control_word = *control_words++;
283 if (synapse_row_sparse_index(control_word, synapse_index_mask) == id) {
284 *offset = n_plastic_synapses - plastic_synapse;
285 *delay = synapse_row_sparse_delay(control_word,
287 *synapse_type = synapse_row_sparse_type(
289 return true;
290 }
291 }
292
293 return false;
294}
295
298 synapse_row_plastic_data_t *plastic_data = (void *)
300 plastic_synapse_t *plastic_words = plastic_data->synapses;
301
302 control_t *control_words = synapse_row_plastic_controls(fixed_region);
303 int32_t plastic_synapse = synapse_row_num_plastic_controls(fixed_region);
304
305 // Delete weight at offset
306 plastic_words[offset] = plastic_words[plastic_synapse - 1];
307
308 // Delete control word at offset
309 control_words[offset] = control_words[plastic_synapse - 1];
310 control_words[plastic_synapse - 1] = 0;
311
312 // Decrement FP
313 fixed_region->num_plastic--;
314 return true;
315}
316
318 weight_t weight, uint32_t delay, uint32_t type) {
321 plastic_synapse_t *plastic_words = plastic_data->synapses;
323 control_t new_control = control_conversion(id, delay, type);
324
325 control_t *control_words = synapse_row_plastic_controls(fixed_region);
326 int32_t plastic_synapse = synapse_row_num_plastic_controls(fixed_region);
327
328 // Add weight at offset
329 plastic_words[plastic_synapse] = new_weight;
330
331 // Add control word at offset
332 control_words[plastic_synapse] = new_control;
333
334 // Increment FP
335 fixed_region->num_plastic++;
336 return true;
337}
uint32_t index_t
static weight_t * ring_buffers
The ring buffers to be used in the simulation.
Definition c_main.c:118
uint32_t time
The current timer tick value.
Definition c_main.c:94
uint32_t * address_t
void log_debug(const char *message,...)
uint32_t synapse_delay_mask
The mask to get the synaptic delay from a "synapse".
Definition local_only.c:71
uint32_t synapse_type_index_bits
The number of bits used by the synapse type and post-neuron index.
Definition local_only.c:74
uint32_t synapse_index_bits
The number of bits used by just the post-neuron index.
Definition local_only.c:77
struct synaptic_row * synaptic_row_t
The type of a synaptic row.
static uint32_t n_neurons
The number of neurons on the core.
Definition neuron.c:45
static uint32_t n_synapse_types
The number of synapse types.
Definition neuron.c:51
static post_event_history_t * post_events_init_buffers(uint32_t n_neurons)
Initialise an array of post-synaptic event histories.
Definition post_events.h:83
static void print_event_history(const post_event_history_t *events)
Print a post-synaptic event history.
Definition post_events.h:71
static post_event_window_t post_events_next(post_event_window_t window)
Advance a post-synaptic event window to the next event.
static void post_events_add(uint32_t time, post_event_history_t *events, post_trace_t trace)
Add a post-synaptic event to the history.
static post_event_window_t post_events_get_window_delayed(const post_event_history_t *events, uint32_t begin_time, uint32_t end_time)
Get the post-synaptic event window.
static void print_delayed_window_events(const post_event_history_t *post_event_history, uint32_t begin_time, uint32_t end_time, uint32_t delay_dendritic)
Print the post-synaptic event history.
uint32_t num_events
The number of events.
Definition post_events.h:59
post_trace_t prev_trace
The previous post-synaptic event trace.
Definition post_events.h:51
const uint32_t * next_time
The next post-synaptic event time.
Definition post_events.h:57
uint32_t prev_time
The previous post-synaptic event time.
Definition post_events.h:53
const post_trace_t * next_trace
The next post-synaptic event trace.
Definition post_events.h:55
uint32_t prev_time_valid
Whether the previous post-synaptic event is valid (based on time)
Definition post_events.h:61
Post event window description.
Definition post_events.h:49
uint32_t count_minus_one
Number of events stored (minus one)
Definition post_events.h:41
uint32_t times[MAX_POST_SYNAPTIC_EVENTS]
Event times.
Definition post_events.h:43
post_trace_t traces[MAX_POST_SYNAPTIC_EVENTS]
Event traces.
Definition post_events.h:45
Trace history of post-synaptic events.
Definition post_events.h:39
#define NULL
plastic_synapse_t synapses[]
The per-synapse information.
pre_event_history_t history
The pre-event history.
The format of the plastic data region of a synaptic row.
STDP core implementation.
uint32_t backprop_delay
The back-propagation delay, in basic simulation timesteps.
static control_t control_conversion(uint32_t id, uint32_t delay, uint32_t type)
packing all of the information into the required plastic control word
static stdp_params params
Configuration parameters.
pre_trace_t prev_trace
The event trace.
static post_event_history_t * post_event_history
The history data of post-events.
static uint32_t num_plastic_pre_synaptic_events
Count of pre-synaptic events relevant to plastic processing.
uint32_t prev_time
The event time.
The type of history data of pre-events.
bool synapse_dynamics_initialise(address_t address, uint32_t n_neurons, uint32_t n_synapse_types, uint32_t *ring_buffer_to_input_buffer_left_shifts)
Initialise the synapse dynamics.
void synapse_dynamics_process_post_synaptic_event(uint32_t time, index_t neuron_index)
Inform the synapses that the neuron fired.
bool synapse_dynamics_find_neuron(uint32_t id, synaptic_row_t row, weight_t *weight, uint16_t *delay, uint32_t *offset, uint32_t *synapse_type)
Search the synaptic row for the the connection with the specified post-synaptic ID.
static final_state_t plasticity_update_synapse(const uint32_t time, const uint32_t last_pre_time, const pre_trace_t last_pre_trace, const pre_trace_t new_pre_trace, const uint32_t delay_dendritic, const uint32_t delay_axonal, update_state_t current_state, const post_event_history_t *post_event_history)
Synapse update loop core.
static index_t sparse_axonal_delay(uint32_t x)
Get the axonal delay.
void synapse_dynamics_print_plastic_synapses(synapse_row_plastic_data_t *plastic_region_data, synapse_row_fixed_part_t *fixed_region, uint32_t *ring_buffer_to_input_buffer_left_shifts)
Print the synapse dynamics.
bool synapse_dynamics_add_neuron(uint32_t id, synaptic_row_t row, weight_t weight, uint32_t delay, uint32_t type)
Add an entry in the synaptic row.
bool synapse_dynamics_remove_neuron(uint32_t offset, synaptic_row_t row)
Remove the entry at the specified offset in the synaptic row.
uint32_t skipped_synapses
Definition synapses.c:84
bool synapse_dynamics_process_plastic_synapses(synapse_row_plastic_data_t *plastic_region_address, synapse_row_fixed_part_t *fixed_region, weight_t *ring_buffers, uint32_t time, uint32_t colour_delay, bool *write_back)
Process the dynamics of the synapses.
static size_t synapse_row_num_plastic_controls(const synapse_row_fixed_part_t *fixed)
Get the number of plastic controls in the row.
static synapse_row_fixed_part_t * synapse_row_fixed_region(synaptic_row_t row)
Get the address of the non-plastic (or fixed) region.
static control_t * synapse_row_plastic_controls(synapse_row_fixed_part_t *fixed)
Get the array of plastic controls in the row.
static index_t synapse_row_sparse_type(uint32_t x, uint32_t synapse_index_bits, uint32_t synapse_type_mask)
Get the type code.
static index_t synapse_row_sparse_index(uint32_t x, uint32_t synapse_index_mask)
Get the index.
static index_t synapse_row_sparse_delay(uint32_t x, uint32_t synapse_type_index_bits, uint32_t synapse_delay_mask)
Get the delay from an encoded synapse descriptor.
static synapse_row_plastic_data_t * synapse_row_plastic_region(synaptic_row_t row)
Get the address of the plastic region.
size_t num_plastic
The number of plastic controls in data
uint16_t control_t
Define the type of the control data.
The type of the fixed part of the row. The fixed-plastic part follows.
static plastic_synapse_t synapse_structure_get_final_synaptic_word(final_state_t final_state)
Get the final plastic synapse data from the final state.
static plastic_synapse_t synapse_structure_create_synapse(weight_t weight)
Create the initial plastic synapse data.
static update_state_t synapse_structure_get_update_state(plastic_synapse_t synaptic_word, index_t synapse_type)
Get the update state from the synapse structure.
static weight_t synapse_structure_get_final_weight(final_state_t final_state)
Get the final weight from the final state.
static final_state_t synapse_structure_get_final_state(update_state_t state)
Get the final state from the update state.
static weight_t synapse_structure_get_weight(plastic_synapse_t synaptic_word)
Get the current synaptic weight from the plastic synapse data.
Plastic synapse contains normal 16-bit weight and an accumulator.
uint32_t synapse_index_mask
Mask to pick out the synapse index.
Definition synapses.c:69
uint32_t synapse_type_mask
Mask to pick out the synapse type.
Definition synapses.c:73
static void synapses_print_weight(weight_t weight, uint32_t left_shift)
Print the weight of a synapse.
Definition synapses.h:68
static post_trace_t timing_add_post_spike(uint32_t time, uint32_t last_time, post_trace_t last_trace)
Add a post spike to the post trace.
static update_state_t timing_apply_pre_spike(uint32_t time, pre_trace_t trace, uint32_t last_pre_time, pre_trace_t last_pre_trace, uint32_t last_post_time, post_trace_t last_post_trace, update_state_t previous_state)
Apply a pre-spike timing rule state update.
static pre_trace_t timing_add_pre_spike(uint32_t time, uint32_t last_time, pre_trace_t last_trace)
Add a pre spike to the pre trace.
static update_state_t timing_apply_post_spike(uint32_t time, post_trace_t trace, uint32_t last_pre_time, pre_trace_t last_pre_trace, uint32_t last_post_time, post_trace_t last_post_trace, update_state_t previous_state)
Apply a post-spike timing rule state update.
The type of post-spike traces.
The type of pre-spike traces.