Skip to content

How to calculate Bert FLOPs #11

Description

@ZLKong

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions