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

API Language: 中文 | English


  • 中文

适配灵汐(Lynxi)芯片的脉冲神经元基类。 与标准的 spikingjelly.activation_based.neuron.BaseNode 不同, 该类需要显式指定时间步数 T,以满足灵汐编译器对静态计算图的要求。 不支持inplace操作,且模型中任何位置均不得出现超过4D的tensor。

神经元动力学分为三步:

  1. 充电(由子类实现的 neuronal_charge);

  2. 放电:\(\text{spike} = \Theta(v - v_{\text{threshold}})\),其中 \(\Theta\) 为 Heaviside 阶跃函数;

  3. 重置:

    • 硬重置(v_resetNone): \(v = (1 - \text{spike}) \cdot v + \text{spike} \cdot v_{\text{reset}}\)

    • 软重置(v_resetNone): \(v = v - \text{spike} \cdot v_{\text{threshold}}\)

参数:
  • v_threshold (float) -- 神经元膜电位的发放阈值

  • v_reset (float) -- 神经元的硬重置电位。若为 None,则采用软重置

  • step_mode (str) -- 步进模式,'s' 为单步模式,'m' 为多步模式

  • T (int) -- 仿真时间步数。多步模式下必须指定

  • return_v (bool) -- 若为 Trueforward 同时返回膜电位 v;否则只返回脉冲


  • 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 steps T to 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:

  1. Charge (neuronal_charge, implemented by subclasses);

  2. Fire: \(\text{spike} = \Theta(v - v_{\text{threshold}})\), where \(\Theta\) is the Heaviside step function;

  3. Reset:

    • Hard reset (v_reset is not None): \(v = (1 - \text{spike}) \cdot v + \text{spike} \cdot v_{\text{reset}}\)

    • Soft reset (v_reset is None): \(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 instead

  • step_mode (str) -- Step mode. 's' for single-step, 'm' for multi-step

  • T (int) -- Number of simulation time steps. Required for multi-step mode

  • return_v (bool) -- If True, forward returns (spike, v); otherwise only spike

neuronal_charge(x: Tensor, v: Tensor)[源代码]#
single_step_forward(x: Tensor, v: Tensor = None)[源代码]#
multi_step_forward(x_seq: Tensor, v_init: Tensor = None)[源代码]#
forward(x: Tensor, v: Tensor = None)[源代码]#
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

API Language: 中文 | English


  • 中文

适配灵汐芯片的积分放电(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 .

API Language: 中文 | English


  • 中文

适配灵汐(Lynxi)芯片的脉冲神经元基类。 与标准的 spikingjelly.activation_based.neuron.BaseNode 不同, 该类需要显式指定时间步数 T,以满足灵汐编译器对静态计算图的要求。 不支持inplace操作,且模型中任何位置均不得出现超过4D的tensor。

神经元动力学分为三步:

  1. 充电(由子类实现的 neuronal_charge);

  2. 放电:\(\text{spike} = \Theta(v - v_{\text{threshold}})\),其中 \(\Theta\) 为 Heaviside 阶跃函数;

  3. 重置:

    • 硬重置(v_resetNone): \(v = (1 - \text{spike}) \cdot v + \text{spike} \cdot v_{\text{reset}}\)

    • 软重置(v_resetNone): \(v = v - \text{spike} \cdot v_{\text{threshold}}\)

参数:
  • v_threshold (float) -- 神经元膜电位的发放阈值

  • v_reset (float) -- 神经元的硬重置电位。若为 None,则采用软重置

  • step_mode (str) -- 步进模式,'s' 为单步模式,'m' 为多步模式

  • T (int) -- 仿真时间步数。多步模式下必须指定

  • return_v (bool) -- 若为 Trueforward 同时返回膜电位 v;否则只返回脉冲


  • 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 steps T to 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:

  1. Charge (neuronal_charge, implemented by subclasses);

  2. Fire: \(\text{spike} = \Theta(v - v_{\text{threshold}})\), where \(\Theta\) is the Heaviside step function;

  3. Reset:

    • Hard reset (v_reset is not None): \(v = (1 - \text{spike}) \cdot v + \text{spike} \cdot v_{\text{reset}}\)

    • Soft reset (v_reset is None): \(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 instead

  • step_mode (str) -- Step mode. 's' for single-step, 'm' for multi-step

  • T (int) -- Number of simulation time steps. Required for multi-step mode

  • return_v (bool) -- If True, forward returns (spike, v); otherwise only spike

neuronal_charge(x: Tensor, v: Tensor)[源代码]#
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

API Language: 中文 | English


  • 中文

适配灵汐芯片的泄漏积分放电(Leaky Integrate-and-Fire,LIF)神经元。继承自 BaseNode ,充电方程取决于重置类型和 decay_input

硬重置( v_resetNone ):

\[\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_resetNone ):

\[\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) -- 膜电位时间常数,控制泄漏速率,\(\tau > 1\)

  • decay_input (bool) -- 若为 True,输入电流也会按 1/tau 衰减

  • v_threshold (float) -- 神经元膜电位的发放阈值

  • v_reset (float) -- 神经元的硬重置电位。若为 None,则采用软重置

  • step_mode (str) -- 步进模式,'s' 为单步模式,'m' 为多步模式

  • T (int) -- 仿真时间步数。多步模式下必须指定

  • return_v (bool) -- 若为 Trueforward 同时返回膜电位 v;否则只返回脉冲


  • English

Lynxi-compatible Leaky Integrate-and-Fire (LIF) neuron. Inherits from BaseNode. The charge equation depends on the reset type and decay_input:

Hard reset (v_reset is not 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}\]

Soft reset (v_reset is 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}\]
参数:
  • tau (float) -- Membrane time constant controlling the leak rate, \(\tau > 1\)

  • decay_input (bool) -- If True, the input current is also scaled by 1/tau

  • v_threshold (float) -- Firing threshold of the membrane potential

  • v_reset (float) -- Hard-reset potential. If None, soft reset is used instead

  • step_mode (str) -- Step mode. 's' for single-step, 'm' for multi-step

  • T (int) -- Number of simulation time steps. Required for multi-step mode

  • return_v (bool) -- If True, forward returns (spike, v); otherwise only spike

neuronal_charge(x: Tensor, v: Tensor)[源代码]#
spikingjelly.activation_based.lynxi_exchange.to_lynxi_supported_module(m_in: Module, T: int)[源代码]#

API Language: 中文 | English


  • 中文

将单个 SpikingJelly 步进模块转换为灵汐芯片兼容的标准 PyTorch 模块。

转换规则如下:

  • spikingjelly.activation_based.layer.Conv2dtorch.nn.Conv2d`(权重通过 ``load_state_dict` 复制)

  • spikingjelly.activation_based.layer.BatchNorm2dtorch.nn.BatchNorm2d

  • spikingjelly.activation_based.layer.MaxPool2dtorch.nn.MaxPool2d

  • spikingjelly.activation_based.layer.AvgPool2dtorch.nn.AvgPool2d

  • spikingjelly.activation_based.layer.AdaptiveAvgPool2dtorch.nn.AdaptiveAvgPool2d

  • spikingjelly.activation_based.layer.Flattentorch.nn.Flatten

  • spikingjelly.activation_based.neuron.IFNode → :class:`IFNode`(本模块内)

  • spikingjelly.activation_based.neuron.LIFNode → :class:`LIFNode`(本模块内)

  • 其他类型:记录 critical 日志,返回深拷贝的原模块(移至 CPU)

