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

Miss attention FLOPS? #14

Open
YeDeming opened this issue Jun 25, 2020 · 4 comments
Open

Miss attention FLOPS? #14

YeDeming opened this issue Jun 25, 2020 · 4 comments

Comments

@YeDeming
Copy link

Hi,

I found in MultiHeadedAttention, thop only count the FLOPS of linear layer, missing the attention operation.

@autoliuweijie
Copy link
Owner

Hello! Could you describe this issue in more detail? E.g, how you find that the attention operation is missed?

In our run_fastbert.py, we use the following code to obtain the total FLOPs of the model with thop.profile:

 # Get FLOPs at this batch
  inputs = (input_ids_batch, label_ids_batch, mask_ids_batch, fast_mode)
  flops, params = profile(model, inputs, verbose=False)
  total_flops += flops

And, in our previous experiment, we measured the FLOPs of self-attention operation, which is about 603.0M, and that of FeedForward layer is about 1207.9M.

@YeDeming
Copy link
Author

YeDeming commented Jun 28, 2020

When testing on the multiheadattention


from uer.layers.multi_headed_attn import MultiHeadedAttention
   
encoder = MultiHeadedAttention(768, 12, 0.0)
l = 512
key = torch.Tensor(1, l, 768)
mask = torch.Tensor(1, l, l)
mask = mask.unsqueeze(1)

inputs = (key, key, key, mask)
macs, params = profile(encoder, inputs, verbose=False)
print (macs)

if you delete the matmul operation in the code, the macs will be the same, e.g., delete the following two lines in multi_headed_attn.py

scores = torch.matmul(query, key.transpose(-2, -1))
....
output = unshape(torch.matmul(probs, value))

@autoliuweijie
Copy link
Owner

Thank you for your testing!
I will analyze it further, and show my results as soon as possible.

@autoliuweijie
Copy link
Owner

autoliuweijie commented Jun 28, 2020

After testing, we found that thop.profile does not calculate FLOPs for torch.matmul() operation.

So, the FLOPs we obtained miss the torch.matmul parts.

References: https://discuss.pytorch.org/t/get-the-matmul-operations-in-a-net/61058

This is a mistake in our work, however, this does not affect the conclusion of this paper, because the speedup is unchanged, and the FLOPs of torch.matmul is only a small part.

Thank you for finding this issue.

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

2 participants