MCPcopy Create free account
hub / github.com/NVIDIA/FasterTransformer / bert_example

Function bert_example

examples/pytorch/bert/bert_example.py:90–316  ·  view source on GitHub ↗
(args)

Source from the content-addressed store, hash-verified

88
89
90def bert_example(args):
91 torch.manual_seed(0)
92 random.seed(0)
93 np.random.seed(0)
94
95 if dist.is_mpi_available():
96 try:
97 dist.init_process_group(backend='mpi')
98 rank = dist.get_rank()
99 world_size = dist.get_world_size()
100 except:
101 rank = dist.get_rank()
102 world_size = dist.get_world_size()
103 else:
104 rank = 0
105
106 batch_size = args['batch_size']
107 seq_len = args['seq_len']
108 if args['weight_path'] is not None:
109 if 'large' in args['weight_path']:
110 layer_num = 24
111 head_num = 16
112 head_size = 64
113 elif 'base' in args['weight_path']:
114 layer_num = 12
115 head_num = 12
116 head_size = 64
117 else:
118 layer_num = args['layer_num']
119 head_num = args['head_num']
120 head_size = args['head_size']
121 else:
122 layer_num = args['layer_num']
123 head_num = args['head_num']
124 head_size = args['head_size']
125 hidden_dim = head_num * head_size
126
127 if args['int8_mode'] == 1:
128 per_channel = True
129 elif args['int8_mode'] == 2 or args['int8_mode'] == 3:
130 per_channel = False
131 elif args['int8_mode'] != 0:
132 raise ValueError("wrong int8_mode argument")
133
134 if rank == 0:
135 print("\n=============== Argument ===============")
136 for key in args:
137 print("{}: {}".format(key, args[key]))
138 print("========================================\n")
139
140 inp = torch.empty(batch_size, seq_len, hidden_dim).cuda()
141 torch.nn.init.normal_(inp, -0.02, 0.02)
142 if args['avg_seq_len'] > 0:
143 mem_seq_lens = torch.ones((batch_size,)) * args['avg_seq_len']
144 mem_seq_lens = mem_seq_lens.to(torch.int32).cuda()
145 elif args['avg_seq_len'] == -1:
146 mem_seq_lens = torch.randint(1, seq_len + 1, (batch_size,), dtype=torch.int32).cuda()
147 else:

Callers 15

test_batch_fp32Method · 0.90
test_batch_fp16Method · 0.90
test_hidden_fp32Method · 0.90
test_hidden_fp16Method · 0.90
test_seqlen_fp32Method · 0.90
test_seqlen_fp16Method · 0.90
test_batch_fp32Method · 0.90
test_batch_fp16Method · 0.90
test_batch_bf16Method · 0.90
test_size_fp32Method · 0.90
test_size_fp16Method · 0.90
test_size_bf16Method · 0.90

Calls 15

to_int8Method · 0.95
to_halfMethod · 0.95
to_bfloat16Method · 0.95
to_cudaMethod · 0.95
EncoderWeightsClass · 0.90
HuggingFaceEncoderClass · 0.90
CustomEncoderClass · 0.90
sequence_maskFunction · 0.85
maxMethod · 0.80
minMethod · 0.80
cudaMethod · 0.45
toMethod · 0.45

Tested by 15

test_batch_fp32Method · 0.72
test_batch_fp16Method · 0.72
test_hidden_fp32Method · 0.72
test_hidden_fp16Method · 0.72
test_seqlen_fp32Method · 0.72
test_seqlen_fp16Method · 0.72
test_batch_fp32Method · 0.72
test_batch_fp16Method · 0.72
test_batch_bf16Method · 0.72
test_size_fp32Method · 0.72
test_size_fp16Method · 0.72
test_size_bf16Method · 0.72