MCPcopy Create free account
hub / github.com/CompVis/diff2flow / count_flops_attn

Function count_flops_attn

diff2flow/models/unet/openaimodel.py:330–347  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

328
329
330def 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
350class QKVAttentionLegacy(nn.Module):

Callers 2

count_flopsMethod · 0.85
count_flopsMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected