Debian Codegraph @

brian2/codegen/codeobject.py

raw
1 """
2 Module providing the base `CodeObject` and related functions.
3 """
4
5 import collections
6 import copy
7 import platform
8 from abc import ABC, abstractmethod
9
10 from brian2.core.base import weakproxy_with_fallback
11 from brian2.core.functions import DEFAULT_FUNCTIONS, Function
12 from brian2.core.names import Nameable
13 from brian2.equations.unitcheck import check_units_statements
14 from brian2.utils.logger import get_logger
15 from brian2.utils.stringtools import code_representation, indent
16
17 from .translation import analyse_identifiers
18
19 __all__ = ["CodeObject", "constant_or_scalar"]
20
21 logger = get_logger(__name__)
22
23
24 #: Dictionary with basic information about the current system (OS, etc.)
25 sys_info = {
26 "system": platform.system(),
27 "architecture": platform.architecture(),
28 "machine": platform.machine(),
29 }
30
31
32 def constant_or_scalar(varname, variable):
33 """
34 Convenience function to generate code to access the value of a variable.
35 Will return ``'varname'`` if the ``variable`` is a constant, and
36 ``array_name[0]`` if it is a scalar array.
37 """
38 from brian2.devices.device import get_device # avoid circular import
39
40 if variable.array:
41 return f"{get_device().get_array_name(variable)}[0]"
42 else:
43 return f"{varname}"
44
45
46 class CodeObject(Nameable, ABC):
47 """
48 Executable code object.
49
50 The ``code`` can either be a string or a
51 `brian2.codegen.templates.MultiTemplate`.
52
53 After initialisation, the code is compiled with the given namespace
54 using ``code.compile(namespace)``.
55
56 Calling ``code(key1=val1, key2=val2)`` executes the code with the given
57 variables inserted into the namespace.
58 """
59
60 #: The `CodeGenerator` class used by this `CodeObject`
61 generator_class = None
62 #: A short name for this type of `CodeObject`
63 class_name = None
64
65 def __init__(
66 self,
67 owner,
68 code,
69 variables,
70 variable_indices,
71 template_name,
72 template_source,
73 compiler_kwds,
74 name="codeobject*",
75 ):
76 Nameable.__init__(self, name=name)
77 self.owner = weakproxy_with_fallback(owner)
78 self.code = code
79 self.compiled_code = {}
80 self.variables = variables
81 self.variable_indices = variable_indices
82 self.template_name = template_name
83 self.template_source = template_source
84 self.compiler_kwds = compiler_kwds
85
86 def __getstate__(self):
87 state = self.__dict__.copy()
88 state["owner"] = self.owner.__repr__.__self__
89 # Replace Function objects for standard functions by their name
90 state["variables"] = self.variables.copy()
91 for k, v in state["variables"].items():
92 if isinstance(v, Function) and v is DEFAULT_FUNCTIONS[k]:
93 state["variables"][k] = k
94 return state
95
96 def __setstate__(self, state):
97 state["owner"] = weakproxy_with_fallback(state["owner"])
98 for k, v in state["variables"].items():
99 if isinstance(v, str):
100 state["variables"][k] = DEFAULT_FUNCTIONS[k]
101 self.__dict__ = state
102
103 @classmethod
104 def is_available(cls):
105 """
106 Whether this target for code generation is available. Should use a
107 minimal example to check whether code generation works in general.
108 """
109 return True
110
111 def update_namespace(self):
112 """
113 Update the namespace for this timestep. Should only deal with variables
114 where *the reference* changes every timestep, i.e. where the current
115 reference in `namespace` is not correct.
116 """
117 pass
118
119 @abstractmethod
120 def compile_block(self, block): ...
121
122 def compile(self):
123 for block in ["before_run", "run", "after_run"]:
124 self.compiled_code[block] = self.compile_block(block)
125
126 def __call__(self, **kwds):
127 self.update_namespace()
128 self.namespace.update(**kwds)
129
130 return self.run()
131
132 @abstractmethod
133 def run_block(self, block): ...
134
135 def before_run(self):
136 """
137 Runs the preparation code in the namespace. This code will only be
138 executed once per run.
139
140 Returns
141 -------
142 return_value : dict
143 A dictionary with the keys corresponding to the `output_variables`
144 defined during the call of `CodeGenerator.code_object`.
145 """
146 return self.run_block("before_run")
147
148 def run(self):
149 """
150 Runs the main code in the namespace.
151
152 Returns
153 -------
154 return_value : dict
155 A dictionary with the keys corresponding to the `output_variables`
156 defined during the call of `CodeGenerator.code_object`.
157 """
158 return self.run_block("run")
159
160 def after_run(self):
161 """
162 Runs the finalizing code in the namespace. This code will only be
163 executed once per run.
164
165 Returns
166 -------
167 return_value : dict
168 A dictionary with the keys corresponding to the `output_variables`
169 defined during the call of `CodeGenerator.code_object`.
170 """
171 return self.run_block("after_run")
172
173
174 def _error_msg(code, name):
175 """
176 Little helper function for error messages.
177 """
178 error_msg = f"Error generating code for code object '{name}' "
179 code_lines = [line for line in code.split("\n") if len(line.strip())]
180 # If the abstract code is only one line, display it in full
181 if len(code_lines) <= 1:
182 error_msg += f"from this abstract code: '{code_lines[0]}'\n"
183 else:
184 error_msg += (
185 f"from {len(code_lines)} lines of abstract code, first line is: "
186 "'code_lines[0]'\n"
187 )
188 return error_msg
189
190
191 def check_compiler_kwds(compiler_kwds, accepted_kwds, target):
192 """
193 Internal function to check the provided compiler keywords against the list
194 of understood keywords.
195
196 Parameters
197 ----------
198 compiler_kwds : dict
199 Dictionary of compiler keywords and respective list of values.
200 accepted_kwds : list of str
201 The compiler keywords understood by the code generation target
202 target : str
203 The name of the code generation target (used for the error message).
204
205 Raises
206 ------
207 ValueError
208 If a compiler keyword is not understood
209 """
210 for key in compiler_kwds:
211 if key not in accepted_kwds:
212 formatted_kwds = ", ".join(f"'{kw}'" for kw in accepted_kwds)
213 raise ValueError(
214 f"The keyword argument '{key}' is not understood by "
215 f"the code generation target '{target}'. The valid "
216 f"arguments are: {formatted_kwds}."
217 )
218
219
220 def _merge_compiler_kwds(list_of_kwds):
221 """
222 Merges a list of keyword dictionaries. Values in these dictionaries are
223 lists of values, the merged dictionaries will contain the concatenations
224 of lists specified for the same key.
225
226 Parameters
227 ----------
228 list_of_kwds : list of dict
229 A list of compiler keyword dictionaries that should be merged.
230
231 Returns
232 -------
233 merged_kwds : dict
234 The merged dictionary
235 """
236 merged_kwds = collections.defaultdict(list)
237 for kwds in list_of_kwds:
238 for key, values in kwds.items():
239 if not isinstance(values, list):
240 raise TypeError(
241 f"Compiler keyword argument '{key}' requires a list of values."
242 )
243 merged_kwds[key].extend(values)
244 return merged_kwds
245
246
247 def _gather_compiler_kwds(function, codeobj_class):
248 """
249 Gather all the compiler keywords for a function and its dependencies.
250
251 Parameters
252 ----------
253 function : `Function`
254 The function for which the compiler keywords should be gathered
255 codeobj_class : type
256 The class of `CodeObject` to use
257
258 Returns
259 -------
260 kwds : dict
261 A dictionary with the compiler arguments, a list of values for each
262 key.
263 """
264 implementation = function.implementations[codeobj_class]
265 all_kwds = [implementation.compiler_kwds]
266 if implementation.dependencies is not None:
267 for dependency in implementation.dependencies.values():
268 all_kwds.append(_gather_compiler_kwds(dependency, codeobj_class))
269 return _merge_compiler_kwds(all_kwds)
270
271
272 def create_runner_codeobj(
273 group,
274 code,
275 template_name,
276 run_namespace,
277 user_code=None,
278 variable_indices=None,
279 name=None,
280 check_units=True,
281 needed_variables=None,
282 additional_variables=None,
283 template_kwds=None,
284 override_conditional_write=None,
285 codeobj_class=None,
286 ):
287 """Create a `CodeObject` for the execution of code in the context of a
288 `Group`.
289
290 Parameters
291 ----------
292 group : `Group`
293 The group where the code is to be run
294 code : str or dict of str
295 The code to be executed.
296 template_name : str
297 The name of the template to use for the code.
298 run_namespace : dict-like
299 An additional namespace that is used for variable lookup (either
300 an explicitly defined namespace or one taken from the local
301 context).
302 user_code : str, optional
303 The code that had been specified by the user before other code was
304 added automatically. If not specified, will be assumed to be identical
305 to ``code``.
306 variable_indices : dict-like, optional
307 A mapping from `Variable` objects to index names (strings). If none is
308 given, uses the corresponding attribute of `group`.
309 name : str, optional
310 A name for this code object, will use ``group + '_codeobject*'`` if
311 none is given.
312 check_units : bool, optional
313 Whether to check units in the statement. Defaults to ``True``.
314 needed_variables: list of str, optional
315 A list of variables that are neither present in the abstract code, nor
316 in the ``USES_VARIABLES`` statement in the template. This is only
317 rarely necessary, an example being a `StateMonitor` where the
318 names of the variables are neither known to the template nor included
319 in the abstract code statements.
320 additional_variables : dict-like, optional
321 A mapping of names to `Variable` objects, used in addition to the
322 variables saved in `group`.
323 template_kwds : dict, optional
324 A dictionary of additional information that is passed to the template.
325 override_conditional_write: list of str, optional
326 A list of variable names which are used as conditions (e.g. for
327 refractoriness) which should be ignored.
328 codeobj_class : class, optional
329 The `CodeObject` class to run code with. If not specified, defaults to
330 the `group`'s ``codeobj_class`` attribute.
331 """
332
333 if name is None:
334 if group is not None:
335 name = f"{group.name}_{template_name}_codeobject*"
336 else:
337 name = f"{template_name}_codeobject*"
338
339 if user_code is None:
340 user_code = code
341
342 if isinstance(code, str):
343 code = {None: code}
344 user_code = {None: user_code}
345
346 msg = (
347 f"Creating code object (group={group.name}, template name={template_name}) for"
348 " abstract code:\n"
349 )
350 msg += indent(code_representation(code))
351 logger.diagnostic(msg)
352 from brian2.devices import get_device
353
354 device = get_device()
355
356 if override_conditional_write is None:
357 override_conditional_write = set()
358 else:
359 override_conditional_write = set(override_conditional_write)
360
361 if codeobj_class is None:
362 codeobj_class = device.code_object_class(group.codeobj_class)
363 else:
364 codeobj_class = device.code_object_class(codeobj_class)
365
366 template = getattr(codeobj_class.templater, template_name)
367 template_variables = getattr(template, "variables", None)
368
369 all_variables = dict(group.variables)
370 if additional_variables is not None:
371 all_variables.update(additional_variables)
372
373 # Determine the identifiers that were used
374 identifiers = set()
375 user_identifiers = set()
376 for v, u_v in zip(code.values(), user_code.values(), strict=True):
377 _, uk, u = analyse_identifiers(v, all_variables, recursive=True)
378 identifiers |= uk | u
379 _, uk, u = analyse_identifiers(u_v, all_variables, recursive=True)
380 user_identifiers |= uk | u
381
382 # Add variables that are not in the abstract code, nor specified in the
383 # template but nevertheless necessary
384 if needed_variables is None:
385 needed_variables = []
386 # Resolve all variables (variables used in the code and variables needed by
387 # the template)
388 variables = group.resolve_all(
389 sorted(identifiers | set(needed_variables) | set(template_variables)),
390 # template variables are not known to the user:
391 user_identifiers=user_identifiers,
392 additional_variables=additional_variables,
393 run_namespace=run_namespace,
394 )
395 # We raise this error only now, because there is some non-obvious code path
396 # where Jinja tries to get a Synapse's "name" attribute via syn['name'],
397 # which then triggers the use of the `group_get_indices` template which does
398 # not exist for standalone. Putting the check for template == None here
399 # means we will first raise an error about the unknown identifier which will
400 # then make Jinja try syn.name
401 if template is None:
402 codeobj_class_name = codeobj_class.class_name or codeobj_class.__name__
403 raise AttributeError(
404 f"'{codeobj_class_name}' does not provide a code "
405 f"generation template '{template_name}'"
406 )
407
408 conditional_write_variables = {}
409 # Add all the "conditional write" variables
410 for var in variables.values():
411 cond_write_var = getattr(var, "conditional_write", None)
412 if cond_write_var in override_conditional_write:
413 continue
414 if cond_write_var is not None:
415 if (
416 cond_write_var.name in variables
417 and variables[cond_write_var.name] is not cond_write_var
418 ):
419 logger.diagnostic(
420 f"Variable '{cond_write_var.name}' is needed for the "
421 "conditional write mechanism of variable "
422 f"'{var.name}'. Its name is already used for "
423 f"{variables[cond_write_var.name]!r}."
424 )
425 else:
426 conditional_write_variables[cond_write_var.name] = cond_write_var
427
428 variables.update(conditional_write_variables)
429
430 if check_units:
431 for c in code.values():
432 # This is the first time that the code is parsed, catch errors
433 try:
434 check_units_statements(c, variables)
435 except (SyntaxError, ValueError) as ex:
436 error_msg = _error_msg(c, name)
437 raise ValueError(error_msg) from ex
438
439 all_variable_indices = copy.copy(group.variables.indices)
440 if additional_variables is not None:
441 all_variable_indices.update(additional_variables.indices)
442 if variable_indices is not None:
443 all_variable_indices.update(variable_indices)
444
445 # Make "conditional write" variables use the same index as the variable
446 # that depends on them
447 for varname, var in variables.items():
448 cond_write_var = getattr(var, "conditional_write", None)
449 if cond_write_var is not None:
450 all_variable_indices[cond_write_var.name] = all_variable_indices[varname]
451
452 # Check that all functions are available
453 for varname, value in variables.items():
454 if isinstance(value, Function):
455 try:
456 value.implementations[codeobj_class]
457 except KeyError as ex:
458 # if we are dealing with numpy, add the default implementation
459 from brian2.codegen.runtime.numpy_rt import NumpyCodeObject
460
461 if codeobj_class is NumpyCodeObject:
462 value.implementations.add_numpy_implementation(value.pyfunc)
463 else:
464 raise NotImplementedError(
465 f"Cannot use function '{varname}': {ex}"
466 ) from ex
467
468 # Gather the additional compiler arguments declared by function
469 # implementations
470 all_keywords = [
471 _gather_compiler_kwds(var, codeobj_class)
472 for var in variables.values()
473 if isinstance(var, Function)
474 ]
475 compiler_kwds = _merge_compiler_kwds(all_keywords)
476
477 # Add the indices needed by the variables
478 for varname in list(variables):
479 var_index = all_variable_indices[varname]
480 if var_index not in ("_idx", "0"):
481 variables[var_index] = all_variables[var_index]
482
483 return device.code_object(
484 owner=group,
485 name=name,
486 abstract_code=code,
487 variables=variables,
488 template_name=template_name,
489 variable_indices=all_variable_indices,
490 template_kwds=template_kwds,
491 codeobj_class=codeobj_class,
492 override_conditional_write=override_conditional_write,
493 compiler_kwds=compiler_kwds,
494 )