-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_energy.py
166 lines (136 loc) · 4.64 KB
/
get_energy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import time
import warnings
import numpy as np
import torch
import torch.backends.cudnn as cudnn
from timm.data import Mixup
from timm.models import create_model
import gvt
import utils
from params import args
warnings.filterwarnings("ignore")
@torch.no_grad()
def throughput(data_loader, model, logger):
model.eval()
for idx, (images, _) in enumerate(data_loader):
images = images.cuda(non_blocking=True)
batch_size = images.shape[0]
for i in range(50):
model(images)
torch.cuda.synchronize()
logger.info(f"throughput averaged with 30 times")
tic1 = time.time()
for i in range(30):
model(images)
torch.cuda.synchronize()
tic2 = time.time()
logger.info(
f"batch_size {batch_size} throughput {30 * batch_size / (tic2 - tic1)}"
)
return
def msa_flops(h, w, dim, heads):
muls = 0
adds = 0
# q@k and attn@v
muls += 2 * h * w * h * w * dim
adds += 2 * h * w * h * w * dim
# scale
muls += heads * h * w * h * w
return muls, adds
def fast_attn_ecoformer_flops(h, w, dim, heads):
H = heads
N = h * w
C = dim // heads
Nh = 16
muls = 0
adds = 0
# kernel
m = 25
adds += H * N * m * C
adds += H * N * m * C
muls += H * N * m * 2
# hashing, H, N, m, Nh
muls += H * N * m * Nh
adds += H * N * m * Nh
# out = linear_attention(q, k, v)
# k_cumsum = k.sum(dim=-2)
adds += H * N * Nh
# D_inv = 1. / (torch.einsum('...nd,...d->...n', q.float(), k_cumsum) + bottom_bias)
muls += H * N * Nh + H * N
adds += H * N * Nh
# context = torch.einsum('...nd,...ne->...de', k, v)
adds += H * N * Nh * C
# out = torch.einsum('...de,...nd->...ne', context, q) + top_bias
adds += H * N * Nh * C + H * N * C
# out2 = torch.einsum('...ne,...n->...ne', out, D_inv)
muls += H * N * C
return muls, adds
def main(args):
utils.init_distributed_mode(args)
print(args)
# fix the seed for reproducibility
seed = args.seed + utils.get_rank()
torch.manual_seed(seed)
np.random.seed(seed)
cudnn.benchmark = True
print(f"Creating model: {args.model}")
args.nb_classes = 1000
model = create_model(
args.model,
pretrained=False,
num_classes=args.nb_classes,
drop_rate=args.drop,
drop_path_rate=args.drop_path,
drop_block_rate=None,
)
print(model)
model.set_retrain_resume()
try:
from mmcv.cnn import get_model_complexity_info
from mmcv.cnn.utils.flops_counter import get_model_complexity_info
flops, params = get_model_complexity_info(
model, (3, 224, 224), as_strings=False, print_per_layer_stat=False
)
H = W = 224
# flop_func = attn_flops[args.attn_type]
muls = flops
adds = flops
for i in range(4):
# stage_muls, stage_adds = msa_flops(H // (4 * (2 ** i)), W // (4 * (2 ** i)), model.embed_dims[i],
# model.num_heads[i])
if i != 3:
if args.train_msa:
stage_muls, stage_adds = msa_flops(
H // (4 * (2 ** i)),
W // (4 * (2 ** i)),
model.embed_dims[i],
model.num_heads[i],
)
else:
stage_muls, stage_adds = fast_attn_ecoformer_flops(
H // (4 * (2 ** i)),
W // (4 * (2 ** i)),
model.embed_dims[i],
model.num_heads[i],
)
else:
stage_muls, stage_adds = msa_flops(
H // (4 * (2 ** i)),
W // (4 * (2 ** i)),
model.embed_dims[i],
model.num_heads[i],
)
muls += stage_muls * model.depths[i]
adds += stage_adds * model.depths[i]
print("{:<30} {:<8}".format("Mul: ", round(muls / 1e9, 2)))
print("{:<30} {:<8}".format("Add: ", round(adds / 1e9, 2)))
print("{:<30} {:<8}".format("Energy: ", (muls * 3.7 + adds * 0.9) / 1e9))
print("{:<30} {:<8}".format("Area: ", (muls * 7700 + adds * 4184) / 1e9))
except ImportError:
raise ImportError("Please upgrade mmcv to >0.6.2")
if __name__ == "__main__":
# parser = argparse.ArgumentParser('Twins training and evaluation script', parents=[get_args_parser()])
# args = parser.parse_args()
# if args.output_dir:
# Path(args.output_dir).mkdir(parents=True, exist_ok=True)
main(args)