spikingjelly.activation_based.op_counter package#
Base Classes and Context Managers#
- class spikingjelly.activation_based.op_counter.base.ActiveModuleTracker[源代码]#
-
中文
中文
模块追踪器,用于在 PyTorch 的前向和反向传播过程中追踪模块的调用层次结构。 它通过在模块的前向和反向钩子上进行回调来记录当前活跃的模块
active_modules。active_modules和parents的区别在于:前者是nn.Module的集合, 后者是str(模块名)的集合。
English
English
Module tracker that tracks the module call hierarchy during PyTorch forward and backward passes. It records the currently executing module instances to
active_modulesthrough callbacks on module forward and backward hooks.Attributes
active_modulesandparentsare different: the former is a set ofnn.Moduleinstances, while the latter is a set ofstr(module names).- 返回:
None
- 返回类型:
None
- class spikingjelly.activation_based.op_counter.base.BaseCounter[源代码]#
基类:
object
中文
中文
操作计数器的基类。所有具体的计数器实现都继承自此类。
该基类提供了计数器的核心属性:
records: 存储计数记录,结构为dict[scope][operation] = countrules: 定义如何计算各个操作的计数的函数ignore_modules: 需要忽略的模块列表,这些模块中的操作不会被计数
子类需要实现具体的规则
rules来定义如何计算特定操作的计数。
English
English
Base class for operation counters. All concrete counter implementations inherit from this class.
This base class provides core attributes for counters:
records: stores count records, structured asdict[scope][operation] = countrules: functions that define how to calculate counts for each operationignore_modules: list of modules to ignore. Operations within these modules will not be counted
Subclasses need to implement specific rule functions in
rulesto define how to calculate counts for particular operations.- 返回:
None
- 返回类型:
None
- has_rule(func) bool[源代码]#
-
中文
- 参数:
func (Any) -- 待判断的函数。其类型应与
rules的键类型一致- 返回:
func是否有对应的计数规则- 返回类型:
English
- 参数:
func (Any) -- the function or operation to be checked. Its type should be the same as the keys in
rules- 返回:
whether
funchas a corresponding counting rule- 返回类型:
- count(func, args: tuple, kwargs: dict, out, active_modules: set[Module] | None = None, parent_names: set[str] | None = None) int[源代码]#
-
中文
根据
rules,计算一次函数或操作调用所产生的计数值。- 参数:
- 返回:
计算得到的计数值
- 返回类型:
English
Calculate the count for a function or operation call according to
rules.- 参数:
func (Any) -- the function or operation to be calculated. Its type should be the same as the keys in
rulesargs (tuple) -- positional arguments of func
kwargs (dict) -- keyword arguments of func
out (Any) -- output of func
active_modules (Optional[set[nn.Module]]) -- currently active module instances. Most counters can ignore it, while context-aware counters may use it for semantic counting
parent_names (Optional[set[str]]) -- names of the currently active parent modules. Most counters can ignore it
- 返回:
the calculated count
- 返回类型:
- record(scope, func, value)[源代码]#
-
中文
向
records中添加记录。- 参数:
English
Record the calculated count to
records.
- get_total() int[源代码]#
-
中文
- 返回:
顶层作用域
"Global"下所有计数的总和。- 返回类型:
English
- 返回:
the total count of all records in the
"Global"scope.- 返回类型:
- reset()[源代码]#
-
中文
重置计数器,清空所有已记录的计数。
此方法会将
records重新初始化为空的嵌套字典,移除之前累积的全部计数结果。 适用于开始新的计数会话之前显式清零计数器状态。- 返回:
None- 返回类型:
None
English
Reset the counter and clear all recorded counts.
This method reinitializes
recordsto an empty nested dictionary, removing all previously accumulated count results. Call it before starting a new counting session when a counter instance is reused.- 返回:
None- 返回类型:
None
- spikingjelly.activation_based.op_counter.base.is_binary_tensor(x: Tensor) bool[源代码]#
-
中文
判断输入张量
x是否为二元张量(即所有元素都在 {0, 1} 中或dtype为bool)。- 参数:
x (torch.Tensor) -- 输入张量
- 返回:
如果
x是二元张量或 bool 张量则返回True- 返回类型:
English
Check if the input tensor
xis a binary tensor (all elements are in {0, 1} or itsdtypeisbool).- 参数:
x (torch.Tensor) -- input tensor
- 返回:
Trueifxis a binary tensor or a bool tensor- 返回类型:
- class spikingjelly.activation_based.op_counter.base.DispatchCounterMode(counters: list[BaseCounter], strict: bool = False, verbose: bool = False)[源代码]#
基类:
TorchDispatchMode
中文
基于 PyTorch 的 Dispatch 机制的 上下文管理器 ,用于计算aten操作对应计数。
该类通过重写
__torch_dispatch__方法来捕捉所有 PyTorch aten 操作的调用,并使用注册的计数器 来统计这些操作的某些计数。机制:
通过
ActiveModuleTracker追踪当前执行所在的模块层级对于每个被拦截的操作,检查是否有对应的计数规则
如果存在规则且不在被忽略的模块中,则调用规则函数计算计数值
将计数值记录到每一个父模块作用域中。
- 参数:
counters (list[BaseCounter]) -- 计数器列表
strict (bool) -- 如果为
True,当遇到未定义规则的操作时会报错;否则,未定义的操作将被跳过。 默认为Falseverbose (bool) -- 如果为
True,会在控制台打印每个被计数的操作及其计数值
- 返回:
上下文管理器对象
- 返回类型:
English
Context manager based on PyTorch's Dispatch mechanism for counting aten operations. It intercepts all PyTorch aten operations through overriding __torch_dispatch__ and uses registered counters to track these operations.
Working Mechanism:
Tracks the current module hierarchy using
ActiveModuleTrackerFor each intercepted operation, checks if there's a corresponding counting rule
If a rule exists and the operation is not in an ignored module, calls the rule function to calculate the count
Records the count to the parent module scope
- 参数:
counters (list[BaseCounter]) -- list of counters
strict (bool) -- if
True, raisesNotImplementedErrorwhen encountering operations without defined rules; ifFalse, skip the operations without defined rules. Default toFalse.verbose (bool) -- if
True, prints each counted operation and its count to the console
- 返回:
Context manager object
- 返回类型:
代码示例 | Example
from spikingjelly.activation_based.op_counter import ( FlopCounter, DispatchCounterMode, ) import torch import torch.nn as nn class SimpleNet(nn.Module): def __init__(self): super().__init__() self.linear = nn.Linear(100, 50) def forward(self, x): return self.linear(x) model = SimpleNet() x = torch.randn(32, 100) # Initialize counter flop_counter = FlopCounter() with DispatchCounterMode([flop_counter], verbose=True): output = model(x) # Get and print results print("FLOP counts:", flop_counter.get_total())
- class spikingjelly.activation_based.op_counter.base.FunctionCounterMode(counters: list[BaseCounter], strict: bool = False, verbose: bool = False)[源代码]#
基类:
TorchFunctionMode
中文
基于 PyTorch Function 机制的 上下文管理器 ,用于计算函数的计数。
该类通过重写
__torch_function__方法来拦截所有 PyTorch 函数调用,并使用注册的计数器来统计这些 操作的某些计数。工作原理与
DispatchCounterMode类似。- 参数:
counters (list[BaseCounter]) -- 计数器列表
strict (bool) -- 如果为
True,当遇到未定义规则的操作时会报错;否则,未定义的操作将被跳过。 默认为Falseverbose (bool) -- 如果为
True,会在控制台打印每个被计数的操作及其计数值
- 返回:
上下文管理器对象
- 返回类型:
English
Context manager based on PyTorch's Function mechanism for counting operations. It intercepts all PyTorch function calls through overriding
__torch_function__and uses registered counters to track these operations.It has a similar working mechanism to
DispatchCounterMode.- 参数:
counters (list[BaseCounter]) -- list of counters
strict (bool) -- if
True, raisesNotImplementedErrorwhen encountering operations without defined rules; ifFalse, skips operations without defined rules. Default toFalseverbose (bool) -- if
True, prints each counted operation and its count to the console
- 返回:
Context manager object
- 返回类型:
FLOP Counter#
- class spikingjelly.activation_based.op_counter.flop.FlopCounter(extra_rules: dict[Any, Callable] = {}, extra_ignore_modules: list[Module] = [])[源代码]#
基类:
BaseCounter
中文
中文
浮点运算次数(FLOPs)计数器。
该计数器统计前向与部分反向算子在算术层面的浮点运算数量,用于粗略估计计算开销。 具体构造参数见
__init__。
English
English
Floating-point operation (FLOP) counter.
This counter tracks arithmetic FLOPs of forward operators and some backward operators as a coarse estimate of compute cost. See
__init__for constructor parameters.
中文
浮点运算计数器,用于计算深度神经网络中的浮点运算次数。
FLOP(Floating Point Operations) 是一个衡量计算复杂度的常用指标:
1 次乘法 = 1 FLOP;1 次加法 = 1 FLOP;......
逐元素操作的FLOP也会纳入考量。
FlopCounter应与DispatchCounterMode搭配使用。警告
目前,
FlopCounter支持的 aten 操作类型有限。查看源代码以获取操作列表。如需添加新操作, 可以使用extra_rules参数;也欢迎提交 pull request 来完善默认的rules!- 参数:
extra_rules (dict[Any, Callable]) -- 额外的操作规则,格式为
{aten_op: func}, 其中func是一个函数,接受(args, kwargs, out)并返回计数值extra_ignore_modules (list[torch.nn.Module]) -- 额外需要忽略的模块列表,这些模块中的操作不会被计数
English
FLOP counter for calculating the number of floating-point operations in deep networks.
FLOP (Floating Point Operations) is a common metric for measuring computational complexity:
1 multiplication = 1 FLOP; 1 addition = 1 FLOP; ......
Element-wise operations are also considered.
FlopCountershould be used withDispatchCounterMode.警告
Currently,
FlopCountersupports a limited number of aten operations. See the source code for the operation list. If you want to add new operations, use theextra_rulesparameter. Welcome to submit a pull request to improve the defaultrules!- 参数:
extra_rules (dict[Any, Callable]) -- additional operation rules, format as
{aten_op: func}, wherefuncis a function that takes(args, kwargs, out)and returns the count valueextra_ignore_modules (list[torch.nn.Module]) -- additional list of modules to ignore. Operations within these modules will not be counted
代码示例 | Example
from spikingjelly.activation_based.op_counter import ( FlopCounter, DispatchCounterMode, ) import torch import torch.nn as nn model = nn.Sequential(nn.Linear(100, 50), nn.ReLU(), nn.Linear(50, 10)) x = torch.randn(32, 100) flop_counter = FlopCounter() with DispatchCounterMode([flop_counter]): output = model(x) # Get FLOP counts total_flops = flop_counter.get_total() print(f"Total FLOPs: {total_flops}")
- 返回:
None
- 返回类型:
None
Memory Access Counter#
- class spikingjelly.activation_based.op_counter.memory_access.MemoryAccessCounter(extra_rules: dict[Any, Callable] = {}, extra_ignore_modules: list[Module] = [])[源代码]#
基类:
BaseCounter
中文
中文
内存访问量估计计数器。
该计数器以输入/输出张量的字节数为基础,估计算子的内存访问下界, 适合用于粗略分析不同网络结构的访存压力。具体构造参数见
__init__。
English
English
Memory-access estimation counter.
The counter estimates a lower bound of operator memory access from the byte size of input and output tensors, which is useful for coarse-grained memory traffic analysis across network structures. See
__init__for constructor parameters.
中文
内存访问计数器,用于粗略估计深度神经网络的内存访问量。
该计数器统计操作所需的输入、输出张量的 字节 数,作为对内存访问量的 下界估计 。真实的内存访问量由算子的load store模式决定,取决于具体实现,在此不做考虑。
MemoryAccessCounter应与DispatchCounterMode搭配使用。警告
目前,
MemoryAccessCounter支持的 aten 操作类型有限。查看源代码以获取操作列表。如需添加新操作, 可以使用extra_rules参数;也欢迎提交 pull request 来完善默认的rules!- 参数:
English
Memory access counter for estimating memory access in deep networks.
This counter tracks the byte count of input and output tensors for operations as a lower bound estimate of memory access. Actual amount of memory access depends on the load store patterns of specific implementations, so it is not considered here.
MemoryAccessCountershould be used withDispatchCounterMode.警告
Currently,
MemoryAccessCountersupports a limited number of aten operations. See the source code for the list of operations. If you want to add a new operation, you can use theextra_rulesparameter. Welcome to submit a pull request to improve the defaultrules!- 参数:
extra_rules (dict[Any, Callable]) -- additional operation rules, format as
{aten_op: func}, wherefuncis a function that takes(args, kwargs, out)and returns byte countextra_ignore_modules (list[nn.Module]) -- additional list of modules to ignore. Operations within these modules will not be counted
代码示例 | Example
from spikingjelly.activation_based.op_counter import ( MemoryAccessCounter, DispatchCounterMode, ) import torch import torch.nn as nn model = nn.Sequential(nn.Linear(100, 50), nn.ReLU(), nn.Linear(50, 10)) x = torch.randn(32, 100) memory_counter = MemoryAccessCounter() with DispatchCounterMode([memory_counter]): output = model(x) total_bytes = memory_counter.get_total() print(f"Total memory access: {total_bytes / 1024:.2f} KB")
- 返回:
None
- 返回类型:
None
MAC / AC / SynOp Counters#
- class spikingjelly.activation_based.op_counter.mac.MACCounter(extra_rules: dict[Any, Callable] = {}, extra_ignore_modules: list[Module] = [])[源代码]#
基类:
BaseCounter
中文
中文
硬件级乘累加(MAC)计数器。
该计数器统计网络中的 MAC 操作次数,并与
ACCounter形成互补视角,用于近似刻画硬件上的乘累加开销。具体构造参数见__init__。
English
English
Hardware-level multiply-accumulate (MAC) counter.
This counter tracks MAC operations in a network and complements
ACCounterfor approximate hardware-oriented compute analysis. See__init__for constructor parameters.
中文
硬件级乘累加(Multiply-Accumulate,MAC)操作计数器,统计网络中所有 MAC 操作次数。
MAC 乘法结果立即累加到累加器(如矩阵内积),而非写入新的内存位置。MAC与AC互斥:若一个计算步骤 计入 MAC,则不会计入 AC;反之亦然。
MACCounter应与DispatchCounterMode搭配使用。警告
MACCounter只能统计前向传播期间的 MAC 数量。部分专用于反向传播的算子还未覆盖。目前,
MACCounter支持的 aten 操作类型有限。查看源代码以获取操作列表。如需添加新操作, 可以使用extra_rules参数;也欢迎提交 pull request 来完善默认的rules!警告
MACCounter会如实考虑 BN 内部的 MAC 操作。如果想在推理时忽略 BN 内部的 MAC,请将 BN 融合到线性层中;或者使用extra_ignore_modules参数将 BN 模块加入忽略列表。- 参数:
extra_rules (dict[Any, Callable]) -- 额外的操作规则,格式为
{aten_op: func}, 其中func是一个函数,接受(args, kwargs, out)并返回 MAC 次数extra_ignore_modules (list[torch.nn.Module]) -- 额外需要忽略的模块列表,这些模块中的操作不会被计数
English
Hardware-level Multiply-Accumulate (MAC) operation counter that counts all MAC operations in a network.
MAC's multiply result is immediately accumulated into a running accumulator (not written to a new memory location).
MACCounteris mutually exclusive withACCounter: if a computation step is counted as MAC, it will not be counted as AC, and vice versa.警告
MACCountercan only count MACs during the forward pass. Some operators dedicated to backward pass are not yet covered.Currently,
MACCountersupports a limited number of aten operations. See the source code for the operation list. If you want to add new operations, use theextra_rulesparameter. Welcome to submit a pull request to improve the defaultrules!- 参数:
extra_rules (dict[Any, Callable]) -- additional operation rules, format as
{aten_op: func}, wherefuncis a function that takes(args, kwargs, out)and returns the MAC countextra_ignore_modules (list[torch.nn.Module]) -- additional list of modules to ignore. Operations within these modules will not be counted
代码示例 | Example
from spikingjelly.activation_based.op_counter import ( MACCounter, ACCounter, DispatchCounterMode, ) import torch import torch.nn as nn model = nn.Sequential(nn.Linear(100, 50), nn.ReLU(), nn.Linear(50, 10)) x = (torch.randn(32, 100) < 0.1).float() # sparse binary input mac_counter = MACCounter() with DispatchCounterMode([mac_counter]): output = model(x) print(f"Total MACs: {mac_counter.get_total()}") # only the 2nd layer counts
- 返回:
None
- 返回类型:
None
- class spikingjelly.activation_based.op_counter.ac.ACCounter(extra_rules: dict[Any, Callable] = {}, extra_ignore_modules: list[Module] = [])[源代码]#
基类:
BaseCounter
中文
硬件级累加(Accumulate,AC)操作计数器,从硬件视角统计网络中的纯加法次数。
与
SynOpCounter的区别:SynOpCounter只关注脉冲驱动的矩阵乘法和卷积;ACCounter还会统计 BN、add/sub 等算子内部的加法,范围更广但语义更宽泛。 例如,SEW ResNet 中残差连接处的加法操作将被计入 AC。ACCounter应与DispatchCounterMode搭配使用。警告
ACCounter只能统计前向传播期间的 AC 数量。部分专用于反向传播的算子还未覆盖。目前,
ACCounter支持的 aten 操作类型有限。查看源代码以获取操作列表。如需添加新操作, 可以使用extra_rules参数;也欢迎提交 pull request 来完善默认的rules!警告
ACCounter会如实考虑 BN 内部的 AC 操作。如果想在推理时忽略 BN 内部的 AC,请将 BN 融合到线性层中;或者使用extra_ignore_modules参数将 BN 模块加入忽略列表。- 参数:
extra_rules (dict[Any, Callable]) -- 额外的操作规则,格式为
{aten_op: func}, 其中func是一个函数,接受(args, kwargs, out)并返回 AC 次数extra_ignore_modules (list[torch.nn.Module]) -- 额外需要忽略的模块列表
English
Hardware-level Accumulate (AC) operation counter that counts pure additions in a network from a hardware perspective.
Compared with
SynOpCounter:SynOpCounteronly covers spike-driven matmul and conv;ACCounteralso covers BN, add/sub, etc., thus is broader but more semantically general.ACCountershould be used withDispatchCounterMode.警告
ACCountercan only count ACs during the forward pass. Some operators dedicated to backward pass are not yet covered.Currently,
ACCountersupports a limited number of aten operations. See the source code for the operation list. If you want to add new operations, use theextra_rulesparameter. Welcome to submit a pull request to improve the defaultrules!警告
ACCountercounts AC operations inside BN. To ignore AC inside BN during inference, please fuse BN into linear/conv layers; or use theextra_ignore_modulesparameter to add BN modules to the ignore list.- 参数:
extra_rules (dict[Any, Callable]) -- additional operation rules, format as
{aten_op: func}, wherefuncis a function that takes(args, kwargs, out)and returns the AC countextra_ignore_modules (list[torch.nn.Module]) -- additional list of modules to ignore
代码示例 | Example
from spikingjelly.activation_based.op_counter import ( ACCounter, DispatchCounterMode, ) import torch import torch.nn as nn model = nn.Sequential(nn.Linear(100, 50), nn.ReLU(), nn.Linear(50, 10)) spike = (torch.rand(32, 100) < 0.1).float() # sparse binary input ac_counter = ACCounter() with DispatchCounterMode([ac_counter]): model(spike) print(f"Total ACs: {ac_counter.get_total()}") # only the 1st layer counts
- 返回:
None
- 返回类型:
None
- class spikingjelly.activation_based.op_counter.synop.SynOpCounter(extra_rules: dict[Any, Callable] = {}, extra_ignore_modules: list[Module] = [])[源代码]#
基类:
BaseCounter
中文
突触操作(Synaptic Operations,SynOps)计数器,用于统计 SNN 中由 spike 驱动的突触权重累加次数。
与
ACCounter的区别:ACCounter除了权重层线性操作外,还会统计 BN、add/sub 等算子内部的加法;SynOpCounter只关注脉冲驱动的矩阵乘法和卷积,范围更窄但语义更直接。 例如,SEW ResNet 中残差连接处的加法操作将不会被计入 SynOps。SynOpCounter应与DispatchCounterMode搭配使用。警告
SynOpCounter只能统计前向传播期间的突触操作数量。部分专用于反向传播的算子还未覆盖。目前,
SynOpCounter支持的 aten 操作类型有限(mm、addmm、bmm、baddbmm、convolution)。 如需添加新操作,可以使用extra_rules参数;也欢迎提交 pull request 来完善默认的rules!- 参数:
extra_rules (dict[Any, Callable]) -- 额外的操作规则,格式为
{aten_op: func}, 其中func是一个函数,接受(args, kwargs, out)并返回 SynOps 次数extra_ignore_modules (list[torch.nn.Module]) -- 额外需要忽略的模块列表,这些模块中的操作不会被计数
English
Synaptic Operations (SynOps) counter that tracks spike-driven weight accumulations in SNNs.
Compared with
ACCounter:ACCounteralso covers BN internals, add/sub, ...SynOpCounteris narrower: only spike-driven matmul and conv are considered. This makes it more directly aligned with the intuitive concept of "synaptic operations" in neuromorphic computing.SynOpCountershould be used withDispatchCounterMode.警告
SynOpCountercan only count SynOps during the forward pass. Some operators dedicated to backward pass are not yet covered.Currently,
SynOpCountersupports mm, addmm, bmm, baddbmm, and convolution. If you want to add new operations, use theextra_rulesparameter. Welcome to submit a pull request to improve the defaultrules!- 参数:
extra_rules (dict[Any, Callable]) -- additional operation rules, format as
{aten_op: func}, wherefuncis a function that takes(args, kwargs, out)and returns the SynOps countextra_ignore_modules (list[torch.nn.Module]) -- additional list of modules to ignore. Operations within these modules will not be counted
代码示例 | Example
from spikingjelly.activation_based.op_counter import ( SynOpCounter, DispatchCounterMode, ) import torch import torch.nn as nn model = nn.Linear(10, 5, bias=False) spike = (torch.rand(4, 10) < 0.2).float() counter = SynOpCounter() with DispatchCounterMode([counter]): model(spike) print(f"SynOp count: {counter.get_total()}")
- 返回:
None
- 返回类型:
None
Compute-Only Energy Estimator#
- class spikingjelly.activation_based.op_counter.compute_energy.ComputeEnergyCostConfig(e_mac_pj: float = 4.6, e_ac_pj: float = 0.9)[源代码]#
基类:
object
中文
仅计算 MAC/AC 的 compute-only 能耗模型成本配置。
默认值采用 SNN 文献中常见的 Horowitz 2014 口径:45nm、32-bit 浮点
E_MAC = 4.6 pJ,E_AC = 0.9 pJ。这是一个 cost-table-driven 的归一化模型。默认不会自动根据运行时
dtype推断成本;如需切换口径,请显式使用fp32(),fp16(),int8()等 preset。
English
Cost configuration for the compute-only MAC/AC energy model.
Defaults follow the widely used Horowitz 2014 reference costs for 45nm, 32-bit floating-point arithmetic:
E_MAC = 4.6 pJandE_AC = 0.9 pJ.This is a normalized, cost-table-driven model. It does not automatically infer energy costs from runtime
dtype; use explicit presets such asfp32(),fp16(), orint8()when a different comparison regime is desired.- classmethod fp32() ComputeEnergyCostConfig[源代码]#
Return the Horowitz 2014 45nm FP32 preset.
- classmethod fp16() ComputeEnergyCostConfig[源代码]#
Return the Horowitz 2014 45nm FP16 preset.
Uses
FMult16 = 1.1 pJandFAdd16 = 0.4 pJ, soE_MAC = 1.5 pJandE_AC = 0.4 pJ.
- classmethod int8() ComputeEnergyCostConfig[源代码]#
Return the Horowitz 2014 45nm INT8 preset.
Uses
Mult8 = 0.2 pJandAdd8 = 0.03 pJ, soE_MAC = 0.23 pJandE_AC = 0.03 pJ.
- class spikingjelly.activation_based.op_counter.compute_energy.ComputeEnergyConfig(strict: bool = False, verbose: bool = False, cost_config: ~spikingjelly.activation_based.op_counter.compute_energy.ComputeEnergyCostConfig = <factory>, extra_ignore_modules: list[type[~torch.nn.modules.module.Module]] | None = None)[源代码]#
基类:
object
中文
控制 compute-only MAC/AC 能耗分析器行为的配置。
默认
cost_config使用ComputeEnergyCostConfig.fp32()对应的口径。
English
Configuration for the compute-only MAC/AC energy profiler.
The default
cost_configmatchesComputeEnergyCostConfig.fp32().strictonly applies to profiler-level validation added by this wrapper. The internalDispatchCounterModeis intentionally kept non-strict because it composes multiple specialized counters with non-identical rule coverage.- cost_config: ComputeEnergyCostConfig#
- class spikingjelly.activation_based.op_counter.compute_energy.ComputeEnergyProfiler(*, config: ComputeEnergyConfig | None = None)[源代码]#
基类:
object
中文
中文
基于 public counter 组装的 compute-only MAC/AC 能耗分析器。
用法与其他能耗分析器一致:以 context manager 方式包住一次真实前向传播, 然后调用
get_report()。
English
English
Compute-only MAC/AC energy profiler composed from public counters.
Use it like the other energy profilers: wrap one real forward pass in the context manager and call
get_report()afterwards.- 参数:
config (ComputeEnergyConfig | None) -- 能耗配置,若为
None则使用默认配置config -- Energy configuration. If
None, uses the default configuration
- 返回:
None
- 返回类型:
None
- get_report() ComputeEnergyReport[源代码]#
- class spikingjelly.activation_based.op_counter.compute_energy.ComputeEnergyReport(energy_total_pj: float, energy_mac_pj: float, energy_ac_pj: float, breakdown_pj: dict[str, float], counts: dict[str, int], warnings: list[str])[源代码]#
基类:
object
中文
compute-only MAC/AC 能耗报告。
该模型只考虑计算能耗,不包含访存、寻址、状态驻留等开销。主结果
energy_total_pj由MAC和AC两部分组成。SynOps与FLOPs作为辅助统计返回,便于与现有 SNN/ANN 文献对齐, 但不参与主能耗计算。该估计器面向“统一比较口径”,而不是对真实 kernel、混合精度累加路径或 特定硬件微架构做精确建模。
English
Report for the compute-only MAC/AC energy model.
This model only accounts for arithmetic compute energy, excluding memory, addressing, and state residency costs. The main result
energy_total_pjconsists ofMACandACcontributions only.SynOpsandFLOPsare returned as auxiliary counts for alignment with existing SNN/ANN literature, but they do not contribute to the primary energy estimate.The estimator is intended as a normalized comparison regime rather than an exact model of real kernels, mixed-precision accumulation paths, or a specific hardware microarchitecture.
- spikingjelly.activation_based.op_counter.compute_energy.estimate_compute_energy(model: Module, inputs, *, config: ComputeEnergyConfig | None = None) ComputeEnergyReport[源代码]#
-
中文
compute-only MAC/AC 能耗估计的便捷入口。该函数执行一次真实前向传播, 并返回总能耗与 MAC/AC 计数。
默认使用 Horowitz 2014 的 FP32 成本口径;若需要 FP16 或 INT8 比较, 请显式传入对应 preset。
- 参数:
model -- 待统计模型
inputs -- 模型输入;若为 tuple/list 则按
model(*inputs)调用config -- compute-only 能耗配置
English
Convenience entry for compute-only MAC/AC energy estimation. It runs one real forward pass and returns the energy report.
The default comparison regime is Horowitz 2014 FP32. For FP16 or INT8 comparisons, pass an explicit preset cost configuration.
- 参数:
model -- model to profile
inputs -- model input; tuple/list will be passed as
model(*inputs)config -- compute-only energy configuration
NeuroMC Energy Profiler#
- class spikingjelly.activation_based.op_counter.neuromc.core.MemoryHierarchyConfig(preset_name: str = 'neuromc_like_v1', technology_nm: int = 32, level_order: tuple[str, str, str, str] = ('dram', 'sram', 'reg', 'noc'), memory_instances: dict[str, ~spikingjelly.activation_based.op_counter.neuromc.config.MemoryInstanceSpec] = <factory>, zero_dram_in_paper_energy: bool = True, zero_noc_in_paper_energy: bool = True, zero_sram_high_directions: bool = True)[源代码]#
基类:
object
中文
NeuroMC v1 硬件预设配置类。
- 参数:
preset_name (str) -- 预设名称
technology_nm -- 工艺节点(纳米)
English
MemoryHierarchyConfig class
- 返回:
None
- 返回类型:
None
- class spikingjelly.activation_based.op_counter.neuromc.core.NeuroMCEnergyProfiler(*, core_type: str = 'fp_soma', memory_config: MemoryHierarchyConfig | None = None, strict: bool = False, verbose: bool = False, extra_ignore_modules: list[Module] | None = None)[源代码]#
基类:
objectHigh-level energy profiler for spiking neural networks using the NeuroMC framework. API Language: 中文 | English
中文
NeuroMC能耗分析器
- 返回类型:
None
Profiles the energy consumption of a model by tracking operation counts and memory access patterns across forward, backward, and optimizer stages.
English
NeuroMC energy profiler
- 返回:
None
- 返回类型:
None
- 参数:
core_type (str) -- Type of compute core (e.g.,
"fp_soma")memory_config (MemoryHierarchyConfig | None) -- Memory hierarchy configuration. If
None, uses the defaultneuromc_like_v1configstrict (bool) -- If
True, raise on unknown operations instead of warningverbose (bool) -- If
True, print progress information during profilingextra_ignore_modules (list[nn.Module] | None) -- Additional module types to ignore during counting
- 抛出:
ValueError -- If
core_typeis not in the supported set- 返回:
None
- 返回类型:
None
- get_report() NeuroMCRuntimeEnergyReport[源代码]#
- class spikingjelly.activation_based.op_counter.neuromc.core.NeuroMCRuntimeEnergyReport(energy_total_pj: float = 0.0, energy_compute_pj: float = 0.0, energy_memory_pj: float = 0.0, energy_by_stage: dict[str, float] | None = None, energy_by_op: dict[str, float] | None = None, primitive_counts: dict[str, Any] | None = None, memory_bits_by_level: dict[str, Any] | None = None, warnings: list[str] | None = None, energy_mac_pj: float = 0.0, energy_base_memory_pj: float = 0.0, energy_extra_memory_pj: float = 0.0, energy_extra_compute_pj: float = 0.0, energy_by_core_type: dict[str, float] | None = None, energy_by_process_key: dict[str, float] | None = None, energy_by_memory_level_dir: dict[str, dict[str, float]] | None = None, counts_by_core_type: dict[str, dict[str, int]] | None = None, counts_by_process_key: dict[str, dict[str, int]] | None = None, mapping_summary: list[dict[str, Any]] | None = None)[源代码]#
基类:
objectEnergy profiling report generated by the NeuroMC framework. API Language: 中文 | English
中文
NeuroMC 运行时能耗报告数据类。
记录一次能耗分析会话的完整结果,包括总能耗、计算能耗、内存能耗、 各阶段能耗分解、各算子类型的能耗分布以及内存访问位宽等详细信息。 可通过
summary()方法获取 关键指标的字符串摘要,便于快速查看分析结果。- 参数:
energy_total_pj (float) -- Total energy consumption in picojoules
energy_compute_pj (float) -- Total compute energy in picojoules
energy_memory_pj (float) -- Total memory access energy in picojoules
energy_by_stage (
dict[str, float]) -- Energy breakdown by execution stageenergy_by_op (
dict[str, float]) -- Energy breakdown by operation typeprimitive_counts (
dict[str, Any]) -- Raw primitive operation countsmemory_bits_by_level (
dict[str, Any]) -- Memory access bits by hierarchy levelwarnings (
list[str]) -- List of warnings generated during profilingenergy_mac_pj (float) -- Energy of MAC operations in picojoules
energy_base_memory_pj (float) -- Base memory energy in picojoules
energy_extra_memory_pj (float) -- Extra memory energy in picojoules
energy_extra_compute_pj (float) -- Extra compute energy in picojoules
- 返回类型:
None
Contains detailed breakdown of compute and memory energy consumption across different stages, operations, and memory hierarchy levels.
English
Neuromcruntimeenergyreport function
- 参数:
energy_total_pj (float) -- Total energy consumption in picojoules
energy_compute_pj (float) -- Total compute energy in picojoules
energy_memory_pj (float) -- Total memory access energy in picojoules
energy_by_stage (
dict[str, float]) -- Energy breakdown by execution stageenergy_by_op (
dict[str, float]) -- Energy breakdown by operation typeprimitive_counts (
dict[str, Any]) -- Raw primitive operation countsmemory_bits_by_level (
dict[str, Any]) -- Memory access bits by hierarchy levelwarnings (
list[str]) -- List of warnings generated during profilingenergy_mac_pj (float) -- Energy of MAC operations in picojoules
energy_base_memory_pj (float) -- Base memory energy in picojoules
energy_extra_memory_pj (float) -- Extra memory energy in picojoules
energy_extra_compute_pj (float) -- Extra compute energy in picojoules
- 返回:
None
- 返回类型:
None
- spikingjelly.activation_based.op_counter.neuromc.core.estimate_neuromc_runtime_energy(model: Module, inputs, *, target: Tensor | None = None, loss_fn: Callable | None = None, optimizer: Optimizer | None = None, core_type: str = 'fp_soma', op_cost_pj: dict[str, float] | None = None, memory_cost_pj_per_bit: dict[str, float] | None = None, memory_level_weights: dict[str, float] | None = None, memory_model: str | None = None, memory_config: MemoryHierarchyConfig | None = None, strict: bool = False, verbose: bool = False, extra_ignore_modules: list[Module] | None = None) NeuroMCRuntimeEnergyReport[源代码]#
-
中文
estimate neuromc runtime energy 函数
- 参数:
model (
nn.Module) -- The PyTorch model to profileinputs (Any) -- Input tensors for the forward pass
target (torch.Tensor | None) -- Target tensors for loss computation
loss_fn (Callable | None) -- Loss function for the backward pass
optimizer (torch.optim.Optimizer | None) -- Optimizer for training-stage profiling
core_type (str) -- Type of compute core (e.g.,
\"fp_soma\")op_cost_pj (dict[str, float] | None) -- (Deprecated) Ignored
memory_cost_pj_per_bit (dict[str, float] | None) -- (Deprecated) Ignored
memory_level_weights (dict[str, float] | None) -- (Deprecated) Ignored
memory_model (str | None) -- (Deprecated) Ignored
memory_config (MemoryHierarchyConfig | None) -- Memory hierarchy configuration. If
None, uses the default configstrict (bool) -- If
True, raise on unknown operations instead of warningverbose (bool) -- If
True, print progress information during profilingextra_ignore_modules (list[nn.Module] | None) -- Additional module types to ignore during counting
- 返回:
Energy profiling report
- 返回类型:
This is a convenience function that creates a
NeuroMCEnergyProfiler, binds the model and optional optimizer, runs the full profile, and returns the energy report.
English
Estimate Neuromc Runtime Energy function
- 参数:
model (
nn.Module) -- The PyTorch model to profileinputs (Any) -- Input tensors for the forward pass
target (torch.Tensor | None) -- Target tensors for loss computation
loss_fn (Callable | None) -- Loss function for the backward pass
optimizer (torch.optim.Optimizer | None) -- Optimizer for training-stage profiling
core_type (str) -- Type of compute core (e.g.,
\"fp_soma\")op_cost_pj (dict[str, float] | None) -- (Deprecated) Ignored
memory_cost_pj_per_bit (dict[str, float] | None) -- (Deprecated) Ignored
memory_level_weights (dict[str, float] | None) -- (Deprecated) Ignored
memory_model (str | None) -- (Deprecated) Ignored
memory_config (MemoryHierarchyConfig | None) -- Memory hierarchy configuration. If
None, uses the default configstrict (bool) -- If
True, raise on unknown operations instead of warningverbose (bool) -- If
True, print progress information during profilingextra_ignore_modules (list[nn.Module] | None) -- Additional module types to ignore during counting
- 返回:
Energy profiling report
- 返回类型:
SpikeSim Event-Driven Energy Profiler#
中文
SpikeSim能耗分析模块,基于脉冲驱动的计算成本建模。
- return:
None
- rtype:
None
English
SpikeSim energy profiling module for spike-driven computation cost modeling.
- return:
None
- rtype:
None
- class spikingjelly.activation_based.op_counter.spikesim.SpikeSimEnergyConfig(xbar_size: int = 64, device: str = 'rram', activity_mode: str = 'dense', require_if_lif_neurons: bool = True, tile_buffer_pj: float = 397.0, temp_buffer_pj: float = 0.2, sub_pj: float = 1.15e-06, adc_pj: float = 2.03084, htree_pj: float = 157.12, mux_pj: float = 0.094245, mem_fetch_pj: float = 4.64, neuron_pj: float = 5.096, rram_xbar_pj: float = 1.76423, sram_xbar_pj: float = 671.089)[源代码]#
基类:
object
中文
SpikeSim 事件驱动能耗估计器的运行时能耗配置。
默认系数有意与已发布的
ela_spikesim.py能耗路径保持一致, 但计数逻辑已替换为基于运行时事件的分析。
English
Runtime energy configuration for the event-driven SpikeSim estimator.
The default coefficients intentionally match the released
ela_spikesim.pyenergy path, while the counting logic is replaced by runtime event analysis.- copy() SpikeSimEnergyConfig[源代码]#
-
中文
复制当前配置并返回新对象。
- 返回:
当前配置的副本
- 返回类型:
English
Return a copied config object.
- 返回:
a copy of the current config
- 返回类型:
- class spikingjelly.activation_based.op_counter.spikesim.SpikeSimCounter(*, config: SpikeSimEnergyConfig, strict: bool, verbose: bool)[源代码]#
基类:
BaseCounter
中文
SpikeSim 计数器,用于在 spike 驱动的模拟中统计计算成本。
- 参数:
config (SpikeSimEnergyConfig) -- SpikeSim 能量配置
strict (bool) -- 严格模式开关
verbose (bool) -- 详细输出开关
- 返回:
None
- 返回类型:
None
English
SpikeSim counter for profiling computation costs in spike-driven simulations.
- 参数:
config (SpikeSimEnergyConfig) -- SpikeSim energy configuration
strict (bool) -- Whether to use strict mode
verbose (bool) -- Whether to produce verbose output
- 返回:
None
- 返回类型:
None
- count(func, args: tuple, kwargs: dict, out, active_modules=None, parent_names=None) int[源代码]#
-
中文
统计单次前向传播的计算成本。
- 参数:
- 返回:
计算成本
- 返回类型:
- 抛出:
NotImplementedError -- 若未注册的算子遇到则抛出
English
Count the computation cost of a single forward propagation.
- 参数:
- 返回:
computation cost
- 返回类型:
- 抛出:
NotImplementedError -- Raised when encountering an unregistered operator
- class spikingjelly.activation_based.op_counter.spikesim.SpikeSimEnergyProfiler(*, config: SpikeSimEnergyConfig | None = None, strict: bool = False, verbose: bool = False)[源代码]#
基类:
object
中文
Runtime SpikeSim-aligned energy profiler.
使用方式:
以 context manager 方式包住一次真实前向传播
结束后调用
get_report()获取能耗报告
English
Runtime SpikeSim-aligned energy profiler.
Usage:
wrap one real forward pass in the profiler context
call
get_report()afterwards to build the energy report
中文
- 参数:
config -- SpikeSim 能耗配置;默认使用
SpikeSimEnergyConfig()strict -- 是否在 unsupported 情况下直接抛异常
verbose -- 是否打印逐 stage 的运行时统计信息
English
- 参数:
config -- SpikeSim energy config; defaults to
SpikeSimEnergyConfig()strict -- whether to raise immediately on unsupported behaviors
verbose -- whether to print per-stage runtime statistics
- 返回:
None
- 返回类型:
None
- get_counts() dict[str, Any][源代码]#
-
中文
返回统计量与 stage 元数据,便于和计数接口对齐。
English
Return event stats and stage metadata in an
op_counter-like count shape.
- get_report() SpikeSimEnergyReport[源代码]#
-
中文
生成并返回完整的 SpikeSim runtime 能耗报告。
English
Build and return the full runtime SpikeSim energy report.
- class spikingjelly.activation_based.op_counter.spikesim.SpikeSimEnergyReport(energy_total_pj: float, energy_by_stage: dict[str, float], energy_by_component: dict[str, Any], event_stats_by_stage: dict[str, dict[str, Any]], stage_metadata: dict[str, dict[str, Any]], warnings: list[str], breakdown_pj: dict[str, float], counts: dict[str, int])[源代码]#
基类:
object
中文
SpikeSim 运行时能耗估计器的报告, 支持
dense和event两种 activity mode。字段包括总能耗、stage 分解、统计量、stage 元数据和 warning。
event_stats_by_stage在两种 mode 下都会填充。
English
Report for the SpikeSim runtime energy estimator, supporting both
denseandeventactivity modes.Fields include total energy, stage-wise energy breakdown, event stats, stage metadata, and warnings.
event_stats_by_stageis populated regardless of the selected activity mode.
- spikingjelly.activation_based.op_counter.spikesim.SpikeSimEventEnergyProfiler#
- spikingjelly.activation_based.op_counter.spikesim.SpikeSimEventEnergyReport#
- spikingjelly.activation_based.op_counter.spikesim.estimate_spikesim_event_energy(model: Module, inputs, *, config: SpikeSimEnergyConfig | None = None, strict: bool = False, verbose: bool = False) SpikeSimEnergyReport[源代码]#
-
中文
SpikeSim runtime 能耗估计的便捷入口。
该函数会执行一次真实前向传播并返回能耗报告。
- 参数:
model -- 待统计模型
inputs -- 模型输入;若为 tuple/list 则按
model(*inputs)调用config -- SpikeSim 能耗配置
strict -- 是否在 unsupported 情况下直接抛异常
verbose -- 是否打印逐 stage 的运行时统计信息
English
Convenience entry for runtime SpikeSim energy estimation. It runs one real forward pass and returns the energy report.
- 参数:
model -- model to profile
inputs -- model input; tuple/list will be passed as
model(*inputs)config -- SpikeSim energy config
strict -- whether to raise immediately on unsupported behaviors
verbose -- whether to print per-stage runtime statistics