参数:
  • m_in (torch.nn.Module) -- 待转换的输入模块

  • T (int) -- 仿真时间步数,传递给神经元节点

返回:

转换后的灵汐兼容模块

返回类型:

torch.nn.Module


  • English

Convert a single SpikingJelly step module to a Lynxi-chip-compatible standard PyTorch module.

Conversion rules:

  • spikingjelly.activation_based.layer.Conv2dtorch.nn.Conv2d (weights copied via load_state_dict)

  • spikingjelly.activation_based.layer.BatchNorm2dtorch.nn.BatchNorm2d

  • spikingjelly.activation_based.layer.MaxPool2dtorch.nn.MaxPool2d

  • spikingjelly.activation_based.layer.AvgPool2dtorch.nn.AvgPool2d

  • spikingjelly.activation_based.layer.AdaptiveAvgPool2dtorch.nn.AdaptiveAvgPool2d

  • spikingjelly.activation_based.layer.Flattentorch.nn.Flatten

  • spikingjelly.activation_based.neuron.IFNodeIFNode (this module)

  • spikingjelly.activation_based.neuron.LIFNodeLIFNode (this module)

  • Other types: log a critical message 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

返回类型:

torch.nn.Module

spikingjelly.activation_based.lynxi_exchange.to_lynxi_supported_modules(net: list | tuple | Sequential, T: int)[源代码]#

API Language: 中文 | English


  • 中文

将 SpikingJelly 模块序列中的每个模块依次通过 to_lynxi_supported_module() 转换为灵汐兼容模块,返回转换后模块的列表。

参数:
  • net (list or tuple or torch.nn.Sequential) -- 待转换的模块序列,可为 listtupletorch.nn.Sequential

  • T (int) -- 仿真时间步数,传递给各神经元节点

