Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Step 7 - Single PSM code string #612

Merged
merged 4 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions include/genn/genn/postsynapticModels.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
//----------------------------------------------------------------------------
// Macros
//----------------------------------------------------------------------------
#define SET_DECAY_CODE(DECAY_CODE) virtual std::string getDecayCode() const override{ return DECAY_CODE; }
#define SET_CURRENT_CONVERTER_CODE(CURRENT_CONVERTER_CODE) virtual std::string getApplyInputCode() const override{ return "Isyn += " CURRENT_CONVERTER_CODE ";"; }
#define SET_APPLY_INPUT_CODE(APPLY_INPUT_CODE) virtual std::string getApplyInputCode() const override{ return APPLY_INPUT_CODE; }
#define SET_SIM_CODE(SIM_CODE) virtual std::string getSimCode() const override{ return SIM_CODE; }
#define SET_NEURON_VAR_REFS(...) virtual VarRefVec getNeuronVarRefs() const override{ return __VA_ARGS__; }

//----------------------------------------------------------------------------
Expand All @@ -32,8 +30,7 @@ class GENN_EXPORT Base : public Models::Base
//! Gets names and types of model variable references
virtual VarRefVec getNeuronVarRefs() const{ return {}; }

virtual std::string getDecayCode() const{ return ""; }
virtual std::string getApplyInputCode() const{ return ""; }
virtual std::string getSimCode() const{ return ""; }

//----------------------------------------------------------------------------
// Public API
Expand Down Expand Up @@ -72,17 +69,15 @@ class GENN_EXPORT Init : public Snippet::Init<Base>
const std::unordered_map<std::string, InitVarSnippet::Init> &getVarInitialisers() const{ return m_VarInitialisers; }
const std::unordered_map<std::string, Models::VarReference> &getNeuronVarReferences() const{ return m_NeuronVarReferences; }

const std::vector<Transpiler::Token> &getDecayCodeTokens() const{ return m_DecayCodeTokens; }
const std::vector<Transpiler::Token> &getApplyInputCodeTokens() const{ return m_ApplyInputCodeTokens; }
const std::vector<Transpiler::Token> &getSimCodeTokens() const{ return m_SimCodeTokens; }

void finalise(double dt);

private:
//------------------------------------------------------------------------
// Members
//------------------------------------------------------------------------
std::vector<Transpiler::Token> m_DecayCodeTokens;
std::vector<Transpiler::Token> m_ApplyInputCodeTokens;
std::vector<Transpiler::Token> m_SimCodeTokens;

std::unordered_map<std::string, InitVarSnippet::Init> m_VarInitialisers;
std::unordered_map<std::string, Models::VarReference> m_NeuronVarReferences;
Expand All @@ -99,9 +94,9 @@ class ExpCurr : public Base
public:
DECLARE_SNIPPET(ExpCurr);

SET_DECAY_CODE("inSyn *= expDecay;");

SET_CURRENT_CONVERTER_CODE("init * inSyn");
SET_SIM_CODE(
"injectCurrent(init * inSyn);\n"
"inSyn *= expDecay;\n");

SET_PARAMS({"tau"});

Expand All @@ -124,9 +119,9 @@ class ExpCond : public Base
public:
DECLARE_SNIPPET(ExpCond);

SET_DECAY_CODE("inSyn*=expDecay;");

SET_CURRENT_CONVERTER_CODE("inSyn * (E - V)");
SET_SIM_CODE(
"injectCurrent(inSyn * (E - V));\n"
"inSyn *= expDecay;\n");

SET_PARAMS({"tau", "E"});

Expand All @@ -145,6 +140,8 @@ class DeltaCurr : public Base
public:
DECLARE_SNIPPET(DeltaCurr);

