-
Notifications
You must be signed in to change notification settings - Fork 10
/
benchmark_warp_rnnt.py
executable file
·159 lines (128 loc) · 3.9 KB
/
benchmark_warp_rnnt.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
#!/usr/bin/env python3
#
# Copyright 2022 Xiaomi Corp. (authors: Fangjun Kuang)
#
# See ../LICENSE for clarification regarding multiple authors
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import torch
import warp_rnnt
from torch.profiler import ProfilerActivity, record_function
from utils import (
Joiner,
ShapeGenerator,
SortedShapeGenerator,
generate_data,
str2bool,
)
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--sort-utterance",
type=str2bool,
help="True to sort utterance duration before batching them up",
)
return parser.parse_args()
def compute_loss(logits, logit_lengths, targets, target_lengths):
with record_function("warp-rnnt"):
loss = warp_rnnt.rnnt_loss(
log_probs=logits.log_softmax(dim=-1),
labels=targets,
frames_lengths=logit_lengths,
labels_lengths=target_lengths,
average_frames=False,
reduction="sum",
blank=0,
gather=True,
fastemit_lambda=0.0,
)
loss.backward()
def main():
args = get_args()
device = torch.device("cpu")
if torch.cuda.is_available():
device = torch.device("cuda", 0)
print(f"device: {device}")
encoder_out_dim = 512
vocab_size = 500
if args.sort_utterance:
max_frames = 10000
suffix = f"max-frames-{max_frames}"
else:
# CUDA OOM when it is 50
batch_size = 30
suffix = batch_size
joiner = Joiner(encoder_out_dim, vocab_size)
joiner.to(device)
if args.sort_utterance:
shape_generator = SortedShapeGenerator(max_frames)
else:
shape_generator = ShapeGenerator(batch_size)
print(f"Benchmarking started (Sort utterance {args.sort_utterance})")
prof = torch.profiler.profile(
activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA],
schedule=torch.profiler.schedule(
wait=10, warmup=10, active=20, repeat=2
),
on_trace_ready=torch.profiler.tensorboard_trace_handler(
f"./log/warp-rnnt-{suffix}"
),
record_shapes=True,
with_stack=True,
profile_memory=True,
)
prof.start()
for i, shape_info in enumerate(shape_generator):
print("i", i)
(
encoder_out,
encoder_out_lengths,
decoder_out,
targets,
target_lengths,
) = generate_data(
shape_info,
vocab_size=vocab_size,
encoder_out_dim=encoder_out_dim,
device=device,
)
encoder_out = encoder_out.unsqueeze(2)
# Now encoder_out is (N, T, 1, C)
decoder_out = decoder_out.unsqueeze(1)
# Now decoder_out is (N, 1, U, C)
x = encoder_out + decoder_out
logits = joiner(x)
compute_loss(
logits,
encoder_out_lengths,
targets,
target_lengths,
)
joiner.zero_grad()
if i > 80:
break
prof.step()
prof.stop()
print("Benchmarking done")
s = str(
prof.key_averages(group_by_stack_n=10).table(
sort_by="self_cuda_time_total", row_limit=8
)
)
with open(f"warp-rnnt-{suffix}.txt", "w") as f:
f.write(s + "\n")
if __name__ == "__main__":
torch.manual_seed(20220227)
main()