forked from graphcore/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbert_model.py
995 lines (841 loc) · 41.6 KB
/
bert_model.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
# Copyright 2019 Graphcore Ltd.
import popart
import numpy as np
from scipy.stats import truncnorm
from typing import NamedTuple, Optional
from contextlib import ExitStack
from collections import defaultdict
from enum import Enum
import logging
import math
import os
from collections import defaultdict
from contextlib import ExitStack, contextmanager
from enum import Enum
from functools import reduce
from typing import List, NamedTuple, Optional
import numpy as np
from scipy.stats import truncnorm
import popart
from pingpong.scope_manager import ScopeProvider
logger = logging.getLogger(__name__)
class ExecutionMode(str, Enum):
DEFAULT = "DEFAULT"
PIPELINE = "PIPELINE"
PHASED = "PHASED"
class BertConfig(NamedTuple):
batch_size: int = 1
sequence_length: int = 128
max_positional_length: int = 512
# Choices: "DEFAULT", "TRANSFORMER", "SIMPLIFIED"
positional_embedding_init_fn: str = "DEFAULT"
# Look up embedding on CPU
# Possible values:
# NONE = all embeddings on IPU
# WORD = word embeddings on CPU, position embeddings on IPU
# ALL = all embeddings on CPU, both word and position embeddings sent to IPU
# MERGE = all embeddings on CPU, sum of word and position embeddings sent to IPU
host_embedding: str = "NONE"
# PRETRAINING Only
mask_tokens: int = 20
vocab_length: int = 30400
hidden_size: int = 768
# Feed Forward is 4 * hidden_size unless specified by --ff-size
ff_size__: Optional[int] = None
@property
def ff_size(self):
if self.ff_size__ is not None:
return self.ff_size__
return self.hidden_size * 4
attention_heads: int = 12
inference: bool = False
num_layers: int = 2
# Specify the ipu to start adding encoders.
# if encoder_start_ipu >= 2: two IPUs will be used for the embeddings
# else: one IPU will be used for the embeddings
encoder_start_ipu: int = 2
# Placement of layers can be specified by either:
# a single element list, which will place num_layers/layers_per_ipu[0] on each IPU
# Or a list specifying the placement on each IPU.
layers_per_ipu: List[int] = [2]
# UNSUPPORTED: Try and fit the model onto fewer IPUs. Intended for inference modes:
squeeze_model: bool = False
split_transformer: bool = False
no_dropout: bool = False
no_attn_dropout: bool = False
dropout_prob: float = 0.1
attn_dropout_prob: float = 0.1
layer_norm_eps: float = 0.001
# Choices: PRETRAINING (MLM + NSP), SQUAD
task: str = "PRETRAINING"
# This option serializes all matmul layers to multiples
# {N, hidden_size} x {hidden_size, hidden_size}.
# This is required for sequence length 384.
split_linear_layers: bool = False
no_mask: bool = False
activation_type: str = 'Gelu'
relu_leak: float = 0.1
# Choices: FLOAT, FLOAT16
popart_dtype: str = "FLOAT16"
@property
def dtype(self):
if self.popart_dtype == "FLOAT":
return np.float32
elif self.popart_dtype == "FLOAT16":
return np.float16
else:
raise ValueError("BertConfig.dtype must be 'FLOAT' or 'FLOAT16'")
@property
def qkv_length(self):
return self.hidden_size / self.attention_heads
# In PRETRAINING this sets how many steps to serialise both the
# embedding and projection
projection_serialization_steps: int = 5
embedding_serialization_vocab_steps: int = 1
update_embedding_dict: bool = True
use_default_available_memory_proportion: bool = False
no_cls_layer: bool = False
projection_bias: bool = False
max_matmul_memory: int = 40000
num_attention_splits: int = 1
num_ffwd_splits: int = 1
execution_mode: ExecutionMode = ExecutionMode.DEFAULT
@property
def available_memory_proportion(self):
return self.max_matmul_memory / (2**18)
class DeviceScope(object):
def __init__(self,
builder,
execution_mode=ExecutionMode.DEFAULT,
virtualGraph=None,
pipelineStage=None,
executionPhase=None,
nameScope=None,
additional_scopes=None):
self.builder = builder
self.execution_mode = execution_mode
self.virtualGraph = virtualGraph
self.pipelineStage = pipelineStage
self.executionPhase = executionPhase
self.nameScope = nameScope
self.additional_scopes = additional_scopes or []
def __enter__(self):
self.stack = ExitStack()
# ExecutionPhase will automatically set the virtualGraph attributes based on ping pong phase
if self.execution_mode != ExecutionMode.PHASED \
and self.virtualGraph is not None:
self.stack.enter_context(
self.builder.virtualGraph(self.virtualGraph))
if self.execution_mode == ExecutionMode.PIPELINE\
and self.pipelineStage is not None:
self.stack.enter_context(
self.builder.pipelineStage(self.pipelineStage))
if self.execution_mode == ExecutionMode.PHASED\
and self.executionPhase is not None:
self.stack.enter_context(
self.builder.executionPhase(self.executionPhase))
if self.nameScope is not None:
self.stack.enter_context(self.builder.nameScope(self.nameScope))
for scope in self.additional_scopes:
self.stack.enter_context(scope)
return self
def __exit__(self, *exp):
self.stack.close()
return False
class Model(object):
def __init__(self, builder=popart.Builder(), initializers=None, execution_mode=ExecutionMode.DEFAULT):
if initializers is None:
initializers = {}
self.builder = builder
self.initializers = initializers
if type(execution_mode) == str:
execution_mode = ExecutionMode(execution_mode)
self.execution_mode = execution_mode
# Keep track of tensors in order to give them different parameters
self.tensors = defaultdict(list)
def normal_init_tensor(self, dtype, shape, mean, std_dev, debug_name=""):
data = self.normal_init_data(dtype, shape, mean, std_dev, debug_name)
tensor = self.builder.addInitializedInputTensor(data, debug_name)
self._add_to_tensor_map(tensor)
return tensor
def normal_init_data(self, dtype, shape, mean, std_dev, debug_name=""):
name = self.builder.getNameScope(debug_name)
data = self.initializers.get(name, None)
if data is None:
# Truncated random normal between 2 standard devations
data = truncnorm.rvs(-2, 2, loc=mean,
scale=std_dev, size=np.prod(shape))
data = data.reshape(shape).astype(dtype)
self.initializers[name] = data
else:
if np.any(data.shape != np.array(shape)):
if np.all(data.T.shape == np.array(shape)):
data = data.T.copy()
logger.warning(
f"Initializer for {name} was provided transposed.")
else:
raise RuntimeError(f"Initializer {name} does not match shapes. \n"
f" Provided {data.shape}. Required {shape}")
return data
def constant_init_tensor(self, dtype, shape, scalar, debug_name="", is_const=False):
data = self.initializers.get(
self.builder.getNameScope(debug_name), None)
if data is None:
data = np.full(shape, scalar).astype(dtype)
else:
if np.any(data.shape != shape):
raise RuntimeError(f"Initializer {self.builder.getNameScope(debug_name)} does not match shapes. \n"
f" Provided {data.shape}. Required {shape}")
if is_const:
return self.builder.aiOnnx.constant(data, debug_name)
tensor = self.builder.addInitializedInputTensor(data, debug_name)
self._add_to_tensor_map(tensor)
return tensor
def constant_tensor(self, value, dtype=None, debug_name=""):
value = np.array(value)
if dtype is not None:
value = value.astype(dtype)
return self.builder.aiOnnx.constant(value, debug_name)
def device_scope(self, virtualGraph=None, pipelineStage=None, executionPhase=None, nameScope=None, additional_scopes=None):
return DeviceScope(self.builder, self.execution_mode, virtualGraph, pipelineStage, executionPhase, nameScope, additional_scopes)
def _add_to_tensor_map(self, tensor):
if self.builder.hasPipelineStage():
pipeline_stage = self.builder.getPipelineStage()
self.tensors[pipeline_stage].append(tensor)
else:
self.tensors[0].append(tensor)
class Bert(Model):
def __init__(self, config, **kwargs):
super().__init__(**kwargs)
self.config = config
self.dropout_modifier = 256
# This is the TensorId for the shared embedding & projection
self.embedding_dict = None
# This dict[ipu,TensorId] reuses any mask already generated on an IPU/pipeline stage
self.masks = {}
self.init_device_placement()
def init_device_placement(self):
''' Create a DeviceScope for each layer, ie Embedding, SQUAD, NSP
'''
# Precompute offset for masks, which are prepared in phase 0 and 1
self.execution_phase_precompute_offset = 2
if self.config.squeeze_model:
raise Exception("Config squeeze_model is no longer supported. "
"Please use `encoder_start_ipu` to specify which ipu the first Encoder layer should be placed.")
layer_offset = self.config.encoder_start_ipu
# Embedding
self.embedding_scope = self.device_scope(
0, 0, self.execution_phase_precompute_offset)
ipu = max(layer_offset - 1, 0)
self.embedding_split_scope = self.device_scope(
ipu, ipu, ipu + self.execution_phase_precompute_offset)
# Transformer Layers
pipeline_stage = self.encoder_ipu(self.config.num_layers - 1) + 1
execution_phase = pipeline_stage + self.execution_phase_precompute_offset
# Task Layers
if self.config.task in ("NSP", "PRETRAINING"):
self.nsp_scope = self.device_scope(
self.embedding_split_scope.virtualGraph, pipeline_stage, execution_phase, "NSP")
self.cls_scope = self.device_scope(
self.embedding_split_scope.virtualGraph, pipeline_stage, execution_phase, "CLS")
if self.config.task == "PRETRAINING":
if (layer_offset - 1) != 0:
pipeline_stage += 1
execution_phase += 1
self.mlm_scope = self.device_scope(
self.embedding_scope.virtualGraph, pipeline_stage, execution_phase, "MLM")
self.final_loss_scope = self.mlm_scope
if self.config.task == "SQUAD":
ipu = self.embedding_scope.virtualGraph
if self.config.inference:
pipeline_stage -= 1
ipu = pipeline_stage
self.squad_scope = self.device_scope(
ipu, pipeline_stage, execution_phase, "Squad")
self.final_loss_scope = self.squad_scope
# Scope to place all IO on first IPU for inference:
if self.config.inference:
pipeline_stage += 1
execution_phase += 1
self.output_scope = self.device_scope(
self.embedding_scope.virtualGraph, pipeline_stage, execution_phase, "Output")
else:
self.output_scope = None
self.total_pipeline_stages = pipeline_stage + 1
self.total_execution_phases = execution_phase + 1
@property
def total_ipus(self):
return self.encoder_ipu(self.config.num_layers - 1) + 1
def encoder_scope(self, layer_index):
ipu = self.encoder_ipu(layer_index)
execution_phase = ipu + self.execution_phase_precompute_offset
logger.debug(f"Encoder Layer {layer_index} -> IPU {ipu}")
return self.device_scope(ipu, ipu, execution_phase, f"Layer{layer_index}")
def encoder_ipu(self, layer_index):
encoder_index = 0
if len(self.config.layers_per_ipu) == 1:
encoder_index = layer_index // self.config.layers_per_ipu[0]
else:
for ipu, num_layers in enumerate(self.config.layers_per_ipu):
layer_index -= num_layers
if layer_index < 0:
encoder_index = ipu
break
return encoder_index + self.config.encoder_start_ipu
def build_graph(self, indices, positions, segments, masks=None):
# Embedding
with self.builder.nameScope("Embedding"):
x = self.embedding(indices, positions, segments)
# This forces the masks to be streamed on to the IPU before the encoder layers.
# Allowing the communication to be better overlapped with compute as the
# compute dominant encoder IPUs will not participate in any streamCopies.
if masks is not None:
with self.embedding_split_scope:
masks = [self.detach(mask) for mask in masks]
# Encoder Layers
for i in range(self.config.num_layers):
with self.encoder_scope(i):
with self.builder.nameScope("Attention"):
x = self.attention(x, masks)
with self.builder.nameScope("FF"):
x = self.feed_forward(x)
outputs = []
# PreTraining tasks
if self.config.task in ("NSP", "PRETRAINING"):
with self.nsp_scope:
outputs.append(self.nsp_head(x))
if self.config.task == "PRETRAINING":
with self.cls_scope:
if self.config.no_cls_layer:
predictions = self.builder.aiOnnx.identity([x])
else:
predictions = self.lm_prediction_head(x)
with self.mlm_scope:
outputs = [self.projection(predictions)] + outputs
# Fine Tuning tasks
if self.config.task == "SQUAD":
with self.squad_scope:
squad_outputs = self.squad_projection(x)
if self.output_scope:
with self.output_scope:
outputs += [self.detach(tensor)
for tensor in squad_outputs]
else:
outputs += squad_outputs
if self.config.task == "MRPC":
# TODO: Implement this: T11026
raise NotImplementedError()
return tuple(outputs)
def norm(self, input_x):
gamma = self.constant_init_tensor(
self.config.dtype, (self.config.hidden_size,), 1, "Gamma")
beta = self.constant_init_tensor(
self.config.dtype, (self.config.hidden_size,), 0, "Beta")
outs = self.builder.aiGraphcore.groupnormalization(
[input_x, gamma, beta], 1, self.config.layer_norm_eps)
return outs[0]
def dropout(self, input_x):
if not self.config.no_dropout:
return self.builder.aiOnnx.dropout([input_x], 1, self.config.dropout_prob)[0]
return input_x
def leaky_relu(self, input_x, alpha):
"""
This function implements the leaky relu activation function.
The mathematical function is:
Leaky_Relu(x) = Relu(x) - alpha*Relu(-x)
"""
alpha_t = self.builder.aiOnnx.constant(
np.asarray([alpha], dtype=self.config.dtype)
)
result_plus = self.builder.aiOnnx.relu([input_x])
minus_x = self.builder.aiOnnx.neg([input_x])
result_minus = self.builder.aiOnnx.relu([minus_x])
result_minus = self.builder.aiOnnx.mul([alpha_t, result_minus])
result = self.builder.aiOnnx.sub([result_plus, result_minus])
return result
def simplified_gelu(self, input_x):
"""
Simpler implementation of the GELU based on the sigmoid.
Coming from the original Gelu paper (https://arxiv.org/abs/1606.08415).
"""
scale = self.builder.aiOnnx.constant(
np.asarray([1.702], dtype=self.config.dtype))
result = self.builder.aiOnnx.mul([scale, input_x])
result = self.builder.aiOnnx.sigmoid([result])
result = self.builder.aiOnnx.mul([input_x, result])
return result
def intermediate_activation_function(self, input_x):
if self.config.activation_type == 'Gelu':
return self.builder.aiGraphcore.gelu([input_x])
elif self.config.activation_type == 'SGelu':
return self.simplified_gelu(input_x)
elif self.config.activation_type == 'LRelu':
return self.leaky_relu(input_x, alpha=self.config.relu_leak)
else:
return self.builder.aiOnnx.relu([input_x])
def feed_forward(self, input_x):
# If using `split_linear_layers` num_splits should make each matmul of size [hidden, hidden]
num_splits = self.config.ff_size // self.config.hidden_size
with self.builder.nameScope("1"):
weight1 = self.normal_init_tensor(self.config.dtype,
[self.config.hidden_size,
self.config.ff_size],
0, 0.02,
"W")
bias1 = self.constant_init_tensor(self.config.dtype,
(self.config.ff_size,),
0,
"B")
x = self.builder.aiOnnx.matmul([input_x, weight1])
if self.config.split_linear_layers:
self.builder.setSerializeMatMul({x},
'output_channels',
num_splits,
keep_precision=True)
if not self.config.use_default_available_memory_proportion:
self.builder.setAvailableMemoryProportion(
x, self.config.available_memory_proportion)
x = self.builder.aiOnnx.add([x, bias1])
x = self.intermediate_activation_function(x)
with self.builder.nameScope("2"):
weight2 = self.normal_init_tensor(self.config.dtype,
[self.config.ff_size,
self.config.hidden_size],
0, 0.02,
"W")
bias2 = self.constant_init_tensor(self.config.dtype,
(self.config.hidden_size,),
0,
"B")
x = self.builder.aiOnnx.matmul([x, weight2])
if self.config.split_linear_layers:
self.builder.setSerializeMatMul({x},
'reducing_dim',
num_splits,
keep_precision=True)
if not self.config.use_default_available_memory_proportion:
self.builder.setAvailableMemoryProportion(
x, self.config.available_memory_proportion)
x = self.builder.aiOnnx.add([x, bias2])
# google-research/bert puts dropout here
x = self.dropout(x)
x = self.builder.aiOnnx.add([input_x, x])
x = self.norm(x)
return x
def detach(self, input_x, pass_through_creation=1):
if self.config.inference:
return input_x
return self.builder.customOp(opName="Detach",
opVersion=1,
domain="ai.graphcore",
inputs=[input_x],
attributes={
"pass_through_creation": pass_through_creation
})[0]
def generate_simplified_periodic_pos_data(self, dtype, shape, scale=4):
def value(x, y):
return .02/.707*np.cos(2*scale*np.pi*x*y/shape[1])
X, Y = np.mgrid[:shape[0], :shape[1]]
return np.vectorize(value)(X, Y,).astype(dtype)
def generate_transformer_periodic_pos_data(self, dtype, shape, min_timescale=1.0, max_timescale=1.0e4):
"""
Periodic position initialiser, from 3.5 of "Attention is All You Need". Adapted from:
https://github.com/tensorflow/models/tree/master/official/transformer/v2
"""
position = np.arange(0, shape[0], dtype=dtype)
num_timescales = shape[1] // 2
log_timescale_increment = (
math.log(float(max_timescale) / float(min_timescale)) / (num_timescales - 1))
hidden_idx = np.arange(0, num_timescales, dtype=dtype)
inv_timescales = min_timescale * np.exp(
hidden_idx * -log_timescale_increment)
expanded_pos = np.expand_dims(position, 1)
expanded_ts = np.expand_dims(inv_timescales, 0)
scaled_time = expanded_pos * expanded_ts
signal = np.concatenate(
[np.sin(scaled_time), np.cos(scaled_time)], axis=1)
return signal
def get_embedding_data(self, dtype, shape, init_fn, debug_name=""):
# Unless specifcally set, fall back to normal tensor initialisation
if init_fn not in ("TRANSFORMER", "SIMPLIFIED"):
return self.normal_init_data(dtype, shape, 0, 0.02, debug_name)
data = self.initializers.get(
self.builder.getNameScope(debug_name), None)
if data is None:
if init_fn == "TRANSFORMER":
data = self.generate_transformer_periodic_pos_data(
dtype, shape)
else:
data = self.generate_simplified_periodic_pos_data(dtype, shape)
else:
if np.any(data.shape != np.array(shape)):
raise RuntimeError(f"Initializer {self.builder.getNameScope(debug_name)} does not match shapes. \n"
f" Provided {data.shape}. Required {shape}")
return data
def embedding_init_tensor(self, dtype, shape, init_fn, debug_name=""):
# Unless specifcally set, fall back to normal tensor initialisation
data = self.get_embedding_data(dtype, shape, init_fn, debug_name)
tensor = self.builder.addInitializedInputTensor(data, debug_name)
self._add_to_tensor_map(tensor)
return tensor
def get_model_embeddings(self):
embedding_dict = None
positional_dict = None
if self.config.host_embedding in ("ALL", "WORD", "MERGE"):
with self.builder.nameScope("Embedding"):
embedding_dict = self.get_embedding_data(self.config.dtype,
(self.config.vocab_length, self.config.hidden_size),
"DEFAULT",
"Embedding_Dict")
if self.config.host_embedding in ("ALL", "MERGE"):
positional_dict = self.get_embedding_data(self.config.dtype,
(self.config.max_positional_length, self.config.hidden_size),
self.config.positional_embedding_init_fn,
"Positional_Dict")
return embedding_dict, positional_dict
def embedding(self, indices, positions, segments):
with self.embedding_scope:
x = self.gather(indices,
self.config.vocab_length,
"Embedding_Dict")
with self.embedding_split_scope:
segments_onehot = self.builder.aiOnnx.onehot([
segments,
self.constant_tensor(2, dtype=np.int32),
self.constant_tensor([0, 1], dtype=self.config.dtype)])
segments_weights = self.normal_init_tensor(
self.config.dtype,
[2, self.config.hidden_size],
0, 0.02, "Segment_Dict")
x_seg = self.builder.aiOnnx.matmul(
[segments_onehot, segments_weights])
if self.config.host_embedding != "MERGE":
x_pos = self.gather(positions,
self.config.max_positional_length,
"Positional_Dict",
init_fn=self.config.positional_embedding_init_fn)
x = self.builder.aiOnnx.add([x, x_pos])
x = self.builder.aiOnnx.add([x, x_seg])
# When outlining is enabled, under certain situations, the `add` above resolves
# to an AddLhsInPlace, which then causes the output to be laid out incorrectly
# for SQuAD. This workaround ensures it stays as an AddRhsInPlace.
self.builder.setInplacePreferences(x, {"AddRhsInplace": 1000.0})
x = self.norm(x)
x = self.dropout(x)
return x
def gather(self, indices, embedding_size, name, init_fn="DEFAULT"):
if self.config.host_embedding in ("ALL", "WORD", "MERGE") and name == "Embedding_Dict":
return indices
if self.config.host_embedding in ("ALL", "MERGE") and name == "Positional_Dict":
return indices
if name == "Embedding_Dict" and self.config.task == "PRETRAINING":
# Important that the tied gather/matmul weight with transpose before the gather.
# This will ensure it matches the custom_ops/tied_gather_pattern.
embedding_dict = self.embedding_init_tensor(
self.config.dtype,
(self.config.hidden_size, embedding_size),
init_fn,
name)
self.embedding_dict = embedding_dict
if self.config.inference:
embedding_dict = self.builder.customOp(opName="PreventConstFolding",
opVersion=1,
domain="ai.graphcore",
inputs=[embedding_dict],
attributes={})[0]
embedding_dict = self.builder.aiOnnx.transpose([embedding_dict])
else:
embedding_dict = self.embedding_init_tensor(
self.config.dtype,
(embedding_size, self.config.hidden_size),
init_fn,
name)
x = self.builder.aiOnnx.gather([embedding_dict, indices])
if name == "Embedding_Dict" and not self.config.update_embedding_dict:
x = self.detach(x)
return x
def attention_mask(self, masks):
"""
Create a mask tensor that has -1000 in positions to be masked and 0 otherwise.
If the task is MLM or PRETRAINING:
masks[0] is the index that masking starts in the mask_tokens
masks[1] is the index that masking starts in the rest of the sequence
Otherwise
masks[0] is the index that masking starts in the rest of the sequence
Example:
Task: PRETRAINING
masks: [2, 5]
mask_tokens: 4
returns: [0,0,-1000.0, -1000.0, 0, -1000.0, ...]
"""
if self.execution_mode == ExecutionMode.PHASED:
mask_idx = self.builder.getExecutionPhase() % 2
additional_scopes = [
self.builder.recomputeOutput(popart.RecomputeType.Checkpoint),
self.builder.outputTensorLocation(popart.TensorLocation.OnChip)
]
else:
mask_idx = self.builder.getVirtualGraph() if self.builder.hasVirtualGraph() else None
additional_scopes = None
if mask_idx in self.masks:
return self.masks[mask_idx]
mask_scope = self.device_scope(mask_idx,
self.builder.getPipelineStage() if self.builder.hasPipelineStage() else None,
mask_idx,
"Mask",
additional_scopes=additional_scopes)
with mask_scope:
base_value = np.arange(self.config.sequence_length)
base = self.constant_tensor(base_value, np.uint32, "mask_sequence")
if self.config.task == "PRETRAINING":
# Mask tokens mask
mmask = self.builder.aiOnnx.less([base, masks[0]])
# No constexpr for greater. Create as const instead
_mask = self.constant_tensor(np.greater_equal(
base_value, self.config.mask_tokens), np.bool)
mmask = self.builder.aiOnnx.logical_or([mmask, _mask])
# Sequence mask
smask = self.builder.aiOnnx.less([base, masks[1]])
final_mask = self.builder.aiOnnx.logical_and([mmask, smask])
else:
final_mask = self.builder.aiOnnx.less([base, masks[0]])
final_mask = self.builder.aiOnnx.cast(
[final_mask], self.config.popart_dtype)
final_mask = self.builder.aiOnnx.sub(
[final_mask, self.constant_tensor(1.0, self.config.dtype)])
final_mask = self.builder.aiOnnx.mul(
[final_mask, self.constant_tensor(1000.0, self.config.dtype)])
final_mask = self.builder.reshape_const(
self.builder.aiOnnx,
[final_mask],
[self.config.batch_size, 1, 1, self.config.sequence_length])
# TODO: This shouldn't be needed. No Variables on this path.
final_mask = self.detach(final_mask)
self.masks[mask_idx] = final_mask
return final_mask
def attention(self, input_x, masks=None):
qkv_weights = self.normal_init_tensor(
self.config.dtype,
[self.config.hidden_size, 3 * self.config.hidden_size],
0, 0.02,
"QKV")
qkv = self.builder.aiOnnx.matmul([input_x, qkv_weights])
if self.config.split_linear_layers:
self.builder.setSerializeMatMul({qkv}, 'output_channels', 3, True)
if not self.config.use_default_available_memory_proportion:
self.builder.setAvailableMemoryProportion(
qkv, self.config.available_memory_proportion)
x = self.attention_onnx(qkv, masks)
projection_weights = self.normal_init_tensor(
self.config.dtype,
[self.config.hidden_size, self.config.hidden_size],
0, 0.02,
"Out")
x = self.builder.aiOnnx.matmul([x, projection_weights])
if not self.config.use_default_available_memory_proportion:
self.builder.setAvailableMemoryProportion(
x, self.config.available_memory_proportion)
x = self.dropout(x)
x = self.builder.aiOnnx.add([input_x, x])
x = self.norm(x)
return x
def attention_onnx(self, qkv, masks):
comb_shape = [self.config.batch_size, self.config.sequence_length,
self.config.attention_heads, self.config.qkv_length]
def extract_heads(tensor, transpose=False):
tensor = self.builder.reshape_const(
self.builder.aiOnnx, [tensor], comb_shape)
perm = [0, 2, 1, 3] if not transpose else [0, 2, 3, 1]
return self.builder.aiOnnx.transpose([tensor], perm=perm)
split_qkv = self.builder.aiOnnx.split(
[qkv],
num_outputs=3,
axis=1,
split=[self.config.hidden_size]*3,
debugPrefix="QKV_Split")
q, kt, v = [extract_heads(t, i == 1) for i, t in enumerate(split_qkv)]
# Attention calculation
with self.builder.nameScope('Z'):
x = self.builder.aiOnnx.matmul([q, kt])
if not self.config.use_default_available_memory_proportion:
self.builder.setAvailableMemoryProportion(
x, self.config.available_memory_proportion)
c = self.constant_tensor(
1 / np.sqrt(self.config.qkv_length), self.config.dtype)
x = self.builder.aiOnnx.mul([x, c])
if self.config.no_mask or masks is not None:
mask = self.attention_mask(masks)
x = self.builder.aiOnnx.add([x, mask], "ApplyMask")
x = self.builder.aiOnnx.softmax([x], axis=-1)
if not self.config.no_attn_dropout:
x = self.dropout(x)
# x[batch_size, attention_heads, sequence_length, sequence_length] * v[batch_size, attention_heads, sequence_length, qkv_length]
z = self.builder.aiOnnx.matmul([x, v])
if not self.config.use_default_available_memory_proportion:
self.builder.setAvailableMemoryProportion(
z, self.config.available_memory_proportion)
# [batch_size, attention_heads, sequence_length, qkv_length] -> [batch_size, sequence_length, attention_heads, qkv_length]
z = self.builder.aiOnnx.transpose([z], perm=[0, 2, 1, 3])
# [batch_size, sequence_length, attention_heads, qkv_length] -> [batch_size*sequence_length, attention_heads*qkv_length]
z = self.builder.reshape_const(self.builder.aiOnnx, [z], [
self.config.sequence_length * self.config.batch_size, self.config.hidden_size])
return z
def projection(self, input_x):
x = self.builder.reshape_const(self.builder.aiOnnx, [input_x], [
self.config.batch_size, self.config.sequence_length, self.config.hidden_size])
x = self.builder.aiOnnxOpset9.slice([x], axes=[1], starts=[
0], ends=[self.config.mask_tokens])
x = self.builder.reshape_const(self.builder.aiOnnx, [x], [
self.config.batch_size * self.config.mask_tokens, self.config.hidden_size])
weight = self.embedding_dict
# Move the weight to the current pipeline stage
if weight in self.tensors[self.embedding_scope.pipelineStage]:
embedding_stage = self.embedding_scope.pipelineStage
self.tensors[embedding_stage].remove(weight)
self._add_to_tensor_map(weight)
x = self.builder.aiOnnx.matmul([x, weight])
num_splits = self.config.projection_serialization_steps
self.builder.setSerializeMatMul(
{x}, 'output_channels', num_splits, True)
if self.config.projection_bias:
bias = self.constant_init_tensor(self.config.dtype, (self.config.vocab_length,), 0, "ProjectionB")
x = self.builder.aiOnnx.add([x, bias])
x = self.builder.reshape_const(self.builder.aiOnnx, [x], [
self.config.batch_size, self.config.mask_tokens, self.config.vocab_length])
return x
def squad_projection(self, input_x):
weight = self.normal_init_tensor(self.config.dtype,
[self.config.hidden_size, 2],
0, 0.02,
"SquadW")
bias = self.constant_init_tensor(self.config.dtype, (2,), 0, "SquadB")
x = self.builder.aiOnnx.gemm([input_x, weight, bias])
# x.shape: [batch_size * sequence_length, 2]
start_logits = self.builder.aiOnnxOpset9.slice(
[x], axes=[1], starts=[0], ends=[1])
end_logits = self.builder.aiOnnxOpset9.slice(
[x], axes=[1], starts=[1], ends=[2])
start_logits = self.builder.reshape_const(
self.builder.aiOnnx,
[start_logits], [self.config.batch_size, self.config.sequence_length], debugPrefix="answer_start")
end_logits = self.builder.reshape_const(
self.builder.aiOnnx,
[end_logits], [self.config.batch_size, self.config.sequence_length], debugPrefix="answer_end")
return start_logits, end_logits
def pooler(self, pooler_input):
"""
Take the [CLS] token as a sentence embedding (assuming it's already been
fine-tuned), then run a FC layer with tanh activation
"""
pooler_input = self.builder.aiOnnxOpset9.slice(
[pooler_input], axes=[1], starts=[self.config.mask_tokens], ends=[self.config.mask_tokens + 1]
)
# This reshape is doing the job of a squeeze, but allows for in-place operation.
pooler_input = self.builder.reshape_const(self.builder.aiOnnx, [pooler_input], [
self.config.batch_size, self.config.hidden_size])
weight = self.normal_init_tensor(
self.config.dtype,
[self.config.hidden_size, self.config.hidden_size],
0,
0.02,
"PoolW",
)
bias = self.constant_init_tensor(
self.config.dtype, (self.config.hidden_size,), 0, "PoolB"
)
x = self.builder.aiOnnx.gemm([pooler_input, weight, bias])
return self.builder.aiOnnx.tanh([x])
def nsp_head(self, input_x):
x = self.builder.reshape_const(self.builder.aiOnnx, [input_x], [
self.config.batch_size, self.config.sequence_length, self.config.hidden_size])
x = self.pooler(x)
cls_weight = self.normal_init_tensor(
self.config.dtype, [self.config.hidden_size, 2], 0, 0.02, "NspW"
)
cls_bias = self.constant_init_tensor(
self.config.dtype, (2,), 0, "NspB")
x = self.builder.aiOnnx.gemm([x, cls_weight, cls_bias])
return x
def lm_prediction_head(self, input_x):
dense_weight = self.normal_init_tensor(self.config.dtype,
[self.config.hidden_size,
self.config.hidden_size],
0,
0.02,
"LMPredictionW")
dense_bias = self.constant_init_tensor(
self.config.dtype, (self.config.hidden_size,), 0, "LMPredictionB")
x = self.builder.aiOnnx.gemm([input_x, dense_weight, dense_bias])
x = self.intermediate_activation_function(x)
x = self.norm(x)
return x
def get_model(config, mode, block=None, initializers={}):
# Specifying ai.onnx opset9 for the slice syntax
builder = popart.Builder(opsets={
"ai.onnx": 9,
"ai.onnx.ml": 1,
"ai.graphcore": 1
})
if mode == ExecutionMode.PHASED:
scope_provider = ScopeProvider()
if not block:
from pingpong.bert_ping_pong import BertModel
return BertModel(config,
builder=builder,
initializers=initializers,
scope_provider=scope_provider)
if block.lower() == 'embedding':
from pingpong.bert_layers_serialised import BertEmbedding
return BertEmbedding(config.vocab_length,
config.hidden_size,
config.sequence_length,
config.max_positional_length,
config.embedding_serialization_vocab_steps,
config.layer_norm_eps,
not config.no_dropout,
config.dropout_prob,
mode,
config.dtype,
config.update_embedding_dict,
weight_transposed=False,
builder=builder,
scope_provider=scope_provider)
if block.lower() == 'attention':
from pingpong.bert_layers import Attention
attention_params = {
'input_size': config.hidden_size,
'hidden_size': config.hidden_size,
'num_heads': config.attention_heads,
'serialize_matmul': config.split_linear_layers,
'avail_mem_prop': config.available_memory_proportion,
'epsilon': config.layer_norm_eps,
'dropout': not config.no_dropout,
'dropout_prob': config.dropout_prob,
'attn_dropout': not config.no_attn_dropout,
'attn_dropout_prob': config.attn_dropout_prob,
'batch_size': config.batch_size,
'sequence_length': config.sequence_length,
'dtype': config.dtype,
'task': config.task,
'num_mask_tokens': config.mask_tokens
}
return Attention('Attention', **attention_params, builder=builder, scope_provider=scope_provider)
if block.lower() == 'feedforward':
from pingpong.bert_layers import FeedForward
return FeedForward('FF',
config.hidden_size,
config.ff_size,
dropout=not config.no_dropout,
dropout_prob=config.dropout_prob,
epsilon=config.layer_norm_eps,
intermediate_act_func=config.activation_type,
dtype=config.dtype,
alpha=config.relu_leak,
serialize_matmul=config.split_linear_layers,
builder=builder,
scope_provider=scope_provider)
else:
return Bert(config,
builder=builder,
initializers=initializers,
execution_mode=mode)