MCPcopy Create free account
hub / github.com/AIS-SNU/Smart-Infinity / _replace

Function _replace

deepspeed/module_inject/replace_module.py:371–409  ·  view source on GitHub ↗
(child, name, conv_linear_layer)

Source from the content-addressed store, hash-verified

369 mp_group = config.tensor_parallel.tp_group
370
371 def _replace(child, name, conv_linear_layer):
372 if getattr(child, "replaced", False) == True:
373 return
374 mp_replace = ReplaceWithTensorSlicing(mp_group=mp_group)
375 weight_shape = child.weight.shape
376 if name in all_reduce_linears:
377 new_weight = torch.empty((
378 weight_shape[1] if conv_linear_layer else weight_shape[0],
379 (weight_shape[0] if conv_linear_layer else weight_shape[1]) // mp_size,
380 ),
381 device=child.weight.device,
382 dtype=child.weight.dtype)
383 if conv_linear_layer:
384 child.weight.data = child.weight.data.transpose(-1, -2).contiguous()
385 data = mp_replace.copy(new_weight, child.weight.data)
386 new_bias = torch.empty((weight_shape[0]), device=child.weight.device, dtype=child.weight.dtype)
387 if child.bias is not None:
388 new_bias.data.copy_(child.bias.data)
389 setattr(child, "replaced", True)
390 return LinearAllreduce(data, child.bias if child.bias is None else \
391 torch.nn.parameter.Parameter(new_bias.to(get_accelerator().current_device_name())), mp_group)
392 else:
393 new_weight = torch.empty((
394 (weight_shape[1] if conv_linear_layer else weight_shape[0]) // mp_size,
395 weight_shape[0] // mp_size if conv_linear_layer else weight_shape[1],
396 ),
397 device=child.weight.device,
398 dtype=child.weight.dtype)
399 if conv_linear_layer:
400 child.weight.data = child.weight.data.transpose(-1, -2).contiguous()
401 data = mp_replace.copy(new_weight, child.weight.data)
402
403 new_bias = torch.empty((weight_shape[0] // mp_size),
404 device=child.weight.device,
405 dtype=child.weight.dtype)
406 bias_data = None if child.bias is None else mp_replace.copy(new_bias, child.bias.data).to(
407 get_accelerator().current_device_name())
408 setattr(child, "replaced", True)
409 return LinearLayer(weight=data.to(get_accelerator().current_device_name()), bias=bias_data)
410
411 def _slice_embedding(child, name, conv_linear_layer):
412 if getattr(child, "replaced", False) == True:

Callers

nothing calls this directly

Calls 7

copyMethod · 0.95
get_acceleratorFunction · 0.90
LinearAllreduceClass · 0.85
LinearLayerClass · 0.85
transposeMethod · 0.45
current_device_nameMethod · 0.45

Tested by

no test coverage detected