-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha2_abcs.py
1161 lines (1056 loc) · 48 KB
/
a2_abcs.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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
This code is provided solely for the personal and private use of students
taking the CSC401H/2511H course at the University of Toronto. Copying for
purposes other than this use is expressly prohibited. All forms of
distribution of this code, including but not limited to public repositories on
GitHub, GitLab, Bitbucket, or any other online platform, whether as given or
with any changes, are expressly prohibited.
Authors: Sean Robertson, Jingcheng Niu, Zining Zhu, and Mohamed Abdall
Updated by: Raeid Saqur <raeidsaqur@cs.toronto.edu>
All of the files in this directory and all subdirectories are:
Copyright (c) 2022 University of Toronto
'''
'''Abstract base classes for building seq2seq models'''
import platform
import abc
import torch
import warnings
from typing import Optional, Union, Tuple, Type, Set
BAD_ENV = '''\
It appears you're using an environment that doesn't match teach. Your code will
be run in an environment matching that of 'xxx@teach.cs.toronto.edu'. If your
code fails to run there, you'll get no pity marks! You've been warned!
Alternatively, you might be on teach, but called 'python3' instead of
'python3.9'. Use the latter!
'''
if (
platform.python_version() != '3.9.7' or
not torch.__version__.startswith('1.9.1')):
warnings.warn(BAD_ENV)
__all__ = [
'EncoderBase',
'DecoderBase',
'EncoderDecoderBase',
]
warnings.simplefilter('always', UserWarning)
class EncoderBase(torch.nn.Module, metaclass=abc.ABCMeta):
'''Encode an input source target sequence into a state sequence
See :func:`__init__` and :func:`init_submodules` for a description of the
attributes.
Attributes
----------
source_vocab_size : int
pad_id : int
word_embedding_size : int
num_hidden_layers : int
hidden_state_size : int
dropout : float
cell_type : {'rnn', 'lstm', 'gru'}
embedding : torch.nn.Embedding
rnn : {torch.nn.RNN, torch.nn.GRU, torch.nn.LSTM}
'''
def __init__(
self,
source_vocab_size: int,
pad_id: int = -1,
word_embedding_size: int = 1024,
num_hidden_layers: int = 2,
hidden_state_size: int = 512,
dropout: float = 0.1,
cell_type: str = 'lstm'):
'''Initialize the encoder
Sets some non-parameter attributes, then calls :func:`init_submodules`.
Parameters
----------
source_vocab_size : int
The number of words in your source language vocabulary, including
`pad_id`
pad_id : int, optional
The index within `source_vocab_size` which is used to right-pad
shorter input to the length of the longest input in the batch.
Negative values between ``-1`` and ``-vocab_size`` inclusive are
converted to positive indices by ``pad_id' = vocab_size + pad_id``.
word_embedding_size : int, optional
The size of your static (source) word embedding vectors.
num_hidden_layers : int, optional
The number of stacked recurrent layers in your encoder.
hidden_state_size : int, optional
The size of the output of a recurrent layer for one slice of time
in one direction.
dropout : float, optional
The probability of applying dropout to hidden states in the RNN.
cell_type : {'rnn', 'lstm', 'gru'}, optional
What underlying recurrent architecture to use when building the
`rnn` submodule. See :func:`init_submodules` for more info
'''
_in_range_check('source_vocab_size', source_vocab_size, 2)
if -source_vocab_size <= pad_id < 0:
pad_id = source_vocab_size + pad_id
else:
_in_range_check(
'pad_id', pad_id, -source_vocab_size, source_vocab_size - 1)
_in_range_check('word_embedding_size', word_embedding_size, 1)
_in_range_check('num_hidden_layers', num_hidden_layers, 1)
_in_range_check('hidden_state_size', hidden_state_size, 1)
_in_range_check('dropout', dropout, 0, 1)
_in_set_check('cell_type', cell_type, {'rnn', 'lstm', 'gru'})
super().__init__()
self.source_vocab_size = source_vocab_size
self.pad_id = pad_id
self.word_embedding_size = word_embedding_size
self.num_hidden_layers = num_hidden_layers
self.hidden_state_size = hidden_state_size
self.dropout = dropout
self.cell_type = cell_type
self.embedding = self.rnn = None
self.init_submodules()
assert self.embedding is not None, 'initialize embedding!'
assert self.rnn is not None, 'initialize rnn!'
@abc.abstractmethod
def init_submodules(self):
'''Initialize the parameterized submodules of this network
This method sets the following object attributes (sets them in
`self`):
embedding : torch.nn.Embedding
A layer that extracts learned token embeddings for each index in
a token sequence. It must not learn an embedding for padded tokens.
rnn : {torch.nn.RNN, torch.nn.GRU, torch.nn.LSTM}
A layer corresponding to the recurrent neural network that
processes source word embeddings. It must be bidirectional.
'''
raise NotImplementedError()
def reset_parameters(self):
self.embedding.reset_parameters()
self.rnn.reset_parameters()
def check_input(
self,
F: torch.LongTensor,
F_lens: torch.LongTensor):
_dim_check('F', F, 2)
_dim_check('F_lens', F_lens, 1)
if torch.any((F < 0) | (F >= self.source_vocab_size)):
raise RuntimeError(
f'F values must be between '
f'[0, {self.source_vocab_size - 1}]')
if torch.any((F_lens > F.shape[0]) | (F_lens < 1)):
raise RuntimeError(
f'F_lens for F of shape ({F.shape[0]}, ...) must be '
f'between [0, {F.shape[0]}]')
if F_lens.max() != F.shape[0]:
raise RuntimeError(
f'The maximum value in F_lens ({F_lens.max()}) does not '
f'equal the sequence dimension of F ({F.shape[0]})')
pad_mask = torch.arange(F.shape[0], device=F.device).unsqueeze(-1)
pad_mask = pad_mask >= F_lens.to(F.device) # (S, N)
if not torch.all(F.masked_select(pad_mask) == self.pad_id):
raise ValueError(
f'Values in F past F_lens are not padding ({self.pad_id})')
if torch.any(F.masked_select(~pad_mask) == self.pad_id):
raise ValueError(
f'Some values in F before F_lens are not padding '
f'({self.pad_id})')
def forward(
self,
F: torch.LongTensor,
F_lens: torch.LongTensor,
h_pad: float = 0.) -> torch.FloatTensor:
self.check_input(F, F_lens)
return self.forward_pass(F, F_lens, h_pad=h_pad)
@abc.abstractmethod
def forward_pass(
self,
F: torch.LongTensor,
F_lens: torch.LongTensor,
h_pad: float = 0.) -> torch.FloatTensor:
'''Defines the structure of the encoder
Parameters
----------
F : torch.LongTensor
An integer tensor of shape ``(S, M)``, where ``S`` is the number of
source time steps and ``M`` is the batch dimension. ``F[s, m]``
is the token id of the ``s``-th word in the ``m``-th source
sequence in the batch. ``F`` has been right-padded with
``self.pad_id`` wherever ``S`` exceeds the length of the original
sequence.
F_lens : torch.LongTensor
An integer tensor of shape ``(M,)`` that stores the original
lengths of each source sequence (and input sequence) in the batch
before right-padding.
h_pad : float
The value to right-pad `h` with, wherever `x` is right-padded.
Returns
-------
h : torch.FloatTensor
A float tensor of shape ``(S, M, 2 * self.hidden_state_size)``
where ``h[s,m,i]`` refers to the ``i``-th index of the encoder
RNN's last layer's hidden state at time step ``s`` of the
``m``-th sequence in the batch. The 2 is because the forward and
backward hidden states are concatenated. If
``x[s,m] == 0.``, then ``h[s,m, :] == h_pad``
'''
raise NotImplementedError()
def get_all_rnn_inputs(self, F: torch.LongTensor) -> torch.FloatTensor:
'''Get all input vectors to the RNN at once
Parameters
----------
F : torch.LongTensor
An integer tensor of shape ``(S, M)``, where ``S`` is the number of
source time steps and ``M`` is the batch dimension. ``F[s, m]``
is the token id of the ``s``-th word in the ``m``-th source
sequence in the batch. ``F`` has been right-padded with
``self.pad_id`` wherever ``S`` exceeds the length of the original
sequence.
Returns
-------
x : torch.FloatTensor
A float tensor of shape ``(S, M, I)`` of input to the encoder RNN,
where ``I`` corresponds to the size of the per-word input vector.
Whenever ``s`` exceeds the original length of ``F[s, m]`` (i.e.
when ``F[s, m] == self.pad_id``), ``x[s, m, :] == 0.``
'''
raise NotImplementedError()
@abc.abstractmethod
def get_all_hidden_states(
self,
x: torch.FloatTensor,
F_lens: torch.LongTensor,
h_pad: float) -> torch.FloatTensor:
'''Get all encoder hidden states for from input sequences
Parameters
----------
x : torch.FloatTensor
A float tensor of shape ``(S, M, I)`` of input to the encoder RNN,
where ``S`` is the number of source time steps, ``M`` is the batch
dimension, and ``I`` corresponds to the size of the per-word input
vector. ``x[s, m, :]`` is the input vector for the ``s``-th word in
the ``m``-th source sequence in the batch. `x` has been padded such
that ``x[F_lens[m]:, m, :] == 0.`` for all ``m``.
F_lens : torch.LongTensor
An integer tensor of shape ``(M,)`` that stores the original
lengths of each source sequence (and input sequence) in the batch
before right-padding.
h_pad : float
The value to right-pad `h` with, wherever `x` is right-padded.
Returns
-------
h : torch.FloatTensor
A float tensor of shape ``(S, M, 2 * self.hidden_state_size)``
where ``h[s,m,i]`` refers to the ``i``-th index of the encoder
RNN's last layer's hidden state at time step ``s`` of the
``m``-th sequence in the batch. The 2 is because the forward and
backward hidden states are concatenated. If
``x[s,m] == 0.``, then ``h[s,m, :] == h_pad``
'''
raise NotImplementedError()
class DecoderBase(torch.nn.Module, metaclass=abc.ABCMeta):
'''Decode source sequence embeddings into distributions over targets
See :func:`__init__` and :func:`init_submodules` for a description of the
attributes.
Attributes
----------
target_vocab_size : int
pad_id : int
word_embedding_size : int
hidden_state_size : int
cell_type : {'rnn', 'lstm', 'gru'}
embedding : torch.nn.Embedding
cell : {torch.nn.GRUCell, torch.nn.LSTMCell, torch.nn.RNNCell}
ff : torch.nn.Linear
'''
def __init__(
self,
target_vocab_size: int,
pad_id: int = -1,
word_embedding_size: int = 1024,
hidden_state_size: int = 1024,
cell_type: str = 'lstm',
heads: Optional[int] = None):
'''Initialize the decoder
Sets some non-parameter attributes, then calls :func:`init_submodules`.
Parameters
----------
target_vocab_size : int
The size of the target language vocabulary, including `pad_id`
pad_id : int, optional
The index within `output_vocab_size` which is used to right-pad
shorter input to the length of the longest input in the batch.
Negative values between ``-1`` and ``-vocab_size`` inclusive are
converted to positive indices by ``pad_id' = vocab_size + pad_id``.
word_embedding_size : int, optional
The size of your static (target) word embedding vectors.
hidden_state_size : int, optional
The size of the output of a recurrent layer for one slice of time
in one direction.
cell_type : {'rnn', 'lstm', 'gru'}, optional
What underlying recurrent architecture to use when building the
`rnn` submodule. See :func:`init_submodules` for more info.
'''
_in_range_check('target_vocab_size', target_vocab_size, 2)
if -target_vocab_size <= pad_id < 0:
pad_id = target_vocab_size + pad_id
else:
_in_range_check(
'pad_id', pad_id, -target_vocab_size, target_vocab_size - 1)
_in_range_check('word_embedding_size', word_embedding_size, 1)
_in_range_check('hidden_state_size', hidden_state_size, 1)
_in_set_check('cell_type', cell_type, {'rnn', 'lstm', 'gru'})
if heads is not None and hidden_state_size % heads:
raise ValueError(
f'heads ({heads}) must evenly divide '
f'hidden_state_size ({hidden_state_size})')
super().__init__()
self.target_vocab_size = target_vocab_size
self.pad_id = pad_id
self.word_embedding_size = word_embedding_size
self.hidden_state_size = hidden_state_size
self.cell_type = cell_type
self.embedding = self.cell = self.ff = self.W = self.Wtilde = self.Q = None
self.heads = heads
self.init_submodules()
assert self.embedding is not None, 'initialize embedding!'
assert self.cell is not None, 'initialize cell!'
assert self.ff is not None, 'initialize ff!'
@abc.abstractmethod
def init_submodules(self):
'''Initialize the parameterized submodules of this network
This method sets the following object attributes (sets them in
`self`):
embedding : torch.nn.Embedding
A layer that extracts learned token embeddings for each index in
a token sequence. It must not learn an embedding for padded tokens.
cell : {torch.nn.RNNCell, torch.nn.GRUCell, torch.nn.LSTMCell}
A layer corresponding to the recurrent neural network that
processes target word embeddings into hidden states. We only define
one cell and one layer
ff : torch.nn.Linear
A fully-connected layer that converts the decoder hidden state
into an un-normalized log probability distribution over target
words
'''
raise NotImplementedError()
def reset_parameters(self):
self.embedding.reset_parameters()
self.cell.reset_parameters()
self.ff.reset_parameters()
if self.W is not None:
self.W.reset_parameters()
if self.Wtilde is not None:
self.Wtilde.reset_parameters()
if self.Q is not None:
self.Q.reset_parameters()
def check_input(
self,
E_tm1: torch.LongTensor,
htilde_tm1: Optional[Union[
torch.FloatTensor,
Tuple[torch.FloatTensor, torch.FloatTensor]]],
h: torch.FloatTensor,
F_lens: torch.LongTensor):
_dim_check('E_tm1', E_tm1, 1)
_dim_check('h', h, 3)
_dim_check('F_lens', F_lens, 1)
batch_size = E_tm1.shape[0]
if h.shape[1] != batch_size or F_lens.shape[0] != batch_size:
raise RuntimeError('batch sizes not consistent')
if htilde_tm1 is not None:
if self.cell_type == 'lstm':
htilde_tm1, c_t = htilde_tm1
_dim_check('htilde_tm1[0]', htilde_tm1, 2)
_dim_check('htilde_tm1[1]', c_t, 2)
if htilde_tm1.shape != c_t.shape:
raise RuntimeError(
f'Expected LSTM h_t shape ({htilde_tm1.shape}) to '
f'match c_t shape ({c_t.shape})')
else:
_dim_check('htilde_tm1', htilde_tm1, 2)
if htilde_tm1.shape[1] != self.hidden_state_size:
raise RuntimeError(
f'Expected htilde_tm1 to have final dim size '
f'{self.hidden_state_size}, got {htilde_tm1.shape[-1]}')
if htilde_tm1.shape[0] != batch_size:
raise RuntimeError('batch sizes not consistent')
if F_lens.max() != h.shape[0]:
raise RuntimeError(
f'The maximum value in F_lens ({F_lens.max()}) does not equal '
f'the sequence dimension of h ({h.shape[0]})')
if torch.any(
(E_tm1 < 0) | (E_tm1 >= self.target_vocab_size)):
raise RuntimeError(
f'E_tm1 values must be between '
f'[0, {self.source_vocab_size - 1}]')
def forward(
self,
E_tm1: torch.LongTensor,
htilde_tm1: Optional[Union[
torch.FloatTensor,
Tuple[torch.FloatTensor, torch.FloatTensor]]],
h: torch.FloatTensor,
F_lens: torch.LongTensor) -> torch.FloatTensor:
self.check_input(E_tm1, htilde_tm1, h, F_lens)
if htilde_tm1 is None:
htilde_tm1 = self.get_first_hidden_state(h, F_lens)
if self.cell_type == 'lstm':
# initialize cell state with zeros
htilde_tm1 = (htilde_tm1, torch.zeros_like(htilde_tm1))
return self.forward_pass(E_tm1, htilde_tm1, h, F_lens)
@abc.abstractmethod
def forward_pass(
self,
E_tm1: torch.LongTensor,
htilde_tm1: Union[
torch.FloatTensor,
Tuple[torch.FloatTensor, torch.FloatTensor]],
h: torch.FloatTensor,
F_lens: torch.LongTensor) -> Tuple[
torch.FloatTensor, Union[
torch.FloatTensor,
Tuple[torch.FloatTensor, torch.FloatTensor]]]:
'''Defines the structure of the decoder
Parameters
----------
E_tm1 : torch.LongTensor
An integer tensor of shape ``(M,)`` denoting the target language
token ids output from the previous decoder step. ``E_tm1[m]`` is
the token corresponding to the ``m``-th element in the batch. If
``E_tm1[m] == self.pad_id``, then the target sequence has ended
htilde_tm1 : torch.FloatTensor or tuple
If this decoder doesn't use an LSTM cell, `htilde_tm1` is a float
tensor of shape ``(M, self.hidden_state_size)``, where
``htilde_tm1[m]`` corresponds to ``m``-th element in the batch.
If this decoder does use an LSTM cell, `htilde_tm1` is a pair of
float tensors corresponding to the previous hidden state and the
previous cell state.
h : torch.FloatTensor
A float tensor of shape ``(S, M, self.hidden_state_size)`` of
hidden states of the encoder. ``h[s, m, i]`` is the
``i``-th index of the encoder RNN's last hidden state at time ``s``
of the ``m``-th sequence in the batch. The states of the
encoder have been right-padded such that ``h[F_lens[m]:, m]``
should all be ignored.
F_lens : torch.LongTensor
An integer tensor of shape ``(M,)`` corresponding to the lengths
of the encoded source sentences.
Returns
-------
logits_t : torch.FloatTensor
A float tensor of shape ``(M, self.target_vocab_size)``.
``logits_t[m]`` is an un-normalized distribution over the next
target word for the ``m``-th sequence:
``Pr_b(i) = softmax(logits_t[m])``
htilde_t : torch.FloatTensor or tuple
Like `htilde_tm1` (either a float tensor or a pair of float
tensors), but matching the current hidden state.
'''
raise NotImplementedError()
@abc.abstractmethod
def get_first_hidden_state(
self,
h: torch.FloatTensor,
F_lens: torch.LongTensor) -> torch.FloatTensor:
'''Get the initial decoder hidden state, prior to the first input
Parameters
----------
h : torch.FloatTensor
A float tensor of shape ``(S, M, self.hidden_state_size)`` of
hidden states of the encoder. ``h[s, m, i]`` is the
``i``-th index of the encoder RNN's last hidden state at time ``s``
of the ``m``-th sequence in the batch. The states of the
encoder have been right-padded such that
``h[F_lens[m]:, m]`` should all be ignored.
F_lens : torch.LongTensor
An integer tensor of shape ``(M,)`` corresponding to the lengths
of the encoded source sentences.
Returns
-------
htilde_0 : torch.FloatTensor
A float tensor of shape ``(M, self.hidden_state_size)``, where
``htilde_0[m, i]`` is the ``i``-th index of the decoder's first
(pre-sequence) hidden state for the ``m``-th sequence in the back
Notes
-----
You will or will not need `h` and `F_lens`, depending on
whether this decoder uses attention.
`h` is the output of a bidirectional layer. Assume
``h[..., :self.hidden_state_size // 2]`` correspond to the
hidden states in the forward direction and
``h[..., self.hidden_state_size // 2:]`` to those in the
backward direction.
In the case of an LSTM, we will initialize the cell state with zeros
later on (don't worry about it).
'''
raise NotImplementedError()
@abc.abstractmethod
def get_current_rnn_input(
self,
E_tm1: torch.LongTensor,
htilde_tm1: Union[
torch.FloatTensor,
Tuple[torch.FloatTensor, torch.FloatTensor]],
h: torch.FloatTensor,
F_lens: torch.LongTensor) -> torch.FloatTensor:
'''Get the current input the decoder RNN
Parameters
----------
E_tm1 : torch.LongTensor
An integer tensor of shape ``(M,)`` denoting the target language
token ids output from the previous decoder step. ``E_tm1[m]`` is
the token corresponding to the ``m``-th element in the batch. If
``E_tm1[m] == self.pad_id``, then the target sequence has ended
h : torch.FloatTensor
A float tensor of shape ``(S, M, self.hidden_state_size)`` of
hidden states of the encoder. ``h[s, m, i]`` is the
``i``-th index of the encoder RNN's last hidden state at time ``s``
of the ``m``-th sequence in the batch. The states of the
encoder have been right-padded such that ``h[F_lens[m]:, m]``
should all be ignored.
F_lens : torch.LongTensor
An integer tensor of shape ``(M,)`` corresponding to the lengths
of the encoded source sentences.
Returns
-------
xtilde_t : torch.FloatTensor
A float tensor of shape ``(M, Itilde)`` denoting the current input
to the decoder RNN. ``xtilde_t[m, :self.word_embedding_size]``
should be a word embedding for ``E_tm1[m]``. If
``E_tm1[m] == self.pad_id``, then ``xtilde_t[m] == 0.``. If this
decoder uses attention, ``xtilde_t[m, self.word_embedding_size:]``
corresponds to the attention context vector.
Notes
-----
You will or will not need `htilde_tm1`, `h` and `F_lens`, depending on
whether this decoder uses attention.
``xtilde_t[m, self.word_embedding_size:]`` should not be masked out,
regardless of whether ``E_tm1[m] == self.pad_id``
'''
raise NotImplementedError()
@abc.abstractmethod
def get_current_hidden_state(
self,
xtilde_t: torch.FloatTensor,
htilde_tm1: Union[
torch.FloatTensor,
Tuple[torch.FloatTensor, torch.FloatTensor]]) -> Union[
torch.FloatTensor,
Tuple[torch.FloatTensor, torch.FloatTensor]]:
'''Calculate the decoder's current hidden state
Converts `E_tm1` to embeddings, and feeds those embeddings into
the recurrent cell alongside `htilde_tm1`.
Parameters
----------
xtilde_t : torch.FloatTensor
A float tensor of shape ``(M, Itilde)`` denoting the current input
to the decoder RNN. ``xtilde_t[m, :]`` is the input vector of the
previous target token's embedding for batch element ``m``.
``xtilde_t[m, :]`` may additionally include an attention context
vector.
htilde_tm1 : torch.FloatTensor or tuple
If this decoder doesn't use an LSTM cell, `htilde_tm1` is a float
tensor of shape ``(M, self.hidden_state_size)``, where
``htilde_tm1[m]`` corresponds to ``m``-th element in the batch.
If this decoder does use an LSTM cell, `htilde_tm1` is a pair of
float tensors corresponding to the previous hidden state and the
previous cell state.
Returns
-------
htilde_t : torch.FloatTensor or tuple
Like `htilde_tm1` (either a float tensor or a pair of float
tensors), but matching the current hidden state.
Notes
-----
This method does not account for finished target sequences. That is
handled downstream.
'''
raise NotImplementedError()
@abc.abstractmethod
def get_current_logits(
self,
htilde_t: torch.FloatTensor) -> torch.FloatTensor:
'''Calculate an un-normalized log distribution over target words
Parameters
----------
htilde_t : torch.FloatTensor
A float tensor of shape ``(M, self.hidden_state_size)`` of the
decoder's current hidden state (excludes the cell state in the
case of an LSTM).
Returns
-------
logits_t : torch.FloatTensor
A float tensor of shape ``(M, self.target_vocab_size)``.
``logits_t[m]`` is an un-normalized distribution over the next
target word for the ``m``-th sequence:
``Pr_b(i) = softmax(logits_t[m])``
'''
raise NotImplementedError()
class EncoderDecoderBase(torch.nn.Module, metaclass=abc.ABCMeta):
'''Decode a source transcription into a target transcription
See :func:`__init__` and :func:`init_submodules` for descriptions of the
attributes
Attributes
----------
source_vocab_size : int
target_vocab_size : int
source_pad_id : int
target_sos : int
target_eos : int
encoder_hidden_size : int
word_embedding_size : int
encoder_num_hidden_layers : int
encoder_dropout : float
cell_type : {'rnn', 'lstm', 'gru'}
beam_width : int
encoder : EncoderBase
decoder : DecoderBase
'''
def __init__(
self,
encoder_class: Type[EncoderBase],
decoder_class: Type[DecoderBase],
source_vocab_size: int,
target_vocab_size: int,
source_pad_id: int = -1,
target_sos: int = -2,
target_eos: int = -1,
encoder_hidden_size: int = 512,
word_embedding_size: int = 1024,
encoder_num_hidden_layers: int = 2,
encoder_dropout: float = 0.1,
cell_type: str = 'lstm',
beam_width: int = 4,
greedy: bool = False,
heads: int = 4,
on_max_beam_iter: str = 'raise'):
'''Initialize the encoder decoder combo
Sets some non-parameter attributes, then calls :func:`init_submodules`.
Parameters
----------
encoder_class : type
A concrete subclass of :class:`EncoderBase`. Used to instantiate
an encoder.
decoder_class : type
A concrete subclass of :class:`DecoderBase`. Used to instantiate
a decoder.
source_vocab_size : int
The number of words in your source vocabulary, including
`source_pad_id`.
target_vocab_size : int
The number of words in your target vocabulary, including
`target_sos` and `target_eos`.
source_pad_id : int, optional
A token id that is used to right-pad source token sequences.
Negative values between ``-1`` and ``-source_vocab_size``
inclusive are converted to positive indices by
``source_pad_id' = source_vocab_size + source_pad_id``.
target_sos : int, optional
A token id denoting the beginning of a target token sequence.
Negative values between ``-1`` and ``-target_vocab_size`` inclusive
are converted to positive indices by
``target_sos' = target_vocab_size + pad_id``.
target_eos : int, optional
A token id denoting the end of a target token sequence. Doubles
as a padding index for target word embeddings.
Negative values between ``-1`` and ``-target_vocab_size`` inclusive
are converted to positive indices by
``target_eos' = target_vocab_size + target_eos``.
encoder_hidden_size : int
The hidden state size of the encoder *in one direction*.
word_embedding_size : int, optional
The static word embedding size. Used in both the encoder and
decoder.
encoder_num_hidden_layers : int, optional
The number of recurrent layers to stack in the encoder.
encoder_dropout : float, optional
The probability of applying dropout to a hidden state in the
encoder RNN.
cell_type : {'rnn', 'lstm', 'gru'}, optional
What recurrent architecture to use for both the encoder and
decoder.
beam_width : int, optional
The number of hypotheses/paths to consider during beam search
greedy: boolean, optional
Use the greedy algorithm instead of beam search
'''
if not issubclass(encoder_class, EncoderBase):
raise ValueError('encoder_class must be an EncoderBase')
if not issubclass(decoder_class, DecoderBase):
raise ValueError('decoder_class must be a DecoderBase')
_in_range_check('source_vocab_size', source_vocab_size, 2)
_in_range_check('target_vocab_size', target_vocab_size, 3)
if -source_vocab_size <= source_pad_id < 0:
source_pad_id = source_vocab_size + source_pad_id
else:
_in_range_check(
'source_pad_id', source_pad_id,
-source_vocab_size, source_vocab_size - 1)
if -target_vocab_size <= target_sos < 0:
target_sos = target_sos + target_vocab_size
else:
_in_range_check(
'target_sos', target_sos,
-target_vocab_size, target_vocab_size - 1)
if -target_vocab_size <= target_eos < 0:
target_eos = target_eos + target_vocab_size
else:
_in_range_check(
'target_eos', target_eos,
-target_vocab_size, target_vocab_size - 1)
if target_sos == target_eos:
raise ValueError('target_sos cannot match target_eos')
_in_range_check('encoder_hidden_size', encoder_hidden_size, 1)
_in_range_check('word_embedding_size', word_embedding_size, 1)
_in_range_check(
'encoder_num_hidden_layers', encoder_num_hidden_layers, 1)
_in_range_check('encoder_dropout', encoder_dropout, 0, 1)
_in_set_check('cell_type', cell_type, {'rnn', 'lstm', 'gru'})
_in_range_check('beam_width', beam_width, 1)
_in_set_check('on_max_beam_iter', on_max_beam_iter, {'raise', 'halt', 'ignore'})
super().__init__()
self.source_vocab_size = source_vocab_size
self.target_vocab_size = target_vocab_size
self.source_pad_id = source_pad_id
self.target_sos = target_sos
self.target_eos = target_eos
self.encoder_hidden_size = encoder_hidden_size
self.word_embedding_size = word_embedding_size
self.encoder_num_hidden_layers = encoder_num_hidden_layers
self.encoder_dropout = encoder_dropout
self.cell_type = cell_type
self.beam_width = beam_width
self.encoder = self.decoder = None
self.greedy = greedy
self.heads = heads
self.on_max = on_max_beam_iter
self.init_submodules(encoder_class, decoder_class)
assert isinstance(self.encoder, encoder_class)
assert isinstance(self.decoder, decoder_class)
@abc.abstractmethod
def init_submodules(
self,
encoder_class: Type[EncoderBase],
decoder_class: Type[DecoderBase]):
'''Initialize encoder and decoder submodules
This method sets the following object attributes (sets them in
`self`):
encoder : encoder_class
The encoder instance in the encoder/decoder pair
decoder : decoder_class
The decoder instance in the encoder/decoder pair
Parameters
----------
encoder_class : type
A concrete subclass of :class:`EncoderBase`. Used to instantiate
``self.encoder``
decoder_class : type
A concrete subclass of :class:`DecoderBase`. Used to instantiate
``self.decoder``
'''
raise NotImplementedError()
def reset_parameters(self):
self.encoder.reset_parameters()
self.decoder.reset_parameters()
def check_input(
self,
F: torch.LongTensor,
F_lens: torch.LongTensor,
E: Optional[torch.LongTensor],
max_T: int,
on_max: str):
self.encoder.check_input(F, F_lens)
if E is not None:
_dim_check('E', E, 2)
if torch.any(
(E < 0) | (E >= self.target_vocab_size)):
raise RuntimeError(
f'E values must be between '
f'[0, {self.target_vocab_size - 1}]')
eos_mask = E == self.target_eos
if (
E.shape[0] < 3 or
not torch.all(E[0] == self.target_sos) or
torch.any(eos_mask[0]) or
not torch.all((eos_mask[1:] ^ eos_mask[:-1]).sum(0) == 1)):
raise RuntimeError(
f'All sequences in E must start with SOS '
f'({self.target_sos}) followed by a non-EOS, end with at '
f'least one EOS ({self.target_eos}), and right-pad with '
f'EOS if too short')
if torch.any(E[1:] == self.target_sos):
raise RuntimeError(
f'Do not include SOS ({self.target_sos}) past t=0')
_in_set_check(
'on_max', on_max, {'raise', 'ignore', 'halt'},
error=RuntimeError)
if on_max != 'ignore':
_in_range_check('max_T', max_T, 1, error=RuntimeError)
def get_target_padding_mask(self, E: torch.LongTensor) -> torch.BoolTensor:
'''Determine what parts of a target sequence batch are padding
`E` is right-padded with end-of-sequence symbols. This method
creates a mask of those symbols, excluding the first in every sequence
(the first eos symbol should not be excluded in the loss).
Parameters
----------
E : torch.LongTensor
A float tensor of shape ``(T - 1, M)``, where ``E[t', m]`` is
the ``t'``-th token id of a gold-standard transcription for the
``m``-th source sequence. *Should* exclude the initial
start-of-sequence token.
Returns
-------
pad_mask : torch.BoolTensor
A boolean tensor of shape ``(T - 1, M)``, where ``pad_mask[t, m]``
is :obj:`True` when ``E[t, m]`` is considered padding.
'''
pad_mask = E == self.target_eos # (T - 1, M)
pad_mask = pad_mask & torch.cat([pad_mask[:1], pad_mask[:-1]], 0)
return pad_mask
def forward(
self,
F: torch.LongTensor,
F_lens: torch.LongTensor,
E: Optional[torch.LongTensor] = None,
max_T: int = 100,
on_max: Optional[str] = None) -> Union[
torch.FloatTensor, torch.LongTensor]:
if on_max is None:
on_max = self.on_max
if self.training:
if E is None:
raise RuntimeError('E must be set for training')
self.check_input(F, F_lens, E, None, 'ignore')
else:
self.check_input(F, F_lens, None, max_T, on_max)
h = self.encoder(F, F_lens) # (S, M, 2 * H)
if self.training:
return self.get_logits_for_teacher_forcing(h, F_lens, E)
else:
return self.beam_search(h, F_lens, max_T, on_max)
@abc.abstractmethod
def get_logits_for_teacher_forcing(
self,
h: torch.FloatTensor,
F_lens: torch.LongTensor,
E: torch.LongTensor) -> torch.FloatTensor:
'''Get un-normed distributions over next tokens via teacher forcing
Parameters
----------
h : torch.FloatTensor
A float tensor of shape ``(S, M, 2 * self.encoder_hidden_size)`` of
hidden states of the encoder. ``h[s, m, i]`` is the
``i``-th index of the encoder RNN's last hidden state at time ``s``
of the ``m``-th sequence in the batch. The states of the
encoder have been right-padded such that ``h[F_lens[m]:, m]``
should all be ignored.
F_lens : torch.LongTensor
An integer tensor of shape ``(M,)`` corresponding to the lengths
of the encoded source sentences.
E : torch.LongTensor
A long tensor of shape ``(T, M)`` where ``E[t, m]`` is the
``t-1``-th token in the ``m``-th target sequence in the batch.
``E[0, :]`` has been populated with ``self.target_sos``. Each
sequence has had at least one ``self.target_eos`` token appended
to it. Further EOS right-pad the shorter sequences to make up the
length.
Returns
-------
logits : torch.FloatTensor
A float tensor of shape ``(T - 1, M, self.target_vocab_size)``
where ``logits[t, m, :]`` is the un-normalized log-probability
distribution predicting the ``t``-th token of the ``m``-th target
sequence in the batch.
Notes
-----
You need not worry about handling padded values of `E` here - it will
be handled in the loss function.
'''
raise NotImplementedError()
def beam_search(
self,
h: torch.FloatTensor,
F_lens: torch.LongTensor,
max_T: int,
on_max: Optional[str] = None) -> torch.LongTensor:
if on_max is None:
on_max = self.on_max
# beam search
assert not self.training
htilde_tm1 = self.decoder.get_first_hidden_state(h, F_lens)
logpb_tm1 = torch.where(
torch.arange(self.beam_width, device=h.device) > 0, # K
torch.full_like(
htilde_tm1[..., 0].unsqueeze(1), -float('inf')), # k > 0
torch.zeros_like(
htilde_tm1[..., 0].unsqueeze(1)), # k == 0
) # (M, K)
assert torch.all(logpb_tm1[:, 0] == 0.)
assert torch.all(logpb_tm1[:, 1:] == -float('inf'))
b_tm1_1 = torch.full_like( # (t, M, K)
logpb_tm1, self.target_sos, dtype=torch.long).unsqueeze(0)
# We treat each beam within the batch as just another batch when
# computing logits, then recover the original batch dimension by
# reshaping
htilde_tm1 = htilde_tm1.unsqueeze(1).repeat(1, self.beam_width, 1)
htilde_tm1 = htilde_tm1.flatten(end_dim=1) # (M * K, 2 * H)
if self.cell_type == 'lstm':
htilde_tm1 = (htilde_tm1, torch.zeros_like(htilde_tm1))
h = h.unsqueeze(2).repeat(1, 1, self.beam_width, 1)
h = h.flatten(1, 2) # (S, M * K, 2 * H)
F_lens = F_lens.unsqueeze(-1).repeat(1, self.beam_width).flatten()
v_is_eos = torch.arange(self.target_vocab_size, device=h.device)
v_is_eos = v_is_eos == self.target_eos # (V,)
t = 0
while torch.any(b_tm1_1[-1, :, 0] != self.target_eos):
if t == max_T:
if on_max == 'raise':
raise RuntimeError(
f'Beam search has not finished by t={t}. Increase the '
f'number of parameters and train longer')
elif on_max == 'halt':
warnings.warn(f'Beam search not finished by t={t}. Halted')