u: r(B D L) delta: r(B D L) A: r(D N) B: r(B N L) C: r(B N L) D: r(D) z: r(B D L) delta_bias: r(D), fp32 ignores: [.float(), +, .softplus, .shape, new_zeros, repeat, stack, to(dtype), silu]
(B=1, L=256, D=768, N=16, with_D=True, with_Z=False, with_Group=True, with_complex=False)
| 42 | |
| 43 | |
| 44 | def flops_selective_scan_ref(B=1, L=256, D=768, N=16, with_D=True, with_Z=False, with_Group=True, with_complex=False): |
| 45 | """ |
| 46 | u: r(B D L) |
| 47 | delta: r(B D L) |
| 48 | A: r(D N) |
| 49 | B: r(B N L) |
| 50 | C: r(B N L) |
| 51 | D: r(D) |
| 52 | z: r(B D L) |
| 53 | delta_bias: r(D), fp32 |
| 54 | |
| 55 | ignores: |
| 56 | [.float(), +, .softplus, .shape, new_zeros, repeat, stack, to(dtype), silu] |
| 57 | """ |
| 58 | import numpy as np |
| 59 | |
| 60 | # fvcore.nn.jit_handles |
| 61 | def get_flops_einsum(input_shapes, equation): |
| 62 | np_arrs = [np.zeros(s) for s in input_shapes] |
| 63 | optim = np.einsum_path(equation, *np_arrs, optimize="optimal")[1] |
| 64 | for line in optim.split("\n"): |
| 65 | if "optimized flop" in line.lower(): |
| 66 | # divided by 2 because we count MAC (multiply-add counted as one flop) |
| 67 | flop = float(np.floor(float(line.split(":")[-1]) / 2)) |
| 68 | return flop |
| 69 | |
| 70 | assert not with_complex |
| 71 | |
| 72 | flops = 0 # below code flops = 0 |
| 73 | if False: |
| 74 | ... |
| 75 | """ |
| 76 | dtype_in = u.dtype |
| 77 | u = u.float() |
| 78 | delta = delta.float() |
| 79 | if delta_bias is not None: |
| 80 | delta = delta + delta_bias[..., None].float() |
| 81 | if delta_softplus: |
| 82 | delta = F.softplus(delta) |
| 83 | batch, dim, dstate = u.shape[0], A.shape[0], A.shape[1] |
| 84 | is_variable_B = B.dim() >= 3 |
| 85 | is_variable_C = C.dim() >= 3 |
| 86 | if A.is_complex(): |
| 87 | if is_variable_B: |
| 88 | B = torch.view_as_complex(rearrange(B.float(), "... (L two) -> ... L two", two=2)) |
| 89 | if is_variable_C: |
| 90 | C = torch.view_as_complex(rearrange(C.float(), "... (L two) -> ... L two", two=2)) |
| 91 | else: |
| 92 | B = B.float() |
| 93 | C = C.float() |
| 94 | x = A.new_zeros((batch, dim, dstate)) |
| 95 | ys = [] |
| 96 | """ |
| 97 | |
| 98 | flops += get_flops_einsum([[B, D, L], [D, N]], "bdl,dn->bdln") |
| 99 | if with_Group: |
| 100 | flops += get_flops_einsum([[B, D, L], [B, N, L], [B, D, L]], "bdl,bnl,bdl->bdln") |
| 101 | else: |
nothing calls this directly
no test coverage detected
searching dependent graphs…