zhifu gao
2024-12-25 3f8294b9d7deaa0cbdb0b2ef6f3802d46ae133a9
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import math
import torch
import numpy as np
import torch.nn.functional as F
 
from funasr.models.scama.utils import sequence_mask
from funasr.models.transformer.utils.nets_utils import make_pad_mask
 
 
class overlap_chunk:
    """
    Author: Speech Lab of DAMO Academy, Alibaba Group
    San-m: Memory equipped self-attention for end-to-end speech recognition
    https://arxiv.org/abs/2006.01713
 
    """
 
    def __init__(
        self,
        chunk_size: tuple = (16,),
        stride: tuple = (10,),
        pad_left: tuple = (0,),
        encoder_att_look_back_factor: tuple = (1,),
        shfit_fsmn: int = 0,
        decoder_att_look_back_factor: tuple = (1,),
    ):
 
        pad_left = self.check_chunk_size_args(chunk_size, pad_left)
        encoder_att_look_back_factor = self.check_chunk_size_args(
            chunk_size, encoder_att_look_back_factor
        )
        decoder_att_look_back_factor = self.check_chunk_size_args(
            chunk_size, decoder_att_look_back_factor
        )
        (
            self.chunk_size,
            self.stride,
            self.pad_left,
            self.encoder_att_look_back_factor,
            self.decoder_att_look_back_factor,
        ) = (
            chunk_size,
            stride,
            pad_left,
            encoder_att_look_back_factor,
            decoder_att_look_back_factor,
        )
        self.shfit_fsmn = shfit_fsmn
        self.x_add_mask = None
        self.x_rm_mask = None
        self.x_len = None
        self.mask_shfit_chunk = None
        self.mask_chunk_predictor = None
        self.mask_att_chunk_encoder = None
        self.mask_shift_att_chunk_decoder = None
        self.chunk_outs = None
        (
            self.chunk_size_cur,
            self.stride_cur,
            self.pad_left_cur,
            self.encoder_att_look_back_factor_cur,
            self.chunk_size_pad_shift_cur,
        ) = (None, None, None, None, None)
 
    def check_chunk_size_args(self, chunk_size, x):
        if len(x) < len(chunk_size):
            x = [x[0] for i in chunk_size]
        return x
 
    def get_chunk_size(self, ind: int = 0):
        # with torch.no_grad:
        chunk_size, stride, pad_left, encoder_att_look_back_factor, decoder_att_look_back_factor = (
            self.chunk_size[ind],
            self.stride[ind],
            self.pad_left[ind],
            self.encoder_att_look_back_factor[ind],
            self.decoder_att_look_back_factor[ind],
        )
        (
            self.chunk_size_cur,
            self.stride_cur,
            self.pad_left_cur,
            self.encoder_att_look_back_factor_cur,
            self.chunk_size_pad_shift_cur,
            self.decoder_att_look_back_factor_cur,
        ) = (
            chunk_size,
            stride,
            pad_left,
            encoder_att_look_back_factor,
            chunk_size + self.shfit_fsmn,
            decoder_att_look_back_factor,
        )
        return (
            self.chunk_size_cur,
            self.stride_cur,
            self.pad_left_cur,
            self.encoder_att_look_back_factor_cur,
            self.chunk_size_pad_shift_cur,
        )
 
    def random_choice(self, training=True, decoding_ind=None):
        chunk_num = len(self.chunk_size)
        ind = 0
        if training and chunk_num > 1:
            ind = torch.randint(0, chunk_num, ()).cpu().item()
        if not training and decoding_ind is not None:
            ind = int(decoding_ind)
 
        return ind
 
    def gen_chunk_mask(self, x_len, ind=0, num_units=1, num_units_predictor=1):
 
        with torch.no_grad():
            x_len = x_len.cpu().numpy()
            x_len_max = x_len.max()
 
            chunk_size, stride, pad_left, encoder_att_look_back_factor, chunk_size_pad_shift = (
                self.get_chunk_size(ind)
            )
            shfit_fsmn = self.shfit_fsmn
            pad_right = chunk_size - stride - pad_left
 
            chunk_num_batch = np.ceil(x_len / stride).astype(np.int32)
            x_len_chunk = (
                (chunk_num_batch - 1) * chunk_size_pad_shift
                + shfit_fsmn
                + pad_left
                + 0
                + x_len
                - (chunk_num_batch - 1) * stride
            )
            x_len_chunk = x_len_chunk.astype(x_len.dtype)
            x_len_chunk_max = x_len_chunk.max()
 
            chunk_num = int(math.ceil(x_len_max / stride))
            dtype = np.int32
            max_len_for_x_mask_tmp = max(chunk_size, x_len_max + pad_left)
            x_add_mask = np.zeros([0, max_len_for_x_mask_tmp], dtype=dtype)
            x_rm_mask = np.zeros([max_len_for_x_mask_tmp, 0], dtype=dtype)
            mask_shfit_chunk = np.zeros([0, num_units], dtype=dtype)
            mask_chunk_predictor = np.zeros([0, num_units_predictor], dtype=dtype)
            mask_shift_att_chunk_decoder = np.zeros([0, 1], dtype=dtype)
            mask_att_chunk_encoder = np.zeros([0, chunk_num * chunk_size_pad_shift], dtype=dtype)
            for chunk_ids in range(chunk_num):
                # x_mask add
                fsmn_padding = np.zeros((shfit_fsmn, max_len_for_x_mask_tmp), dtype=dtype)
                x_mask_cur = np.diag(np.ones(chunk_size, dtype=np.float32))
                x_mask_pad_left = np.zeros((chunk_size, chunk_ids * stride), dtype=dtype)
                x_mask_pad_right = np.zeros((chunk_size, max_len_for_x_mask_tmp), dtype=dtype)
                x_cur_pad = np.concatenate([x_mask_pad_left, x_mask_cur, x_mask_pad_right], axis=1)
                x_cur_pad = x_cur_pad[:chunk_size, :max_len_for_x_mask_tmp]
                x_add_mask_fsmn = np.concatenate([fsmn_padding, x_cur_pad], axis=0)
                x_add_mask = np.concatenate([x_add_mask, x_add_mask_fsmn], axis=0)
 
                # x_mask rm
                fsmn_padding = np.zeros((max_len_for_x_mask_tmp, shfit_fsmn), dtype=dtype)
                padding_mask_left = np.zeros((max_len_for_x_mask_tmp, pad_left), dtype=dtype)
                padding_mask_right = np.zeros((max_len_for_x_mask_tmp, pad_right), dtype=dtype)
                x_mask_cur = np.diag(np.ones(stride, dtype=dtype))
                x_mask_cur_pad_top = np.zeros((chunk_ids * stride, stride), dtype=dtype)
                x_mask_cur_pad_bottom = np.zeros((max_len_for_x_mask_tmp, stride), dtype=dtype)
                x_rm_mask_cur = np.concatenate(
                    [x_mask_cur_pad_top, x_mask_cur, x_mask_cur_pad_bottom], axis=0
                )
                x_rm_mask_cur = x_rm_mask_cur[:max_len_for_x_mask_tmp, :stride]
                x_rm_mask_cur_fsmn = np.concatenate(
                    [fsmn_padding, padding_mask_left, x_rm_mask_cur, padding_mask_right], axis=1
                )
                x_rm_mask = np.concatenate([x_rm_mask, x_rm_mask_cur_fsmn], axis=1)
 
                # fsmn_padding_mask
                pad_shfit_mask = np.zeros([shfit_fsmn, num_units], dtype=dtype)
                ones_1 = np.ones([chunk_size, num_units], dtype=dtype)
                mask_shfit_chunk_cur = np.concatenate([pad_shfit_mask, ones_1], axis=0)
                mask_shfit_chunk = np.concatenate([mask_shfit_chunk, mask_shfit_chunk_cur], axis=0)
 
                # predictor mask
                zeros_1 = np.zeros([shfit_fsmn + pad_left, num_units_predictor], dtype=dtype)
                ones_2 = np.ones([stride, num_units_predictor], dtype=dtype)
                zeros_3 = np.zeros(
                    [chunk_size - stride - pad_left, num_units_predictor], dtype=dtype
                )
                ones_zeros = np.concatenate([ones_2, zeros_3], axis=0)
                mask_chunk_predictor_cur = np.concatenate([zeros_1, ones_zeros], axis=0)
                mask_chunk_predictor = np.concatenate(
                    [mask_chunk_predictor, mask_chunk_predictor_cur], axis=0
                )
 
                # encoder att mask
                zeros_1_top = np.zeros([shfit_fsmn, chunk_num * chunk_size_pad_shift], dtype=dtype)
 
                zeros_2_num = max(chunk_ids - encoder_att_look_back_factor, 0)
                zeros_2 = np.zeros([chunk_size, zeros_2_num * chunk_size_pad_shift], dtype=dtype)
 
                encoder_att_look_back_num = max(chunk_ids - zeros_2_num, 0)
                zeros_2_left = np.zeros([chunk_size, shfit_fsmn], dtype=dtype)
                ones_2_mid = np.ones([stride, stride], dtype=dtype)
                zeros_2_bottom = np.zeros([chunk_size - stride, stride], dtype=dtype)
                zeros_2_right = np.zeros([chunk_size, chunk_size - stride], dtype=dtype)
                ones_2 = np.concatenate([ones_2_mid, zeros_2_bottom], axis=0)
                ones_2 = np.concatenate([zeros_2_left, ones_2, zeros_2_right], axis=1)
                ones_2 = np.tile(ones_2, [1, encoder_att_look_back_num])
 
                zeros_3_left = np.zeros([chunk_size, shfit_fsmn], dtype=dtype)
                ones_3_right = np.ones([chunk_size, chunk_size], dtype=dtype)
                ones_3 = np.concatenate([zeros_3_left, ones_3_right], axis=1)
 
                zeros_remain_num = max(chunk_num - 1 - chunk_ids, 0)
                zeros_remain = np.zeros(
                    [chunk_size, zeros_remain_num * chunk_size_pad_shift], dtype=dtype
                )
 
                ones2_bottom = np.concatenate([zeros_2, ones_2, ones_3, zeros_remain], axis=1)
                mask_att_chunk_encoder_cur = np.concatenate([zeros_1_top, ones2_bottom], axis=0)
                mask_att_chunk_encoder = np.concatenate(
                    [mask_att_chunk_encoder, mask_att_chunk_encoder_cur], axis=0
                )
 
                # decoder fsmn_shift_att_mask
                zeros_1 = np.zeros([shfit_fsmn, 1])
                ones_1 = np.ones([chunk_size, 1])
                mask_shift_att_chunk_decoder_cur = np.concatenate([zeros_1, ones_1], axis=0)
                mask_shift_att_chunk_decoder = np.concatenate(
                    [mask_shift_att_chunk_decoder, mask_shift_att_chunk_decoder_cur], axis=0
                )
 
            self.x_add_mask = x_add_mask[:x_len_chunk_max, : x_len_max + pad_left]
            self.x_len_chunk = x_len_chunk
            self.x_rm_mask = x_rm_mask[:x_len_max, :x_len_chunk_max]
            self.x_len = x_len
            self.mask_shfit_chunk = mask_shfit_chunk[:x_len_chunk_max, :]
            self.mask_chunk_predictor = mask_chunk_predictor[:x_len_chunk_max, :]
            self.mask_att_chunk_encoder = mask_att_chunk_encoder[:x_len_chunk_max, :x_len_chunk_max]
            self.mask_shift_att_chunk_decoder = mask_shift_att_chunk_decoder[:x_len_chunk_max, :]
            self.chunk_outs = (
                self.x_add_mask,
                self.x_len_chunk,
                self.x_rm_mask,
                self.x_len,
                self.mask_shfit_chunk,
                self.mask_chunk_predictor,
                self.mask_att_chunk_encoder,
                self.mask_shift_att_chunk_decoder,
            )
 
        return self.chunk_outs
 
    def split_chunk(self, x, x_len, chunk_outs):
        """
        :param x: (b, t, d)
        :param x_length: (b)
        :param ind: int
        :return:
        """
        x = x[:, : x_len.max(), :]
        b, t, d = x.size()
        x_len_mask = (~make_pad_mask(x_len, maxlen=t)).to(x.device)
        x *= x_len_mask[:, :, None]
 
        x_add_mask = self.get_x_add_mask(chunk_outs, x.device, dtype=x.dtype)
        x_len_chunk = self.get_x_len_chunk(chunk_outs, x_len.device, dtype=x_len.dtype)
        pad = (0, 0, self.pad_left_cur, 0)
        x = F.pad(x, pad, "constant", 0.0)
        b, t, d = x.size()
        x = torch.transpose(x, 1, 0)
        x = torch.reshape(x, [t, -1])
        x_chunk = torch.mm(x_add_mask, x)
        x_chunk = torch.reshape(x_chunk, [-1, b, d]).transpose(1, 0)
 
        return x_chunk, x_len_chunk
 
    def remove_chunk(self, x_chunk, x_len_chunk, chunk_outs):
        x_chunk = x_chunk[:, : x_len_chunk.max(), :]
        b, t, d = x_chunk.size()
        x_len_chunk_mask = (~make_pad_mask(x_len_chunk, maxlen=t)).to(x_chunk.device)
        x_chunk *= x_len_chunk_mask[:, :, None]
 
        x_rm_mask = self.get_x_rm_mask(chunk_outs, x_chunk.device, dtype=x_chunk.dtype)
        x_len = self.get_x_len(chunk_outs, x_len_chunk.device, dtype=x_len_chunk.dtype)
        x_chunk = torch.transpose(x_chunk, 1, 0)
        x_chunk = torch.reshape(x_chunk, [t, -1])
        x = torch.mm(x_rm_mask, x_chunk)
        x = torch.reshape(x, [-1, b, d]).transpose(1, 0)
 
        return x, x_len
 
    def get_x_add_mask(self, chunk_outs=None, device="cpu", idx=0, dtype=torch.float32):
        with torch.no_grad():
            x = chunk_outs[idx] if chunk_outs is not None else self.chunk_outs[idx]
            x = torch.from_numpy(x).type(dtype).to(device)
        return x
 
    def get_x_len_chunk(self, chunk_outs=None, device="cpu", idx=1, dtype=torch.float32):
        with torch.no_grad():
            x = chunk_outs[idx] if chunk_outs is not None else self.chunk_outs[idx]
            x = torch.from_numpy(x).type(dtype).to(device)
        return x
 
    def get_x_rm_mask(self, chunk_outs=None, device="cpu", idx=2, dtype=torch.float32):
        with torch.no_grad():
            x = chunk_outs[idx] if chunk_outs is not None else self.chunk_outs[idx]
            x = torch.from_numpy(x).type(dtype).to(device)
        return x
 
    def get_x_len(self, chunk_outs=None, device="cpu", idx=3, dtype=torch.float32):
        with torch.no_grad():
            x = chunk_outs[idx] if chunk_outs is not None else self.chunk_outs[idx]
            x = torch.from_numpy(x).type(dtype).to(device)
        return x
 
    def get_mask_shfit_chunk(
        self, chunk_outs=None, device="cpu", batch_size=1, num_units=1, idx=4, dtype=torch.float32
    ):
        with torch.no_grad():
            x = chunk_outs[idx] if chunk_outs is not None else self.chunk_outs[idx]
            x = np.tile(
                x[
                    None,
                    :,
                    :,
                ],
                [batch_size, 1, num_units],
            )
            x = torch.from_numpy(x).type(dtype).to(device)
        return x
 
    def get_mask_chunk_predictor(
        self, chunk_outs=None, device="cpu", batch_size=1, num_units=1, idx=5, dtype=torch.float32
    ):
        with torch.no_grad():
            x = chunk_outs[idx] if chunk_outs is not None else self.chunk_outs[idx]
            x = np.tile(
                x[
                    None,
                    :,
                    :,
                ],
                [batch_size, 1, num_units],
            )
            x = torch.from_numpy(x).type(dtype).to(device)
        return x
 
    def get_mask_att_chunk_encoder(
        self, chunk_outs=None, device="cpu", batch_size=1, idx=6, dtype=torch.float32
    ):
        with torch.no_grad():
            x = chunk_outs[idx] if chunk_outs is not None else self.chunk_outs[idx]
            x = np.tile(
                x[
                    None,
                    :,
                    :,
                ],
                [batch_size, 1, 1],
            )
            x = torch.from_numpy(x).type(dtype).to(device)
        return x
 
    def get_mask_shift_att_chunk_decoder(
        self, chunk_outs=None, device="cpu", batch_size=1, idx=7, dtype=torch.float32
    ):
        with torch.no_grad():
            x = chunk_outs[idx] if chunk_outs is not None else self.chunk_outs[idx]
            x = np.tile(x[None, None, :, 0], [batch_size, 1, 1])
            x = torch.from_numpy(x).type(dtype).to(device)
        return x
 
 