SET_CURRENT_CONVERTER_CODE("inSyn; inSyn = 0");
SET_SIM_CODE(
"injectCurrent(inSyn);\n"
"inSyn = 0.0;\n");
};
} // namespace GeNN::PostsynapticModels
22 changes: 11 additions & 11 deletions pygenn/genn_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,8 +1020,8 @@ def create_neuron_model(class_name, params=None, param_names=None,

def create_postsynaptic_model(class_name, params=None, param_names=None,
var_name_types=None, neuron_var_refs=None,
derived_params=None, decay_code=None,
apply_input_code=None,
derived_params=None, sim_code=None,
decay_code=None, apply_input_code=None,
extra_global_params=None):
"""This helper function creates a custom PostsynapticModel class.
See also:
Expand All @@ -1042,21 +1042,21 @@ def create_postsynaptic_model(class_name, params=None, param_names=None,
derived_params -- list of pairs, where the first member is string
with name of the derived parameter and the second
should be a functor returned by create_dpf_class
sim_code -- string with the decay code
decay_code -- string with the decay code
apply_input_code -- string with the apply input code
extra_global_params -- list of pairs of strings with names and
types of additional parameters
"""
body = {}

if decay_code is not None:
body["get_decay_code"] =\
lambda self: dedent(upgrade_code_string(decay_code, class_name))

if apply_input_code is not None:
body["get_apply_input_code"] =\
lambda self: dedent(upgrade_code_string(apply_input_code,
class_name))
if decay_code is not None or apply_input_code is not None:
raise RuntimeError("Creating postsynaptic models with seperate "
"'decay_code' and 'apply_code' code strings is no "
"longer supported. Please provide 'sim_code' using "
"the injectCurrent(X) function to provide input.")
if sim_code is not None:
body["get_sim_code"] =\
lambda self: dedent(upgrade_code_string(sim_code, class_name))

if var_name_types is not None:
body["get_vars"] = \
Expand Down
10 changes: 4 additions & 6 deletions pygenn/src/genn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ class PyPostsynapticModelBase : public PySnippet<PostsynapticModels::Base>
virtual std::vector<Models::Base::Var> getVars() const override{ PYBIND11_OVERRIDE_NAME(std::vector<Models::Base::Var>, Base, "get_vars", getVars); }
virtual VarRefVec getNeuronVarRefs() const override { PYBIND11_OVERRIDE_NAME(VarRefVec, Base, "get_neuron_var_refs", getNeuronVarRefs); }

virtual std::string getDecayCode() const override { PYBIND11_OVERRIDE_NAME(std::string, Base, "get_decay_code", getDecayCode); }
virtual std::string getApplyInputCode() const override { PYBIND11_OVERRIDE_NAME(std::string, Base, "get_apply_input_code", getApplyInputCode); }
virtual std::string getSimCode() const override { PYBIND11_OVERRIDE_NAME(std::string, Base, "get_sim_code", getSimCode); }
};

//----------------------------------------------------------------------------
Expand Down Expand Up @@ -846,7 +845,7 @@ PYBIND11_MODULE(genn, m)
.def("get_additional_input_vars", &NeuronModels::Base::getAdditionalInputVars)

.def("is_auto_refractory_required", &NeuronModels::Base::isAutoRefractoryRequired);

//------------------------------------------------------------------------
// genn.PostsynapticModelBase
//------------------------------------------------------------------------
Expand All @@ -856,9 +855,8 @@ PYBIND11_MODULE(genn, m)
.def("get_vars", &PostsynapticModels::Base::getVars)
.def("get_neuron_var_refs", &PostsynapticModels::Base::getNeuronVarRefs)

.def("get_decay_code", &PostsynapticModels::Base::getDecayCode)
.def("get_apply_input_code", &PostsynapticModels::Base::getApplyInputCode);

.def("get_sim_code", &PostsynapticModels::Base::getSimCode);

