A counter for the `thop` package to count the operations in an attention operation. Meant to be used like: macs, params = thop.profile( model, inputs=(inputs, timestamps), custom_ops={QKVAttention: QKVAttention.count_flops}, )
(model, _x, y)
| 328 | |
| 329 | |
| 330 | def count_flops_attn(model, _x, y): |
| 331 | """ |
| 332 | A counter for the `thop` package to count the operations in an |
| 333 | attention operation. |
| 334 | Meant to be used like: |
| 335 | macs, params = thop.profile( |
| 336 | model, |
| 337 | inputs=(inputs, timestamps), |
| 338 | custom_ops={QKVAttention: QKVAttention.count_flops}, |
| 339 | ) |
| 340 | """ |
| 341 | b, c, *spatial = y[0].shape |
| 342 | num_spatial = int(np.prod(spatial)) |
| 343 | # We perform two matmuls with the same number of ops. |
| 344 | # The first computes the weight matrix, the second computes |
| 345 | # the combination of the value vectors. |
| 346 | matmul_ops = 2 * b * (num_spatial ** 2) * c |
| 347 | model.total_ops += th.DoubleTensor([matmul_ops]) |
| 348 | |
| 349 | |
| 350 | class QKVAttentionLegacy(nn.Module): |
no outgoing calls
no test coverage detected