def build_scama_mask_for_cross_attention_decoder(
    predictor_alignments: torch.Tensor,
    encoder_sequence_length: torch.Tensor,
    chunk_size: int = 5,
    encoder_chunk_size: int = 5,
    attention_chunk_center_bias: int = 0,
    attention_chunk_size: int = 1,
    attention_chunk_type: str = "chunk",
    step=None,
    predictor_mask_chunk_hopping: torch.Tensor = None,
    decoder_att_look_back_factor: int = 1,
    mask_shift_att_chunk_decoder: torch.Tensor = None,
    target_length: torch.Tensor = None,
    is_training=True,
    dtype: torch.dtype = torch.float32,
):
    with torch.no_grad():
        device = predictor_alignments.device
        batch_size, chunk_num = predictor_alignments.size()
        maximum_encoder_length = encoder_sequence_length.max().item()
        int_type = predictor_alignments.dtype
        if not is_training:
            target_length = predictor_alignments.sum(dim=-1).type(encoder_sequence_length.dtype)
        maximum_target_length = target_length.max()
        predictor_alignments_cumsum = torch.cumsum(predictor_alignments, dim=1)
        predictor_alignments_cumsum = predictor_alignments_cumsum[:, None, :].repeat(
            1, maximum_target_length, 1
        )
 
        index = torch.ones([batch_size, maximum_target_length], dtype=int_type).to(device)
        index = torch.cumsum(index, dim=1)
        index = index[:, :, None].repeat(1, 1, chunk_num)
 
        index_div = torch.floor(torch.divide(predictor_alignments_cumsum, index)).type(int_type)
        index_div_bool_zeros = index_div == 0
        index_div_bool_zeros_count = torch.sum(index_div_bool_zeros.type(int_type), dim=-1) + 1
 
        index_div_bool_zeros_count = torch.clip(index_div_bool_zeros_count, min=1, max=chunk_num)
 
        index_div_bool_zeros_count *= chunk_size
        index_div_bool_zeros_count += attention_chunk_center_bias
        index_div_bool_zeros_count = torch.clip(
            index_div_bool_zeros_count - 1, min=0, max=maximum_encoder_length
        )
        index_div_bool_zeros_count_ori = index_div_bool_zeros_count
 
        index_div_bool_zeros_count = (
            torch.floor(index_div_bool_zeros_count / encoder_chunk_size) + 1
        ) * encoder_chunk_size
        max_len_chunk = math.ceil(maximum_encoder_length / encoder_chunk_size) * encoder_chunk_size
 
        mask_flip, mask_flip2 = None, None
        if attention_chunk_size is not None:
            index_div_bool_zeros_count_beg = index_div_bool_zeros_count - attention_chunk_size
            index_div_bool_zeros_count_beg = torch.clip(
                index_div_bool_zeros_count_beg, 0, max_len_chunk
            )
            index_div_bool_zeros_count_beg_mask = sequence_mask(
                index_div_bool_zeros_count_beg, maxlen=max_len_chunk, dtype=int_type, device=device
            )
            mask_flip = 1 - index_div_bool_zeros_count_beg_mask
            attention_chunk_size2 = attention_chunk_size * (decoder_att_look_back_factor + 1)
            index_div_bool_zeros_count_beg = index_div_bool_zeros_count - attention_chunk_size2
 
            index_div_bool_zeros_count_beg = torch.clip(
                index_div_bool_zeros_count_beg, 0, max_len_chunk
            )
            index_div_bool_zeros_count_beg_mask = sequence_mask(
                index_div_bool_zeros_count_beg, maxlen=max_len_chunk, dtype=int_type, device=device
            )
            mask_flip2 = 1 - index_div_bool_zeros_count_beg_mask
 
        mask = sequence_mask(
            index_div_bool_zeros_count, maxlen=max_len_chunk, dtype=dtype, device=device
        )
 
        if predictor_mask_chunk_hopping is not None:
            b, k, t = mask.size()
            predictor_mask_chunk_hopping = predictor_mask_chunk_hopping[:, None, :, 0].repeat(
                1, k, 1
            )
 
            mask_mask_flip = mask
            if mask_flip is not None:
                mask_mask_flip = mask_flip * mask
 
            def _fn():
                mask_sliced = mask[:b, :k, encoder_chunk_size:t]
                zero_pad_right = torch.zeros(
                    [b, k, encoder_chunk_size], dtype=mask_sliced.dtype
                ).to(device)
                mask_sliced = torch.cat([mask_sliced, zero_pad_right], dim=2)
                _, _, tt = predictor_mask_chunk_hopping.size()
                pad_right_p = max_len_chunk - tt
                predictor_mask_chunk_hopping_pad = torch.nn.functional.pad(
                    predictor_mask_chunk_hopping, [0, pad_right_p], "constant", 0
                )
                masked = mask_sliced * predictor_mask_chunk_hopping_pad
 
                mask_true = mask_mask_flip + masked
                return mask_true
 
            mask = _fn() if t > chunk_size else mask_mask_flip
 
        if mask_flip2 is not None:
            mask *= mask_flip2
 
        mask_target = sequence_mask(
            target_length, maxlen=maximum_target_length, dtype=mask.dtype, device=device
        )
        mask = mask[:, :maximum_target_length, :] * mask_target[:, :, None]
 
        mask_len = sequence_mask(
            encoder_sequence_length, maxlen=maximum_encoder_length, dtype=mask.dtype, device=device
        )
        mask = mask[:, :, :maximum_encoder_length] * mask_len[:, None, :]
 
        if attention_chunk_type == "full":
            mask = torch.ones_like(mask).to(device)
        if mask_shift_att_chunk_decoder is not None:
            mask = mask * mask_shift_att_chunk_decoder
        mask = mask[:, :maximum_target_length, :maximum_encoder_length].type(dtype).to(device)
 
    return mask