API Reference: hxtorch¶
hxtorch from Python¶
-
hxtorch.spiking.experiment.BaseExperiment : public ABC
Subclassed by hxtorch.spiking.experiment.Experiment
-
hxtorch.spiking.backend.module_manager.BaseModuleManager : public ABC
Abstract base class for module manager.
Subclassed by hxtorch.spiking.backend.module_manager.ModuleManager
Public Functions
-
__init__
(self)¶
-
__str__
(self)¶ Add proper object string.
-
add_node
(self, Module module, Source sources, Target target)¶ Adds a new module to the manager.
This method adds a node to the internal graph to reporesent the module. It assigned edges to this node holding the data in
sources
, resp.target
. :param module: Module to represented in to the graph. :param sources: The sources to the node representingmodule
. :param targets: The targets of the node representingmodule
.
-
add_wrapper
(self, Wrapper wrapper)¶ Adds a new wrapper to the manager.
This must be called after all modules wrapped by this wrapper are represented in the graph. internal graph to reporesent the module. It assigned edges to this node holding the data in
sources
, resp.target
. :param module: Module to represented in to the graph.
-
clear
(self)¶ Clear the internal graph and remove the module -> node and wrapper -> node mapping.
-
done
(self)¶ Create a list of elements of form (module, sources, targets), which can be looped in the correct order.
:return: Returns the a list of nodes to be executed.
Public Members
-
graph
¶
-
-
hxtorch.spiking.modules.batch_dropout.BatchDropout : public hxtorch.spiking.modules.types.Population
Batch dropout layer.
Caveat: In-place operations on TensorHandles are not supported. Must be placed after a neuron layer, i.e. Neuron.
Public Functions
-
__init__
(self, int size, float dropout, Experiment experiment, Union[Callable, torch.autograd.Function] func=F.batch_dropout)¶ Initialize BatchDropout layer.
This layer disables spiking neurons in the previous spiking Neuron layer with a probability of
dropout
. Note,size
has to be equal to the size in the corresponding spiking layer. The spiking mask is maintained for the whole batch.:param size: Size of the population this dropout layer is applied to. :param dropout: Probability that a neuron in the precessing layer gets disabled during training. :param experiment: Experiment to append layer to. :param func: Callable function implementing the module’s forward functionallity or a torch.autograd.Function implementing the module’s forward and backward operation. Defaults to
batch_dropout
.
-
mask
(self)¶ Getter for spike mask.
:returns: Returns the current spike mask.
-
mask
(self, torch.Tensor mask)¶ Setter for the spike mask.
:param mask: Spike mask. Must be of shape
(self.size,)
.
-
set_mask
(self)¶ Creates a new random dropout mask, applied to the spiking neurons in the previous module.
If
module.eval()
dropout will be disabled.:returns: Returns a random boolen spike mask of size
self.size
.
Public Static Attributes
-
Type
¶
-
-
hxtorch.perceptron.nn.Conv1d : public hxtorch.perceptron.nn.ConvNd , public torch.nn.Conv1d
Applies a 1D convolution over an input signal composed of several input planes.
Subclassed by hxtorch.perceptron.nn.ExpandedConv1d
Public Functions
-
__init__
(self, Integral in_channels, Integral out_channels, Union[Integral, Tuple[Integral]] kernel_size, Integral stride=1, Union[Integral, Tuple[Integral, Integral]] padding=0, Union[Integral, Tuple] dilation=1, Integral groups=1, bool bias=True, str padding_mode='zeros', Optional[Integral] num_sends=None, Integral wait_between_events=defaults.wait_between_events, bool mock=False, *Optional[Callable[[torch.Tensor], torch.Tensor]] input_transform=None, Optional[Callable[[torch.Tensor], torch.Tensor]] weight_transform=clamp_weight_)¶ :param in_channels: Number of channels in the input :param out_channels: Number of channels produced by the convolution :param kernel_size: Size of the convolving kernel :param stride: Stride of the convolution :param padding: Zero-padding added to both sides of the input :param padding_mode: ‘zeros’, ‘reflect’, ‘replicate’ or ‘circular’ :param dilation: Spacing between kernel elements :param groups: Number of blocked connections from input channels to output channels :param bias: If
True
, adds a learnable bias to the output :param num_sends: Number of sends of the input.Values greater than 1 result in higher output to the neurons and increases the s/n ratio. For
None
this is automatically adjusted during initialization. :param wait_between_events: Wait time between two successive vector inputs, in FPGA clock cycles. Shorter wait time can lead to saturation of the synaptic input. :param mock: Enable mock mode. :param input_transform: Function that receives the input and returns a tensor to be used as input to the chip. :param weight_transform: Function that receives the weight and returns a tensor to be used as weight matrix on the chip.
Private Members
-
_conv
¶
-
-
hxtorch.perceptron.nn.Conv2d : public hxtorch.perceptron.nn.ConvNd , public torch.nn.Conv2d
Applies a 2D convolution over an input image composed of several input planes.
Public Functions
-
__init__
(self, Integral in_channels, Integral out_channels, Union[Integral, Tuple[Integral, Integral]] kernel_size, Integral stride=1, Union[Integral, Tuple[Integral, Integral]] padding=0, Integral dilation=1, Integral groups=1, bool bias=True, str padding_mode='zeros', Optional[Integral] num_sends=None, Integral wait_between_events=defaults.wait_between_events, bool mock=False, *Optional[Callable[[torch.Tensor], torch.Tensor]] input_transform=None, Optional[Callable[[torch.Tensor], torch.Tensor]] weight_transform=clamp_weight_)¶ :param in_channels: Number of channels in the input :param out_channels: Number of channels produced by the convolution :param kernel_size: Size of the convolving kernel :param stride: Stride of the convolution :param padding: Zero-padding added to both sides of the input :param padding_mode: ‘zeros’, ‘reflect’, ‘replicate’ or ‘circular’ :param dilation: Spacing between kernel elements :param groups: Number of blocked connections from input channels to output channels :param bias: If
True
, adds a learnable bias to the output :param num_sends: Number of sends of the input.Values greater than 1 result in higher output to the neurons and increases the s/n ratio. For
None
this is automatically adjusted during initialization. :param mock: Enable mock mode. :param wait_between_events: Wait time between two successive vector inputs, in FPGA clock cycles. Shorter wait time can lead to saturation of the synaptic input. :param input_transform: Function that receives the input and returns a tensor to be used as input to the chip. :param weight_transform: Function that receives the weight and returns a tensor to be used as weight matrix on the chip.
Private Members
-
_conv
¶
-
-
hxtorch.perceptron.nn.ConvertingReLU : public hxtorch.perceptron.nn.ReLU
Applies a rectified linear unit to the input, shifts and clips to the input range of the chip.
Public Functions
-
__init__
(self, Integral shift=2, bool mock=False)¶ :param shift: Number of bits the result is shifted by :param mock: Enable mock mode
-
forward
(self, torch.Tensor input)¶
Public Members
-
shift
¶
-
-
hxtorch.perceptron.nn.ConvNd : public hxtorch.perceptron.nn.MACLayer , public torch.nn.modules.conv._ConvNd
Base class for n-dimensional convolution.
Subclassed by hxtorch.perceptron.nn.Conv1d, hxtorch.perceptron.nn.Conv2d
Public Functions
-
forward
(self, input)¶
Public Members
-
padding_mode
¶
Private Functions
-
_conv
(self, torch.Tensor input, torch.Tensor weight, torch.Tensor bias, Tuple[Integral, ...] stride, **kwargs)¶ Implementation of convolution function.
-
-
hxtorch.spiking.transforms.encode.CoordinatesToSpikes : public torch.nn.Module
Convert values between 0 and 1 to spikes in a given timeframe.
Public Functions
-
__init__
(self, int seq_length, float t_early, float t_late, float dt=1.e-6, Optional[float] t_bias=None, torch.device device=torch.device("cpu"))¶ Construct a coordinates-to-spikes converter.
This converter takes coordinate values in [0, 1] and maps them to spike times in interval [t_early, t_late]. A spike is indicated by a 1 on a dense time axis of length seq_length with temporal resolution dt. Further, it adds a bias spike at time t_bias, if t_bias is not None.
:param seq_length: Number of time steps in the resulting time sequence. The effective time length is given by seq_length * dt. :param t_early: The earliest time a spike will occur. :param t_late: The latest time a spike will occur. :param dt: The temporal resolution. :param t_bias: The time a bias spike occurs.
-
forward
(self, torch.Tensor coordinate_values)¶ Convert coordinate values of to dense spike tensor of zeros and ones.
:param coordinate_values: Tensor with values in [0, 1], shaped (batch_size, num_channels)
:returns: Returns a dense tensor of shape (batch_size, seq_length, num_channels)
-
-
hxtorch.spiking.functional.iaf.CUBAIAFParams : public NamedTuple
Parameters for IAF integration and backward path.
-
hxtorch.spiking.functional.lif.CUBALIFParams : public NamedTuple
Parameters for CUBA LIF integration and backward path.
-
hxtorch.spiking.functional.li.CUBALIParams : public NamedTuple
Parameters for CUBA LI integration and backward path.
Public Static Attributes
-
Tensor
¶
-
-
hxtorch.perceptron.nn.ExpandedConv1d : public hxtorch.perceptron.nn.Conv1d
Unrolls the weight matrix for execution on hardware.
This maximizes the use of the synapses array.
Caveat: Fixed-pattern noise cannot be individually compensated for during training, because the same weights are used at different locations!
Public Functions
-
__init__
(self, int in_channels, int out_channels, Union[int, Tuple[int]] kernel_size, int stride=1, Union[int, Tuple[int, int]] padding=0, Union[int, Tuple] dilation=1, int groups=1, bool bias=True, str padding_mode='zeros', Optional[int] num_sends=None, int wait_between_events=defaults.wait_between_events, bool mock=False, *Optional[Callable[[torch.Tensor], torch.Tensor]] input_transform=None, Optional[Callable[[torch.Tensor], torch.Tensor]] weight_transform=clamp_weight_, Optional[int] num_expansions=None)¶ :param in_channels: Number of channels in the input :param out_channels: Number of channels produced by the convolution :param kernel_size: Size of the convolving kernel :param stride: Stride of the convolution :param padding: Zero-padding added to both sides of the input :param padding_mode: ‘zeros’, ‘reflect’, ‘replicate’ or ‘circular’ :param dilation: Spacing between kernel elements :param groups: Number of blocked connections from input channels to output channels :param bias: If
True
, adds a learnable bias to the output :param num_sends: Number of sends of the input.Values greater than 1 result in higher output to the neurons and increases the s/n ratio. For
None
this is automatically adjusted during initialization. :param wait_between_events: Wait time between two successive vector inputs, in FPGA clock cycles. Shorter wait time can lead to saturation of the synaptic input. :param mock: Enable mock mode. :param input_transform: Function that receives the input and returns a tensor to be used as input to the chip. :param weight_transform: Function that receives the weight and returns a tensor to be used as weight matrix on the chip. :param num_expansions: Number of enrolled kernels in a single operation
-
extra_repr
(self)¶
Public Members
-
num_expansions
¶
Private Functions
-
_conv
(self, *args, **kwargs)¶
-
-
hxtorch.spiking.experiment.Experiment : public hxtorch.spiking.experiment.BaseExperiment
Experiment class for describing experiments on hardware.
Public Functions
-
__init__
(self, bool mock=False, float dt=1e-6, Optional[Union[Path, str]] calib_path=None, hw_routing_func=grenade.network.build_routing, grenade.signal_flow.ExecutionInstance execution_instance=grenade.signal_flow.ExecutionInstance())¶ Instanziate a new experiment, represting an experiment on hardware and/or in software.
:param mock: Indicating whether module is executed on hardware (False) or simulated in software (True).
-
clear
(self)¶ Reset the experiments’s state.
Corresponds to creating a new Experiment instance.
-
connect
(self, torch.nn.Module module, Tuple[handle.TensorHandle] input_handles, handle.TensorHandle output_handle)¶ Add an module to the experiment and connect it to other experiment modules via input and output handles.
:param module: The HXModule to add to the experiment. :param input_handles: The TensorHandle serving as input to the module (its obsv_state). :param output_handle: The TensorHandle outputted by the module, serving as input to subsequent HXModules.
-
get_hw_results
(self, Optional[int] runtime)¶ Executes the experiment in mock or on hardware using the information added to the experiment for a time given by
runtime
and returns a dict of hardware data represented as PyTorch data types.:param runtime: The runtime of the experiment on hardware in ms.
:returns: Returns the data map as dict, where the keys are the population descriptors and values are tuples of values returned by the correpsonding module’s
post_process
method.
-
load_calib
(self, Optional[Union[Path, str]] calib_path=None)¶ Load a calibration from path
calib_path
and apply to the experiment`s chip object.If no path is specified a nightly calib is applied. :param calib_path: The path to the calibration. It None, the nightly calib is loaded. :return: Returns the chip object for the given calibration.
-
register_population
(self, spiking_modules.HXModule module)¶ Register a module as population.
:param module: The module to register as population.
-
register_projection
(self, spiking_modules.HXModule module)¶ Register a module as projection.
:param module: The module to register as projection.
-
wrap_modules
(self, List[spiking_modules.HXModule] modules, Optional[Callable] func=None)¶ Wrap a number of given modules into a wrapper to which a single function
func
can be assigned.In the PyTorch graph the individual module functions are then bypassed and only the wrapper’s function is considered when building the PyTorch graph. This functionality is of interest if several modules have cyclic dependencies and need to be represented by one PyTorch function. :param modules: A list of module to be wrapped. These modules need to constitute a closed sub-graph with no modules in between that are not element of the wrapper. :func: The function to assign to the wrapper. TODO: Add info about this functions signature.
Public Members
-
cadc_recording
¶
-
execution_instance
¶
-
grenade_network
¶
-
grenade_network_graph
¶
-
has_madc_recording
¶
-
hw_routing_func
¶
-
id_counter
¶
-
injection_inside_realtime_begin
¶
-
injection_inside_realtime_end
¶
-
injection_post_realtime
¶
-
injection_pre_realtime
¶
-
injection_pre_static_config
¶
-
neuron_placement
¶
Private Functions
-
_configure_populations
(self)¶ Configure the population on hardware.
-
_generate_inputs
(self, grenade.network.NetworkGraph network_graph)¶ Generate external input events from the routed network graph representation.
-
_generate_network_graphs
(self)¶ Generate grenade network graph from the populations and projections in modules.
:return: Returns the grenade network graph.
-
_generate_playback_hooks
(self)¶ Handle injected config (not suppored yet)
-
_get_population_observables
(self, grenade.network.NetworkGraph network_graph, grenade.signal_flow.IODataMap result_map, runtime)¶ Takes the greade network graph and the result map returned by grenade after experiment execution and returns a data map where for each population descriptor of registered populations the population-specific hardware observables are represented as Optional[torch.Tensor]s.
Note: This function calles the modules
post_process
method. :param network_graph: The logical grenade network graph describing the logic of th experiment. :param result_map: The result map returned by grenade holding all recorded hardware observables. :param runtime: The runtime of the experiment executed on hardware in ms. :returns: Returns the data map as dict, where the keys are the population descriptors and values are tuples of values returned by the correpsonding module’spost_process
method.
-
_prepare_static_config
(self)¶ Prepares all the static chip config.
Accesses the chip object initialized by hxtorch.hardware_init and appends corresponding configurations to. Additionally this method defines the pre_static_config builder injected to grenade at run.
-
-
hxtorch.spiking.handle.HandleMeta : public type
Meta class to create Handle types with properties.
Subclassed by hxtorch.spiking.handle.TensorHandle
Public Functions
-
__new__
(mcs, clsname, bases, dict_)¶ Create a new instance of the given class.
-
-
hxtorch.spiking.modules.hx_module.HXModule : public torch.nn.Module
PyTorch module supplying basic functionality for building SNNs on HX.
Subclassed by hxtorch.spiking.modules.hx_module_wrapper.HXModuleWrapper, hxtorch.spiking.modules.input_neuron.InputNeuron, hxtorch.spiking.modules.types.Population, hxtorch.spiking.modules.types.Projection
Public Functions
-
__init__
(self, Experiment experiment, Union[Callable, torch.autograd.Function] func)¶ :param experiment: Experiment to append layer to.
:param func: Callable function implementing the module’s forward functionallity or a torch.autograd.Function implementing the module’s forward and backward operation. TODO: Inform about func args
-
changed_since_last_run
(self)¶ Getter for changed_since_last_run.
:returns: Boolean indicating wether module changed since last run.
-
exec_forward
(self, Union[Tuple[TensorHandle], TensorHandle] input, TensorHandle output, Dict[grenade.PopulationDescriptor, Tuple[torch.Tensor]] hw_map)¶ Inject hardware observables into TensorHandles or execute forward in mock-mode.
-
forward
(self, *Union[Tuple[TensorHandle], TensorHandle] input)¶ Forward method registering layer operation in given experiment.
Input and output references will hold corresponding data as soon as ‘hxtorch.run’ in executed.
:param input: Reference to TensorHandle holding data tensors as soon as required.
:returns: Returns a Reference to TensorHandle holding result data asociated with this layer after ‘hxtorch.run’ is executed.
-
func
(self)¶
-
func
(self, Callable function)¶ Assign a PyTorch-differentiable function to the module.
:param function: The function describing the modules f
-
post_process
(self, Optional[DataHandle] hw_spikes, Optional[DataHandle] hw_cadc, Optional[DataHandle] hw_madc)¶ This methods needs to be overridden for every derived module that demands hardware observables.
:returns: Hardware data represented as torch.Tensors. Note that torch.Tensors are required here to enable gradient flow.
-
reset_changed_since_last_run
(self)¶ Reset changed_since_last_run.
Sets the corresponding flag to false.
Public Static Attributes
-
Type
¶
Private Functions
-
_prepare_func
(self, function)¶ Strips all args and kwargs excluding
input
andhw_data
from self._func.If self._func does not have an
hw_data
keyword argument the prepared function will have it. This unifies the signature of all functions used inexec_forward
tofunc(input, hw_data=...)
. :param function: The function to be used for building the PyTorch graph. :returns: Returns the member ‘func(input, *args, **kwrags, hw_data=…)’ stripped down to ‘func(input, hw_data=…).
-
_wrap_func
(self, function)¶
-
-
hxtorch.spiking.modules.hx_module_wrapper.HXModuleWrapper : public hxtorch.spiking.modules.hx_module.HXModule
Class to wrap HXModules.
Public Functions
-
__init__
(self, Experiment experiment, List[HXModule] modules, Optional[Callable] func)¶ A module which wrappes a number of HXModules defined in
modules
to which a single PyTorch-differential functionfunc
is defined.For instance, this allows to wrap a Synapse and a Neuron to descripe recurrence. :param experiment: The experiment to register this wrapper in. :param modules: A list of modules to be represented by this wrapper. :param func: The function describing the unified functionallity of all modules assigned to this wrapper. As for HXModules, this needs to be a PyTorch-differentiable function and can be either an autograd.Function or a function defined by PyTorch operation. The signature of this function is expected as:
All positional arguments of each function in
modules
appended in the order given inmodules
.All keywords arguments of each function in
modules
. If a keyword is occurred multiple times it is post-fixed_i
, where i is an integered incremented with each occurrence.A keyword argument
hw_data
if hardware data is expected, which is a tuple holding the data for each module for which data is expected. The order is defined bymodules
. The function is expected to output a tensor or a tuple of tensors for each module inmodules
, that can be assigned to the output handle of the corresponding HXModule.
-
contains
(self, List[HXModule] modules)¶ Checks whether a list of modules
modules
is registered in the wrapper.:param modules: The modules for which to check if they are registered. :return: Returns a bool indicating whether
modules
are a subset.
-
exec_forward
(self, Tuple[TensorHandle] input, Tuple[TensorHandle] output, Dict[grenade.PopulationDescriptor, Tuple[torch.Tensor]] hw_map)¶ Execute the the forward function of the wrapper.
This method assigns each output handle in
output
their corresponding PyTorch tensors and adds the wrapper’sfunc
to the PyTorch graph. :param input: A tuple of the input handles where each handle corresponds to a certain module. The order is defined bymodules
. Note, a module can have multiple input handles. :param output: A tuole of output handles, each correspnding to one module. The order is defined bymodules
. :param hw_map: The hardware data map.
-
update
(self, List[HXModule] modules, Optional[Callable] func=None)¶ Update the modules and the function in the wrapper.
:param modules: The new modules to assign to the wrapper. :param func: The new function to represent the modules in the wrapper.
-
update_args
(self, List[HXModule] modules)¶ Gathers the args and kwargs of all modules in
modules
and renames keyword arguments that occur multiple times.:param modules: The modules represented by the wrapper.
-
-
hxtorch.spiking.functional.iaf.IAF : public torch.autograd.Function
IAF forward mock and backward.
-
hxtorch.spiking.modules.iaf_neuron.IAFNeuron : public hxtorch.spiking.modules.neuron.Neuron
Integrate-and-fire neuron Caveat: For execution on hardware, this module can only be used in conjuction with a preceding Synapse module.
Public Functions
-
__init__
(self, int size, Experiment experiment, Union[Callable, torch.autograd.Function] func=F.IAF, Optional[NamedTuple] params=None, bool enable_spike_recording=True, bool enable_cadc_recording=True, bool enable_madc_recording=False, Optional[int] record_neuron_id=None, Optional[List[halco.LogicalNeuronOnDLS]] placement_constraint=None, Union[Dict[halco.AtomicNeuronOnDLS, float], torch.Tensor, float] trace_offset=0., Union[Dict[halco.AtomicNeuronOnDLS, float], torch.Tensor, float] trace_scale=1., int cadc_time_shift=1, bool shift_cadc_to_first=False, str interpolation_mode="linear", Optional[Morphology] neuron_structure=None)¶ Initialize an IAFNeuron.
This module creates a population of a non- leaking spiking neurons of size
size
. This module has a internal spiking mask, which allows to disable the event ouput and spike recordings of specific neurons within the layer. This is particularly useful for dropout.:param size: Size of the population. :param experiment: Experiment to append layer to. :param func: Callable function implementing the module’s forward functionallity or a torch.autograd.Function implementing the module’s forward and backward operation. Defaults to
LIF
. :param params: Neuron Parameters in case of mock neuron integration of for backward path. If func does have a param argument the params object will get injected automatically. :param enable_spike_recording: Boolean flag to enable or disable spike recording. Note, this does not disable the event out put of neurons. The event output has to be disabled viamask
. :param enable_cadc_recording: Enables or disables parallel sampling of the populations membrane trace via the CADC. A maximum sample rate of 1.7us is possible. :param enable_madc_recording: Enables or disables the recording of the neuronsrecord_neuron_id
membrane trace via the MADC. Only a single neuron can be recorded. This membrane traces is samples with a significant higher resolution as with the CADC. :param record_neuron_id: The in-population neuron index of the neuron to be recorded with the MADC. This has only an effect whenenable_madc_recording
is enabled. :param placement_constraint: An optional list of logical neurons defining where to place the module`s neurons on hardware. :param trace_offset: The value by which the measured CADC traces are shifted before the scaling is applied. If this offset is given as float the same value is applied to all neuron traces in this population. One can also provide a torch tensor holding one offset for each individual neuron in this population. The corresponding tensor has to be of sizesize
. Further, the offsets can be supplied in a dictionary where the keys are the hardware neuron coordinates and the values are the offsets, i.e. Dict[AtomicNeuronOnDLS, float]. The dictionary has to provide one coordinate for each hardware neuron represented by this population, but might also hold neuron coordinates that do not correspond to this layer. The layer-specific offsets are then picked and applied implicitly. :param trace_scale: The value by which the measured CADC traces are scaled after the offset is applied. If this scale is given as float all neuron traces are scaled with the same value population. One can also provide a torch tensor holding one scale for each individual neuron in this population. The corresponding tensor has to be of sizesize
. Further, the scales can be supplied in a dictionary where the keys are the hardware neuron coordinates and the values are the scales, i.e. Dict[AtomicNeuronOnDLS, float]. The dictionary has to provide one coordinate for each hardware neuron represented by this population, but might also hold neuron coordinates that do not correspond to this layer. The layer- specific scales are then picked and applied implicitly. :param cadc_time_shift: An integer indicating by how many time steps the CADC values are shifted in time. A positive value shifts later CADC samples to earlier times and vice versa for a negative value. :param shift_cadc_to_first: A boolean indicating that the first measured CADC value is used as an offset. Note, this disables the paramtrace_offset
. :param interpolation_mode: The method used to interpolate the measured CADC traces onto the given time grid. :param neuron_structure: Structure of the neuron. If not supplied a single neuron circuit is used.
-
configure_hw_entity
(self, int neuron_id, lola.NeuronBlock neuron_block, halco.LogicalNeuronOnDLS coord)¶ Disables the neurons leak to behave like a integrate-and-fire neuron.
:param neuron_id: In-population neuron index. :param neuron_block: The neuron block hardware entity. :param coord: Coordinate of neuron on hardware. :returns: Configured neuron block.
-
-
hxtorch.spiking.modules.input_neuron.InputNeuron : public hxtorch.spiking.modules.hx_module.HXModule
Spike source generating spikes at the times [ms] given in the spike_times array.
Public Functions
-
__init__
(self, int size, Experiment experiment)¶ Instanziate a InputNeuron.
This module serves as an External Population for input injection and is created within
experiment
if not present in the considerd model. This module performes an identity mapping whenforward
is called.:param size: Number of input neurons. :param experiment: Experiment to which this module is assigned.
-
add_to_input_generator
(self, NeuronHandle input, grenade.InputGenerator builder)¶ Add the neurons events represented by this instance to grenades input generator.
:param input: Dense spike tensor. These spikes are implictely converted to spike times. TODO: Allow sparse tensors. :param builder: Grenade’s input generator to append the events to.
-
add_to_network_graph
(self, grenade.NetworkBuilder builder)¶ Adds instance to grenade’s network builder.
:param builder: Grenade network builder to add extrenal population to. :returns: External population descriptor.
-
post_process
(self, Optional[DataHandle] hw_spikes, Optional[DataHandle] hw_cadc, Optional[DataHandle] hw_madc)¶ This methods needs to be overridden for every derived module that demands hardware observables.
:returns: Hardware data represented as torch.Tensors. Note that torch.Tensors are required here to enable gradient flow.
-
register_hw_entity
(self)¶ Register instance in member
experiment
.
Public Static Attributes
-
Type
¶
-
-
class
hxtorch.perceptron.nn.
Layer
¶ Base class of all layers in :mod:
hxtorch.nn
.Subclassed by hxtorch.perceptron.nn.MACLayer, hxtorch.perceptron.nn.ReLU
Public Members
-
mock
¶
-
-
hxtorch.spiking.functional.li.LI : public torch.autograd.Function
LI forward mock and backward.
-
hxtorch.spiking.functional.lif.LIF : public torch.autograd.Function
LIF forward mock and backward.
-
hxtorch.perceptron.nn.Linear : public hxtorch.perceptron.nn.MACLayer , public torch.nn.Linear
Applies a linear transformation to the incoming data on Hicann-X.
Public Functions
-
__init__
(self, Integral in_features, Integral out_features, bool bias=True, Optional[Integral] num_sends=None, Integral wait_between_events=defaults.wait_between_events, bool mock=False, *Integral avg=1, Optional[Callable[[torch.Tensor], torch.Tensor]] input_transform=None, Optional[Callable[[torch.Tensor], torch.Tensor]] weight_transform=clamp_weight_)¶ :param in_features: Size of each input sample :param out_features: Size of each output sample :param bias: If set to
True
, the layer will learn an additive bias.:param num_sends: Number of sends of the input. Values greater than 1 result in higher output to the neurons and increases the s/n ratio. For
None
this is automatically adjusted during initialization. :param wait_between_events: Wait time between two successive vector inputs, in FPGA clock cycles. Shorter wait time can lead to saturation of the synaptic input. :param mock: Enable mock mode. :param avg: Number of neurons to average over. This option is targeted at reducing statistical noise. Beware: We average over different fixed-pattern instances, but they are all configured at the same weight, so they are not trained individually. This could potentially have negative implications. :param input_transform: Function that receives the input and returns a tensor to be used as input to the chip. :param weight_transform: Function that receives the weight and returns a tensor to be used as weight matrix on the chip.
-
forward
(self, input)¶
Public Members
-
avg
¶
Private Members
-
_matmul
¶
-
-
hxtorch.spiking.functional.linear.Linear : public torch.autograd.Function
Linear autograd example.
-
hxtorch.perceptron.nn.MACLayer : public hxtorch.perceptron.nn.Layer
Layer that performs a multiply accumulate operation.
Subclassed by hxtorch.perceptron.nn.ConvNd, hxtorch.perceptron.nn.Linear
Public Functions
-
__init__
(self, Optional[Integral] num_sends=None, Integral wait_between_events=defaults.wait_between_events, bool mock=False, *Optional[Callable[[torch.Tensor], torch.Tensor]] input_transform=None, Optional[Callable[[torch.Tensor], torch.Tensor]] weight_transform=clamp_weight_)¶ :param num_sends: Number of sends of the input.
Values greater than 1 result in higher output to the neurons and increases the s/n ratio. For
None
this is automatically adjusted during initialization. :param wait_between_events: Wait time between two successive vector inputs, in FPGA clock cycles. Shorter wait time can lead to saturation of the synaptic input. :param mock: Enable mock mode. :param input_transform: Function that receives the input and returns a tensor to be used as input to the chip. :param weight_transform: Function that receives the weight and returns a tensor to be used as weight matrix on the chip.
-
reset_parameters
(self, Real weight_mean=0., Integral relu_shift=1)¶ Reset parameters to reasonable initialization values.
Method based on Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification - He, K. et al. (2015)
:param weight_mean: Mean value of the weight distribution :param relu_shift: Bit shift assumed in subsequent ConvertingReLU
-
-
hxtorch.spiking.transforms.decode.MaxOverTime : public torch.nn.Module
Simple max-over-time decoding.
Public Functions
-
forward
(self, torch.Tensor input)¶ Translate an
input
tensor of shape (batch_size, time_length, population_size) into a tensor of shape (batch_size, population_size), where the time dimension is discarded by picking the maximum value along the time.Hence this module performs a ‘max-over-time’ operation.
:param input: The input tensor to transform. expected shape: (batch_size, time_length, population_size) :return: Returns the tensor holding the max-over-time values.
-
-
hxtorch.spiking.transforms.decode.MeanOverTime : public torch.nn.Module
Simple mean-over-time decoding.
Public Functions
-
forward
(self, torch.Tensor input)¶ Translate an
input
tensor of shape (batch_size, time_length, population_size) into a tensor of shape (batch_size, population_size), where the time dimension is discarded by computing the average value along the time.Hence this module performs a ‘mean-over-time’ operation.
:param input: The input tensor to transform. expected shape: (batch_size, time_length, population_size) :return: Returns the tensor holding the mean-over-time values.
-
-
class
hxtorch.spiking.utils.model_mapper.
MixinHXModelMapper
¶ Mapper mixin to map snn models to hxtorch.spiking models.
Public Functions
-
map_from
(self, Any state_dict, Dict mapping, bool map_all=True)¶ Map an SNN model with state dict
state_dict
to this model.:param state_dict: The state dict used to map from. :param mapping: The mapping from the keys of this model’s state dict to the keys in
state_dict
. :param map_all: A boolean value indicating if a state dict key is looked up instate_dict
if not present inmapping
. This allows to only define a mapping for some keys and load the others implicitly if the key names match. Defaults to true.
-
-
mnist.MNIST : public torch.utils.data.TensorDataset
The MNIST dataset.
Public Functions
-
__init__
(self, root, train)¶ :param root: Root directory of the dataset that contains
MNIST/processed/training.pt
andMNIST/processed/test.pt
.:param train: If True, creates dataset from
training.pt
, otherwise fromtest.pt
.
-
-
mnist.Model : public torch.nn.Module
Simple CNN model to classify written digits from the MNIST database.
Model topology:
Conv2d with 10x10 kernel, stride 5
Linear layer with 128 hidden neurons
-
yinyang_model.Model : public torch.nn.Module
Complete model with encoder, network (snn) and decoder.
Public Functions
-
__init__
(self, torch.nn.Module encoder, torch.nn.Module network, torch.nn.Module decoder, float readout_scale=1.)¶ Initialize the model by assigning encoder, network and decoder.
:param encoder: Module to encode input data :param network: Network module containing layers and parameters / weights :param decoder: Module to decode network output
-
forward
(self, torch.Tensor inputs)¶ Perform forward pass through whole model, i.e.
data -> encoder -> network -> decoder -> output
:param inputs: tensor input data
:returns: Returns tensor output
-
regularize
(self, float reg_readout, float reg_bursts, float reg_w_hidden, float reg_w_output)¶ Get regularization terms for bursts and weights like factor * (thing to be regularized) ** 2.
:param reg_bursts: prefactor of burst / hidden spike regulaization :param reg_weights_hidden: prefactor of hidden weight regularization :param reg_weights_output: prefactor of output weight regularization
:returns: Returns sum of regularization terms
-
-
hxtorch.spiking.backend.module_manager.ModuleManager : public hxtorch.spiking.backend.module_manager.BaseModuleManager
Object representing all nodes in a graph-like data structure.
Public Functions
-
__init__
(self)¶ Initialize a
Modules
object.This object holds a list of
nodes
.
-
__str__
(self)¶ Append string.
-
add_node
(self, Module module, Source sources, Target target)¶ Adds a new module to the manager.
This method adds a node to the internal graph to reporesent the module. It assigned edges to this node holding the data in
sources
, resp.target
. :param module: Module to represented in to the graph. :param sources: The sources to the node representingmodule
. :param targets: The targets of the node representingmodule
.
-
add_wrapper
(self, Wrapper wrapper)¶ Adds a new wrapper to the manager.
This must be called after all modules wrapped by this wrapper are represented in the graph. internal graph to reporesent the module. It assigned edges to this node holding the data in
sources
, resp.target
. :param module: Module to represented in to the graph.
-
changed_since_last_run
(self)¶ Check if any module is marked dirty.
:return: Returns true if at least one module has been marked dirty.
-
clear
(self)¶ Override clear to also clear open sources and open targets.
This method resets the Manager without removing implictly created input modules such that they can be reused.
-
done
(self)¶ Create a list of elements of form (module, sources, targets), which can be looped in the correct order.
:param instance: The instance to work on. :return: Returns the ordered modules in the form (module, sources, targets).
-
find_edges
(self, Any handle)¶ Find all edges with data associated with
handle
.:param handle: The edge data to match agains. :return: Returns a list of all edges in the graph which hold the same data
handle
.
-
get_id_by_module
(self, Module module)¶ Finds the ID of the node which corresponds to module
module
.If no ID is found,
None
is returned. :param module: The module to find the node ID for. :returns: Returns the node ID orNone
if no ID is found.
-
get_id_by_wrapper
(self, Wrapper wrapper)¶ Finds the ID of the wrapper which corresponds to wrapper
wrapper
.If no ID is found,
None
is returned. :param wrapper: The wrapper module to find the node ID for. :returns: Returns the node ID orNone
if no ID is found.
-
get_module_by_id
(self, int node_id)¶ Finds the module of the node with ID
node_id
and returns it.If multiple modules assigned to this ID are found, only the first one is returned. However, this should never be the case and if so, it is a bug. :param node_id: The ID of the node to find the module for. :returns: Returns the corresponding module.
-
get_node_id
(self)¶ Get the ID of the next node to add.
:returns: Returns the next usable ID of a node to add.
-
get_wrapper_by_id
(self, int wrapper_id)¶ Finds the wrapper of the node with ID
wrapper_id
and returns it.If multiple wrapper assigned to this ID are found, only the first one is returned. However, this should never be the case and if so, it is a bug. :param wrapper_id: The ID of the node to find the module for. :returns: Returns the corresponding wrapper.
-
get_wrapper_id
(self)¶ Get the ID of the next wrapper to add.
:returns: Returns the next usable ID of a wrapper to add.
-
has_module
(self, Module module)¶ Checks whether the moudle
mdoule
is already registered within the graph.:param module: The module to check its existance for. :return: Returns a bool indicating whether the module exists or not.
-
input_data
(self)¶ Finds all edge data associated to edges with a source node in _open_sources, those nodes are roots.
:return: Returns a list of all input data.
-
pre_process
(self, instance)¶ Handle all preprocessing needed prior to hardware execution.
This includes input module injection as well as setting the dropout masks.
-
reset_changed_since_last_run
(self)¶ Restes all registered modules changed_since_last_run flags.
-
source_populations
(self, Module module)¶ Find the source populations of module
module
, i.e.modules which are of type self._population_types. :param module: The module to find the source population. :return: Returns a list of source populations of module.
-
target_populations
(self, Module module)¶ Find the target populations of module
module
, i.e.modules which are of type self._population_types. :param module: The module to find the target populations for. :return: Returns a list of target populations of module.
Public Static Attributes
-
input_type
= spiking_module.InputNeuron¶
-
population_types
= (spiking_module.InputNeuron, spiking_module.Neuron,spiking_module.ReadoutNeuron)¶
Private Functions
-
_get_populations
(self, Module module, bool target=False)¶ Find the target, resp.
source populations of module
module
, i.e. modules which are of type self._population_types. :param module: The module to find the source population (target=False) or the target populations for(target=True) :param target: A bool indicating whether the search is performed for source or target populations. :return: Returns a list of source, resp. target populations of module.
-
_handle_dropout_mask
(self)¶ Method to back-assign spiking mask from Dropout layers to the previous Neuron layer in order to configure the neurons appropriately on hardware.
Note: BatchDropout layers have to be preceded by a Neuron layer.
-
_handle_inputs
(self, instance)¶ On hardware, synapse have external populations as sources.
Hence we have to augment Synapses with InputNeurons.
-
_handle_wrappers
(self)¶ Handle registered wrappers.
This method needs to be called after all modules are registered. It replaces all nodes associated with the nodes in a given wrapper in the graph. All internal target handles are gathered in the wrapper’s node attributes ‘targets’. All external input sources to the wrapper are gathered in the node attributes ‘sources’.
-
_order
(self)¶ This method checks whether the internal graph representation has no cyclic dependencies.
Cyclic dependencies need to be wrapped manually at the moment. TODO: Introduce implicit module wrapping here. Further, a topological sort is performed in order to return the nodes in an order in which all modules can be executed successively. :return: Returns a list of tuples of shape (module, sources, targets), where sources is a tuple of all input sources and targets is a tuple of all output targets of module
module
.
Private Members
-
_graph_hash
¶
-
-
hxtorch.spiking.morphology.Morphology : public ABC
Represents the internal structure of a neuron.
This neuron might be made up of several compartments and the compartments themselves can consist of several neuron circuits.
:note: Currently spike and voltage recording is only supported in the first neuron circuit of the first compartment.
Subclassed by hxtorch.spiking.morphology.SingleCompartmentNeuron
Public Functions
-
compartments
(self)¶ Unplaced coordinate of the logical neuron.
-
implement_morphology
(self, halco.LogicalNeuronOnDLS coord, lola.NeuronBlock neuron_block)¶ Configure the atomic neurons in the given neuron block to represent this morphology.
:param coord: Coordinate of the logical neuron which should be configured. :param neuron_block: The configuration of neurons at
coord
will be changed such that a neuron with the given morphology is implemented.
-
logical_neuron
(self)¶ Base configuration of the logical neuron.
Default constructed logical neuron, the connections between neuron circuits are configured such that the specified morphology is implemented.
Public Static Functions
-
disable_leak
(halco.LogicalNeuronOnDLS coord, lola.NeuronBlock neuron_block)¶ Disable the leak for the given neuron.
:param coord: Coordinate of the logical neuron for which the leak is disabled. :param neuron_block: Neuron block in which the configuration of the atomic neurons is changed.
-
disable_spiking
(halco.LogicalNeuronOnDLS coord, lola.NeuronBlock neuron_block)¶ Disable spiking for the given neuron.
Disable the threshold comparator and the digital spike output.
:param coord: Coordinate of the logical neuron for which spiking is disabled. :param neuron_block: Neuron block in which the configuration of the atomic neurons is changed.
-
enable_madc_recording
(halco.LogicalNeuronOnDLS coord, lola.NeuronBlock neuron_block, hal.NeuronConfig.ReadoutSource readout_source)¶ Configure neuron such that traces can be recorded with the MADC.
:param coord: Coordinate of the logical neuron for which the recording is enabled. :param neuron_block: Neuron block in which the configuration of the atomic neurons is changed. :param readout_source: Voltage which should be recorded.
-
set_spike_recording
(bool enable, halco.LogicalNeuronOnDLS coord, lola.NeuronBlock neuron_block)¶ Set whether spikes are forwarded digitally.
:param enable: Enable/disable the digital routing of spikes. :param coord: Coordinate of the logical neuron for which the recording is enabled. :param neuron_block: Neuron block in which the configuration of the atomic neurons is changed.
-
-
hxtorch.spiking.modules.neuron.Neuron : public hxtorch.spiking.modules.types.Population
Neuron layer.
Caveat: For execution on hardware, this module can only be used in conjuction with a preceding Synapse module.
Subclassed by hxtorch.spiking.modules.iaf_neuron.IAFNeuron, hxtorch.spiking.modules.readout_neuron.ReadoutNeuron
Public Functions
-
__init__
(self, int size, Experiment experiment, Union[Callable, torch.autograd.Function] func=F.LIF, Optional[NamedTuple] params=None, bool enable_spike_recording=True, bool enable_cadc_recording=True, bool enable_madc_recording=False, Optional[int] record_neuron_id=None, Optional[List[halco.LogicalNeuronOnDLS]] placement_constraint=None, Union[Dict[halco.LogicalNeuronOnDLS, float], torch.Tensor, float] trace_offset=0., Union[Dict[halco.LogicalNeuronOnDLS, float], torch.Tensor, float] trace_scale=1., int cadc_time_shift=1, bool shift_cadc_to_first=False, str interpolation_mode="linear", Optional[Morphology] neuron_structure=None)¶ Initialize a Neuron.
This module creates a population of spiking neurons of size
size
. This module has a internal spiking mask, which allows to disable the event ouput and spike recordings of specific neurons within the layer. This is particularly useful for dropout.:param size: Size of the population. :param experiment: Experiment to append layer to. :param func: Callable function implementing the module’s forward functionallity or a torch.autograd.Function implementing the module’s forward and backward operation. Defaults to
LIF
. :param params: Neuron Parameters in case of mock neuron integration of for backward path. If func does have a param argument the params object will get injected automatically. :param enable_spike_recording: Boolean flag to enable or disable spike recording. Note, this does not disable the event out put of neurons. The event output has to be disabled viamask
. :param enable_cadc_recording: Enables or disables parallel sampling of the populations membrane trace via the CADC. A maximum sample rate of 1.7us is possible. :param enable_madc_recording: Enables or disables the recording of the neuronsrecord_neuron_id
membrane trace via the MADC. Only a single neuron can be recorded. This membrane traces is samples with a significant higher resolution as with the CADC. :param record_neuron_id: The in-population neuron index of the neuron to be recorded with the MADC. This has only an effect whenenable_madc_recording
is enabled. :param placement_constraint: An optional list of logical neurons defining where to place the module`s neurons on hardware. :param trace_offset: The value by which the measured CADC traces are shifted before the scaling is applied. If this offset is given as float the same value is applied to all neuron traces in this population. One can also provide a torch tensor holding one offset for each individual neuron in this population. The corresponding tensor has to be of sizesize
. Further, the offsets can be supplied in a dictionary where the keys are the logical neuron coordinates and the values are the offsets, i.e. Dict[LogicalNeuronOnDLS, float]. The dictionary has to provide one coordinate for each hardware neuron represented by this population, but might also hold neuron coordinates that do not correspond to this layer. The layer-specific offsets are then picked and applied implicitly. :param trace_scale: The value by which the measured CADC traces are scaled after the offset is applied. If this scale is given as float all neuron traces are scaled with the same value population. One can also provide a torch tensor holding one scale for each individual neuron in this population. The corresponding tensor has to be of sizesize
. Further, the scales can be supplied in a dictionary where the keys are the logical neuron coordinates and the values are the scales, i.e. Dict[LogicalNeuronOnDLS, float]. The dictionary has to provide one coordinate for each hardware neuron represented by this population, but might also hold neuron coordinates that do not correspond to this layer. The layer- specific scales are then picked and applied implicitly. :param cadc_time_shift: An integer indicating by how many time steps the CADC values are shifted in time. A positive value shifts later CADC samples to earlier times and vice versa for a negative value. :param shift_cadc_to_first: A boolean indicating that the first measured CADC value is used as an offset. Note, this disables the paramtrace_offset
. :param interpolation_mode: The method used to interpolate the measured CADC traces onto the given time grid. :param neuron_structure: Structure of the neuron. If not supplied a single neuron circuit is used.
-
add_to_network_graph
(self, grenade.NetworkBuilder builder)¶ Add the layer’s neurons to grenades network builder.
If
enable_spike_recording
is enabled the neuron’s spikes are recorded according to the layer’s spiking mask. If no spiking mask is given all neuron spikes will be recorded. Note, the event output of the neurons are configured inconfigure_hw_entity
. Ifenable_cadc_recording
is enabled the populations neuron’s are registered for CADC membrane recording. Ifenable_madc_recording
is enabled the neuron with in-population indexrecord_neuron_id
will be recording via the MADC. Note, since the MADC can only record a single neuron on hardware, other Neuron layers registering also MADC recording might overwrite the setting here.:param builder: Grenade’s network builder to add the layer’s population to. :returns: Returns the builder with the population added.
-
configure_hw_entity
(self, int neuron_id, lola.NeuronBlock neuron_block, halco.LogicalNeuronOnDLS coord)¶ Configures a neuron in the given layer with its specific properties.
The neurons digital event outputs are enabled according to the given spiking mask.
TODO: Additional parameterization should happen here, i.e. with population-specific parameters.
:param neuron_id: In-population neuron index. :param neuron_block: The neuron block hardware entity. :param coord: Coordinate of neuron on hardware. :returns: Configured neuron block.
-
mask
(self)¶ Getter for spike mask.
:returns: Returns the current spike mask.
-
mask
(self, torch.Tensor mask)¶ Setter for the spike mask.
:param mask: Spike mask. Must be of shape
(self.size,)
.
-
post_process
(self, Optional[SpikeHandle] hw_spikes, Optional[CADCHandle] hw_cadc, Optional[MADCHandle] hw_madc)¶ User defined post process method called as soon as population-specific hardware observables are returned.
This function has to convert the data types returned by grenade into PyTorch tensors. This function can be overridden by the user if non-default grenade-PyTorch data type conversion is required. Note: This function should return Tuple[Optional[torch.Tensor], …], like (cadc or madc,). This should match the ReadoutTensorHandle signature.
:param hw_spikes: A SpikeHandle holding the population’s spikes recorded by grenade as a sparse tensor. This data can be ignored for this readout neuron. :param hw_cadc: The CADCHandle holding the CADC membrane readout events in a sparse tensor. :param hw_madc: The MADCHandle holding the MADC membrane readout events in a sparse tensor.
:returns: Returns a tuple of optional torch.Tensors holding the hardware data (madc or cadc,)
-
register_hw_entity
(self)¶ Infere neuron ids on hardware and register them.
Public Members
-
cadc_time_shift
¶
-
descriptor
¶
-
interpolation_mode
¶
-
offset
¶
-
params
¶
-
scale
¶
-
shift_cadc_to_first
¶
-
size
¶
-
unit_ids
¶
Public Static Functions
-
add_to_input_generator
(HXModule module, grenade.InputGenerator builder)¶ Add the input to an input module to grenades input generator.
:param module: The module to add the input for. :param builder: Grenade’s logical network builder.
-
create_default_hw_entity
()¶ At the moment, the default neuron is loaded from grenade’s ChipConfig object, which holds the atomic neurons configured as a calibration is loaded in
hxtorch.hardware_init()
.TODO: - Needed?
Maybe this can return a default neuron, when pop-specific calibration is needed.
-
-
hxtorch.spiking.handle.NeuronHandle : public hxtorch.spiking.handle.TensorHandle
Specialization for HX neuron observables.
Public Functions
-
__init__
(self, Optional[torch.Tensor] spikes=None, Optional[torch.Tensor] v_cadc=None, Optional[torch.Tensor] current=None, Optional[torch.Tensor] v_madc=None)¶ Instantiate a neuron handle able to hold spike and membrane tensors.
:param spikes: Optional spike tensor. :param v_cadc: Optional membrane tensor, holding CADC recordings. :param current: Optional current tensor, holding synpatic current. :param v_madc: Optional membrane tensor, holding MADC recordings.
-
-
class
hxtorch.spiking.experiment.
NeuronPlacement
¶ Tracks assignment of pyNN IDs of HXNeuron based populations to the corresponding hardware entities, i.e.
LogicalNeuronOnDLS.
Public Functions
-
__init__
(self)¶
-
id2logicalneuron
(self, Union[List[int], int] neuron_id)¶ Get hardware coordinate from pyNN int :param neuron_id: pyNN neuron int.
-
register_id
(self, Union[List[int], int] neuron_id, halco.LogicalNeuronCompartments shape, Optional[Union[List[halco.LogicalNeuronOnDLS], halco.LogicalNeuronOnDLS]] placement_constraint=None)¶ Register a new ID to placement :param neuron_id: pyNN neuron ID to be registered :param shape: The shape of the neurons on hardware.
:param placement_constraint: A logical neuron or a list of logical neurons, each coresponding to one ID in
neuron_id
.
Private Functions
-
_register
(self, Union[List[int], int] neuron_id, halco.LogicalNeuronCompartments shape)¶ Register neurons with global IDs
neuron_id
with creating logical neurons implicitly.:param neuron_id: An int or list of ints corresponding to the global IDs of the neurons. :param shape: The shape of the neurons on hardware.
-
_register_with_logical_neurons
(self, Union[List[int], int] neuron_id, Union[List[halco.LogicalNeuronOnDLS], halco.LogicalNeuronOnDLS] placement_constraint)¶ Register neurons with global IDs
neuron_id
with given logical neuronslogical_neuron
.:param neuron_id: An int or list of ints corresponding to the global IDs of the neurons. :param placement_constraint: A logical neuron or a list of logical neurons, each coresponding to one ID in
neuron_id
.
-
-
hxtorch.spiking.transforms.encode.PixelsToSpikeTimes : public torch.nn.Module
Encode image by spike trains.
Public Functions
-
__init__
(self, tau=20, threshold=0.2, t_max=1.0, epsilon=1e-7)¶ Initialize a pixel to spike time transformation.
:param tau: Streching factor of pixel to spike-time transformation. Lager values correspond to later spike times. :param threshold: The threshold under which a pixel is considered non-spiking (resp. spike at time t_max). :param t_max: Maximum spike time. :param epsilon: Safty margin to prevent zero-division.
-
forward
(self, torch.Tensor pic)¶ Turns an image into a spike representation.
Image pixels >= threshold are assigned a spike time according to:
If a pixel is < threshold it gets assigned the spike time t_max.t_spike = \tau * log(pixel / (pixel - threshold))
:param pic: Image to transform into spike trains. Has to be of shape (color_channel, width, height).
:return: Returns the image represented by spike trains, given as a tensor of shape (spike_time, color_channel, width, height).
-
-
hxtorch.spiking.modules.types.Population : public hxtorch.spiking.modules.hx_module.HXModule
Base class for populations on BSS-2.
Subclassed by hxtorch.spiking.modules.batch_dropout.BatchDropout, hxtorch.spiking.modules.neuron.Neuron
-
hxtorch.spiking.modules.types.Projection : public hxtorch.spiking.modules.hx_module.HXModule
Base class for projections on BSS-2.
Subclassed by hxtorch.spiking.modules.synapse.Synapse
Private Static Attributes
-
__constants__
= ['in_features', 'out_features']¶
-
-
hxtorch.spiking.modules.readout_neuron.ReadoutNeuron : public hxtorch.spiking.modules.neuron.Neuron
Readout neuron layer.
Caveat: For execution on hardware, this module can only be used in conjuction with a preceding Synapse module.
Public Functions
-
__init__
(self, int size, Experiment experiment, Union[Callable, torch.autograd.Function] func=F.LI, Optional[NamedTuple] params=None, bool enable_cadc_recording=True, bool enable_madc_recording=False, Optional[int] record_neuron_id=None, Optional[List[halco.LogicalNeuronOnDLS]] placement_constraint=None, Union[Dict[halco.AtomicNeuronOnDLS, float], torch.Tensor, float] trace_offset=0., Union[Dict[halco.AtomicNeuronOnDLS, float], torch.Tensor, float] trace_scale=1., int cadc_time_shift=1, bool shift_cadc_to_first=False, str interpolation_mode="linear", Optional[Morphology] neuron_structure=None)¶ Initialize a ReadoutNeuron.
This module creates a population of non- spiking neurons of size
size
and is equivalent to Neuron when its spiking mask is disabled for all neurons.:param size: Size of the population. :param experiment: Experiment to regsiter the module in. :param func: Callable function implementing the module’s forward functionallity or a torch.autograd.Function implementing the module’s forward and backward operation. Defaults to
LI
. :param params: Neuron Parameters in case of mock neuron integration of for backward path. If func does have a param argument the params object will get injected automatically. :param enable_cadc_recording: Enables or disables parallel sampling of the populations membrane trace via the CADC. A maximum sample rate of 1.7us is possible. :param enable_madc_recording: Enables or disables the recording of the neuronsrecord_neuron_id
membrane trace via the MADC. Only a single neuron can be recorded. This membrane traces is samples with a significant higher resolution as with the CADC. :param record_neuron_id: The in-population neuron index of the neuron to be recorded with the MADC. This has only an effect whenenable_madc_recording
is enabled. :param placement_constraint: An optional list of logical neurons defining where to place the module`s neurons on hardware. :param trace_offset: The value by which the measured CADC traces are shifted before the scaling is applied. If this offset is given as float the same value is applied to all neuron traces in this population. One can also provide a torch tensor holding one offset for each individual neuron in this population. The corresponding tensor has to be of sizesize
. Further, the offsets can be supplied in a dictionary where the keys are the hardware neuron coordinates and the values are the offsets, i.e. Dict[AtomicNeuronOnDLS, float]. The dictionary has to provide one coordinate for each hardware neuron represented by this population, but might also hold neuron coordinates that do not correspond to this layer. The layer-specific offsets are then picked and applied implicitly. :param trace_scale: The value by which the measured CADC traces are scaled after the offset is applied. If this scale is given as float all neuron traces are scaled with the same value population. One can also provide a torch tensor holding one scale for each individual neuron in this population. The corresponding tensor has to be of sizesize
. Further, the scales can be supplied in a dictionary where the keys are the hardware neuron coordinates and the values are the scales, i.e. Dict[AtomicNeuronOnDLS, float]. The dictionary has to provide one coordinate for each hardware neuron represented by this population, but might also hold neuron coordinates that do not correspond to this layer. The layer- specific scales are then picked and applied implicitly. :param cadc_time_shift: An integer indicating by how many time steps the CADC values are shifted in time. A positive value shifts later CADC samples to earlier times and vice versa for a negative value. :param shift_cadc_to_first: A boolean indicating that the first measured CADC value is used as an offset. Note, this disables the paramtrace_offset
. :param interpolation_mode: The method used to interpolate the measured CADC traces onto the given time grid. :param neuron_structure: Structure of the neuron. If not supplied a single neuron circuit is used.
-
configure_hw_entity
(self, int neuron_id, lola.NeuronBlock neuron_block, halco.LogicalNeuronOnDLS coord)¶ Configures a neuron in the given module with its specific properties.
The neurons digital event outputs are enabled according to the given spiking mask.
TODO: Additional parameterization should happen here, i.e. with population-specific parameters.
:param neuron_id: In-population neuron index. :param neuron_block: The neuron block hardware entity. :param coord: Coordinate of neuron on hardware. :returns: Configured neuron block.
-
post_process
(self, Optional[SpikeHandle] hw_spikes, Optional[CADCHandle] hw_cadc, Optional[MADCHandle] hw_madc)¶ User defined post process method called as soon as population-specific hardware observables are returned.
This function has to convert the data types returned by grenade into PyTorch tensors. This function can be overridden by the user if non-default grenade-PyTorch data type conversion is required. Note: This function should return Tuple[Optional[torch.Tensor], …], like (cadc or madc,). This should match the ReadoutTensorHandle signature.
:param hw_spikes: A SpikeHandle holding the population’s spikes recorded by grenade as a sparse tensor. This data can be ignored for this readout neuron. :param hw_cadc: The CADCHandle holding the CADC membrane readout events in a sparse tensor. :param hw_madc: The MADCHandle holding the MADC membrane readout events in a sparse tensor.
:returns: Returns a tuple of optional torch.Tensors holding the hardware data (madc or cadc,)
Public Static Attributes
-
Type
¶
-
-
hxtorch.spiking.handle.ReadoutNeuronHandle : public hxtorch.spiking.handle.TensorHandle
Specialization for HX neuron observables.
Public Functions
-
__init__
(self, Optional[torch.Tensor] v_cadc=None, Optional[torch.Tensor] current=None, Optional[torch.Tensor] v_madc=None)¶ Instantiate a readout neuron handle able to hold MADC, current, and CADC data.
This handle defines the CADC values as the observable state, this can be changed by deriving this class.
:param v_cadc: Optional membrane tensor, holding CADC recordings. :param current: Optional current tensor, holding synpatic current. :param v_madc: Optional membrane tensor, holding MADC recordings.
-
-
hxtorch.perceptron.nn.ReLU : public hxtorch.perceptron.nn.Layer , public torch.nn.ReLU
Applies a rectified linear unit to the input.
Subclassed by hxtorch.perceptron.nn.ConvertingReLU
-
hxtorch.spiking.morphology.SingleCompartmentNeuron : public hxtorch.spiking.morphology.Morphology
Neuron with a single iso-potential compartment.
The compartment can consist of several neuron circuits. For all but the first neuron circuit leak, threshold and capacitance are disabled.
Public Functions
-
__init__
(self, int size, bool expand_horizontally=False)¶ Create a single-compartment neuron.
:param size: Number of neuron circuits per compartment. :param expand_horizontally: Expand the neurons in the same row before starting a second row. If False, the columns are filled before the shape is expanded horizontally.
-
compartments
(self)¶ Unplaced coordinate of the logical neuron.
-
implement_morphology
(self, halco.LogicalNeuronOnDLS coord, lola.NeuronBlock neuron_block)¶ Configure the atomic neurons in the given neuron block to represent this morphology.
:param coord: Coordinate of the logical neuron which should be configured. :param neuron_block: The configuration of neurons at
coord
will be changed such that a neuron with the given morphology is implemented.
-
logical_neuron
(self)¶ Base configuration of the logical neuron.
Default constructed logical neuron, the connections between neuron circuits are configured such that the specified morphology is implemented.
Private Members
-
_logical_neuron
¶
Private Static Functions
-
_one_active_circuit
(halco.LogicalNeuronOnDLS coord, lola.NeuronBlock neuron_block)¶ Enable fire signal forwarding for first circuit and disable leak, capacitance as well as threshold for all other circuit.
-
-
yinyang_model.SNN : public torch.nn.Module
SNN with one hidden LIF layer and one readout LI layer.
Public Functions
-
__init__
(self, int n_in, int n_hidden, int n_out, bool mock, str calib_path, float dt=1.0e-6, float tau_mem=8e-6, float tau_syn=8e-6, float alpha=50, int trace_shift_hidden=0, int trace_shift_out=0, Optional[Tuple[float, float]] weight_init_hidden=None, Optional[Tuple[float, float]] weight_init_output=None, float weight_scale=1., float trace_scale=1., int input_repetitions=1, torch.device device=torch.device("cpu"))¶ Initialize the SNN.
:param n_in: Number of input units. :param n_hidden: Number of hidden units. :param n_out: Number of output units. :param mock: Indicating whether to train in software or on hardware. :param calib_path: Path to hardware calibration file. :param dt: Time-binning width. :param tau_mem: Membrane time constant. :param tau_syn: Synaptic time constant. :param trace_shift_hidden: Indicates how many indices the membrane trace of hidden layer is shifted to left along time axis. :param trace_shift_out: Indicates how many indices the membrane trace of readout layer is shifted to left along time axis. :param weight_init_hidden: Hidden layer weight initialization mean and std value. :param weight_init_output: Output layer weight initialization mean and std value. :param weight_scale: The factor with which the software weights are scaled when mapped to hardware. :param input_repetitions: Number of times to repeat input channels. :param device: The used PyTorch device used for tensor operations in software.
-
forward
(self, torch.Tensor spikes)¶ Perform a forward path.
:param spikes: NeuronHandle holding spikes as input.
:return: Returns the output of the network, i.e. membrane traces of the readout neurons.
-
-
hxtorch.spiking.transforms.encode.SpikeTimesToDense : public torch.nn.Module
Convert spike times to a dense matrix of zeros and ones.
Public Functions
-
__init__
(self, float bin_width, int size=100)¶ Initialize the conversion of spike times to a dense matrix of zeros and ones.
:param bin_width: Binning interval in seconds. :param size: number of bins along time axis.
-
forward
(self, torch.Tensor spikes)¶ Convert spike times to dense matrix of zeros and ones.
:param spikes: Spike times of shape ‘(color_channels, x0[, x1, …])’.
:returns: Returns a dense matirx of shape ‘(time_bins, color_channels, x0[, x1, …])’.
Private Members
-
_to_sparse
¶
-
-
hxtorch.spiking.transforms.encode.SpikeTimesToSparseTensor : public torch.nn.Module
Convert spike times to a dense matrix of zeros and ones.
Public Functions
-
__init__
(self, float bin_width, int size=100)¶ Initialize the conversion of spike times to a dense matrix of zeros and ones.
:param bin_width: Binning interval in seconds. :param size: number of bins along time axis.
-
forward
(self, torch.Tensor spikes)¶ Convert spike times to dense matrix of zeros and ones.
:param spikes: Spike times of shape ‘(color_channels, x0[, x1, …])’.
:returns: Returns a dense matirx of shape ‘(time_bins, color_channels, x0[, x1, …])’.
-
-
hxtorch.spiking.transforms.decode.SumOverTime : public torch.nn.Module
Simple sum-over-time decoding.
Public Functions
-
forward
(self, torch.Tensor input)¶ Translate an
input
tensor of shape (batch_size, time_length, population_size) into a tensor of shape (batch_size, population_size), where the time dimension is discarded by computing the sum along the time.Hence this module performs a ‘sum-over-time’ operation.
:param input: The input tensor to transform. expected shape: (batch_size, time_length, population_size) :return: Returns the tensor holding the sum-over-time values.
-
-
hxtorch.spiking.functional.superspike.SuperSpike : public torch.autograd.Function
Define Surrogate Gradient ‘SuperSpike’ (negative side of Fast Sigmoid) See: https://arxiv.org/abs/1705.11146.
Public Static Functions
-
backward
(ctx, torch.Tensor grad_output)¶ Implements surrogate gradient ‘SuperSpike’ for backward.
:param grad_output: Backpropagted gradient.
:returns: SuperSpike gradient multiplied to backpropagated gradient.
-
forward
(ctx, torch.Tensor input, float alpha)¶ Forward function, generating spikes at positions > 0.
:param input: Tensor holding
v_mem - v_th
, of > 0 a spike at the corresponding entry is generated. Saved for backward. :param alpha: Parameter controlling the slope of the surrogate derivative. Saved for backward.:returns: A boolean tensor holding True at the entries for neurons emitting a spike.
-
-
hxtorch.spiking.modules.synapse.Synapse : public hxtorch.spiking.modules.types.Projection
Synapse layer.
Caveat: For execution on hardware, this module can only be used in conjuction with a subsequent Neuron module.
Public Functions
-
__init__
(self, int in_features, int out_features, Experiment experiment, Union[Callable, torch.autograd.Function] func=F.linear, str device=None, Type dtype=None, Callable transform=weight_transforms.linear_saturating)¶ TODO: Think about what to do with device here.
:param in_features: Size of input dimension. :param out_features: Size of output dimension. :param device: Device to execute on. Only considered in mock-mode. :param dtype: Data type of weight tensor. :param experiment: Experiment to append layer to. :param func: Callable function implementing the module’s forward functionallity or a torch.autograd.Function implementing the module’s forward and backward operation. Required function args: [input (torch.Tensor), weight (torch.Tensor)]
-
add_to_network_graph
(self, grenade.PopulationDescriptor pre, grenade.PopulationDescriptor post, grenade.NetworkBuilder builder)¶ Adds the projection to a grenade network builder by providing the population descriptor of the corresponding pre and post population.
Note: This creates one inhibitory and one excitatory population on hardware in order to represent signed hardware weights.
:param pre: Population descriptor of pre-population. :param post: Population descriptor of post-population. :param builder: Greande netowrk builder to add projection to.
:returns: A tuple of grenade ProjectionDescriptors holding the descriptors for the excitatory and inhibitory projection.
-
changed_since_last_run
(self)¶ Getter for changed_since_last_run.
:returns: Boolean indicating wether module changed since last run.
-
register_hw_entity
(self)¶ Add the synapse layer to the experiment’s projections.
-
reset_changed_since_last_run
(self)¶ Reset changed_since_last_run.
Sets the corresponding flag to false.
-
reset_parameters
(self, float mean, float std)¶ Resets the synapses weights by reinitalization using torch.nn.normal_ with mean=
mean
and std=std
.:param mean: The mean of the normal distribution used to initialize the weights from. :param std: The standard deviation of the normal distribution used to initialized the weights from.
Public Static Attributes
-
Type
¶
Private Members
-
_weight_old
¶
-
-
hxtorch.spiking.handle.SynapseHandle : public hxtorch.spiking.handle.TensorHandle
Specialization for HX synapses.
Public Functions
-
__init__
(self, Optional[torch.Tensor] graded_spikes=None)¶ Instantiate a synapse handle able to hold a graded spikes tensors as input to neurons.
:param graded_spikes: Optional tensor holding graded spikes.
Public Members
-
graded_spikes
¶
-
-
hxtorch.spiking.handle.TensorHandle : public metaclass , public hxtorch.spiking.handle.HandleMeta
Base class for HX tensor handles.
New tensor handles have to be derived from this class. The name of tensors the tensor handle ‘carries’ has to be indicated in the class member ‘_carries’. For all elements in this list a property is created implicitly.
The observable state of the tensor handle is definied in ‘_obsv_state’ and defines which tensor is passed to the subsequent layer as input. Member ‘_obsv_state’ has to be in ‘_carries’.
Subclassed by hxtorch.spiking.handle.NeuronHandle, hxtorch.spiking.handle.ReadoutNeuronHandle, hxtorch.spiking.handle.SynapseHandle
Public Functions
-
__init__
(self)¶ Instanziate a new HX handle holding references to torch tensors.
-
clear
(self)¶ Set all data in handle to ‘None’.
-
holds
(self, str name)¶ Checks whether the tensor handle already holds a tensor with key
name
.:param name: Key of reference to tensor. :return: Returns a bool indicating whether the data present at key
name
is not None.
-
observable_state
(self)¶ Getter for observable state.
:return: Returns the tensor assoicated with ‘_obsv_state’
-
observable_state_identifier
(self)¶ Getter for observable state identifier.
:return: Returns the identifier string associated with the observable state of the handle.
-
observable_state_identifier
(self, str identifier)¶ Setter for observable state identifier.
This identifier has to be in
_carries
.
-
put
(self, *tensors, **kwargs)¶ Fill the tensor handle with actual data given by tensors or kwargs.
If tensors are given as positional arguments the tensors are assigned in the order given by class member ‘_carries’. Keyword arguments are assigned to the corresponding key. Therefore, the key has to be in ‘_carries.’
:param tensors: Tensors which are assigned to the tensor handle. The given tensors are associated with the elements in ‘_carries’ in successive order.
:keyword param kwargs: Assigns the items in kwargs to the elements in the tensor handle associated with the corresponding kwargs keys.
Public Members
-
data
¶
Public Static Attributes
-
str
¶
Private Members
-
_obsv_state
¶
-
-
hxtorch.spiking.functional.unterjubel.Unterjubel : public torch.autograd.Function
Unterjubel hardware observables to allow correct gradient flow.
Public Static Functions
-
backward
(torch.Tensor ctx, torch.Tensor grad_output)¶ Backward the gradient.
:param grad_output: The backwarded gradient.
:returns: Returns simply the backpropagated gradient at first position.
-
forward
(ctx, torch.Tensor input, torch.Tensor input_prime)¶ Returns
input_prime
insteat ofinput
to injectinput_prime
but direct the gradient toinput
.:param input: Input tensor. :param input_prime: The returned tensor.
:returns: Returns the primed tensor. Thereby, this tensor is forwarded while the gradient is directed to to
input
.
-
-
hxtorch.spiking.datasets.yinyang.YinYangDataset : public Dataset
YinYang dataset.
Public Functions
-
__init__
(self, float r_small=0.1, float r_big=0.5, int size=1000, int seed=42, Optional[torch.nn.Module] transform=None)¶ Instantiate the YinYang dataset.
This dataset provides datapoints on a 2-dimensional plane within a yin-yang sign. Each data point is assigned to one of three classes: The eyes, the yin or the yang. :param r_small: The radius of the eyes in the yin-yang sign. :param r_big: The radius of the whole sign. :param size: The size of the dataset, i.e. number of data points within the sign. :param seed: Random seed. :param transform: An optional transformation applied to the returned samples.
-
__getitem__
(self, int index)¶ Get an item from the dataset.
:param index: Index of the item in the dataset. :returns: Returns a tuple (sample, target), where sample is an array with values (x, y, 1 - x, 1 - y).
-
__len__
(self)¶
-
dist_to_left_dot
(self, int x, int y)¶ Compute the distance to the left dot.
:param x: The x-coordinate. :param y: The y-coordinate. :returns: Returns the distance to the left dot.
-
dist_to_right_dot
(self, int x, int y)¶ Compute the distance to the right dot.
:param x: The x-coordinate. :param y: The y-coordinate. :returns: Returns the distance to the right dot.
-
get_sample
(self, Optional[int] goal=None)¶ Sample one data point from the yin-yang sign with goal class
goal
.If
goal
is None any sample of any class is returned. :param goal: The target class of the sample to return. If None is given, any sample regardless of its class is retuned. :returns: Returns a tuple (x coordiante, y coordinate, class), where the coordinates are on the xy-plane.
-
which_class
(self, float x, float y)¶ Assign a sample on the xy-plane with coordinates (x, y) to its class.
:param x: The x-coordinate. :param y: The y-coordinate. :returns: An integer indicating the samples class.
-
-
module
_hxtorch_core
¶
-
module
_hxtorch_perceptron
¶
-
module
hxtorch
¶
-
module
examples
¶
-
module
perceptron
¶
-
module
hxtorch.perceptron.
nn
¶ This module contains layers that can be used in modules together with the building blocks from py:mod:
torch.nn
.Unlike their counterparts, their multiply-accumulate operations are performed with the BrainScaleS-2 accelerator. Additional digital operations are performed in the SIMD processors of BSS-2.
Functions
-
clamp_weight_
(torch.Tensor weight)¶ Clamps all elements of the weight in-place into the maximal weight range of BrainScaleS-2.
-
scale_input
(torch.Tensor x_in)¶ Scales the tensor to the maximal input range of BrainScaleS-2.
-
scale_weight
(torch.Tensor weight)¶ Scales the tensor to the maximal weight range of BrainScaleS-2.
-
-
module
spiking
¶
-
module
backend
¶
-
module
hxtorch.spiking.backend.
module_manager
¶ Define graph-based datastructure managing Nodes.
-
module
datasets
¶
-
module
yinyang
¶ YinYangDataset class from https://github.com/lkriener/yin_yang_data_set/blob/master/dataset.py with minor changes.
-
module
hxtorch.spiking.
experiment
¶ Defining basic types to create hw-executable instances.
Variables
-
log
= logger.get("hxtorch.spiking.experiment")¶
-
-
module
functional
¶
-
module
hxtorch.spiking.functional.
dropout
¶ Custom BatchDropout function.
Functions
-
batch_dropout
(torch.Tensor input, torch.Tensor mask)¶ Applies a dropout mask to a batch of inputs.
:param input: The input tensor to apply dropout to. :param mask: The dropout mask. Entires in the mask which are
False
will disable their corresponding entry ininput
.:returns: The input tensor with dropout mask applied.
-
-
module
hxtorch.spiking.functional.
iaf
¶ Integrate and fire neurons.
Functions
-
cuba_iaf_integration
(torch.Tensor input, NamedTuple params, Optional[torch.Tensor] hw_data=None, float dt=1e-6)¶ Leaky-integrate and fire neuron integration for realization of simple spiking neurons with exponential synapses.
Integrates according to: v^{t+1} = dt / \tau_{men} * (v_l - v^t + i^t) + v^t i^{t+1} = i^t * (1 - dt / \tau_{syn}) + x^t z^{t+1} = 1 if v^{t+1} > params.v_th v^{t+1} = v_reset if z^{t+1} == 1 Assumes i^0, v^0 = 0., v_reset :note: One
dt
synaptic delay between input and output :param input: Input spikes in shape (batch, time, neurons). :param params: LIFParams object holding neuron prameters. :return: Returns the spike trains in shape and membrane trace as a tuple. Both tensors are of shape (batch, time, neurons).
-
cuba_refractory_iaf_integration
(torch.Tensor input, NamedTuple params, Optional[torch.Tensor] hw_data=None, float dt=1e-6)¶ Integrate and fire neuron integration for realization of simple spiking neurons with exponential synapses and refractory period.
Integrates according to: v^{t+1} = dt / \tau_{men} * i^t + v^t i^{t+1} = i^t * (1 - dt / \tau_{syn}) + x^t z^{t+1} = 1 if v^{t+1} > params.v_th v^{t+1} = params.v_reset if z^{t+1} == 1 or ref^t > 0 ref^{t+1} -= 1 ref^{t+1} = params.tau_ref if z^{t+1} == 1 Assumes i^0, v^0 = 0., v_reset :note: One
dt
synaptic delay between input and output :param input: Input spikes in shape (batch, time, neurons). :param params: LIFParams object holding neuron prameters. :return: Returns the spike trains in shape and membrane trace as a tuple. Both tensors are of shape (batch, time, neurons).
-
iaf_step
(torch.Tensor z, torch.Tensor v, torch.Tensor i, torch.Tensor input, torch.Tensor z_hw, torch.Tensor v_hw, NamedTuple params, float dt)¶ Integrate the membrane of a neurons one time step further according to the integrate and fire dynamcis.
:param z: The spike tensor at time step t. :param v: The membrane tensor at time step t. :param i: The current tensor at time step t. :param input: The input tensor at time step t (graded spikes). :param z_hw: The hardware spikes corresponding to the current time step. In case this is None, no HW spikes will be injected. :param v_hw: The hardware cadc traces corresponding to the current time step. In case this is None, no HW cadc values will be injected. :param params: Parameter object holding the LIF parameters. :param dt: Integration step width. :returns: Returns a tuple (z, v, i) holding the tensors of time step t + 1.
-
-
module
hxtorch.spiking.functional.
li
¶ Leaky-integrate neurons.
Functions
-
cuba_li_integration
(torch.Tensor input, CUBALIParams params, Optional[torch.Tensor] hw_data=None, float dt=1e-6)¶ Leaky-integrate neuron integration for realization of readout neurons with exponential synapses.
Integrates according to: v^{t+1} = dt / \tau_{mem} * (v_l - v^t + i^t) + v^t i^{t+1} = i^t * (1 - dt / \tau_{syn}) + x^t
Assumes i^0, v^0 = 0. :note: One
dt
synaptic delay between input and output :param input: Input spikes in shape (batch, time, neurons). :param params: LIParams object holding neuron parameters. :param dt: Integration step width:return: Returns the membrane trace in shape (batch, time, neurons).
-
-
module
hxtorch.spiking.functional.
lif
¶ Leaky-integrate and fire neurons.
Functions
-
cuba_lif_integration
(torch.Tensor input, CUBALIFParams params, Optional[torch.Tensor] hw_data=None, float dt=1e-6)¶ Leaky-integrate and fire neuron integration for realization of simple spiking neurons with exponential synapses.
Integrates according to: i^{t+1} = i^t * (1 - dt / \tau_{syn}) + x^t v^{t+1} = dt / \tau_{men} * (v_l - v^t + i^t) + v^t z^{t+1} = 1 if v^{t+1} > params.v_th v^{t+1} = params.v_reset if z^{t+1} == 1
Assumes i^0, v^0 = 0, v_leak :note: One
dt
synaptic delay between input and outputTODO: Issue 3992
:param input: Input spikes in shape (batch, time, neurons). :param params: LIFParams object holding neuron prameters. :param dt: Step width of integration.
:return: Returns the spike trains in shape and membrane trace as a tuple. Both tensors are of shape (batch, time, neurons).
-
-
module
hxtorch.spiking.functional.
linear
¶ Implement linear autograd function.
Functions
-
linear
(torch.Tensor input, torch.nn.parameter.Parameter weight, torch.nn.parameter.Parameter bias=None)¶ Wrap
linear
to allow signature inspection.
-
-
module
hxtorch.spiking.functional.
refractory
¶ Refractory update for neurons with refractory behaviour.
Functions
-
refractory_update
(torch.Tensor z, torch.Tensor v, torch.Tensor ref_state, NamedTuple params, float dt=1e-6)¶ Update neuron membrane and spikes to account for refractory period.
This implemention is widly adopted from: https://github.com/norse/norse/blob/main/norse/torch/functional/lif_refrac.py :param z: The spike tensor at time step t. :param v: The membrane tensor at time step t. :param ref_state: The refractory state holding the number of time steps the neurons has to remain in the refractory period. :param params: Parameter object holding the LIF parameters. :returns: Returns a tuple (z, v, ref_state) holding the tensors of time step t.
-
-
module
hxtorch.spiking.functional.
spike_source
¶ Define different input spike sources.
Functions
-
input_neuron
(torch.Tensor input)¶ Identity input neuron.
This forwards only the input tensor and is only used for consistancy in
HXInputNeuron
which can also takes autograd functions. This enables gradient flow though input layers.:param input: Input spike tensor.
:retuns: Returns the input spike tensor.
-
-
module
hxtorch.spiking.functional.
superspike
¶ Surrograte gradient for SuperSpike.
Basically reimplemented from: https://github.com/norse/norse/blob/18cd8aa256c3e7b5a28a852f5e928b104bc637fc/norse/torch/functional/superspike.py#L33
Functions
-
superspike_func
(torch.Tensor input, float alpha)¶
-
-
module
hxtorch.spiking.functional.
threshold
¶ Threshold function providing functionality for making spiking outputs differentiable.
Inspired by https://github.com/norse/norse/blob/ 72a812e17da23487878a667ad82a075ef7ad91ec/norse/torch/functional/ superspike.py
Functions
-
threshold
(torch.Tensor input, str method, float alpha)¶ Selection of the used threshold function.
:param input: Input tensor to threshold function. :param method: The string indicator of the the threshold function. Currently supported: ‘super_spike’. :param alpha: Parameter controlling the slope of the surrogate derivative in case of ‘superspike’. :return: Returns the tensor of the threshold function.
-
-
module
unterjubel
¶ Autograd function to ‘unterjubel’ (german for ‘inject’) hardware observables and allow correct gradient backpropagation.
Basically taken from: https://gerrit.bioai.eu:9443/gitweb?p=model-hx-strobe.git;a=blob;f=src/py/strobe/unterjubel.py;h=4b13de159bd54b629a0b6278cb9c2f58f62883bf;hb=HEAD;js=1
-
module
handle
¶ Defining tensor handles able to hold references to tensors for lazy assignment after hardware data acquisition.
-
module
modules
¶
-
module
hxtorch.spiking.modules.
batch_dropout
¶ Implementing BatchDropout Module.
Variables
-
log
= logger.get("hxtorch.spiking.modules")¶
-
-
module
hxtorch.spiking.modules.
hx_module
¶ Implementing the base module HXModule.
Variables
-
log
= logger.get("hxtorch.spiking.modules")¶
-
-
module
hxtorch.spiking.modules.
hx_module_wrapper
¶ Implementing a module wrapper to wrap multiple modules as one.
Variables
-
log
= logger.get("hxtorch.spiking.modules")¶
-
-
module
hxtorch.spiking.modules.
iaf_neuron
¶ Implementing integrate-and-fire neuron module.
Variables
-
log
= logger.get("hxtorch.spiking.modules")¶
-
-
module
hxtorch.spiking.modules.
input_neuron
¶ Implementing input neuron module.
Variables
-
log
= logger.get("hxtorch.spiking.modules")¶
-
-
module
hxtorch.spiking.modules.
neuron
¶ Implementing SNN modules.
Variables
-
log
= logger.get("hxtorch.spiking.modules")¶
-
-
module
hxtorch.spiking.modules.
readout_neuron
¶ Implementing SNN modules.
Variables
-
log
= logger.get("hxtorch.spiking.modules")¶
-
-
module
hxtorch.spiking.modules.
synapse
¶ Implementing SNN modules.
Variables
-
log
= logger.get("hxtorch.spiking.modules")¶
-
-
module
types
¶ Define module types.
-
module
morphology
¶ User defined neuron morphologies.
-
module
hxtorch.spiking.
run
¶ Run function to execute a SNN given in an experiment.
Functions
-
run
(Experiment experiment, Optional[int] runtime)¶ Execute the given experiment.
TODO: Why is this a standalone function?
:param experiment: The experiment representing the computational graph to be executed on hardware and/or in software. :param runtime: Only relevant for hardware experiements. Indicates the runtime resolved with experiment.dt.
Variables
-
log
= logger.get("hxtorch.snn.run")¶
-
-
module
transforms
¶
-
module
decode
¶ Define modules to decode SNN observables.
-
module
encode
¶ Define transformations for spiking input coding.
-
module
hxtorch.spiking.transforms.
weight_transforms
¶ Define convenient software <-> hardware weight mappings.
TODO: Implement stochastic rounding?
Functions
-
linear_saturating
(torch.Tensor weight, float scale=1., float min_weight=-63., float max_weight=63.)¶ Scale all weights according to:
TODO: Maybe make this function member of HXSynapse and allow different weight transformations by inheritance.w <- clip(scale * w, min_weight, max_weight)
:param weight: The weight tensor to be transformed. :param scale: A constant the weight tensor is scaled with. :param min_weight: The minimum value, smaller values are clipped to after scaling. :param max_weight: The maximum value, bigger values are clipped to after scaling.
:returns: The transformed weight tensor.
-
-
module
utils
¶
-
module
hxtorch.spiking.utils.
calib_helper
¶ Helpers to handle calibrations.
Functions
-
chip_from_file
(str path)¶ Extract chip config from coco file dump.
:param path: path to file containing coco dump.
-
chip_from_portable_binary
(bytes data)¶ Convert portable binary data to chip object.
:param data: Coco list in portable binary format. :return: lola chip configuration.
-
nightly_calib_path
(str name="spiking")¶ Find path for nightly calibration.
-
-
module
model_mapper
¶ Mapper mixin to map Norse models to hxtorch models.
-
module
mnist
¶ Training example for the MNIST handwritten-digits dataset using a host machine with the BrainScaleS-2 ASIC in the loop.
Functions
-
get_parser
()¶ Returns an argument parser with all the options.
-
init
(str calibration_path, bool mock, Real mock_noise_std, Real mock_gain)¶ Initialize hxtorch connection and load calibration.
Caveat: This also measures and sets the gain, therefore do this before initializing the model (as this influences layer initialization).
:param calibration_path: Path of custom calibration :param mock: Whether to simulate the hardware :param mock_noise_std: Standard deviation of artificial noise in mock mode :param mock_gain: Multiplication gain used in mock mode
-
main
(argparse.Namespace args)¶ The main experiment function.
:param args: Command-line arguments
-
shrink_dataset
(torch.utils.data.Dataset dataset, Real fraction)¶ Returns a fraction of the original dataset
-
test
(torch.nn.Module model, torch.utils.data.DataLoader loader)¶ Test the model.
:param model: The model to test :param loader: Data loader containing the test data set :returns: Test accuracy
-
train
(torch.nn.Module model, torch.utils.data.DataLoader loader, torch.optim.Optimizer optimizer)¶ Train the model.
:param model: The model :param loader: Data loader containing the train data set :param optimizer: Optimizer that handles the weight updates
Variables
-
log
= hxtorch.logger.get("hxtorch.examples.perceptron.mnist")¶
-
-
namespace
std
STL namespace.
-
namespace
torch
¶
-
namespace
autograd
¶
-
namespace
nn
¶
-
namespace
modules
¶
-
namespace
conv
¶
-
namespace
utils
¶
-
namespace
data
¶
-
module
yinyang
¶ Spiking HX torch yinyang example.
Functions
-
get_parser
()¶ Returns an argument parser with all the options.
-
main
(argparse.Namespace args)¶ Entrypoint for SNN training on the YinYang dataset.
Loads the dataset and executes training.
:param args: Argparse Namespace providing necessary constants.
:return: Returns the achieved accuracy after the last test epoch.
-
plot
(train_loss, train_acc, test_loss, test_acc, args)¶ Plot losses and accuracies.
:param train_loss: List holding the average training loss for each epoch :param train_acc: List holding the average training accuracy for each epoch :param test_loss: List holding the average test loss each epoch :param test_acc: List holding the average accuracy for each epoch :param args: The arguments.
-
test
(torch.nn.Module model, torch.utils.data.DataLoader loader, argparse.Namespace args, int epoch)¶ Test the model.
:param model: The model to test :param loader: Data loader containing the test data set :param epoch: Current trainings epoch.
:returns: Tuple of (test loss, test accuracy)
-
train
(torch.nn.Module model, DataLoader loader, torch.optim.Optimizer optimizer, argparse.Namespace args, int epoch)¶ Perform training for one epoch.
:param model: The model to train. :param loader: Pytorch DataLoader instance providing training data. :param optimizer: The optimizer used or weight optimization. :param args: The argparse Namespace. :param epoch: Current epoch for logging.
:returns: Tuple (training loss, training accuracy)
-
-
file
minimal.py
-
file
mnist.py
-
file
yinyang_model.py
-
file
__init__.py
-
file
__init__.py
-
file
__init__.py
-
file
__init__.py
-
file
__init__.py
-
file
__init__.py
-
file
__init__.py
-
file
__init__.py
-
file
__init__.py
-
file
__init__.py
-
file
nn.py
-
file
module_manager.py
-
file
yinyang.py
-
file
yinyang.py
-
file
experiment.py
-
file
dropout.py
-
file
iaf.py
-
file
li.py
-
file
lif.py
-
file
linear.py
-
file
refractory.py
-
file
spike_source.py
-
file
superspike.py
-
file
threshold.py
-
file
unterjubel.py
-
file
handle.py
-
file
batch_dropout.py
-
file
hx_module.py
-
file
hx_module_wrapper.py
-
file
iaf_neuron.py
-
file
input_neuron.py
-
file
neuron.py
-
file
readout_neuron.py
-
file
synapse.py
-
file
types.py
-
file
morphology.py
-
file
run.py
-
file
decode.py
-
file
encode.py
-
file
weight_transforms.py
-
file
calib_helper.py
-
file
model_mapper.py
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/src/pyhxtorch/hxtorch/spiking/backend
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/src/pyhxtorch/hxtorch/spiking/datasets
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/src/pyhxtorch/hxtorch/examples
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/src/pyhxtorch/hxtorch/spiking/functional
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/src/pyhxtorch/hxtorch
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/src/pyhxtorch/hxtorch/spiking/modules
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/src/pyhxtorch/hxtorch/examples/perceptron
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/src/pyhxtorch/hxtorch/perceptron
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/src/pyhxtorch
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/src/pyhxtorch/hxtorch/examples/spiking
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/src/pyhxtorch/hxtorch/spiking
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/src
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/src/pyhxtorch/hxtorch/spiking/transforms
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/src/pyhxtorch/hxtorch/spiking/utils
hxtorch from C++¶
-
template<typename
R
, typenameTrafo
, typenameT
, size_tN
, template<typename U> classPtrTraits
, typenameindex_t
>
structhxtorch::perceptron::detail
::
ConvertToVector
¶ - #include <util.h>
Implementation for the at::TensorAccessor<T, N, PtrTraits, index_t> to std::vector<…<R>> (nesting-level N) conversion helper below.
Note
Other template parameaters come from at::TensorAccessor
- tparam R
return value type
- tparam Trafo
transformation function
Public Types
-
typedef std::vector<typename unpacked_type::result_type>
result_type
¶
Public Static Functions
-
static inline result_type
apply
(value_type const &value, Trafo t)¶
-
template<typename
R
, typenameTrafo
, typenameT
, template<typename U> classPtrTraits
, typenameindex_t
>
structhxtorch::perceptron::detail
::
ConvertToVector
<R, Trafo, T, 1, PtrTraits, index_t>¶ - #include <util.h>
Public Types
Public Static Functions
-
static inline result_type
apply
(value_type const &value, Trafo t)¶
-
static inline result_type
-
class
hxtorch::spiking
::
DataHandle
¶ - #include <types.h>
Subclassed by hxtorch::spiking::CADCHandle, hxtorch::spiking::MADCHandle, hxtorch::spiking::SpikeHandle
-
struct
hxtorch::core
::
HWDBPath
¶ - #include <connection.h>
Path to a hardware database.
Public Functions
-
struct
hxtorch::perceptron::detail
::
InferenceTracer
¶ - #include <inference_tracer.h>
Inference tracer implementation.
Currently only traces operation names.
Public Functions
-
class
hxtorch::perceptron
::
InferenceTracer
¶ - #include <inference_tracer.h>
Inference tracer for a linear sequence of operations.
The traced operations’ state is saved as a grenade::compute::Sequence, which can be executed as a single operation without transformation to and from PyTorch tensors. It is ensured, that no untraced modifications are made in-between traced operations by comparing the last traced operation’s output with the currently traced operation’s input value.
Note
Not final API or implementation, see Issue #3694
-
struct
hxtorch::perceptron
::
MockParameter
¶ - #include <mock.h>
Parameter of hardware mock.
-
struct
hxtorch::perceptron::detail
::
MultidimIterator
¶ - #include <iterator.h>
Public Functions
-
inline MultidimIterator
end
() const¶
-
inline bool
operator!=
(MultidimIterator const &other) const¶
-
inline MultidimIterator &
operator++
()¶
-
inline bool
operator==
(MultidimIterator const &other) const¶
-
inline MultidimIterator
-
namespace
grenade
-
namespace
vx
-
namespace
execution
-
namespace
signal_flow
-
namespace
hxtorch
¶
-
namespace
hxtorch
::
core
¶ Functions
-
std::string
get_unique_identifier
(std::optional<HWDBPath> const &hwdb_path = std::nullopt)¶ Get unique identifier.
- Parameters
hwdb_path – Optional path to the hwdb to use
-
void
init_hardware
(CalibrationPath const &calibration_path)¶ Initialize the hardware with calibration path.
- Parameters
calibration_path – Calibration path to load from
-
void
init_hardware
(std::optional<HWDBPath> const &hwdb_path = std::nullopt, bool ann = false)¶ Initialize the hardware automatically from the environment.
- Parameters
hwdb_path – Optional path to the hwdb to use. Only effective if param
ann
is true.ann – Bool indicating whether additionally a default chip object is constructed for ANNs from a calibration loaded from
hwdb_path
, or if not given, from the latest nightly calibration.
-
void
init_hardware_minimal
()¶ Initialize automatically from the environment without ExperimentInit and without any calibration.
-
void
release_hardware
()¶ Release hardware resource.
-
std::string
-
namespace
hxtorch
::
perceptron
¶ Functions
-
torch::Tensor
add
(torch::Tensor const &input, torch::Tensor const &other, double alpha = 1., bool mock = false)¶ Elementwise addition operating on int8 value range.
- Parameters
input – Input tensor
other – Other tensor, which must be broadcastable to input tensor dimension
alpha – The scalar multiplier for other
mock – Enable mock mode
-
torch::Tensor
argmax
(torch::Tensor const &input, c10::optional<int64_t> dim = c10::nullopt, bool keepdim = false, bool mock = false)¶ Arg max operation on int8 value range.
- Parameters
input – The input tensor
dim – The dimension to reduce. If unspecified, the argmax of the flattened input is returned.
keepdim – Whether the output tensor has
dim
retained or not. Ignored ifdim
is unspecified.mock – Enable mock mode
- Returns
The indices of the maximum values of a tensor across a dimension
-
torch::Tensor
conv1d
(torch::Tensor const &input, torch::Tensor const &weight, c10::optional<torch::Tensor> const &bias, int64_t stride = 1, int64_t num_sends = 1, int64_t wait_between_events = constants::defaults::wait_between_events, bool mock = false)¶
-
torch::Tensor
conv1d
(torch::Tensor const &input, torch::Tensor const &weight, c10::optional<torch::Tensor> const &bias, std::array<int64_t, 1> stride, int64_t num_sends = 1, int64_t wait_between_events = constants::defaults::wait_between_events, bool mock = false)¶
-
torch::Tensor
conv2d
(torch::Tensor const &input, torch::Tensor const &weight, c10::optional<torch::Tensor> const &bias, int64_t stride = 1, int64_t num_sends = 1, int64_t wait_between_events = constants::defaults::wait_between_events, bool mock = false)¶
-
torch::Tensor
conv2d
(torch::Tensor const &input, torch::Tensor const &weight, c10::optional<torch::Tensor> const &bias, std::array<int64_t, 2> stride, int64_t num_sends = 1, int64_t wait_between_events = constants::defaults::wait_between_events, bool mock = false)¶
-
template<typename
R
, typenameT
, size_tN
, template<typename U> classPtrTraits
, typenameindex_t
, typenameTrafo
= decltype(detail::default_transform<R, T>)>
detail::ConvertToVector<R, Trafo, T, N, PtrTraits, index_t>::result_typeconvert_to_vector
(at::TensorAccessor<T, N, PtrTraits, index_t> const &tensor, Trafo func = detail::default_transform)¶ Conversion helper for converting at::TensorAccessor<T, N, PtrTraits, index_t> to std::vector<…<R>> (nesting-level N) types.
Note
The underlying value_type T is converted to R. All other template parameters come from at::TensorAccessor
- Template Parameters
R – value_type to be returned
Trafo – Conversion type (defaults to R)
- Parameters
tensor – The tensor accessor to be converted to a nested vector
func – The conversion function for the value_type inside (defaults to R())
- Returns
A nested std::vector<std::vector<…<R>>> (N nested vectors).
-
torch::Tensor
converting_relu
(torch::Tensor const &input, int64_t shift = 2, bool mock = false)¶ Rectified linear unit operating on int8 value range converting to uint5 value range.
The result is bit-shifted by
shift
after applying the ReLU and clipped to the input range of BrainScaleS-2.- Parameters
input – Input tensor
shift – Amount of bits to shift before clipping
mock – Enable mock mode
-
torch::Tensor
expanded_conv1d
(torch::Tensor const &input, torch::Tensor const &weight, c10::optional<torch::Tensor> const &bias, int64_t stride = 1, int64_t num_expansions = 1, int64_t num_sends = 1, int64_t wait_between_events = constants::defaults::wait_between_events, bool mock = false)¶ 1D convolution operation that unrolls the weight matrix for execution on hardware.
This maximizes the use of the synapses array.
Note
Fixed-pattern noise cannot be individually compensated for during training, because the same weights are used at different locations!
- Parameters
input – Input tensor of shape (minibatch, in_channels, iW)
weight – Filters of shape (out_channels, in_channels / groups, kW)
bias – Optional bias of shape (out_channels)
stride – Stride of the convolving kernel
num_expansions – Number of enrolled kernels that will be placed side by side in a single operation
num_sends – How often to send the (same) input vector
wait_between_events – How long to wait (in FPGA cycles) between events
mock – Enable mock mode
-
torch::Tensor
expanded_conv1d
(torch::Tensor const &input, torch::Tensor const &weight, c10::optional<torch::Tensor> const &bias, std::array<int64_t, 1> stride, int64_t num_expansions = 1, int64_t num_sends = 1, int64_t wait_between_events = constants::defaults::wait_between_events, bool mock = false)¶
-
MockParameter
get_mock_parameter
()¶
-
torch::Tensor
inference_trace
(torch::Tensor const &input, std::string const &filename)¶ Execute inference of stored trace.
- Parameters
input – Input data to use
filename – Filename to serialized operation trace
-
torch::Tensor
mac
(torch::Tensor const &x, torch::Tensor const &weights, int64_t num_sends = 1, int64_t wait_between_events = wait_between_events, bool mock = false, int64_t madc_recording_neuron_id = 0, std::string madc_recording_path = "")¶ The bare mutliply-accumulate operation of BrainScaleS-2.
A 1D input
x
is multiplied by the weight matrixweights
. Ifx
is two-dimensional, the weights are sent only once to the synapse array and the inputs are consecutively multiplied as a 1D vector.- Parameters
x – Input tensor
weights – The weights of the synapse array
num_sends – How often to send the (same) input vector
wait_between_events – How long to wait (in FPGA cycles) between events
mock – Enable mock mode
madc_recording_neuron_id – Neuron ID to record via MADC
madc_recording_path – Path to which to store MADC neuron membrane recordings in CSV format. If file exists new data is appended. By default recording is disabled.
- Throws
std::runtime_error – When MADC recording is enabled but mock-mode is used.
- Returns
Resulting tensor
-
torch::Tensor
matmul
(torch::Tensor const &input, torch::Tensor const &other, int64_t num_sends = 1, int64_t wait_between_events = wait_between_events, bool mock = false, int64_t madc_recording_neuron_id = 0, std::string madc_recording_path = "")¶ Drop-in replacement for the torch.matmul operation that uses BrainScaleS-2.
Note
The current implementation only supports
other
to be 1D or 2D.- Parameters
input – First input tensor
other – Second input tensor
num_sends – How often to send the (same) input vector
wait_between_events – How long to wait (in FPGA cycles) between events
mock – Enable mock mode
madc_recording_neuron_id – Neuron ID to record via MADC
madc_recording_path – Path to which to store MADC neuron membrane recordings in CSV format. If file exists new data is appended. By default recording is disabled.
- Throws
std::runtime_error – When MADC recording is enabled but mock-mode is used.
- Returns
Resulting tensor
-
MockParameter
measure_mock_parameter
()¶
-
torch::Tensor
relu
(torch::Tensor const &input, bool mock = false)¶ Rectified linear unit operating on int8 value range.
- Parameters
input – Input tensor
mock – Enable mock mode
-
void
set_mock_parameter
(MockParameter const ¶meter)¶
-
torch::Tensor
-
namespace
hxtorch::perceptron
::
constants
¶ Variables
-
static constexpr intmax_t
hardware_matrix_height
= halco::hicann_dls::vx::v3::SynapseRowOnSynram::size / 2¶
-
static constexpr intmax_t
hardware_matrix_width
= halco::hicann_dls::vx::v3::SynapseOnSynapseRow::size¶
-
static constexpr intmax_t
input_activation_max
= grenade::vx::signal_flow::UInt5::max¶
-
static constexpr intmax_t
input_activation_min
= grenade::vx::signal_flow::UInt5::min¶
-
static constexpr intmax_t
output_activation_max
= std::numeric_limits<grenade::vx::signal_flow::Int8::value_type>::max()¶
-
static constexpr intmax_t
-
namespace
hxtorch::perceptron::constants
::
defaults
¶
-
namespace
hxtorch::perceptron
::
detail
¶ Functions
-
torch::autograd::variable_list
add_backward
(torch::Tensor const &grad_output, torch::Tensor const &input, torch::Tensor const &other)¶
-
torch::Tensor
argmax
(torch::Tensor const &input, c10::optional<int64_t> dim = c10::nullopt, bool keepdim = false)¶
-
torch::Tensor
argmax_mock
(torch::Tensor const &input, c10::optional<int64_t> dim = c10::nullopt, bool keepdim = false)¶
-
torch::Tensor
conv
(torch::Tensor const &input, torch::Tensor const &weights, c10::optional<torch::Tensor> const &bias, std::vector<int64_t> const &stride, std::vector<int64_t> const &dilation, int64_t num_sends, int64_t wait_between_events, bool mock)¶
-
int64_t
conv1d_output_size
(int64_t input_size, int64_t kernel_size, int64_t stride = 1, int64_t dilation = 1)¶ Returns the output size of a convolution with given input size, kernel size, stride and dilation.
-
std::vector<int64_t>
conv_output_size
(std::vector<int64_t> input_size, std::vector<int64_t> kernel_size, std::vector<int64_t> stride, std::vector<int64_t> dilation)¶ Returns the output size of a convolution with given input size, kernel size, stride and dilation.
-
grenade::vx::signal_flow::UInt5
convert_activation
(float value)¶
-
float
convert_membrane
(int8_t value)¶
-
torch::autograd::variable_list
converting_relu_backward
(torch::Tensor const &grad_output, torch::Tensor const &input, int64_t shift)¶
-
template<typename
R
, typenameT
>
Rdefault_transform
(T const &t)¶ Default transformation function from T to R.
-
torch::Tensor
expanded_conv1d
(torch::Tensor const &input, torch::Tensor const &weights, c10::optional<torch::Tensor> const &bias, int64_t stride, int64_t dilation, int64_t num_expansions, int64_t num_sends, int64_t wait_between_events, bool mock)¶
-
std::unordered_set<std::shared_ptr<InferenceTracer>> &
getInferenceTracer
()¶ Get singleton set of registered inference tracers.
-
MockParameter &
getMockParameter
()¶
-
bool
has_tracer
()¶ Check whether inference tracers are registered.
- Returns
Boolean value
-
torch::autograd::variable_list
mac_backward
(torch::Tensor grad_output, torch::Tensor x, torch::Tensor weights)¶
-
torch::Tensor
mac_forward
(torch::Tensor x, torch::Tensor weights, int64_t num_sends, int64_t wait_between_events, int64_t madc_recording_neuron_id, std::string madc_recording_path)¶ Calculate forward-pass of multiply accumulate operation.
Input dimensions supported are 1D or 2D, where in the latter the input plane is the highest dimension and the first dimension describes which input vector to choose. The multiply accumulate therefore multiplies the last input dimension with the first weights dimension like y = x^T W.
- Parameters
x – Input (1D or 2D)
weights – 2D weight matrix
num_sends – How often to send the (same) input vector
madc_recording_neuron_id – Neuron ID to record via MADC
madc_recording_path – Path to which to store MADC neuron membrane recordings. If file exists new data is appended. By default recording is disabled.
- Returns
Resulting tensor
-
torch::Tensor
mac_mock_forward
(torch::Tensor const &x, torch::Tensor const &weights, int64_t num_sends)¶ Mocks the forward-pass of the multiply accumulate operation.
Input dimensions supported are 1D or 2D, where in the latter the input plane is the highest dimension and the first dimension describes which input vector to choose. The multiply accumulate therefore multiplies the last input dimension with the first weights dimension like y = x^T W.
- Parameters
x – Input (1D or 2D)
weights – 2D weight matrix
num_sends – How often to send the (same) input vector
- Returns
Resulting tensor
-
template<typename
T
>
automulti_narrow
(T &t, std::vector<int64_t> dim, std::vector<int64_t> start, std::vector<int64_t> length)¶
-
torch::autograd::variable_list
relu_backward
(torch::Tensor const &grad_output, torch::Tensor const &input)¶
-
void
tracer_add
(std::string const &name, grenade::vx::compute::Sequence::Entry &&op)¶ Add operation to trace.
- Parameters
name – Name to use
op – Operation to add
-
torch::autograd::variable_list
-
namespace
hxtorch
::
spiking
¶ Functions
-
std::map<grenade::vx::network::PopulationDescriptor, CADCHandle>
extract_cadc
(grenade::vx::signal_flow::IODataMap const &data, grenade::vx::network::NetworkGraph const &network_graph, int runtime)¶ Convert recorded CADC samples in IODataMap to population-specific CADCHandles holding the samples in a sparse tensor representation.
- Parameters
data – The IODataMap returned by grenade holding all recorded data.
network_graph – The logical grenade graph representation of the network.
runtime – The runtime of the experiment given in FPGA clock cycles.
- Returns
Returns a mapping between population descriptors and CADC handles.
-
std::map<grenade::vx::network::PopulationDescriptor, MADCHandle>
extract_madc
(grenade::vx::signal_flow::IODataMap const &data, grenade::vx::network::NetworkGraph const &network_graph, int runtime)¶ Convert recorded MADC samples in IODataMap to population-specific MADCHandles holding the samples in a sparse tensor representation.
- Parameters
data – The IODataMap returned by grenade holding all recorded data.
network_graph – The logical grenade graph representation of the network.
runtime – The runtime of the experiment given in FPGA clock cycles.
- Returns
Returns a mapping between population descriptors and MADC handles.
-
std::map<grenade::vx::network::PopulationDescriptor, SpikeHandle>
extract_spikes
(grenade::vx::signal_flow::IODataMap const &data, grenade::vx::network::NetworkGraph const &network_graph, int runtime)¶ Convert recorded spikes in IODataMap to population-specific SpikeHandles holding the spikes in a sparse tensor representation.
- Parameters
data – The IODataMap returned by grenade holding all recorded data.
network_graph – The logical grenade graph representation of the network.
runtime – The runtime of the experiment given in FPGA clock cycles.
- Returns
Returns a mapping between population descriptors and spike handles.
-
grenade::vx::signal_flow::IODataMap
run
(lola::vx::v3::Chip const &config, grenade::vx::network::NetworkGraph const &network_graph, grenade::vx::signal_flow::IODataMap const &inputs, grenade::vx::signal_flow::ExecutionInstancePlaybackHooks &playback_hooks)¶ Strips connection from grenade::vx::network::run for python exposure.
-
std::vector<std::vector<std::vector<float>>>
tensor_to_spike_times
(torch::Tensor times, float dt)¶ Convert a torch tensor of spikes with a dense (but discrete) time representation into spike times.
- Parameters
times – A tensor of shape (batch_size, time_length, population_size) holding spike represemted as ones. Absent spikes are represented by zeros.
dt – The temporal resolution of the spike tensor.
- Returns
A vector with the first dimension being the batch dimension and the second dimension the neuron index, holding a list of spike times of the corresonding neuron, i.e. shape (batch, neuron index, spike times).
-
grenade::vx::network::Projection::Connections
weight_to_connection
(torch::Tensor weight)¶ Turns a weight matrix given as a rectangular torch tensor into grenade connections.
Each entry in the weight matrix is translated to a single connection.
- Parameters
weight – Torch tensor holding the weights.
- Returns
All grenade connections given as a vector of connections.
-
std::map<grenade::vx::network::PopulationDescriptor, CADCHandle>
-
namespace
lola
-
namespace
vx
-
namespace
v3
-
namespace
std
STL namespace.
-
namespace
torch
-
namespace
autograd
-
file
connection.h
- #include <optional>#include <string>
-
file
connection.h
- #include <memory>
-
file
constants.h
- #include “grenade/vx/compute/mac.h”#include “grenade/vx/signal_flow/types.h”#include “halco/hicann-dls/vx/v3/synapse.h”#include <limits>
-
file
conv.h
- #include <array>#include <torch/torch.h>#include “hxtorch/perceptron/constants.h”
-
file
conv.h
- #include <array>#include <torch/torch.h>
-
file
add.h
- #include <torch/torch.h>
-
file
add.h
- #include <torch/torch.h>
-
file
argmax.h
- #include <torch/torch.h>
-
file
argmax.h
- #include <torch/torch.h>
-
file
conv1d.h
- #include <torch/torch.h>
-
file
conversion.h
- #include “grenade/vx/compute/mac.h”#include “grenade/vx/signal_flow/types.h”
-
file
iterator.h
- #include <cstdint>#include <vector>
-
file
mac.h
- #include <torch/torch.h>
-
file
mac.h
- #include “hxtorch/perceptron/constants.h”#include <string>#include <torch/torch.h>
-
file
narrow.h
- #include <array>#include <torch/torch.h>
-
file
relu.h
- #include <torch/torch.h>
-
file
relu.h
- #include <torch/torch.h>
-
file
util.h
- #include <type_traits>#include <vector>#include <torch/torch.h>
-
file
docstrings.h
Variables
-
static const char * __doc_hxtorch_CalibrationPath = R"doc(Path to a calibration.)doc"
-
static const char * __doc_hxtorch_CalibrationPath_CalibrationPath = R"doc()doc"
-
static const char * __doc_hxtorch_get_unique_identifier =R"doc(Return the unique identifier of the chip with the initialized connection.@param hwdb_path Optional path to the hwdb to use@return The identifier as string)doc"
-
static const char * __doc_hxtorch_HWDBPath = R"doc(Path to a hardware database.)doc"
-
static const char * __doc_hxtorch_HWDBPath_HWDBPath = R"doc()doc"
-
static const char * __doc_hxtorch_init_hardware =R"doc(Initialize the hardware automatically from the environment.@param hwdb_path Optional path to the hwdb to use@param spiking Boolean flag indicating whether spiking or non-spiking calibration is loaded)doc"
-
static const char * __doc_hxtorch_init_hardware_2 =R"doc(Initialize the hardware with calibration path.@param calibration_path Calibration path to load from)doc"
-
static const char * __doc_hxtorch_init_hardware_minimal =R"doc(Initialize automatically from the environmentwithout ExperimentInit and without any calibration.)doc"
-
static const char * __doc_hxtorch_release_hardware = R"doc(Release hardware resource.)doc"
-
-
file
docstrings.h
Variables
-
static const char * __doc_hxtorch_add =R"doc(Elementwise addition operating on int8 value range.@param input Input tensor@param other Other tensor, which must be broadcastable to input tensor dimension@param alpha The scalar multiplier for other@param mock Enable mock mode)doc"
-
static const char * __doc_hxtorch_argmax =R"doc(Arg max operation on int8 value range.@param input The input tensor@param dim The dimension to reduce. If unspecified, the argmax of the flattenedinput is returned.@param keepdim Whether the output tensor has @p dim retained or not. Ignoredif @p dim is unspecified.@param mock Enable mock mode@return The indices of the maximum values of a tensor across a dimension)doc"
-
static const char * __doc_hxtorch_conv1d = R"doc()doc"
-
static const char * __doc_hxtorch_conv1d_2 = R"doc()doc"
-
static const char * __doc_hxtorch_conv2d = R"doc()doc"
-
static const char * __doc_hxtorch_conv2d_2 = R"doc()doc"
-
static const char * __doc_hxtorch_converting_relu =R"doc(Rectified linear unit operating on int8 value range converting to uint5value range.The result is bit-shifted by @p shift after applying the ReLU and clippedto the input range of BrainScaleS-2.@param input Input tensor@param shift Amount of bits to shift before clipping@param mock Enable mock mode)doc"
-
static const char * __doc_hxtorch_expanded_conv1d =R"doc(1D convolution operation that unrolls the weight matrix for executionon hardware. This maximizes the use of the synapses array.@noteFixed-pattern noise cannot be individually compensated for duringtraining, because the same weights are used at different locations!@param input Input tensor of shape (minibatch, in_channels, *iW*)@param weight Filters of shape (out_channels, in_channels / groups, *kW*)@param bias Optional bias of shape (out_channels)@param stride Stride of the convolving kernel@param num_expansions Number of enrolled kernels that will be placed sideby side in a single operation@param num_sends How often to send the (same) input vector@param wait_between_events How long to wait (in FPGA cycles) between events@param mock Enable mock mode)doc"
-
static const char * __doc_hxtorch_expanded_conv1d_2 = R"doc()doc"
-
static const char * __doc_hxtorch_get_mock_parameter =R"doc(Returns the current mock parameters.)doc"
-
static const char * __doc_hxtorch_inference_trace =R"doc(Execute inference of stored trace.@param input Input data to use@param filename Filename to serialized operation trace)doc"
-
static const char * __doc_hxtorch_InferenceTracer =R"doc(Inference tracer for a linear sequence of operations.The traced operations' state is saved as a grenade::compute::Sequence,which can be executed as a single operation without transformation to andfrom PyTorch tensors.It is ensured, that no untraced modifications are made in-between tracedoperations by comparing the last traced operation's output with thecurrently traced operation's input value.@noteNot final API or implementation, see Issue #3694)doc"
-
static const char * __doc_hxtorch_InferenceTracer_InferenceTracer =R"doc(Construct inference tracer with filename to store traced operations to.)doc"
-
static const char * __doc_hxtorch_InferenceTracer_start =R"doc(Start tracing operations by registering tracer.)doc"
-
static const char * __doc_hxtorch_InferenceTracer_stop =R"doc(Stop tracing operations by deregistering tracer and save tracedoperations to given file.@return List of traced operation names)doc"
-
static const char * __doc_hxtorch_mac =R"doc(The bare mutliply-accumulate operation of BrainScaleS-2. A 1D input @p xis multiplied by the weight matrix @p weights. If @p x is two-dimensional,the weights are sent only once to the synapse array and the inputs areconsecutively multiplied as a 1D vector.@param x Input tensor@param weights The weights of the synapse array@param num_sends How often to send the (same) input vector@param wait_between_events How long to wait (in FPGA cycles) between events@param mock Enable mock mode@return Resulting tensor)doc"
-
static const char * __doc_hxtorch_matmul =R"doc(Drop-in replacement for the torch.matmul operation that uses BrainScaleS-2.@noteThe current implementation only supports @p other to be 1D or 2D.@param input First input tensor@param other Second input tensor@param num_sends How often to send the (same) input vector@param wait_between_events How long to wait (in FPGA cycles) between events@param mock: Enable mock mode@return Resulting tensor)doc"
-
static const char * __doc_hxtorch_measure_mock_parameter =R"doc(Measures the mock parameters, i.e. gain and noise_std, by multiplying afull weight with an artificial test input on the BSS-2 chip.For this purpose a random pattern is used, whose mean value is successivelyreduced to also work with higher gain factors.The output for the actual calibration is chosen such that it is close tothe middle of the available range.)doc"
-
static const char * __doc_hxtorch_MockParameter = R"doc(Parameter of hardware mock.)doc"
-
static const char * __doc_hxtorch_MockParameter_MockParameter =R"doc(Construct with noise standard deviation and gain.@param noise_std Noise standard deviation to use@param gain Gain to use)doc"
-
static const char * __doc_hxtorch_relu =R"doc(Rectified linear unit operating on int8 value range.@param input Input tensor@param mock Enable mock mode)doc"
-
static const char * __doc_hxtorch_set_mock_parameter = R"doc(Sets the mock parameters.)doc"
-
-
file
docstrings.h
-
file
inference_tracer.h
- #include <memory>#include <optional>#include <string>#include <unordered_set>#include <vector>#include <torch/torch.h>#include “grenade/vx/compute/sequence.h”
-
file
inference_tracer.h
- #include <string>#include <torch/torch.h>
-
file
matmul.h
- #include <torch/torch.h>#include “hxtorch/perceptron/constants.h”
-
file
mock.h
- #include “hxtorch/perceptron/mock.h”
-
file
mock.h
- #include “hxtorch/perceptron/constants.h”
-
file
to_dense.h
- #include <torch/torch.h>
-
file
extract_tensors.h
- #include “grenade/vx/network/network_graph.h”#include “grenade/vx/network/population.h”#include “hxtorch/spiking/types.h”#include <map>#include <torch/torch.h>
-
file
run.h
- #include “grenade/vx/network/network_graph.h”#include “grenade/vx/signal_flow/execution_instance_playback_hooks.h”#include “grenade/vx/signal_flow/io_data_map.h”#include “lola/vx/v3/chip.h”
-
file
tensor_to_spike_times.h
- #include <vector>#include <torch/torch.h>
-
file
types.h
- #include <string>#include <tuple>#include <ATen/SparseTensorUtils.h>#include <torch/torch.h>
-
file
weight_to_connection.h
- #include “grenade/vx/network/projection.h”#include <torch/torch.h>
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/include/hxtorch/core
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/include/hxtorch/core/detail
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/include/hxtorch/perceptron/detail
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/include/hxtorch/spiking/detail
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/include/hxtorch
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/include
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/include/hxtorch/perceptron
-
dir
/jenkins/jenlib_workspaces_f9/doc_gerrit_documentation-brainscales2.ZG9jX2dlcnJpdF9kb2N1bWVudGF0aW9uLWJyYWluc2NhbGVzMiMzODM.x/hxtorch/include/hxtorch/spiking