Skip to main content

This package is designed to compute the theoretical amount of FLOPs(floating-point operations)、MACs(multiply-add operations) and Parameters in all various neural networks, such as Linear、 CNN、 RNN、 GCN、Transformer(Bert、LlaMA etc Large Language Model),including any custom models via ```torch.nn.function.*``` as long as based on the Pytorch implementation.

Project description

calculate-flops.pytorch

This tool(calflops) is designed to compute the theoretical amount of FLOPs(floating-point operations)、MACs(multiply-add operations) and Parameters in all various neural networks, such as Linear、 CNN、 RNN、 GCN、Transformer(Bert、LlaMA etc Large Language Model),including any custom models via torch.nn.function.* as long as based on the Pytorch implementation.

In addition, the implementation process of this package inspired by ptflops and deepspeed libraries, for which I am very grateful for their efforts, they are both very good work. Meanwhile this package also improves some aspects(more simple、more model support) based on them.

Install the latest version

From PyPI:

pip install calflops

And you also can download latest *.whl files from https://pypi.org/project/calflops/

pip install *.whl

Example

from calflops import calculate_flops

# Deep Learning Model, such as alexnet.
from torchvision import models

model = models.alexnet()
batch_size = 1
flops, macs, params = calculate_flops(model=model, 
                                      input_shape=(batch_size, 3, 224, 224))
print("alexnet FLOPs:%s   MACs:%s   Params:%s \n" %(flops, macs, params))
# alexnet FLOPs:1.43 GFLOPS   MACs:714.188 MMACs   Params:61.101 M


# Transformers Model, such as bert.
from transformers import AutoModel
from transformers import AutoTokenizer
batch_size = 1
max_seq_length = 128
model_name = "hfl/chinese-roberta-wwm-ext/"
model_save = "../pretrain_models/" + model_name
model = AutoModel.from_pretrained(model_save)
tokenizer = AutoTokenizer.from_pretrained(model_save)
flops, macs, params = calculate_flops_pytorch(model=model, 
                                              input_shape=(batch_size, max_seq_length),
                                              transformer_tokenizer=tokenizer)
print("bert(hfl/chinese-roberta-wwm-ext) FLOPs:%s   MACs:%s   Params:%s \n" %(flops, macs, params))
#bert(hfl/chinese-roberta-wwm-ext) FLOPs:22.363 GFLOPS   MACs:11.174 GMACs   Params:102.268 M 


# Large Languase Model, such as llama2-7b.
from transformers import LlamaTokenizer
from transformers import LlamaForCausalLM
batch_size = 1
max_seq_length = 128
model_name = "llama2_hf_7B"
model_save = "../model/" + model_name
model = LlamaForCausalLM.from_pretrained(model_save)
tokenizer = LlamaTokenizer.from_pretrained(model_save)
flops, macs, params = calculate_flops(model=model,
                                      input_shape=(batch_size, max_seq_length),
                                      transformer_tokenizer=tokenizer)
print("llama2(7B) FLOPs:%s   MACs:%s   Params:%s \n" %(flops, macs, params))
#llama2(7B) FLOPs:1.7 TFLOPS   MACs:850.001 GMACs   Params:6.738 B 

Concact Author

Author: MrYXJ

Mail: code_job@163.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

calflops-0.0.3.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

calflops-0.0.3-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file calflops-0.0.3.tar.gz.

File metadata

  • Download URL: calflops-0.0.3.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for calflops-0.0.3.tar.gz
Algorithm Hash digest
SHA256 8ef484af3fcdaa70fb279de150c36be8a117d04dcd7e3d44a19d427c77aa0948
MD5 aea9f2a13323582915404bfa397fee49
BLAKE2b-256 9c7b9a60c8c08dc0e174f47549d1675029ca758e3b0da6e9fb8b7d4d03e80e47

See more details on using hashes here.

File details

Details for the file calflops-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: calflops-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for calflops-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 71972f84675383488f0cc44d3dcf70233d1581d46a02afae59aed69bfa55fdbf
MD5 2d6f7d8282a9f973b3861846f17a2549
BLAKE2b-256 2c56ca988914155adcd806ab0fdecc8ec253d50a54c2ed9fb1c732cafab58447

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page