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

Class CustomEncoder

examples/pytorch/bert/utils/encoder.py:270–324  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

268
269
270class CustomEncoder(torch.nn.Module):
271 def __init__(self, layer_num, head_num, head_size, weights,
272 int8_mode=0, remove_padding=False, sparse=False,
273 path='./lib/libth_transformer.so', tensor_para_size=1,
274 pipeline_para_size=1):
275 super().__init__()
276 self.layer_num = layer_num
277 self.remove_padding = remove_padding
278 self.int8_mode = int8_mode
279 torch.classes.load_library(path)
280
281 weights_ = weights.listed_weights()
282
283 self.use_mpi = dist.is_mpi_available()
284
285 if self.use_mpi:
286 try:
287 dist.init_process_group(backend='mpi')
288 except:
289 print("[INFO] WARNING: Exception occurred in dist.init_process_group(backend='mpi'). Maybe the process group has been initialized somewhere else.")
290 else:
291 print("[INFO] MPI is not available in this PyTorch build.")
292 assert tensor_para_size == 1, "[FATAL] MPI is required for tensor_para_size > 1."
293 assert pipeline_para_size == 1, "[FATAL] MPI is required for pipeline_para_size > 1."
294
295 if int8_mode == 0:
296 assert len(weights_) == 16
297 try:
298 self.encoders = torch.classes.FasterTransformer.Bert(
299 *weights_,
300 head_num, head_size, 4 * head_num * head_size, remove_padding, layer_num, sparse, 1.0,
301 tensor_para_size, pipeline_para_size)
302 except:
303 # legacy ths for 20.03 image
304 self.encoders = torch.classes.FasterTransformerBert(
305 *weights_,
306 head_num, head_size, 4 * head_num * head_size, remove_padding, layer_num, sparse, 1.0,
307 tensor_para_size, pipeline_para_size)
308 else:
309 assert len(weights_) == 18
310 assert tensor_para_size == 1, "INT8 BERT still only support tensor_para_size = 1"
311 assert pipeline_para_size == 1, "INT8 BERT still only support pipeline_para_size = 1"
312 try:
313 self.encoders = torch.classes.FasterTransformer.INT8Bert(
314 *weights_,
315 head_num, head_size, remove_padding, layer_num, int8_mode, sparse, 1.0)
316 except:
317 # legacy ths for 20.03 image
318 self.encoders = torch.classes.FasterTransformerINT8Bert(
319 *weights_,
320 head_num, head_size, remove_padding, layer_num, int8_mode, sparse, 1.0)
321
322 def forward(self, hidden_states, attention_mask, sequence_lengths):
323 hidden_states = self.encoders.forward(hidden_states, sequence_lengths)
324 return (hidden_states,)
325
326
327class HuggingFaceEncoder(torch.nn.Module):

Callers 3

mainFunction · 0.90
mainFunction · 0.90
bert_exampleFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected