The calflops is designed to calculate FLOPs、MACs and Parameters in all various neural networks, such as Linear、 CNN、 RNN、 GCN、Transformer(Bert、LlaMA etc Large Language Model)
This is a toy example of multi-head self-attention implemented using `torch.einsum`. By running this script, you could get the following `AttributeError` raised in function `_einsum_flops_compute()`:
```bash
Traceback (most recent call last):
File "my_compute_flops.py", line 60, in <module>
flops, macs, params = calculate_flops(model=model, kwargs=inputs, print_results=False)
File "[anomyous]/lib/python3.7/site-packages/calflops/flops_counter.py", line 154, in calculate_flops
_ = model(*args, **kwargs)
File "[anomyous]/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1071, in _call_impl
result = forward_call(*input, **kwargs)
File "my_compute_flops.py", line 51, in forward
atten = self.softmax(torch.einsum('nhcp,nhcq->nhpq', [q, k]) / (self.qkv_dim))
File "[anomyous]/lib/python3.7/site-packages/calflops/pytorch_ops.py", line 360, in newFunc
flops, macs = funcFlopCompute(*args, **kwds)
File "[anomyous]/lib/python3.7/site-packages/calflops/pytorch_ops.py", line 295, in _einsum_flops_compute
input_shapes = [o.shape for o in operands]
File "[anomyous]/lib/python3.7/site-packages/calflops/pytorch_ops.py", line 295, in <listcomp>
input_shapes = [o.shape for o in operands]
AttributeError: 'list' object has no attribute 'shape'
class Model(nn.Module): def init(self, in_channels=768, num_heads=8, qkv_dim=256): super().init() self.num_heads = num_heads self.qkv_dim = qkv_dim self.to_qkvs = nn.Conv1d(in_channels, 3 num_heads qkv_dim, 1) self.softmax = nn.Softmax(-1)
inputs ={ 'x': torch.rand((1, 768, 64)), } model = Model() flops, macs, params = calculate_flops(model=model, kwargs=inputs, print_results=False) print(flops) print(macs) print(params)