-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtest_rescorer.py
More file actions
277 lines (221 loc) · 8.06 KB
/
Copy pathtest_rescorer.py
File metadata and controls
277 lines (221 loc) · 8.06 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
def test_rnnlmrescorer(tmpdir, device):
import torch
from sentencepiece import SentencePieceProcessor
from speechbrain.lobes.models.RNNLM import RNNLM
from speechbrain.utils.parameter_transfer import Pretrainer
source = "speechbrain/asr-crdnn-rnnlm-librispeech"
lm_model_path = source + "/lm.ckpt"
tokenizer_path = source + "/tokenizer.ckpt"
# Define your tokenizer and RNNLM from the HF hub
tokenizer = SentencePieceProcessor()
lm_model = RNNLM(
output_neurons=1000,
embedding_dim=128,
activation=torch.nn.LeakyReLU,
dropout=0.0,
rnn_layers=2,
rnn_neurons=2048,
dnn_blocks=1,
dnn_neurons=512,
return_hidden=True,
)
pretrainer = Pretrainer(
collect_in=tmpdir,
loadables={"lm": lm_model, "tokenizer": tokenizer},
paths={"lm": lm_model_path, "tokenizer": tokenizer_path},
)
pretrainer.collect_files()
pretrainer.load_collected()
from speechbrain.decoders.scorer import RescorerBuilder, RNNLMRescorer
rnnlm_rescorer = RNNLMRescorer(
language_model=lm_model,
tokenizer=tokenizer,
temperature=1.0,
bos_index=0,
eos_index=0,
pad_index=0,
)
# Define a rescorer builder
rescorer = RescorerBuilder(
rescorers=[rnnlm_rescorer], weights={"rnnlm": 1.0}
)
# Topk hypotheses
topk_hyps = [["HELLO", "HE LLO", "H E L L O"]]
topk_scores = [[-2, -2, -2]]
rescored_hyps, rescored_scores = rescorer.rescore(topk_hyps, topk_scores)
# check all hyps are still there
for hyp in topk_hyps[0]:
assert hyp in rescored_hyps[0]
# check rescored_scores are sorted
for i in range(len(rescored_scores[0]) - 1):
assert rescored_scores[0][i] >= rescored_scores[0][i + 1]
# check normalized_text is working
text = "hello"
normalized_text = rnnlm_rescorer.normalize_text(text)
assert normalized_text == text.upper()
# check lm is on the right device
rnnlm_rescorer.to_device(device)
assert rnnlm_rescorer.lm.parameters().__next__().device.type == device
# check preprocess_func
padded_hyps, enc_hyps_length = rnnlm_rescorer.preprocess_func(topk_hyps)
assert padded_hyps.shape[0] == 3
assert len(padded_hyps) == 3
def test_transformerlmrescorer(tmpdir, device):
import torch
from sentencepiece import SentencePieceProcessor
from speechbrain.lobes.models.transformer.TransformerLM import TransformerLM
from speechbrain.utils.parameter_transfer import Pretrainer
source = "speechbrain/asr-transformer-transformerlm-librispeech"
lm_model_path = source + "/lm.ckpt"
tokenizer_path = source + "/tokenizer.ckpt"
tokenizer = SentencePieceProcessor()
lm_model = TransformerLM(
vocab=5000,
d_model=768,
nhead=12,
num_encoder_layers=12,
num_decoder_layers=0,
d_ffn=3072,
dropout=0.0,
activation=torch.nn.GELU,
normalize_before=False,
)
pretrainer = Pretrainer(
collect_in=tmpdir,
loadables={"lm": lm_model, "tokenizer": tokenizer},
paths={"lm": lm_model_path, "tokenizer": tokenizer_path},
)
_ = pretrainer.collect_files()
pretrainer.load_collected()
from speechbrain.decoders.scorer import (
RescorerBuilder,
TransformerLMRescorer,
)
transformerlm_rescorer = TransformerLMRescorer(
language_model=lm_model,
tokenizer=tokenizer,
temperature=1.0,
bos_index=1,
eos_index=2,
pad_index=0,
)
rescorer = RescorerBuilder(
rescorers=[transformerlm_rescorer], weights={"transformerlm": 1.0}
)
# Topk hypotheses
topk_hyps = [["HELLO", "HE LLO", "H E L L O"]]
topk_scores = [[-2, -2, -2]]
rescored_hyps, rescored_scores = rescorer.rescore(topk_hyps, topk_scores)
# check all hyps are still there
for hyp in topk_hyps[0]:
assert hyp in rescored_hyps[0]
# check rescored_scores are sorted
for i in range(len(rescored_scores[0]) - 1):
assert rescored_scores[0][i] >= rescored_scores[0][i + 1]
# check normalized_text is working
text = "hello"
normalized_text = transformerlm_rescorer.normalize_text(text)
assert normalized_text == text.upper()
# check lm is on the right device
transformerlm_rescorer.to_device(device)
assert (
transformerlm_rescorer.lm.parameters().__next__().device.type == device
)
# check preprocess_func
padded_hyps, enc_hyps_length = transformerlm_rescorer.preprocess_func(
topk_hyps
)
assert padded_hyps.shape[0] == 3
assert len(padded_hyps) == 3
def test_huggingfacelmrescorer(device):
from speechbrain.decoders.scorer import (
HuggingFaceLMRescorer,
RescorerBuilder,
)
source = "gpt2-medium"
huggingfacelm_rescorer = HuggingFaceLMRescorer(model_name=source)
rescorer = RescorerBuilder(
rescorers=[huggingfacelm_rescorer], weights={"huggingfacelm": 1.0}
)
# Topk hypotheses
topk_hyps = [["HELLO", "HE LLO", "H E L L O"]]
topk_scores = [[-2, -2, -2]]
rescored_hyps, rescored_scores = rescorer.rescore(topk_hyps, topk_scores)
# check all hyps are still there
for hyp in topk_hyps[0]:
assert hyp in rescored_hyps[0]
# check rescored_scores are sorted
for i in range(len(rescored_scores[0]) - 1):
assert rescored_scores[0][i] >= rescored_scores[0][i + 1]
# check normalized_text is working
text = "hello"
normalized_text = huggingfacelm_rescorer.normalize_text(text)
assert normalized_text == text
# check lm is on the right device
huggingfacelm_rescorer.to_device(device)
assert huggingfacelm_rescorer.lm.device.type == device
# check preprocess_func
padded_hyps = huggingfacelm_rescorer.preprocess_func(topk_hyps)
assert padded_hyps.input_ids.shape[0] == 3
def test_rescorerbuilder(tmpdir, device):
import torch
from sentencepiece import SentencePieceProcessor
from speechbrain.lobes.models.RNNLM import RNNLM
from speechbrain.utils.parameter_transfer import Pretrainer
source = "speechbrain/asr-crdnn-rnnlm-librispeech"
lm_model_path = source + "/lm.ckpt"
tokenizer_path = source + "/tokenizer.ckpt"
# Define your tokenizer and RNNLM from the HF hub
tokenizer = SentencePieceProcessor()
lm_model = RNNLM(
output_neurons=1000,
embedding_dim=128,
activation=torch.nn.LeakyReLU,
dropout=0.0,
rnn_layers=2,
rnn_neurons=2048,
dnn_blocks=1,
dnn_neurons=512,
return_hidden=True,
)
pretrainer = Pretrainer(
collect_in=tmpdir,
loadables={"lm": lm_model, "tokenizer": tokenizer},
paths={"lm": lm_model_path, "tokenizer": tokenizer_path},
)
pretrainer.collect_files()
pretrainer.load_collected()
from speechbrain.decoders.scorer import (
HuggingFaceLMRescorer,
RescorerBuilder,
RNNLMRescorer,
)
rnnlm_rescorer = RNNLMRescorer(
language_model=lm_model,
tokenizer=tokenizer,
temperature=1.0,
bos_index=0,
eos_index=0,
pad_index=0,
)
source = "gpt2-medium"
huggingfacelm_rescorer = HuggingFaceLMRescorer(model_name=source)
# check combine both rescorers
rescorer = RescorerBuilder(
rescorers=[rnnlm_rescorer, huggingfacelm_rescorer],
weights={"rnnlm": 1.0, "huggingfacelm": 1.0},
)
rescorer.move_rescorers_to_device(device)
# check lm is on the right device
assert rnnlm_rescorer.lm.parameters().__next__().device.type == device
assert huggingfacelm_rescorer.lm.device.type == device
# Topk hypotheses
topk_hyps = [["HELLO", "HE LLO", "H E L L O"]]
topk_scores = [[-2, -2, -2]]
rescored_hyps, rescored_scores = rescorer.rescore(topk_hyps, topk_scores)
# check all hyps are still there
for hyp in topk_hyps[0]:
assert hyp in rescored_hyps[0]
# check rescored_scores are sorted
for i in range(len(rescored_scores[0]) - 1):
assert rescored_scores[0][i] >= rescored_scores[0][i + 1]