Skip to content

Commit

Permalink
Merge pull request #1424 from SpiNNakerManchester/stochastic_neurons
Browse files Browse the repository at this point in the history
Stochastic neurons
  • Loading branch information
rowleya committed Apr 23, 2024
2 parents 0a9574e + b7be66a commit df7ce07
Show file tree
Hide file tree
Showing 31 changed files with 2,510 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (c) 2024 The University of Manchester
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

APP = $(notdir $(CURDIR))

NEURON_MODEL_H = $(NEURON_DIR)/neuron/models/neuron_model_lif_impl.h
INPUT_TYPE_H = $(NEURON_DIR)/neuron/input_types/input_type_delta.h
NEURON_IMPL_H = $(NEURON_DIR)/neuron/implementations/neuron_impl_standard.h
THRESHOLD_TYPE_H = $(NEURON_DIR)/neuron/threshold_types/threshold_type_fixed_prob.h
SYNAPSE_TYPE_H = $(NEURON_DIR)/neuron/synapse_types/synapse_types_delta_impl.h
SYNAPSE_DYNAMICS = $(NEURON_DIR)/neuron/plasticity/synapse_dynamics_static_impl.c

include ../neural_build.mk
24 changes: 24 additions & 0 deletions neural_modelling/makefiles/neuron/IF_trunc_delta/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (c) 2024 The University of Manchester
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

APP = $(notdir $(CURDIR))

NEURON_MODEL_H = $(NEURON_DIR)/neuron/models/neuron_model_if_trunc.h
INPUT_TYPE_H = $(NEURON_DIR)/neuron/input_types/input_type_current.h
NEURON_IMPL_H = $(NEURON_DIR)/neuron/implementations/neuron_impl_standard.h
THRESHOLD_TYPE_H = $(NEURON_DIR)/neuron/threshold_types/threshold_type_static.h
SYNAPSE_TYPE_H = $(NEURON_DIR)/neuron/synapse_types/synapse_types_delta_impl.h
SYNAPSE_DYNAMICS = $(NEURON_DIR)/neuron/plasticity/synapse_dynamics_static_impl.c

include ../neural_build.mk
7 changes: 6 additions & 1 deletion neural_modelling/makefiles/neuron/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ MODELS = IF_curr_exp \
IZK_cond_exp_dual \
IF_curr_delta \
IF_curr_exp_ca2_adaptive \
IF_curr_delta_ca2_adaptive
IF_curr_delta_ca2_adaptive \
stoc_exp \
stoc_exp_stable \
stoc_sigma \
IF_curr_delta_fixed_prob \
IF_trunc_delta

ifneq ($(SPYNNAKER_DEBUG), DEBUG)
MODELS += external_device_lif_control \
Expand Down
21 changes: 21 additions & 0 deletions neural_modelling/makefiles/neuron/stoc_exp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) 2023 The University of Manchester
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


APP = $(notdir $(CURDIR))

NEURON_IMPL_H = $(NEURON_DIR)/neuron/implementations/neuron_impl_stoc_exp.h
SYNAPSE_DYNAMICS = $(NEURON_DIR)/neuron/plasticity/synapse_dynamics_static_impl.c

include ../neural_build.mk
21 changes: 21 additions & 0 deletions neural_modelling/makefiles/neuron/stoc_exp_stable/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) 2023 The University of Manchester
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


APP = $(notdir $(CURDIR))

NEURON_IMPL_H = $(NEURON_DIR)/neuron/implementations/neuron_impl_stoc_exp_stable.h
SYNAPSE_DYNAMICS = $(NEURON_DIR)/neuron/plasticity/synapse_dynamics_static_impl.c

include ../neural_build.mk
21 changes: 21 additions & 0 deletions neural_modelling/makefiles/neuron/stoc_sigma/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) 2023 The University of Manchester
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


APP = $(notdir $(CURDIR))

NEURON_IMPL_H = $(NEURON_DIR)/neuron/implementations/neuron_impl_stoc_sigma.h
SYNAPSE_DYNAMICS = $(NEURON_DIR)/neuron/plasticity/synapse_dynamics_static_impl.c

include ../neural_build.mk
69 changes: 69 additions & 0 deletions neural_modelling/src/common/maths-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ static inline REAL kdivk(REAL a, REAL b) {
return kbits((uint32_t) udiv64(((uint64_t) bitsk(a) << 15), (uint64_t) bitsk(b)));
}

//! \brief Divides an unsigned accum by another unsigned accum
//! \param[in] a The dividend
//! \param[in] b The divisor
//! \return a divided by b
static inline UREAL ukdivuk(UREAL a, UREAL b) {
return ukbits((uint32_t) udiv64(((uint64_t) bitsuk(a) << 16), (uint64_t) bitsuk(b)));
}

//! \brief Divides an integer by an accum
//! \param[in] a The dividend
//! \param[in] b The divisor
Expand All @@ -239,4 +247,65 @@ static inline REAL kdivui(REAL a, uint32_t b) {
return kbits((uint32_t) __LI(udiv64(__U64(bitsk(a)), __U64(b))));
}

//! 2^(1/(2^n)) for values of n between 1 and 15, in U1616 format, then converted
//! to uint32_t for computation.
//! These can be used to work out fractional powers of 2 by multiplication.
static const uint32_t fract_powers_2[] = {
0x16a09, 0x1306f, 0x1172b, 0x10b55, 0x1059b, 0x102c9, 0x10163, 0x100b1,
0x10058, 0x1002c, 0x10016, 0x1000b, 0x10005, 0x10002, 0x10001
};


//! 2^-(1/2^n)) for values of n between 1 and 15, in U1616 format, then converted
//! to uint32_t for computation.
//! These can be used to work out negative fractional power of 2 by multiplication.
static const uint32_t fract_powers_half[] = {
0xb504, 0xd744, 0xeac0, 0xf525, 0xfa83, 0xfd3e, 0xfe9e, 0xff4e, 0xffa7,
0xffd3, 0xffe9, 0xfff4, 0xfffa, 0xfffd, 0xfffe
};

//! \brief Calculates 2^p where p is a real number (rather than just an integer).
//! This is still quicker than general power operations.
//! \param[in] p The power to raise 2 to; if p >= 16 or <= -16 the result will
//! be saturated to MAX_U_REAL or 0 respectively.
static inline UREAL pow_of_2(REAL p) {

// The variable that will hold the return value
uint32_t accumulator;

// The fractional bits from the input
uint32_t fract_bits;

// The powers to use in the calculation
const uint32_t *powers;

if (p >= 0) {
if (p >= 16) {
return kbits(0xFFFFFFFF);
}
accumulator = bitsuk(UREAL_CONST(1)) << (bitsk(p) >> 15);
fract_bits = bitsk(p) & 0x7FFF;
powers = fract_powers_2;
} else {
if (p <= -16) {
return kbits(0);
}
REAL val = p * REAL_CONST(-1);
accumulator = bitsuk(UREAL_CONST(1)) >> (bitsk(val) >> 15);
fract_bits = bitsk(val) & 0x7FFF;
powers = fract_powers_half;
}

// Multiply in fractional powers for each non-zero fractional bits
for (uint32_t i = 0; i < 15; i++) {
uint32_t bit = (fract_bits >> (14 - i)) & 0x1;
if (bit) {
uint32_t f = bit * powers[i];
accumulator = __stdfix_smul_uk(accumulator, f);
}
}

return ukbits(accumulator);
}

#endif // _MATHS_UTIL_

0 comments on commit df7ce07

Please sign in to comment.