//------------------------------------------------------------------------
// genn.WeightUpdateModelBase
//------------------------------------------------------------------------
Expand Down
14 changes: 6 additions & 8 deletions src/genn/genn/code_generator/neuronUpdateGroupMerged.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ void NeuronUpdateGroupMerged::InSynPSM::generate(const BackendBase &backend, Env

// **TODO** naming convention
psmEnv.add(getScalarType(), "inSyn", "linSyn");

// Allow synapse group's PS output var to override what Isyn points to
psmEnv.add(getScalarType(), "Isyn", "$(_" + getArchetype().getPostTargetVar() + ")");

// Define inject current function
psmEnv.add(Type::ResolvedType::createFunction(Type::Void, {getScalarType()}),
"injectCurrent", "$(_" + getArchetype().getPostTargetVar() + ") += $(0)");

// Create an environment which caches variables in local variables if they are accessed
EnvironmentLocalVarCache<SynapsePSMVarAdapter, InSynPSM, NeuronUpdateGroupMerged> varEnv(
Expand All @@ -133,11 +134,8 @@ void NeuronUpdateGroupMerged::InSynPSM::generate(const BackendBase &backend, Env
});

// Pretty print code back to environment
Transpiler::ErrorHandler applyInputErrorHandler("Synapse group '" + getArchetype().getName() + "' postsynaptic model apply input code");
prettyPrintStatements(getArchetype().getPSInitialiser().getApplyInputCodeTokens(), getTypeContext(), varEnv, applyInputErrorHandler);

Transpiler::ErrorHandler decayErrorHandler("Synapse group '" + getArchetype().getName() + "' postsynaptic model decay code");
prettyPrintStatements(getArchetype().getPSInitialiser().getDecayCodeTokens(), getTypeContext(), varEnv, decayErrorHandler);
Transpiler::ErrorHandler errorHandler("Synapse group '" + getArchetype().getName() + "' postsynaptic model apply sim code");
prettyPrintStatements(getArchetype().getPSInitialiser().getSimCodeTokens(), getTypeContext(), varEnv, errorHandler);

// Write back linSyn
varEnv.printLine("$(_out_post)[" + ng.getVarIndex(batchSize, VarAccessDim::BATCH | VarAccessDim::ELEMENT, "$(id)") + "] = linSyn;");
Expand Down
3 changes: 1 addition & 2 deletions src/genn/genn/neuronGroup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,7 @@ bool NeuronGroup::isSimRNGRequired() const
return std::any_of(getInSyn().cbegin(), getInSyn().cend(),
[](const SynapseGroupInternal *sg)
{
return (Utils::isRNGRequired(sg->getPSInitialiser().getApplyInputCodeTokens()) ||
Utils::isRNGRequired(sg->getPSInitialiser().getDecayCodeTokens()));
return Utils::isRNGRequired(sg->getPSInitialiser().getSimCodeTokens());
});
}
//----------------------------------------------------------------------------
Expand Down
8 changes: 3 additions & 5 deletions src/genn/genn/postsynapticModels.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ boost::uuids::detail::sha1::digest_type Base::getHashDigest() const
Snippet::Base::updateHash(hash);
Utils::updateHash(getVars(), hash);
Utils::updateHash(getNeuronVarRefs(), hash);
Utils::updateHash(getDecayCode(), hash);
Utils::updateHash(getApplyInputCode(), hash);
Utils::updateHash(getSimCode(), hash);
return hash.get_digest();
}
//----------------------------------------------------------------------------
Expand Down Expand Up @@ -60,13 +59,12 @@ Init::Init(const Base *snippet, const std::unordered_map<std::string, Type::Nume
Models::checkVarReferenceTypes(getNeuronVarReferences(), getSnippet()->getNeuronVarRefs());

// Scan code tokens
m_DecayCodeTokens = Utils::scanCode(getSnippet()->getDecayCode(), "Postsynaptic model decay code");
m_ApplyInputCodeTokens = Utils::scanCode(getSnippet()->getApplyInputCode(), "Postsynaptic model apply input code");
m_SimCodeTokens = Utils::scanCode(getSnippet()->getSimCode(), "Postsynaptic model sim code");
}
//----------------------------------------------------------------------------
bool Init::isRNGRequired() const
{
return (Utils::isRNGRequired(m_DecayCodeTokens) || Utils::isRNGRequired(m_ApplyInputCodeTokens));
return Utils::isRNGRequired(m_SimCodeTokens);
}
//----------------------------------------------------------------------------
bool Init::isVarInitRequired() const
Expand Down
18 changes: 4 additions & 14 deletions src/genn/genn/synapseGroup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -575,13 +575,8 @@ bool SynapseGroup::canPSBeFused(const NeuronGroup *ng) const
// **NOTE** this is kind of silly as, if it's not referenced in either of
// these code strings, there wouldn't be a lot of point in a PSM EGP existing!
for(const auto &egp : getPSInitialiser().getSnippet()->getExtraGlobalParams()) {
// If this EGP is referenced in decay code, return false
if(Utils::isIdentifierReferenced(egp.name, getPSInitialiser().getDecayCodeTokens())) {
return false;
}

// If this EGP is referenced in apply input code, return false
if(Utils::isIdentifierReferenced(egp.name, getPSInitialiser().getApplyInputCodeTokens())) {
// If this EGP is referenced in sim code, return false
if(Utils::isIdentifierReferenced(egp.name, getPSInitialiser().getSimCodeTokens())) {
return false;
}
}
Expand All @@ -590,13 +585,8 @@ bool SynapseGroup::canPSBeFused(const NeuronGroup *ng) const
for(const auto &p : getPSInitialiser().getSnippet()->getParams()) {
// If parameter is dynamic
if(isPSParamDynamic(p.name)) {
// If this parameter is referenced in decay code, return false
if(Utils::isIdentifierReferenced(p.name, getPSInitialiser().getDecayCodeTokens())) {
return false;
}

// If this parameter is referenced in apply input code, return false
if(Utils::isIdentifierReferenced(p.name, getPSInitialiser().getApplyInputCodeTokens())) {
// If this parameter is referenced in sim code, return false
if(Utils::isIdentifierReferenced(p.name, getPSInitialiser().getSimCodeTokens())) {
return false;
}
}
Expand Down
7 changes: 2 additions & 5 deletions tests/features/test_dynamic_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,12 @@ def test_dynamic_param(make_model, backend, precision):

postsynaptic_model = create_postsynaptic_model(
"postsynaptic",
decay_code=
sim_code=
"""
injectCurrent(inSyn);
psmX = t + psmShift + psmInput;
$(inSyn) = 0;
""",
apply_input_code=
"""
$(Isyn) += $(inSyn);
""",
params=["psmInput"],
var_name_types=[("psmX", "scalar"), ("psmShift", "scalar")])

Expand Down
9 changes: 4 additions & 5 deletions tests/unit/modelSpec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ class AlphaCurr : public PostsynapticModels::Base
public:
DECLARE_SNIPPET(AlphaCurr);

SET_DECAY_CODE(
"x = (DT * expDecay * inSyn * init) + (expDecay * x);\n"
"inSyn*=expDecay;\n");

SET_CURRENT_CONVERTER_CODE("x");
SET_SIM_CODE(
"injectCurrent(x);\n"
"x = (dt * expDecay * inSyn * init) + (expDecay * x);\n"
"inSyn *= expDecay;\n");

SET_PARAMS({"tau"});

Expand Down
5 changes: 2 additions & 3 deletions tests/unit/modelSpecMerged.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ class AlphaCurr : public PostsynapticModels::Base
public:
DECLARE_SNIPPET(AlphaCurr);

SET_DECAY_CODE(
SET_SIM_CODE(
"injectCurrent(x);\n"
"x = (dt * expDecay * inSyn * init) + (expDecay * x);\n"
"inSyn *= expDecay;\n");

SET_CURRENT_CONVERTER_CODE("x");

SET_PARAMS({"tau"});

SET_VARS({{"x", "scalar"}});
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/models.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ class AlphaCurr : public PostsynapticModels::Base
public:
DECLARE_SNIPPET(AlphaCurr);

SET_DECAY_CODE(
SET_SIM_CODE(
"injectCurrent(x);\n"
"x = (dt * expDecay * inSyn * init) + (expDecay * x);\n"
"inSyn *= expDecay;\n");

SET_CURRENT_CONVERTER_CODE("x");

SET_PARAMS({"tau"});

SET_VARS({{"x", "scalar"}});
Expand Down
7 changes: 3 additions & 4 deletions tests/unit/neuronGroup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ class AlphaCurr : public PostsynapticModels::Base
public:
DECLARE_SNIPPET(AlphaCurr);

SET_DECAY_CODE(
SET_SIM_CODE(
"injectCurrent(x);\n"
"x = (dt * expDecay * inSyn * init) + (expDecay * x);\n"
"inSyn*=expDecay;\n");

SET_CURRENT_CONVERTER_CODE("x");
"inSyn *= expDecay;\n");

SET_PARAMS({"tau"});

Expand Down
6 changes: 3 additions & 3 deletions tests/unit/postsynapticModels.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ using namespace GeNN;
class ExpCurrCopy : public PostsynapticModels::Base
{
public:
SET_DECAY_CODE("inSyn *= expDecay;");

SET_CURRENT_CONVERTER_CODE("init * inSyn");
SET_SIM_CODE(
"injectCurrent(init * inSyn);\n"
"inSyn *= expDecay;\n");

SET_PARAMS({"tau"});

Expand Down