返回:

转换后的灵汐兼容模块列表

返回类型:

list


  • 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, or torch.nn.Sequential

  • T (int) -- Number of simulation time steps, forwarded to each neuron node

返回:

List of Lynxi-compatible modules

返回类型:

list

spikingjelly.activation_based.lynxi_exchange.torch_tensor_to_lynxi(x: Tensor, device_id: int = 0, to_apu: bool = True)[源代码]#

API Language: 中文 | English


  • 中文

将 PyTorch tensor 转换为灵汐(Lynxi) lynpy.Tensor,并可选地将其搬运至 APU 设备显存。

转换流程:

  1. 计算 tensor 占用的字节数;

  2. 将 tensor 搬至 CPU 并转为 NumPy 数组;

  3. 构造 lynpy.Tensor 并从 NumPy 数组初始化;

  4. 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:

  1. Compute the byte size of the tensor;

  2. Move the tensor to CPU and convert to a NumPy array;

  3. Construct a lynpy.Tensor and initialise it from the NumPy array;

  4. 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)[源代码]#

API Language: 中文 | English


  • 中文

将灵汐(Lynxi) lynpy.Tensor 转换回 PyTorch tensor。

转换流程:

  1. 若同时提供 shapedtype,先调用 view_as 重新解释内存布局;

  2. 若 tensor 仍在 APU 设备(devptrNone),调用 .cpu() 将数据 搬回主机内存;

  3. 调用 .numpy() 取得 NumPy 数组,再通过 torch.from_numpy 转为 PyTorch tensor。

参数:
  • x (lynpy.Tensor) -- 待转换的灵汐 tensor

  • shape (tuple or list, optional) -- 目标形状。需与 dtype 同时提供

  • dtype (str, optional) -- 目标数据类型字符串(如 'float32')。需与 shape 同时提供

返回:

转换后的 PyTorch tensor(位于 CPU)

返回类型:

torch.Tensor


  • English

Convert a Lynxi lynpy.Tensor back to a PyTorch tensor.

Conversion steps:

  1. If both shape and dtype are provided, call view_as to reinterpret the memory layout;

  2. If the tensor is still on the APU device (devptr is not None), call .cpu() to transfer data back to host memory;

  3. Call .numpy() to obtain a NumPy array, then convert to a PyTorch tensor via torch.from_numpy.

参数:
  • x (lynpy.Tensor) -- Lynxi tensor to convert

  • shape (tuple or list, optional) -- Target shape. Must be provided together with dtype

  • dtype (str, optional) -- Target dtype string (e.g. 'float32'). Must be provided together with shape

返回:

PyTorch tensor on CPU

返回类型:

torch.Tensor

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 = {})[源代码]#

API Language: 中文 | English


  • 中文

使用 lyngor 将 PyTorch 模型编译为灵汐 APU 离线模型,并将编译产物保存至 指定目录。

编译流程:

  1. 创建 lyngor.DLModel 并以 Pytorch 格式加载模型;

  2. 创建 lyngor.Builder 并以 APU 为目标执行离线编译;

  3. 打印输出目录内容并返回主网络路径。

备注

传入的 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

返回类型:

str


  • English

Compile a PyTorch model into a Lynxi APU offline model using lyngor, and save the compilation artifacts to the specified directory.

Compilation steps:

  1. Create a lyngor.DLModel and load the model in Pytorch format;

  2. Create a lyngor.Builder targeting the APU and run offline compilation;

  3. Print the output directory contents and return the main network path.

备注

The net passed in should have been converted to Lynxi-compatible modules via to_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)

返回类型:

str

spikingjelly.activation_based.lynxi_exchange.load_lynxi_model(device_id: int, model_path: str)[源代码]#

API Language: 中文 | English


  • 中文

从磁盘加载已编译的灵汐离线模型,返回可用于推理的 lynpy.Model 实例。

模型路径通常由 compile_lynxi_model() 返回的路径(Net_0 目录)给出。

参数:
  • device_id (int) -- 目标灵汐设备 ID

  • model_path (str) -- 已编译模型所在目录路径(通常为 Net_0

返回:

可用于推理的灵汐模型实例

返回类型:

lynpy.Model


  • English

Load a compiled Lynxi offline model from disk and return a lynpy.Model instance ready for inference.

The model path is typically the Net_0 directory returned by compile_lynxi_model().

参数:
  • device_id (int) -- Target Lynxi device ID

  • model_path (str) -- Path to the compiled model directory (usually Net_0)

返回:

Lynxi model instance ready for inference

返回类型:

lynpy.Model