Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to calculate Bert FLOPs #11

Open
ZLKong opened this issue Jun 8, 2020 · 4 comments
Open

How to calculate Bert FLOPs #11

ZLKong opened this issue Jun 8, 2020 · 4 comments

Comments

@ZLKong
Copy link

ZLKong commented Jun 8, 2020

Hi,

I have a very rookie question. How can I calculate the FLOPs of BERT model?
I tried to use thop,

macs, params = profile(model, inputs=(input, ), 
                        custom_ops={YourModule: count_your_model})

but I don't know how what is the input and custom_ops={YourModule: count_your_model}

For example, I want to run the models given by Huggingface. https://github.com/huggingface/transformers/tree/master/examples/text-classification

CUDA_VISIBLE_DEVICES=1 python run_glue.py \
  --model_type bert \
  --model_name_or_path /tmp/fintune_CoLA_output-bert/ \

I tried to put the macs, params = profile(model, inputs.....) command line in run_glue.py, but I'm not sure where to put it.
I get errors like:
[WARN] Cannot find rule for <class 'torch.nn.modules.sparse.Embedding'>. Treat it as zero Macs and zero Params.
[WARN] Cannot find rule for <class 'torch.nn.modules.normalization.LayerNorm'>. Treat it as zero Macs and zero Params.

File "/home/zhk20002/anaconda2/envs/Py3.6/lib/python3.6/site-packages/transformers/trainer.py", line 677, in _training_step model, inputs=inputs, custom_ops={ File "/home/zhk20002/anaconda2/envs/Py3.6/lib/python3.6/site-packages/thop/profile.py", line 188, in profile model(*inputs) File "/home/zhk20002/anaconda2/envs/Py3.6/lib/python3.6/site-packages/torch/nn/modules/module.py", line 532, in __call__ result = self.forward(*input, **kwargs) File "/home/zhk20002/anaconda2/envs/Py3.6/lib/python3.6/site-packages/transformers/modeling_bert.py", line 1144, in forward inputs_embeds=inputs_embeds, File "/home/zhk20002/anaconda2/envs/Py3.6/lib/python3.6/site-packages/torch/nn/modules/module.py", line 532, in __call__ result = self.forward(*input, **kwargs) File "/home/zhk20002/anaconda2/envs/Py3.6/lib/python3.6/site-packages/transformers/modeling_bert.py", line 691, in forward input_shape = input_ids.size() AttributeError: 'str' object has no attribute 'size'

Do you have a general code like this where I can test out the Flops of models such as BERT, RoBERTa, DistilBERT by just changing the --model_type?

Thanks!

Tony

@autoliuweijie
Copy link
Owner

There is no a general code that can test the FLOPs of all models, because different models are implemented differently.

If you want to get the FLOPs of a model, you should add the following code to the models' script:

flops, params = profile(model, inputs, verbose=False)

the model is a torch instance, the inputs is the input tensor for this model.

@ZLKong
Copy link
Author

ZLKong commented Jun 10, 2020

Thanks! It worked!

@ZLKong ZLKong closed this as completed Jun 10, 2020
@ZLKong ZLKong reopened this Jul 2, 2020
@ZLKong
Copy link
Author

ZLKong commented Jul 2, 2020

There is no a general code that can test the FLOPs of all models, because different models are implemented differently.

If you want to get the FLOPs of a model, you should add the following code to the models' script:

flops, params = profile(model, inputs, verbose=False)

the model is a torch instance, the inputs is the input tensor for this model.

Hi,

In your paper, is the total FLOPS of BERT 21785M? It looks very small.
Is thop capable of calculating FC layers?
I see that FLOPs depend on input sequence length. What is the FLOPs for one token?

Thanks,
ZK

@llsj14
Copy link

llsj14 commented Sep 19, 2022

https://arxiv.org/pdf/2003.10555.pdf
It seems that this paper also represents the inference FLOPs of BERT-BASE as 29 BFLOPs.
so 21785M looked quite affordable for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants