spikingjelly.activation_based.lynxi_exchange module#
- class spikingjelly.activation_based.lynxi_exchange.BaseNode(v_threshold: float = 1.0, v_reset: float = 0.0, step_mode='s', T: int = None, return_v: bool = False)[源代码]#
基类:
Module
中文
适配灵汐(Lynxi)芯片的脉冲神经元基类。 与标准的
spikingjelly.activation_based.neuron.BaseNode不同, 该类需要显式指定时间步数T,以满足灵汐编译器对静态计算图的要求。 不支持inplace操作,且模型中任何位置均不得出现超过4D的tensor。神经元动力学分为三步:
充电(由子类实现的
neuronal_charge);放电:\(\text{spike} = \Theta(v - v_{\text{threshold}})\),其中 \(\Theta\) 为 Heaviside 阶跃函数;
重置:
硬重置(
v_reset非None): \(v = (1 - \text{spike}) \cdot v + \text{spike} \cdot v_{\text{reset}}\)软重置(
v_reset为None): \(v = v - \text{spike} \cdot v_{\text{threshold}}\)
- 参数:
English
Base class for Lynxi-chip-compatible spiking neurons. Unlike the standard
spikingjelly.activation_based.neuron.BaseNode, this class requires an explicit number of time stepsTto satisfy the static computation-graph requirements of the Lynxi compiler. Inplace operations are not supported, and tensors with more than 4 dimensions must not appear anywhere in the model.Neuron dynamics follow three steps:
Charge (
neuronal_charge, implemented by subclasses);Fire: \(\text{spike} = \Theta(v - v_{\text{threshold}})\), where \(\Theta\) is the Heaviside step function;
Reset:
Hard reset (
v_resetis notNone): \(v = (1 - \text{spike}) \cdot v + \text{spike} \cdot v_{\text{reset}}\)Soft reset (
v_resetisNone): \(v = v - \text{spike} \cdot v_{\text{threshold}}\)
- 参数:
v_threshold (float) -- Firing threshold of the membrane potential
v_reset (float) -- Hard-reset potential. If
None, soft reset is used insteadstep_mode (str) -- Step mode.
's'for single-step,'m'for multi-stepT (int) -- Number of simulation time steps. Required for multi-step mode
return_v (bool) -- If
True,forwardreturns(spike, v); otherwise onlyspike
- class spikingjelly.activation_based.lynxi_exchange.IFNode(v_threshold: float = 1.0, v_reset: float = 0.0, step_mode='s', T: int = None, return_v: bool = False)[源代码]#
基类:
BaseNode
中文
适配灵汐芯片的积分放电(Integrate-and-Fire,IF)神经元。 继承自
BaseNode,充电方程为:\[v[t] = v[t-1] + x[t]\]构造参数请参见
BaseNode。
English
Lynxi-compatible Integrate-and-Fire (IF) neuron. Inherits from
BaseNode. The charge equation is:\[v[t] = v[t-1] + x[t]\]For constructor parameters, see
BaseNode.
中文
适配灵汐(Lynxi)芯片的脉冲神经元基类。 与标准的
spikingjelly.activation_based.neuron.BaseNode不同, 该类需要显式指定时间步数T,以满足灵汐编译器对静态计算图的要求。 不支持inplace操作,且模型中任何位置均不得出现超过4D的tensor。神经元动力学分为三步:
充电(由子类实现的
neuronal_charge);放电:\(\text{spike} = \Theta(v - v_{\text{threshold}})\),其中 \(\Theta\) 为 Heaviside 阶跃函数;
重置:
硬重置(
v_reset非None): \(v = (1 - \text{spike}) \cdot v + \text{spike} \cdot v_{\text{reset}}\)软重置(
v_reset为None): \(v = v - \text{spike} \cdot v_{\text{threshold}}\)
- 参数:
English
Base class for Lynxi-chip-compatible spiking neurons. Unlike the standard
spikingjelly.activation_based.neuron.BaseNode, this class requires an explicit number of time stepsTto satisfy the static computation-graph requirements of the Lynxi compiler. Inplace operations are not supported, and tensors with more than 4 dimensions must not appear anywhere in the model.Neuron dynamics follow three steps:
Charge (
neuronal_charge, implemented by subclasses);Fire: \(\text{spike} = \Theta(v - v_{\text{threshold}})\), where \(\Theta\) is the Heaviside step function;
Reset:
Hard reset (
v_resetis notNone): \(v = (1 - \text{spike}) \cdot v + \text{spike} \cdot v_{\text{reset}}\)Soft reset (
v_resetisNone): \(v = v - \text{spike} \cdot v_{\text{threshold}}\)
- 参数:
v_threshold (float) -- Firing threshold of the membrane potential
v_reset (float) -- Hard-reset potential. If
None, soft reset is used insteadstep_mode (str) -- Step mode.
's'for single-step,'m'for multi-stepT (int) -- Number of simulation time steps. Required for multi-step mode
return_v (bool) -- If
True,forwardreturns(spike, v); otherwise onlyspike
- class spikingjelly.activation_based.lynxi_exchange.LIFNode(tau: float = 2.0, decay_input: bool = True, v_threshold: float = 1.0, v_reset: float = 0.0, step_mode='s', T: int = None, return_v: bool = False)[源代码]#
基类:
BaseNode
中文
适配灵汐芯片的泄漏积分放电(Leaky Integrate-and-Fire,LIF)神经元。继承自
BaseNode,充电方程取决于重置类型和decay_input:硬重置(
v_reset非None):\[\begin{split}v[t] = \left(1 - \frac{1}{\tau}\right)(v[t-1] - v_{\text{reset}}) + \begin{cases} \dfrac{x[t]}{\tau} & \text{if decay\_input} \\ x[t] & \text{otherwise} \end{cases}\end{split}\]软重置(
v_reset为None):\[\begin{split}v[t] = \left(1 - \frac{1}{\tau}\right) v[t-1] + \begin{cases} \dfrac{x[t]}{\tau} & \text{if decay\_input} \\ x[t] & \text{otherwise} \end{cases}\end{split}\]- 参数:
English
Lynxi-compatible Leaky Integrate-and-Fire (LIF) neuron. Inherits from
BaseNode. The charge equation depends on the reset type anddecay_input:Hard reset (
v_resetis notNone):\[\begin{split}v[t] = \left(1 - \frac{1}{\tau}\right)(v[t-1] - v_{\text{reset}}) + \begin{cases} \dfrac{x[t]}{\tau} & \text{if decay\_input} \\ x[t] & \text{otherwise} \end{cases}\end{split}\]Soft reset (
v_resetisNone):\[\begin{split}v[t] = \left(1 - \frac{1}{\tau}\right) v[t-1] + \begin{cases} \dfrac{x[t]}{\tau} & \text{if decay\_input} \\ x[t] & \text{otherwise} \end{cases}\end{split}\]- 参数:
tau (float) -- Membrane time constant controlling the leak rate, \(\tau > 1\)
decay_input (bool) -- If
True, the input current is also scaled by1/tauv_threshold (float) -- Firing threshold of the membrane potential
v_reset (float) -- Hard-reset potential. If
None, soft reset is used insteadstep_mode (str) -- Step mode.
's'for single-step,'m'for multi-stepT (int) -- Number of simulation time steps. Required for multi-step mode
return_v (bool) -- If
True,forwardreturns(spike, v); otherwise onlyspike
- spikingjelly.activation_based.lynxi_exchange.to_lynxi_supported_module(m_in: Module, T: int)[源代码]#
-
中文
将单个 SpikingJelly 步进模块转换为灵汐芯片兼容的标准 PyTorch 模块。
转换规则如下:
spikingjelly.activation_based.layer.Conv2d→torch.nn.Conv2d`(权重通过 ``load_state_dict`复制)spikingjelly.activation_based.layer.BatchNorm2d→torch.nn.BatchNorm2dspikingjelly.activation_based.layer.MaxPool2d→torch.nn.MaxPool2dspikingjelly.activation_based.layer.AvgPool2d→torch.nn.AvgPool2dspikingjelly.activation_based.layer.AdaptiveAvgPool2d→torch.nn.AdaptiveAvgPool2dspikingjelly.activation_based.layer.Flatten→torch.nn.Flattenspikingjelly.activation_based.neuron.IFNode→ :class:`IFNode`(本模块内)spikingjelly.activation_based.neuron.LIFNode→ :class:`LIFNode`(本模块内)其他类型:记录
critical日志,返回深拷贝的原模块(移至 CPU)
- 参数:
m_in (torch.nn.Module) -- 待转换的输入模块
T (int) -- 仿真时间步数,传递给神经元节点
- 返回:
转换后的灵汐兼容模块
- 返回类型:
English
Convert a single SpikingJelly step module to a Lynxi-chip-compatible standard PyTorch module.
Conversion rules:
spikingjelly.activation_based.layer.Conv2d→torch.nn.Conv2d(weights copied viaload_state_dict)spikingjelly.activation_based.layer.BatchNorm2d→torch.nn.BatchNorm2dspikingjelly.activation_based.layer.MaxPool2d→torch.nn.MaxPool2dspikingjelly.activation_based.layer.AvgPool2d→torch.nn.AvgPool2dspikingjelly.activation_based.layer.AdaptiveAvgPool2d→torch.nn.AdaptiveAvgPool2dspikingjelly.activation_based.layer.Flatten→torch.nn.Flattenspikingjelly.activation_based.neuron.IFNode→IFNode(this module)spikingjelly.activation_based.neuron.LIFNode→LIFNode(this module)Other types: log a
criticalmessage and return a deep-copied CPU version of the original module
- 参数:
m_in (torch.nn.Module) -- Input module to convert
T (int) -- Number of simulation time steps, forwarded to neuron nodes
- 返回:
Lynxi-compatible module
- 返回类型:
- spikingjelly.activation_based.lynxi_exchange.to_lynxi_supported_modules(net: list | tuple | Sequential, T: int)[源代码]#
-
中文
将 SpikingJelly 模块序列中的每个模块依次通过
to_lynxi_supported_module()转换为灵汐兼容模块,返回转换后模块的列表。- 参数:
net (list or tuple or torch.nn.Sequential) -- 待转换的模块序列,可为
list、tuple或torch.nn.SequentialT (int) -- 仿真时间步数,传递给各神经元节点
- 返回:
转换后的灵汐兼容模块列表
- 返回类型:
English
Convert every module in a SpikingJelly module sequence to a Lynxi-compatible module by calling
to_lynxi_supported_module()on each element, and return the results as a list.- 参数:
net (list or tuple or torch.nn.Sequential) -- Module sequence to convert. Can be a
list,tuple, ortorch.nn.SequentialT (int) -- Number of simulation time steps, forwarded to each neuron node
- 返回:
List of Lynxi-compatible modules
- 返回类型:
- spikingjelly.activation_based.lynxi_exchange.torch_tensor_to_lynxi(x: Tensor, device_id: int = 0, to_apu: bool = True)[源代码]#
-
中文
将 PyTorch tensor 转换为灵汐(Lynxi)
lynpy.Tensor,并可选地将其搬运至 APU 设备显存。转换流程:
计算 tensor 占用的字节数;
将 tensor 搬至 CPU 并转为 NumPy 数组;
构造
lynpy.Tensor并从 NumPy 数组初始化;若
to_apu=True,调用.apu()将数据搬运至 APU 设备。
- 参数:
x (torch.Tensor) -- 待转换的 PyTorch tensor
device_id (int) -- 目标灵汐设备 ID
to_apu (bool) -- 若为
True,在构造后立即将 tensor 搬运至 APU 设备
- 返回:
灵汐设备上的 tensor
- 返回类型:
lynpy.Tensor
English
Convert a PyTorch tensor to a Lynxi
lynpy.Tensor, optionally moving it to APU device memory.Conversion steps:
Compute the byte size of the tensor;
Move the tensor to CPU and convert to a NumPy array;
Construct a
lynpy.Tensorand initialise it from the NumPy array;If
to_apu=True, call.apu()to transfer data to the APU device.
- 参数:
x (torch.Tensor) -- PyTorch tensor to convert
device_id (int) -- Target Lynxi device ID
to_apu (bool) -- If
True, move the tensor to the APU device after construction
- 返回:
Tensor on the Lynxi device
- 返回类型:
lynpy.Tensor
- spikingjelly.activation_based.lynxi_exchange.lynxi_tensor_to_torch(x: lynpy.Tensor, shape: tuple | list = None, dtype: str = None)[源代码]#
-
中文
将灵汐(Lynxi)
lynpy.Tensor转换回 PyTorch tensor。转换流程:
若同时提供
shape和dtype,先调用view_as重新解释内存布局;若 tensor 仍在 APU 设备(
devptr非None),调用.cpu()将数据 搬回主机内存;调用
.numpy()取得 NumPy 数组,再通过torch.from_numpy转为 PyTorch tensor。
- 参数:
- 返回:
转换后的 PyTorch tensor(位于 CPU)
- 返回类型:
English
Convert a Lynxi
lynpy.Tensorback to a PyTorch tensor.Conversion steps:
If both
shapeanddtypeare provided, callview_asto reinterpret the memory layout;If the tensor is still on the APU device (
devptris notNone), call.cpu()to transfer data back to host memory;Call
.numpy()to obtain a NumPy array, then convert to a PyTorch tensor viatorch.from_numpy.
- 参数:
- 返回:
PyTorch tensor on CPU
- 返回类型:
- spikingjelly.activation_based.lynxi_exchange.compile_lynxi_model(output_dir: str, net: Module, in_data_type: str = 'float32', out_data_type: str = 'float32', input_shape_dict: Dict = {})[源代码]#
-
中文
使用
lyngor将 PyTorch 模型编译为灵汐 APU 离线模型,并将编译产物保存至 指定目录。编译流程:
创建
lyngor.DLModel并以 Pytorch 格式加载模型;创建
lyngor.Builder并以 APU 为目标执行离线编译;打印输出目录内容并返回主网络路径。
备注
传入的
net应已通过to_lynxi_supported_modules()转换为 灵汐兼容模块,且其中不得出现超过4D的tensor或inplace操作。- 参数:
output_dir (str) -- 编译产物的输出目录路径
net (torch.nn.Module) -- 待编译的 PyTorch 模型
in_data_type (str) -- 模型输入数据类型,默认
'float32'out_data_type (str) -- 模型输出数据类型,默认
'float32'input_shape_dict (dict) -- 输入张量形状字典,键为输入名,值为形状
- 返回:
编译后主网络的路径(
output_dir/Net_0)- 返回类型:
English
Compile a PyTorch model into a Lynxi APU offline model using
lyngor, and save the compilation artifacts to the specified directory.Compilation steps:
Create a
lyngor.DLModeland load the model in Pytorch format;Create a
lyngor.Buildertargeting the APU and run offline compilation;Print the output directory contents and return the main network path.
备注
The
netpassed in should have been converted to Lynxi-compatible modules viato_lynxi_supported_modules(). Tensors with more than 4 dimensions and inplace operations are not allowed.- 参数:
output_dir (str) -- Output directory path for compilation artifacts
net (torch.nn.Module) -- PyTorch model to compile
in_data_type (str) -- Input data type for the model, default
'float32'out_data_type (str) -- Output data type for the model, default
'float32'input_shape_dict (dict) -- Dict mapping input names to their shapes
- 返回:
Path to the compiled main network (
output_dir/Net_0)- 返回类型:
- spikingjelly.activation_based.lynxi_exchange.load_lynxi_model(device_id: int, model_path: str)[源代码]#
-
中文
从磁盘加载已编译的灵汐离线模型,返回可用于推理的
lynpy.Model实例。模型路径通常由
compile_lynxi_model()返回的路径(Net_0目录)给出。- 参数:
- 返回:
可用于推理的灵汐模型实例
- 返回类型:
lynpy.Model
English
Load a compiled Lynxi offline model from disk and return a
lynpy.Modelinstance ready for inference.The model path is typically the
Net_0directory returned bycompile_lynxi_model().