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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
"""RNN encoder implementation for transducer-based models.

These classes are based on the ones in espnet.nets.pytorch_backend.rnn.encoders,
and modified to output intermediate layers representation based on a list of
layers given as input. These additional outputs are intended to be used with
auxiliary tasks.
It should be noted that, here, RNN class rely on a stack of 1-layer LSTM instead
of a multi-layer LSTM for that purpose.

"""

import argparse
import logging
from typing import List
from typing import Optional
from typing import Tuple
from typing import Union

import numpy as np
import torch
import torch.nn.functional as F
from torch.nn.utils.rnn import pack_padded_sequence
from torch.nn.utils.rnn import pad_packed_sequence

from espnet.nets.e2e_asr_common import get_vgg2l_odim
from espnet.nets.pytorch_backend.nets_utils import make_pad_mask
from espnet.nets.pytorch_backend.nets_utils import to_device


class RNNP(torch.nn.Module):
    """RNN with projection layer module.

    Args:
        idim: Dimension of inputs
        elayers: Dimension of encoder layers
        cdim: Number of units (results in cdim * 2 if bidirectional)
        hdim: Number of projection units
        subsample: List of subsampling number
        dropout: Dropout rate
        typ: RNN type
        aux_task_layer_list: List of layer ids for intermediate output

    """

    def __init__(
        self,
        idim: int,
        elayers: int,
        cdim: int,
        hdim: int,
        subsample: np.ndarray,
        dropout: float,
        typ: str = "blstm",
        aux_task_layer_list: List = [],
    ):
        """Initialize RNNP module."""
        super(RNNP, self).__init__()

        bidir = typ[0] == "b"
        for i in range(elayers):
            if i == 0:
                inputdim = idim
            else:
                inputdim = hdim

            RNN = torch.nn.LSTM if "lstm" in typ else torch.nn.GRU
            rnn = RNN(
                inputdim, cdim, num_layers=1, bidirectional=bidir, batch_first=True
            )

            setattr(self, "%s%d" % ("birnn" if bidir else "rnn", i), rnn)

            if bidir:
                setattr(self, "bt%d" % i, torch.nn.Linear(2 * cdim, hdim))
            else:
                setattr(self, "bt%d" % i, torch.nn.Linear(cdim, hdim))

        self.elayers = elayers
        self.cdim = cdim
        self.subsample = subsample
        self.typ = typ
        self.bidir = bidir
        self.dropout = dropout

        self.aux_task_layer_list = aux_task_layer_list

    def forward(
        self,
        xs_pad: torch.Tensor,
        ilens: torch.Tensor,
        prev_state: Optional[torch.Tensor] = None,
    ) -> Union[Tuple[torch.Tensor, List], torch.Tensor]:
        """RNNP forward.

        Args:
            xs_pad: Batch of padded input sequences (B, Tmax, idim)
            ilens: Batch of lengths of input sequences (B)
            prev_state: Batch of previous RNN states

        Returns:
            : Batch of padded output sequences (B, Tmax, hdim)
                    or tuple w/ aux outputs ((B, Tmax, hdim), [L x (B, Tmax, hdim)])
            : Batch of lengths of output sequences (B)
            : Batch of hidden state sequences (B, Tmax, hdim)

        """
        logging.debug(self.__class__.__name__ + " input lengths: " + str(ilens))

        aux_xs_list = []
        elayer_states = []
        for layer in range(self.elayers):
            if not isinstance(ilens, torch.Tensor):
                ilens = torch.tensor(ilens)

            xs_pack = pack_padded_sequence(xs_pad, ilens.cpu(), batch_first=True)
            rnn = getattr(self, ("birnn" if self.bidir else "rnn") + str(layer))
            rnn.flatten_parameters()

            if prev_state is not None and rnn.bidirectional:
                prev_state = reset_backward_rnn_state(prev_state)

            ys, states = rnn(
                xs_pack, hx=None if prev_state is None else prev_state[layer]
            )
            elayer_states.append(states)

            ys_pad, ilens = pad_packed_sequence(ys, batch_first=True)

            sub = self.subsample[layer + 1]
            if sub > 1:
                ys_pad = ys_pad[:, ::sub]
                ilens = torch.tensor([int(i + 1) // sub for i in ilens])

            projection_layer = getattr(self, "bt%d" % layer)
            projected = projection_layer(ys_pad.contiguous().view(-1, ys_pad.size(2)))
            xs_pad = projected.view(ys_pad.size(0), ys_pad.size(1), -1)

            if layer in self.aux_task_layer_list:
                aux_xs_list.append(xs_pad)

            if layer < self.elayers - 1:
                xs_pad = torch.tanh(F.dropout(xs_pad, p=self.dropout))

        if aux_xs_list:
            return (xs_pad, aux_xs_list), ilens, elayer_states
        else:
            return xs_pad, ilens, elayer_states


class RNN(torch.nn.Module):
    """RNN module.

    Args:
        idim: Dimension of inputs
        elayers: Number of encoder layers
        cdim: Number of rnn units (resulted in cdim * 2 if bidirectional)
        hdim: Number of final projection units
        dropout: Dropout rate
        typ: The RNN type

    """

    def __init__(
        self,
        idim: int,
        elayers: int,
        cdim: int,
        hdim: int,
        dropout: float,
        typ: str = "blstm",
        aux_task_layer_list: List = [],
    ):
        """Initialize RNN module."""
        super(RNN, self).__init__()

        bidir = typ[0] == "b"

        for i in range(elayers):
            if i == 0:
                inputdim = idim
            else:
                inputdim = cdim

            layer_type = torch.nn.LSTM if "lstm" in typ else torch.nn.GRU
            rnn = layer_type(
                inputdim, cdim, num_layers=1, bidirectional=bidir, batch_first=True
            )

            setattr(self, "%s%d" % ("birnn" if bidir else "rnn", i), rnn)

        self.dropout = torch.nn.Dropout(p=dropout)

        self.elayers = elayers
        self.cdim = cdim
        self.hdim = hdim
        self.typ = typ
        self.bidir = bidir

        self.l_last = torch.nn.Linear(cdim, hdim)

        self.aux_task_layer_list = aux_task_layer_list

    def forward(
        self,
        xs_pad: torch.Tensor,
        ilens: torch.Tensor,
        prev_state: Optional[torch.Tensor] = None,
    ) -> Union[Tuple[torch.Tensor, List], torch.Tensor]:
        """RNN forward.

        Args:
            xs_pad: Batch of padded input sequences (B, Tmax, idim)
            ilens: Batch of lengths of input sequences (B)
            prev_state: Batch of previous RNN states

        Returns:
            : Batch of padded output sequences (B, Tmax, hdim)
                    or tuple w/ aux outputs ((B, Tmax, hdim), [L x (B, Tmax, hdim)])
            : Batch of lengths of output sequences (B)
            : Batch of hidden state sequences (B, Tmax, hdim)

        """
        logging.debug(self.__class__.__name__ + " input lengths: " + str(ilens))

        aux_xs_list = []
        elayer_states = []
        for layer in range(self.elayers):
            if not isinstance(ilens, torch.Tensor):
                ilens = torch.tensor(ilens)

            xs_pack = pack_padded_sequence(xs_pad, ilens.cpu(), batch_first=True)

            rnn = getattr(self, ("birnn" if self.bidir else "rnn") + str(layer))
            rnn.flatten_parameters()

            if prev_state is not None and rnn.bidirectional:
                prev_state = reset_backward_rnn_state(prev_state)

            xs, states = rnn(
                xs_pack, hx=None if prev_state is None else prev_state[layer]
            )
            elayer_states.append(states)

            xs_pad, ilens = pad_packed_sequence(xs, batch_first=True)

            if self.bidir:
                xs_pad = xs_pad[:, :, : self.cdim] + xs_pad[:, :, self.cdim :]

            if layer in self.aux_task_layer_list:
                aux_projected = torch.tanh(
                    self.l_last(xs_pad.contiguous().view(-1, xs_pad.size(2)))
                )
                aux_xs_pad = aux_projected.view(xs_pad.size(0), xs_pad.size(1), -1)

                aux_xs_list.append(aux_xs_pad)

            if layer < self.elayers - 1:
                xs_pad = self.dropout(xs_pad)

        projected = torch.tanh(
            self.l_last(xs_pad.contiguous().view(-1, xs_pad.size(2)))
        )
        xs_pad = projected.view(xs_pad.size(0), xs_pad.size(1), -1)

        if aux_xs_list:
            return (xs_pad, aux_xs_list), ilens, elayer_states
        else:
            return xs_pad, ilens, elayer_states


def reset_backward_rnn_state(
    states: Union[torch.Tensor, Tuple, List]
) -> Union[torch.Tensor, Tuple, List]:
    """Set backward BRNN states to zeroes.

    Args:
        states: RNN states

    Returns:
        states: RNN states with backward set to zeroes

    """
    if isinstance(states, (list, tuple)):
        for state in states:
            state[1::2] = 0.0
    else:
        states[1::2] = 0.0
    return states


class VGG2L(torch.nn.Module):
    """VGG-like module.

    Args:
        in_channel: number of input channels

    """

    def __init__(self, in_channel: int = 1):
        """Initialize VGG-like module."""
        super(VGG2L, self).__init__()

        # CNN layer (VGG motivated)
        self.conv1_1 = torch.nn.Conv2d(in_channel, 64, 3, stride=1, padding=1)
        self.conv1_2 = torch.nn.Conv2d(64, 64, 3, stride=1, padding=1)
        self.conv2_1 = torch.nn.Conv2d(64, 128, 3, stride=1, padding=1)
        self.conv2_2 = torch.nn.Conv2d(128, 128, 3, stride=1, padding=1)

        self.in_channel = in_channel

    def forward(self, xs_pad: torch.Tensor, ilens: torch.Tensor, **kwargs):
        """VGG2L forward.

        Args:
            xs_pad: Batch of padded input sequences (B, Tmax, D)
            ilens: Batch of lengths of input sequences (B)

        Returns:
            : Batch of padded output sequences (B, Tmax // 4, 128 * D // 4)
            : Batch of lengths of output sequences (B)

        """
        logging.debug(self.__class__.__name__ + " input lengths: " + str(ilens))

        xs_pad = xs_pad.view(
            xs_pad.size(0),
            xs_pad.size(1),
            self.in_channel,
            xs_pad.size(2) // self.in_channel,
        ).transpose(1, 2)

        xs_pad = F.relu(self.conv1_1(xs_pad))
        xs_pad = F.relu(self.conv1_2(xs_pad))
        xs_pad = F.max_pool2d(xs_pad, 2, stride=2, ceil_mode=True)

        xs_pad = F.relu(self.conv2_1(xs_pad))
        xs_pad = F.relu(self.conv2_2(xs_pad))
        xs_pad = F.max_pool2d(xs_pad, 2, stride=2, ceil_mode=True)

        if torch.is_tensor(ilens):
            ilens = ilens.cpu().numpy()
        else:
            ilens = np.array(ilens, dtype=np.float32)
        ilens = np.array(np.ceil(ilens / 2), dtype=np.int64)
        ilens = np.array(
            np.ceil(np.array(ilens, dtype=np.float32) / 2), dtype=np.int64
        ).tolist()

        xs_pad = xs_pad.transpose(1, 2)
        xs_pad = xs_pad.contiguous().view(
            xs_pad.size(0), xs_pad.size(1), xs_pad.size(2) * xs_pad.size(3)
        )

        return xs_pad, ilens, None


class Encoder(torch.nn.Module):
    """Encoder module.

    Args:
        etype: Type of encoder network
        idim: Number of dimensions of encoder network
        elayers: Number of layers of encoder network
        eunits: Number of RNN units of encoder network
        eprojs: Number of projection units of encoder network
        subsample: List of subsampling numbers
        dropout: Dropout rate
        in_channel: Number of input channels

    """

    def __init__(
        self,
        etype: str,
        idim: int,
        elayers: int,
        eunits: int,
        eprojs: int,
        subsample: np.ndarray,
        dropout: float,
        in_channel: int = 1,
        aux_task_layer_list: List = [],
    ):
        """Initialize Encoder module."""
        super(Encoder, self).__init__()

        typ = etype.lstrip("vgg").rstrip("p")
        if typ not in ["lstm", "gru", "blstm", "bgru"]:
            logging.error("Error: need to specify an appropriate encoder architecture")

        if etype.startswith("vgg"):
            if etype[-1] == "p":
                self.enc = torch.nn.ModuleList(
                    [
                        VGG2L(in_channel),
                        RNNP(
                            get_vgg2l_odim(idim, in_channel=in_channel),
                            elayers,
                            eunits,
                            eprojs,
                            subsample,
                            dropout,
                            typ=typ,
                            aux_task_layer_list=aux_task_layer_list,
                        ),
                    ]
                )
                logging.info("Use CNN-VGG + " + typ.upper() + "P for encoder")
            else:
                self.enc = torch.nn.ModuleList(
                    [
                        VGG2L(in_channel),
                        RNN(
                            get_vgg2l_odim(idim, in_channel=in_channel),
                            elayers,
                            eunits,
                            eprojs,
                            dropout,
                            typ=typ,
                            aux_task_layer_list=aux_task_layer_list,
                        ),
                    ]
                )
                logging.info("Use CNN-VGG + " + typ.upper() + " for encoder")
            self.conv_subsampling_factor = 4
        else:
            if etype[-1] == "p":
                self.enc = torch.nn.ModuleList(
                    [
                        RNNP(
                            idim,
                            elayers,
                            eunits,
                            eprojs,
                            subsample,
                            dropout,
                            typ=typ,
                            aux_task_layer_list=aux_task_layer_list,
                        )
                    ]
                )
                logging.info(typ.upper() + " with every-layer projection for encoder")
            else:
                self.enc = torch.nn.ModuleList(
                    [
                        RNN(
                            idim,
                            elayers,
                            eunits,
                            eprojs,
                            dropout,
                            typ=typ,
                            aux_task_layer_list=aux_task_layer_list,
                        )
                    ]
                )
                logging.info(typ.upper() + " without projection for encoder")
            self.conv_subsampling_factor = 1

    def forward(self, xs_pad, ilens, prev_states=None):
        """Forward encoder.

        Args:
            xs_pad: Batch of padded input sequences (B, Tmax, idim)
            ilens: Batch of lengths of input sequences (B)
            prev_state: Batch of previous encoder hidden states (B, ??)

        Returns:
            : Batch of padded output sequences (B, Tmax, hdim)
                    or tuple w/ aux outputs ((B, Tmax, hdim), [L x (B, Tmax, hdim)])
            : Batch of lengths of output sequences (B)
            : Batch of hidden state sequences (B, Tmax, hdim)

        """
        if prev_states is None:
            prev_states = [None] * len(self.enc)
        assert len(prev_states) == len(self.enc)

        current_states = []
        for module, prev_state in zip(self.enc, prev_states):
            xs_pad, ilens, states = module(
                xs_pad,
                ilens,
                prev_state=prev_state,
            )
            current_states.append(states)

        if isinstance(xs_pad, tuple):
            final_xs_pad, aux_xs_list = xs_pad[0], xs_pad[1]

            mask = to_device(final_xs_pad, make_pad_mask(ilens).unsqueeze(-1))

            aux_xs_list = [layer.masked_fill(mask, 0.0) for layer in aux_xs_list]

            return (
                (
                    final_xs_pad.masked_fill(mask, 0.0),
                    aux_xs_list,
                ),
                ilens,
                current_states,
            )
        else:
            mask = to_device(xs_pad, make_pad_mask(ilens).unsqueeze(-1))

            return xs_pad.masked_fill(mask, 0.0), ilens, current_states


def encoder_for(
    args: argparse.Namespace,
    idim: Union[int, List],
    subsample: np.ndarray,
    aux_task_layer_list: List = [],
) -> Union[torch.nn.Module, List[torch.nn.Module]]:
    """Instantiate an encoder module given the program arguments.

    Args:
        args: The model arguments
        idim: Dimension of inputs or list of dimensions of inputs for each encoder
        subsample: subsample factors or list of subsample factors for each encoder

    Returns:
        : The encoder module or list of encoder modules

    """
    return Encoder(
        args.etype,
        idim,
        args.elayers,
        args.eunits,
        args.eprojs,
        subsample,
        args.dropout_rate,
        aux_task_layer_list=aux_task_layer_list,
    )