-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathmodeling.py
1519 lines (1283 loc) Β· 60 KB
/
modeling.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
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
# Copyright 2021 The HuggingFace Inc. team.
#
# 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 paddle
import paddle.nn as nn
import paddle.nn.functional as F
from paddle import tensor
from paddle.nn import Layer
from .. import PretrainedModel, register_base_model
from ..activations import get_activation
__all__ = [
"ConvBertModel",
"ConvBertPretrainedModel",
"ConvBertForTotalPretraining",
"ConvBertDiscriminator",
"ConvBertGenerator",
"ConvBertClassificationHead",
"ConvBertForSequenceClassification",
"ConvBertForTokenClassification",
"ConvBertPretrainingCriterion",
"ConvBertForQuestionAnswering",
"ConvBertForMultipleChoice",
"ConvBertForPretraining",
]
dtype_float = paddle.get_default_dtype()
def _convert_attention_mask(attn_mask, dtype):
if attn_mask is not None and attn_mask.dtype != dtype:
attn_mask_dtype = attn_mask.dtype
if attn_mask_dtype in [paddle.bool, paddle.int8, paddle.int16, paddle.int32, paddle.int64]:
attn_mask = (paddle.cast(attn_mask, dtype) - 1.0) * 1e9
else:
attn_mask = paddle.cast(attn_mask, dtype)
return attn_mask
class GroupedLinear(nn.Layer):
def __init__(self, input_size, output_size, num_groups):
super().__init__()
self.input_size = input_size
self.output_size = output_size
self.num_groups = num_groups
self.group_in_dim = self.input_size // self.num_groups
self.group_out_dim = self.output_size // self.num_groups
self.weight = paddle.create_parameter(
shape=[self.num_groups, self.group_in_dim, self.group_out_dim], dtype=dtype_float
)
self.bias = paddle.create_parameter(shape=[output_size], dtype=dtype_float, is_bias=True)
def forward(self, hidden_states):
batch_size = hidden_states.shape[0]
x = tensor.reshape(hidden_states, [-1, self.num_groups, self.group_in_dim])
x = tensor.transpose(x, perm=[1, 0, 2])
x = tensor.matmul(x, self.weight)
x = tensor.transpose(x, perm=[1, 0, 2])
x = tensor.reshape(x, [batch_size, -1, self.output_size])
x = x + self.bias
return x
class SeparableConv1D(nn.Layer):
"""This class implements separable convolution, i.e. a depthwise and a pointwise layer"""
def __init__(self, input_filters, output_filters, kernel_size):
super().__init__()
self.depthwise = nn.Conv1D(
input_filters,
input_filters,
kernel_size=kernel_size,
groups=input_filters,
padding=kernel_size // 2,
bias_attr=False,
data_format="NLC",
)
self.pointwise = nn.Conv1D(
input_filters,
output_filters,
kernel_size=1,
bias_attr=False,
data_format="NLC",
)
self.bias = paddle.create_parameter(shape=[output_filters], dtype=dtype_float, is_bias=True)
def forward(self, hidden_states):
x = self.depthwise(hidden_states)
x = self.pointwise(x) + self.bias
return x
class MultiHeadAttentionWithConv(Layer):
def __init__(
self,
embed_dim,
num_heads,
dropout=0.0,
kdim=None,
vdim=None,
need_weights=False,
conv_kernel_size=None,
head_ratio=None,
):
super(MultiHeadAttentionWithConv, self).__init__()
self.embed_dim = embed_dim
self.kdim = kdim if kdim is not None else embed_dim
self.vdim = vdim if vdim is not None else embed_dim
self.need_weights = need_weights
self.head_dim = embed_dim // num_heads
self.scale = self.head_dim**-0.5
assert self.head_dim * num_heads == self.embed_dim, "embed_dim must be divisible by num_heads"
new_num_attention_heads = num_heads // head_ratio
if num_heads // head_ratio < 1:
self.num_heads = 1
self.conv_type = "noconv"
else:
self.num_heads = new_num_attention_heads
self.conv_type = "sdconv"
self.all_head_size = self.num_heads * self.head_dim
self.dropout = nn.Dropout(dropout)
self.q_proj = nn.Linear(embed_dim, self.all_head_size)
self.k_proj = nn.Linear(self.kdim, self.all_head_size)
self.v_proj = nn.Linear(self.vdim, self.all_head_size)
self.out_proj = nn.Linear(embed_dim, embed_dim)
if self.conv_type == "sdconv":
self.conv_kernel_size = conv_kernel_size
self.key_conv_attn_layer = SeparableConv1D(embed_dim, self.all_head_size, self.conv_kernel_size)
self.conv_kernel_layer = nn.Linear(self.all_head_size, self.num_heads * self.conv_kernel_size)
self.conv_out_layer = nn.Linear(embed_dim, self.all_head_size)
self.padding = (self.conv_kernel_size - 1) // 2
def forward(self, query, key=None, value=None, attn_mask=None, cache=None):
key = query if key is None else key
value = query if value is None else value
q = self.q_proj(query)
k = self.k_proj(key)
v = self.v_proj(value)
if self.conv_type == "sdconv":
bs = paddle.shape(q)[0]
seqlen = paddle.shape(q)[1]
mixed_key_conv_attn_layer = self.key_conv_attn_layer(query)
conv_attn_layer = mixed_key_conv_attn_layer * q
# conv_kernel_layer
conv_kernel_layer = self.conv_kernel_layer(conv_attn_layer)
conv_kernel_layer = tensor.reshape(conv_kernel_layer, shape=[-1, self.conv_kernel_size, 1])
conv_kernel_layer = F.softmax(conv_kernel_layer, axis=1)
conv_out_layer = self.conv_out_layer(query)
conv_out_layer = F.pad(conv_out_layer, pad=[self.padding, self.padding], data_format="NLC")
conv_out_layer = paddle.stack(
[
paddle.slice(conv_out_layer, axes=[1], starts=[i], ends=[i + seqlen])
for i in range(self.conv_kernel_size)
],
axis=-1,
)
conv_out_layer = tensor.reshape(conv_out_layer, shape=[-1, self.head_dim, self.conv_kernel_size])
conv_out_layer = tensor.matmul(conv_out_layer, conv_kernel_layer)
conv_out = tensor.reshape(conv_out_layer, shape=[bs, seqlen, self.num_heads, self.head_dim])
q = tensor.reshape(x=q, shape=[0, 0, self.num_heads, self.head_dim])
q = tensor.transpose(x=q, perm=[0, 2, 1, 3])
k = tensor.reshape(x=k, shape=[0, 0, self.num_heads, self.head_dim])
k = tensor.transpose(x=k, perm=[0, 2, 1, 3])
v = tensor.reshape(x=v, shape=[0, 0, self.num_heads, self.head_dim])
v = tensor.transpose(x=v, perm=[0, 2, 1, 3])
product = tensor.matmul(x=q, y=k, transpose_y=True) * self.scale
if attn_mask is not None:
attn_mask = _convert_attention_mask(attn_mask, product.dtype)
product = product + attn_mask
weights = F.softmax(product)
weights = self.dropout(weights)
out = tensor.matmul(weights, v)
# combine heads
out = tensor.transpose(out, perm=[0, 2, 1, 3])
if self.conv_type == "sdconv":
out = tensor.concat([out, conv_out], axis=2)
out = tensor.reshape(x=out, shape=[0, 0, out.shape[2] * out.shape[3]])
# project to output
out = self.out_proj(out)
outs = [out]
if self.need_weights:
outs.append(weights)
if cache is not None:
outs.append(cache)
return out if len(outs) == 1 else tuple(outs)
class TransformerEncoderLayerWithConv(nn.TransformerEncoderLayer):
def __init__(
self,
d_model,
nhead,
dim_feedforward,
dropout=0.1,
activation="relu",
attn_dropout=None,
act_dropout=None,
normalize_before=False,
conv_kernel_size=None,
head_ratio=None,
num_groups=None,
**kwargs
):
super().__init__(
d_model,
nhead,
dim_feedforward,
dropout=dropout,
activation=activation,
attn_dropout=attn_dropout,
act_dropout=act_dropout,
normalize_before=normalize_before,
)
self.self_attn = MultiHeadAttentionWithConv(
d_model,
nhead,
dropout=attn_dropout,
conv_kernel_size=conv_kernel_size,
head_ratio=head_ratio,
)
if num_groups > 1:
self.linear1 = GroupedLinear(d_model, dim_feedforward, num_groups=num_groups)
self.linear2 = GroupedLinear(dim_feedforward, d_model, num_groups=num_groups)
self._config.update({"conv_kernel_size": conv_kernel_size, "head_ratio": head_ratio, "num_groups": num_groups})
class ConvBertEmbeddings(nn.Layer):
"""
Include embeddings from word, position and token_type embeddings
"""
def __init__(
self,
vocab_size,
embedding_size,
hidden_dropout_prob,
max_position_embeddings,
type_vocab_size,
):
super(ConvBertEmbeddings, self).__init__()
self.word_embeddings = nn.Embedding(vocab_size, embedding_size)
self.position_embeddings = nn.Embedding(max_position_embeddings, embedding_size)
self.token_type_embeddings = nn.Embedding(type_vocab_size, embedding_size)
self.layer_norm = nn.LayerNorm(embedding_size, epsilon=1e-12)
self.dropout = nn.Dropout(hidden_dropout_prob)
def forward(self, input_ids, token_type_ids=None, position_ids=None):
if position_ids is None:
ones = paddle.ones_like(input_ids, dtype="int64")
seq_length = paddle.cumsum(ones, axis=-1)
position_ids = seq_length - ones
position_ids.stop_gradient = True
if token_type_ids is None:
token_type_ids = paddle.zeros_like(input_ids, dtype="int64")
input_embeddings = self.word_embeddings(input_ids)
position_embeddings = self.position_embeddings(position_ids)
token_type_embeddings = self.token_type_embeddings(token_type_ids)
embeddings = input_embeddings + position_embeddings + token_type_embeddings
embeddings = self.layer_norm(embeddings)
embeddings = self.dropout(embeddings)
return embeddings
class ConvBertDiscriminatorPredictions(nn.Layer):
"""
Prediction layer for the discriminator.
"""
def __init__(self, hidden_size, hidden_act):
super(ConvBertDiscriminatorPredictions, self).__init__()
self.dense = nn.Linear(hidden_size, hidden_size)
self.dense_prediction = nn.Linear(hidden_size, 1)
self.act = get_activation(hidden_act)
def forward(self, discriminator_hidden_states):
hidden_states = self.dense(discriminator_hidden_states)
hidden_states = self.act(hidden_states)
logits = self.dense_prediction(hidden_states).squeeze()
return logits
class ConvBertGeneratorPredictions(nn.Layer):
"""
Prediction layer for the generator.
"""
def __init__(self, embedding_size, hidden_size, hidden_act):
super(ConvBertGeneratorPredictions, self).__init__()
self.layer_norm = nn.LayerNorm(embedding_size)
self.dense = nn.Linear(hidden_size, embedding_size)
self.act = get_activation(hidden_act)
def forward(self, generator_hidden_states):
hidden_states = self.dense(generator_hidden_states)
hidden_states = self.act(hidden_states)
hidden_states = self.layer_norm(hidden_states)
return hidden_states
class ConvBertPretrainedModel(PretrainedModel):
"""
An abstract class for pretrained ConvBert models. It provides ConvBert related
`model_config_file`, `pretrained_init_configuration`, `resource_files_names`,
`pretrained_resource_files_map`, `base_model_prefix` for downloading and
loading pretrained models.
See :class:`~paddlenlp.transformers.model_utils.PretrainedModel` for more details.
"""
base_model_prefix = "convbert"
# pretrained general configuration
gen_weight = 1.0
disc_weight = 50.0
tie_word_embeddings = True
untied_generator_embeddings = False
use_softmax_sample = True
# model init configuration
pretrained_init_configuration = {
"convbert-base": {
"attention_probs_dropout_prob": 0.1,
"embedding_size": 768,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 768,
"initializer_range": 0.02,
"intermediate_size": 3072,
"max_position_embeddings": 512,
"num_attention_heads": 12,
"num_hidden_layers": 12,
"pad_token_id": 0,
"type_vocab_size": 2,
"vocab_size": 30522,
"conv_kernel_size": 9,
"head_ratio": 2,
"num_groups": 1,
},
"convbert-medium-small": {
"attention_probs_dropout_prob": 0.1,
"embedding_size": 128,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 384,
"initializer_range": 0.02,
"intermediate_size": 1536,
"max_position_embeddings": 512,
"num_attention_heads": 8,
"num_hidden_layers": 12,
"pad_token_id": 0,
"type_vocab_size": 2,
"vocab_size": 30522,
"conv_kernel_size": 9,
"head_ratio": 2,
"num_groups": 2,
},
"convbert-small": {
"attention_probs_dropout_prob": 0.1,
"embedding_size": 128,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 256,
"initializer_range": 0.02,
"intermediate_size": 1024,
"max_position_embeddings": 512,
"num_attention_heads": 4,
"num_hidden_layers": 12,
"pad_token_id": 0,
"type_vocab_size": 2,
"vocab_size": 30522,
"conv_kernel_size": 9,
"head_ratio": 2,
"num_groups": 1,
},
}
pretrained_resource_files_map = {
"model_state": {
"convbert-base": "http://bj.bcebos.com/paddlenlp/models/transformers/convbert/convbert-base/model_state.pdparams",
"convbert-medium-small": "http://bj.bcebos.com/paddlenlp/models/transformers/convbert/convbert-medium-small/model_state.pdparams",
"convbert-small": "http://bj.bcebos.com/paddlenlp/models/transformers/convbert/convbert-small/model_state.pdparams",
}
}
def init_weights(self):
"""
Initializes and tie weights if needed.
"""
# Initialize weights
self.apply(self._init_weights)
# Tie weights if needed
self.tie_weights()
def tie_weights(self):
"""
Tie the weights between the input embeddings and the output embeddings.
"""
if hasattr(self, "get_output_embeddings") and hasattr(self, "get_input_embeddings"):
output_embeddings = self.get_output_embeddings()
if output_embeddings is not None:
self._tie_or_clone_weights(output_embeddings, self.get_input_embeddings())
def _init_weights(self, layer):
"""Initialize the weights"""
if isinstance(layer, (nn.Linear, nn.Embedding, GroupedLinear)):
layer.weight.set_value(
paddle.tensor.normal(
mean=0.0,
std=self.initializer_range
if hasattr(self, "initializer_range")
else self.convbert.config["initializer_range"],
shape=layer.weight.shape,
)
)
elif isinstance(layer, nn.LayerNorm):
layer.bias.set_value(paddle.zeros_like(layer.bias))
layer.weight.set_value(paddle.full_like(layer.weight, 1.0))
layer._epsilon = 1e-12
elif isinstance(layer, SeparableConv1D):
layer.depthwise.weight.set_value(
paddle.tensor.normal(
mean=0.0,
std=self.initializer_range
if hasattr(self, "initializer_range")
else self.convbert.config["initializer_range"],
shape=layer.depthwise.weight.shape,
)
)
layer.pointwise.weight.set_value(
paddle.tensor.normal(
mean=0.0,
std=self.initializer_range
if hasattr(self, "initializer_range")
else self.convbert.config["initializer_range"],
shape=layer.pointwise.weight.shape,
)
)
if isinstance(layer, (nn.Linear, GroupedLinear, SeparableConv1D)) and layer.bias is not None:
layer.bias.set_value(paddle.zeros_like(layer.bias))
def _tie_or_clone_weights(self, output_embeddings, input_embeddings):
"""Tie or clone layer weights"""
if output_embeddings.weight.shape == input_embeddings.weight.shape:
output_embeddings.weight = input_embeddings.weight
elif output_embeddings.weight.shape == input_embeddings.weight.t().shape:
output_embeddings.weight.set_value(input_embeddings.weight.t())
else:
raise ValueError(
"when tie input/output embeddings, the shape of output embeddings: {}"
"should be equal to shape of input embeddings: {}"
"or should be equal to the shape of transpose input embeddings: {}".format(
output_embeddings.weight.shape,
input_embeddings.weight.shape,
input_embeddings.weight.t().shape,
)
)
if getattr(output_embeddings, "bias", None) is not None:
if output_embeddings.weight.shape[-1] != output_embeddings.bias.shape[0]:
raise ValueError(
"the weight lase shape: {} of output_embeddings is not equal to the bias shape: {}"
"please check output_embeddings configuration".format(
output_embeddings.weight.shape[-1],
output_embeddings.bias.shape[0],
)
)
@register_base_model
class ConvBertModel(ConvBertPretrainedModel):
"""
The bare ConvBert Model transformer outputting raw hidden-states.
This model inherits from :class:`~paddlenlp.transformers.model_utils.PretrainedModel`.
Refer to the superclass documentation for the generic methods.
This model is also a Paddle `paddle.nn.Layer <https://www.paddlepaddle.org.cn/documentation
/docs/en/api/paddle/fluid/dygraph/layers/Layer_en.html>`__ subclass. Use it as a regular Paddle Layer
and refer to the Paddle documentation for all matter related to general usage and behavior.
Args:
vocab_size (int):
Vocabulary size of `inputs_ids` in `ConvBertModel`. Also is the vocab size of token embedding matrix.
Defines the number of different tokens that can be represented by the `inputs_ids` passed when calling `ConvBertModel`.
embedding_size (int, optional):
Dimensionality of the embedding layer. Defaults to `768`.
hidden_size (int, optional):
Dimensionality of the encoder layer and pooler layer. Defaults to `768`.
num_hidden_layers (int, optional):
Number of hidden layers in the Transformer encoder. Defaults to `12`.
num_attention_heads (int, optional):
Number of attention heads for each attention layer in the Transformer encoder.
Defaults to `12`.
intermediate_size (int, optional):
Dimensionality of the feed-forward (ff) layer in the encoder. Input tensors
to ff layers are firstly projected from `hidden_size` to `intermediate_size`,
and then projected back to `hidden_size`. Typically `intermediate_size` is larger than `hidden_size`.
Defaults to `3072`.
hidden_act (str, optional):
The non-linear activation function in the feed-forward layer.
``"gelu"``, ``"relu"`` and any other paddle supported activation functions
are supported. Defaults to `"gelu"`.
hidden_dropout_prob (float, optional):
The dropout probability for all fully connected layers in the embeddings and encoder.
Defaults to `0.1`.
attention_probs_dropout_prob (float, optional):
The dropout probability used in MultiHeadAttention in all encoder layers to drop some attention target.
Defaults to `0.1`.
max_position_embeddings (int, optional):
The maximum value of the dimensionality of position encoding, which dictates the maximum supported length of an input
sequence. Defaults to `512`.
type_vocab_size (int, optional):
The vocabulary size of `token_type_ids`.
Defaults to `2`.
initializer_range (float, optional):
The standard deviation of the normal initializer.
Defaults to 0.02.
.. note::
A normal_initializer initializes weight matrices as normal distributions.
See :meth:`ConvBertPretrainedModel.init_weights()` for how weights are initialized in `ConvBertModel`.
pad_token_id (int, optional):
The index of padding token in the token vocabulary.
Defaults to `0`.
conv_kernel_size (int, optional):
The size of the convolutional kernel.
Defaults to `9`.
head_ratio (int, optional):
Ratio gamma to reduce the number of attention heads.
Defaults to `2`.
num_groups (int, optional):
The number of groups for grouped linear layers for ConvBert model.
Defaults to `1`.
"""
def __init__(
self,
vocab_size,
embedding_size=768,
hidden_size=768,
num_hidden_layers=12,
num_attention_heads=12,
intermediate_size=3072,
hidden_act="gelu",
hidden_dropout_prob=0.1,
attention_probs_dropout_prob=0.1,
max_position_embeddings=512,
type_vocab_size=2,
initializer_range=0.02,
pad_token_id=0,
conv_kernel_size=9,
head_ratio=2,
num_groups=1,
):
super(ConvBertModel, self).__init__()
self.pad_token_id = pad_token_id
self.initializer_range = initializer_range
self.embeddings = ConvBertEmbeddings(
vocab_size,
embedding_size,
hidden_dropout_prob,
max_position_embeddings,
type_vocab_size,
)
if embedding_size != hidden_size:
self.embeddings_project = nn.Linear(embedding_size, hidden_size)
encoder_layer = TransformerEncoderLayerWithConv(
hidden_size,
num_attention_heads,
intermediate_size,
dropout=hidden_dropout_prob,
activation=hidden_act,
attn_dropout=attention_probs_dropout_prob,
act_dropout=0,
conv_kernel_size=conv_kernel_size,
head_ratio=head_ratio,
num_groups=num_groups,
)
self.encoder = nn.TransformerEncoder(encoder_layer, num_hidden_layers)
self.init_weights()
def get_input_embeddings(self):
return self.embeddings.word_embeddings
def set_input_embeddings(self, value):
self.embeddings.word_embeddings = value
def forward(self, input_ids, token_type_ids=None, position_ids=None, attention_mask=None):
r"""
The ConvBertModel forward method, overrides the `__call__()` special method.
Args:
input_ids (Tensor):
Indices of input sequence tokens in the vocabulary. They are
numerical representations of tokens that build the input sequence.
Its data type should be `int64` and it has a shape of [batch_size, sequence_length].
token_type_ids (Tensor, optional):
Segment token indices to indicate different portions of the inputs.
Selected in the range ``[0, type_vocab_size - 1]``.
If `type_vocab_size` is 2, which means the inputs have two portions.
Indices can either be 0 or 1:
- 0 corresponds to a *sentence A* token,
- 1 corresponds to a *sentence B* token.
Its data type should be `int64` and it has a shape of [batch_size, sequence_length].
Defaults to `None`, which means we don't add segment embeddings.
position_ids(Tensor, optional):
Indices of positions of each input sequence tokens in the position embeddings. Selected in the range ``[0,
max_position_embeddings - 1]``.
Shape as `(batch_size, num_tokens)` and dtype as int64. Defaults to `None`.
attention_mask (Tensor, optional):
Mask used in multi-head attention to avoid performing attention on to some unwanted positions,
usually the paddings or the subsequent positions.
Its data type can be int, float and bool.
If its data type is int, the values should be either 0 or 1.
- **1** for tokens that **not masked**,
- **0** for tokens that **masked**.
It is a tensor with shape broadcasted to `[batch_size, num_attention_heads, sequence_length, sequence_length]`.
Defaults to `None`, which means nothing needed to be prevented attention to.
Returns:
Tensor: Returns Tensor `sequence_output`, sequence of hidden-states at the last layer of the model.
Shape as `[batch_size, sequence_length, hidden_size]` and dtype as float32.
Example:
.. code-block::
import paddle
from paddlenlp.transformers import ConvBertModel, ConvBertTokenizer
tokenizer = ConvBertTokenizer.from_pretrained('convbert-base')
model = ConvBertModel.from_pretrained('convbert-base')
inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!")
inputs = {k:paddle.to_tensor([v]) for (k, v) in inputs.items()}
output = model(**inputs)
"""
if attention_mask is None:
attention_mask = paddle.unsqueeze((input_ids == self.pad_token_id).astype(dtype_float) * -1e4, axis=[1, 2])
else:
attention_mask = paddle.unsqueeze(attention_mask, axis=[1, 2]).astype(dtype_float)
attention_mask = (1.0 - attention_mask) * -1e4
embedding_output = self.embeddings(
input_ids=input_ids,
position_ids=position_ids,
token_type_ids=token_type_ids,
)
if hasattr(self, "embeddings_project"):
embedding_output = self.embeddings_project(embedding_output)
sequence_output = self.encoder(embedding_output, attention_mask)
return sequence_output
class ConvBertDiscriminator(ConvBertPretrainedModel):
"""
ConvBert Model with a discriminator prediction head on top.
Args:
convbert (:class:`ConvBertModel`):
An instance of ConvBertModel.
"""
def __init__(self, convbert):
super(ConvBertDiscriminator, self).__init__()
self.convbert = convbert
self.discriminator_predictions = ConvBertDiscriminatorPredictions(
self.convbert.config["hidden_size"], self.convbert.config["hidden_act"]
)
self.init_weights()
def forward(self, input_ids, token_type_ids=None, position_ids=None, attention_mask=None):
r"""
The ConvBertDiscriminator forward method, overrides the `__call__()` special method.
Args:
input_ids (Tensor):
Indices of input sequence tokens in the vocabulary. They are
numerical representations of tokens that build the input sequence.
Its data type should be `int64` and it has a shape of [batch_size, sequence_length].
token_type_ids (Tensor, optional):
Segment token indices to indicate different portions of the inputs.
Selected in the range ``[0, type_vocab_size - 1]``.
If `type_vocab_size` is 2, which means the inputs have two portions.
Indices can either be 0 or 1:
- 0 corresponds to a *sentence A* token,
- 1 corresponds to a *sentence B* token.
Its data type should be `int64` and it has a shape of [batch_size, sequence_length].
Defaults to `None`, which means we don't add segment embeddings.
position_ids(Tensor, optional):
Indices of positions of each input sequence tokens in the position embeddings. Selected in the range ``[0,
max_position_embeddings - 1]``.
Shape as `(batch_size, num_tokens)` and dtype as int64. Defaults to `None`.
attention_mask (Tensor, optional):
Mask used in multi-head attention to avoid performing attention on to some unwanted positions,
usually the paddings or the subsequent positions.
Its data type can be int, float and bool.
If its data type is int, the values should be either 0 or 1.
- **1** for tokens that **not masked**,
- **0** for tokens that **masked**.
It is a tensor with shape broadcasted to `[batch_size, num_attention_heads, sequence_length, sequence_length]`.
Defaults to `None`, which means nothing needed to be prevented attention to.
Returns:
Tensor: Returns tensor `logits`, a tensor of the discriminator prediction logits.
Shape as `[batch_size, sequence_length]` and dtype as float32.
Example:
.. code-block::
import paddle
from paddlenlp.transformers import ConvBertDiscriminatorPredictions, ConvBertTokenizer
tokenizer = ConvBertTokenizer.from_pretrained('convbert-base')
model = ConvBertDiscriminatorPredictions.from_pretrained('convbert-base')
inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!")
inputs = {k:paddle.to_tensor([v]) for (k, v) in inputs.items()}
logits = model(**inputs)
"""
discriminator_sequence_output = self.convbert(input_ids, token_type_ids, position_ids, attention_mask)
logits = self.discriminator_predictions(discriminator_sequence_output)
return logits
class ConvBertGenerator(ConvBertPretrainedModel):
"""
ConvBert Model with a generator prediction head on top.
Args:
convbert (:class:`ConvBertModel`):
An instance of ConvBertModel.
"""
def __init__(self, convbert):
super(ConvBertGenerator, self).__init__()
self.convbert = convbert
self.generator_predictions = ConvBertGeneratorPredictions(
self.convbert.config["embedding_size"],
self.convbert.config["hidden_size"],
self.convbert.config["hidden_act"],
)
if not self.tie_word_embeddings:
self.generator_lm_head = nn.Linear(
self.convbert.config["embedding_size"], self.convbert.config["vocab_size"]
)
else:
self.generator_lm_head_bias = paddle.create_parameter(
shape=[self.convbert.config["vocab_size"]],
dtype=dtype_float,
is_bias=True,
)
self.init_weights()
def get_input_embeddings(self):
return self.convbert.embeddings.word_embeddings
def forward(
self,
input_ids=None,
token_type_ids=None,
position_ids=None,
attention_mask=None,
):
r"""
The ConvBertGenerator forward method, overrides the `__call__()` special method.
Args:
input_ids (Tensor):
Indices of input sequence tokens in the vocabulary. They are
numerical representations of tokens that build the input sequence.
Its data type should be `int64` and it has a shape of [batch_size, sequence_length].
token_type_ids (Tensor, optional):
Segment token indices to indicate different portions of the inputs.
Selected in the range ``[0, type_vocab_size - 1]``.
If `type_vocab_size` is 2, which means the inputs have two portions.
Indices can either be 0 or 1:
- 0 corresponds to a *sentence A* token,
- 1 corresponds to a *sentence B* token.
Its data type should be `int64` and it has a shape of [batch_size, sequence_length].
Defaults to `None`, which means we don't add segment embeddings.
position_ids(Tensor, optional):
Indices of positions of each input sequence tokens in the position embeddings. Selected in the range ``[0,
max_position_embeddings - 1]``.
Shape as `(batch_size, num_tokens)` and dtype as int64. Defaults to `None`.
attention_mask (Tensor, optional):
Mask used in multi-head attention to avoid performing attention on to some unwanted positions,
usually the paddings or the subsequent positions.
Its data type can be int, float and bool.
If its data type is int, the values should be either 0 or 1.
- **1** for tokens that **not masked**,
- **0** for tokens that **masked**.
It is a tensor with shape broadcasted to `[batch_size, num_attention_heads, sequence_length, sequence_length]`.
Defaults to `None`, which means nothing needed to be prevented attention to.
Returns:
Tensor: Returns tensor `prediction_scores`, a tensor of the generator prediction scores.
Shape as `[batch_size, sequence_length, vocab_size]` and dtype as float32.
Example:
.. code-block::
import paddle
from paddlenlp.transformers import ConvBertGenerator, ConvBertTokenizer
tokenizer = ConvBertTokenizer.from_pretrained('convbert-base')
model = ConvBertGenerator.from_pretrained('convbert-base')
inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!")
inputs = {k:paddle.to_tensor([v]) for (k, v) in inputs.items()}
prediction_scores = model(**inputs)
"""
generator_sequence_output = self.convbert(input_ids, token_type_ids, position_ids, attention_mask)
prediction_scores = self.generator_predictions(generator_sequence_output)
if not self.tie_word_embeddings:
prediction_scores = self.generator_lm_head(prediction_scores)
else:
prediction_scores = paddle.add(
paddle.matmul(
prediction_scores,
self.get_input_embeddings().weight,
transpose_y=True,
),
self.generator_lm_head_bias,
)
return prediction_scores
class ConvBertClassificationHead(nn.Layer):
"""
ConvBert head for sentence-level classification tasks.
"""
def __init__(self, hidden_size, hidden_dropout_prob, num_classes):
super(ConvBertClassificationHead, self).__init__()
self.dense = nn.Linear(hidden_size, hidden_size)
self.dropout = nn.Dropout(hidden_dropout_prob)
self.out_proj = nn.Linear(hidden_size, num_classes)
self.act = get_activation("gelu")
def forward(self, features, **kwargs):
x = features[:, 0, :] # take [CLS] token
x = self.dropout(x)
x = self.dense(x)
x = self.act(x) # ConvBert paper used gelu here
x = self.dropout(x)
x = self.out_proj(x)
return x
class ConvBertForSequenceClassification(ConvBertPretrainedModel):
"""
ConvBert Model with a linear layer on top of the output layer,
designed for sequence classification/regression tasks like GLUE tasks.
Args:
convbert (:class:`ConvBertModel`):
An instance of ConvBertModel.
num_classes (int, optional):
The number of classes. Defaults to `2`.
dropout (float, optional):
The dropout probability for output of ConvBert.
If None, use the same value as `hidden_dropout_prob` of `ConvBertModel`
instance `convbert`. Defaults to None.
"""
def __init__(self, convbert, num_classes=2, dropout=None):
super(ConvBertForSequenceClassification, self).__init__()
self.num_classes = num_classes
self.convbert = convbert
self.classifier = ConvBertClassificationHead(
hidden_size=self.convbert.config["hidden_size"],
hidden_dropout_prob=dropout if dropout is not None else self.convbert.config["hidden_dropout_prob"],
num_classes=self.num_classes,
)
self.init_weights()
def forward(
self,
input_ids=None,
token_type_ids=None,
position_ids=None,
attention_mask=None,
):
r"""
The ConvBertForSequenceClassification forward method, overrides the __call__() special method.
Args:
input_ids (Tensor):
See :class:`ConvBertModel`.
token_type_ids (Tensor, optional):
See :class:`ConvBertModel`.
position_ids(Tensor, optional):
See :class:`ConvBertModel`.
attention_mask (list, optional):
See :class:`ConvBertModel`.
Returns:
Tensor: Returns tensor `logits`, a tensor of the input text classification logits.
Shape as `[batch_size, num_classes]` and dtype as float32.
Example:
.. code-block::
import paddle
from paddlenlp.transformers import ConvBertForSequenceClassification, ConvBertTokenizer
tokenizer = ConvBertTokenizer.from_pretrained('convbert-base')
model = ConvBertForSequenceClassification.from_pretrained('convbert-base')
inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!")
inputs = {k:paddle.to_tensor([v]) for (k, v) in inputs.items()}
logits = model(**inputs)
"""
sequence_output = self.convbert(input_ids, token_type_ids, position_ids, attention_mask)
logits = self.classifier(sequence_output)
return logits
class ConvBertForTokenClassification(ConvBertPretrainedModel):
"""
ConvBert Model with a linear layer on top of the hidden-states output layer,
designed for token classification tasks like NER tasks.
Args:
convbert (:class:`ConvBertModel`):
An instance of ConvBertModel.
num_classes (int, optional):
The number of classes. Defaults to `2`.
dropout (float, optional):
The dropout probability for output of ConvBert.
If None, use the same value as `hidden_dropout_prob` of `ConvBertModel`
instance `convbert`. Defaults to None.
"""