Code Monkey home page Code Monkey logo

syops-counter's Introduction

Synaptic OPerations (SyOPs) counter for spiking neural networks

Pypi version

This script is designed to compute the theoretical amount of synaptic operations in spiking neural networks, including accumulated (AC) and multiply-accumulate (MAC) operations. It can also compute the number of parameters and print per-layer computational cost of a given network. This tool is still under construction. Comments, issues, contributions, and collaborations are all welcomed!

Supported layers:

  • Conv1d/2d/3d (including grouping)
  • ConvTranspose1d/2d/3d (including grouping)
  • BatchNorm1d/2d/3d, GroupNorm, InstanceNorm1d/2d/3d
  • Activations (ReLU, PReLU, ELU, ReLU6, LeakyReLU, GELU)
  • Linear
  • Upsample
  • Poolings (AvgPool1d/2d/3d, MaxPool1d/2d/3d and adaptive ones)
  • LF/LIF/PLIF (spikingjelly)

Experimental support:

  • RNN, LSTM, GRU (NLH layout is assumed)
  • RNNCell, LSTMCell, GRUCell
  • MultiheadAttention

Requirements: Pytorch >= 1.1, torchvision >= 0.3, spikingjelly<=0.0.0.0.12

Usage

  • This script doesn't take into account torch.nn.functional.* operations. For an instance, if one have a semantic segmentation model and use torch.nn.functional.interpolate to upscale features, these operations won't contribute to overall amount of flops. To avoid that one can use torch.nn.Upsample instead of torch.nn.functional.interpolate.
  • syops launches a given model on a random tensor or a DataLoader and estimates amount of computations during inference. Complicated models can have several inputs, some of them could be optional.
    • To construct non-trivial input one can use the input_constructor argument of the get_model_complexity_info. input_constructor is a function that takes the input spatial resolution as a tuple and returns a dict with named input arguments of the model. Next this dict would be passed to the model as a keyword arguments.
    • To construct a DataLoader input one can use the dataLoader argument of the get_model_complexity_info based on torch.utils.data.DataLoader. The number of computations would be estimated based on the input fire rate of spike signals.
  • verbose parameter allows to get information about modules that don't contribute to the final numbers.
  • ignore_modules option forces syops to ignore the listed modules. This can be useful for research purposes. For an instance, one can drop all batch normalization from the counting process specifying ignore_modules=[torch.nn.BatchNorm2d].

Install the latest version

From PyPI:

pip install syops

From this repository:

pip install --upgrade git+https://github.com/iCGY96/syops-counter

Example

import torch
from spikingjelly.activation_based import surrogate, neuron, functional
from spikingjelly.activation_based.model import spiking_resnet
from syops import get_model_complexity_info

dataloader = ...
with torch.cuda.device(0):
    net = spiking_resnet.spiking_resnet18(pretrained=True, spiking_neuron=neuron.IFNode, 
			surrogate_function=surrogate.ATan(), detach_reset=True)
    ops, params = get_model_complexity_info(net, (3, 224, 224), dataloader, as_strings=True,
                                            print_per_layer_stat=True, verbose=True)
    print('{:<30}  {:<8}'.format('Computational complexity ACs:', acs))
    print('{:<30}  {:<8}'.format('Computational complexity MACs:', macs))
    print('{:<30}  {:<8}'.format('Number of parameters: ', params))

Benchmark

Model Input Resolution Params(M) ACs(G) MACs(G) Energy (mJ) Acc@1 Acc@5
spiking_resnet18 224x224 11.69 0.10 0.14 0.734 62.32 84.05
sew_resnet18 224x224 11.69 0.50 2.75 13.10 63.18 84.53
DSNN18 (AAP) 224x224 11.69 1.69 0.20 2.44 63.46 85.14
resnet18 224x224 11.69 0.00 1.82 8.372 69.76 89.08
  • ACs(G) - The theoretical amount of accumulated operations based on spike signals.
  • MACs(G) - The theoretical amount of multiply-accumulate operations based on non-spike signals.
  • Energy(mJ) - Energy consumption is based on 45nm technology, where AC cost 0.9pJ and MAC cost 4.6pJ.
  • Acc@1 - ImageNet single-crop top-1 accuracy on validation images of the same size used during the training process.
  • Acc@5 - ImageNet single-crop top-5 accuracy on validation images of the same size used during the training process.

Citation

If you find our work useful for your repo, please consider giving a star ⭐ and citation 🍺:

@article{chen2023training,
  title={Training Full Spike Neural Networks via Auxiliary Accumulation Pathway},
  author={Chen, Guangyao and Peng, Peixi and Li, Guoqi and Tian, Yonghong},
  journal={arXiv preprint arXiv:2301.11929},
  year={2023}
}

Acknowledgements

This repository is developed based on ptflops

syops-counter's People

Contributors

icgy96 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

syops-counter's Issues

Import error with newest versions of SpikingJelly + error in git repo homepage

Hi all,
i was trying to install this library and import the module named "get_model_complexity_info". However it is not possible since it requires the old 'spikingjelly.clock_driven', while the newest version uses '.activation_based'.

I've cloned the repo in Colab and changed the module name to .activation_based and removed all the references to old neuros like "MultiStepLIF". It Works


I think that there is a small error in the landing page of this repo

    ops, params = get_model_complexity_info(net, (3, 224, 224), dataloader, as_strings=True,
                                            print_per_layer_stat=True, verbose=True)
    print('{:<30}  {:<8}'.format('Computational complexity ACs:', acs))
    print('{:<30}  {:<8}'.format('Computational complexity MACs:', macs))
    print('{:<30}  {:<8}'.format('Number of parameters: ', params))

There are no references to 'acs' and 'macs', but by checking the returns of the function, i assume that they should be something similar to 'ops[1]' and 'ops[2]'

Why are these modules not counted?

Warning: module Conv2d is treated as a zero-op.
Warning: module BatchNorm2d is treated as a zero-op.
Warning: module ATan is treated as a zero-op.
Warning: module MaxPool2d is treated as a zero-op.
Warning: module BasicBlock is treated as a zero-op.
Warning: module AdaptiveAvgPool2d is treated as a zero-op.
Warning: module Linear is treated as a zero-op.
Warning: module SpikingResNet is treated as a zero-op.

A possible bug in ops.py

When calculating firing rate for the spiking neurons, spike, rate = spike_rate(output[0]) was used. However, output is actually a torch.tensor, not a tuple, therefore this only takes spikes of the first time step into account, which I think is not correct. Adding an if-statement if isinstance(output, tuple) may solve this problem.

关于EC(mJ)计算的一些疑问

您好,我在看您的论文时您论文中给出了几种模型的Estimated Consumption (EC)和Dynamic Consumption (DC),根据您论文中的描述,
Estimated Consumption (EC)似乎是由公式(15)计算得出的。
$$\mathrm E(\mathcal F)\in[T\cdot E_{m a c}\cdot O_{m a c},T\cdot(E_{a c}\cdot O_{a c}+E_{m a c}\cdot O_{m a c})] \quad(15)$$
您论文中的结果如下:
image

以Spiking ResNet-34为例, $O_{ac}(G),O_{mac}(G)$ 分别为5.34,0.15,T=6。在45nm工艺中 $E_{MAC}$$E_{AC}$ 分别为 4.6pJ ,0.9pJ。
我讲这些数据带入公式(15)并未得到您论文中的结果: $EC(mJ) \in [0.69, 19.85]$
请问是否是我的理解有误?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.