Skip to content

Commit 2fe2891

Browse files
committed
improve
1 parent 4b41bb5 commit 2fe2891

4 files changed

Lines changed: 20 additions & 20 deletions

File tree

internlm/model/modules/linear.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,12 @@ def __init__(
349349
else:
350350
super().__init__(in_features, out_features, bias=bias, device=device, dtype=dtype)
351351

352-
def forward(self, input: torch.Tensor, no_communication=False) -> torch.Tensor: # pylint: disable=W0622
352+
self.last_block_layer = False
353+
354+
def forward(self, input: torch.Tensor) -> torch.Tensor: # pylint: disable=W0622
353355
_class_name = self.__class__.__name__
354356
assert self._communicator is not None, f"{_class_name} should register with a communicator first."
357+
no_communication = bool(gpc.recompute_forward_no_comm and self.last_block_layer)
355358
return fused_dense_func(
356359
input,
357360
self.weight,
@@ -414,6 +417,7 @@ def __init__(
414417
multiple_of: int = 1,
415418
device: torch.device = None,
416419
dtype: torch.dtype = None,
420+
layer_name: str = "default",
417421
) -> None:
418422
if in_features % multiple_of:
419423
raise ValueError(f"in_features ({in_features}) must be a multiple of {multiple_of}")
@@ -430,6 +434,9 @@ def __init__(
430434
split_mode="row",
431435
)
432436

437+
if layer_name == "w2":
438+
self.last_block_layer = True
439+
433440

434441
class ScaleColumnParallelLinear(ParallelLinearWithCommExt):
435442
"""
@@ -471,7 +478,7 @@ def __init__(
471478
self.first_eval_flag = True
472479
self.tmp_weight = None
473480

474-
def forward(self, input, no_communication=False): # pylint: disable=W0622
481+
def forward(self, input): # pylint: disable=W0622
475482
_class_name = self.__class__.__name__
476483
assert self._communicator is not None, f"{_class_name} should register with a communicator first."
477484

@@ -502,7 +509,6 @@ def forward(self, input, no_communication=False): # pylint: disable=W0622
502509
communicator=self._communicator,
503510
module=self,
504511
bias=self.bias,
505-
no_communication=no_communication,
506512
)
507513

508514

@@ -603,6 +609,7 @@ def new_linear(
603609
multiple_of,
604610
device,
605611
dtype,
612+
layer_name=name,
606613
)
607614
else:
608615
err_msg = (

internlm/model/modules/mlp.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import torch
77
from torch import nn
88

9-
from internlm.core.context.parallel_context import global_context as gpc
109
from internlm.model.modules.linear import new_linear
1110
from internlm.model.modules.utils import Silu
1211
from internlm.utils.logger import get_logger
@@ -99,7 +98,7 @@ def forward(self, x):
9998
else:
10099
fussed_out = self.fused_w1_w3(x)
101100
w1_o, w3_o = torch.split(fussed_out, fussed_out.shape[-1] // 2, dim=-1)
102-
out = self.w2(Silu(w1_o, w3_o), no_communication=gpc.recompute_forward_no_comm)
101+
out = self.w2(Silu(w1_o, w3_o))
103102
return out
104103

105104

internlm/model/utils.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from internlm.core.context import ParallelMode
66
from internlm.core.context.parallel_context import global_context as gpc
7-
from internlm.core.parallel.comm.tensor import _GATHER_DIM
87
from internlm.model.modules.mha import MHA
98

109

@@ -60,21 +59,16 @@ def convert_attn_args_to_kwargs(args, kwargs) -> Dict[str, Any]:
6059

6160
def padding_residual(residual):
6261
requires_grad = residual.requires_grad
63-
pad_before = gpc.get_local_rank(ParallelMode.TENSOR) * residual.shape[_GATHER_DIM]
64-
pad_after = (
65-
gpc.get_world_size(ParallelMode.TENSOR) - gpc.get_local_rank(ParallelMode.TENSOR) - 1
66-
) * residual.shape[_GATHER_DIM]
67-
68-
pad_before_tensor = torch.zeros(
69-
(*residual.shape[:_GATHER_DIM], pad_before, *residual.shape[_GATHER_DIM + 1 :]),
70-
dtype=residual.dtype,
71-
device=residual.device,
72-
)
73-
pad_after_tensor = torch.zeros(
74-
(*residual.shape[:_GATHER_DIM], pad_after, *residual.shape[_GATHER_DIM + 1 :]),
62+
_GATHER_DIM = 1
63+
total_size = gpc.get_world_size(ParallelMode.TENSOR) * residual.shape[_GATHER_DIM]
64+
zero_padding_tensor = torch.zeros(
65+
(*residual.shape[:_GATHER_DIM], total_size, *residual.shape[_GATHER_DIM + 1 :]),
7566
dtype=residual.dtype,
7667
device=residual.device,
7768
)
78-
residual = torch.cat([pad_before_tensor, residual, pad_after_tensor], dim=1).requires_grad_(requires_grad)
69+
start_idx = gpc.get_local_rank(ParallelMode.TENSOR) * residual.shape[_GATHER_DIM]
70+
end_idx = start_idx + residual.shape[_GATHER_DIM]
71+
zero_padding_tensor[:, start_idx:end_idx, :] = residual
72+
residual = zero_padding_tensor.requires_grad_(requires_grad)
7973

8074
return residual

internlm/solver/activation_checkpoint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def backward(ctx, *args):
151151
for i, idx in enumerate(tensor_indices):
152152
inputs[idx] = tensors[i]
153153

154-
# no_communication
154+
# when checkpoint_tp_no_comm==True, we use TP recomputation communication optimization
155155
no_communication = getattr(gpc.config.model, "checkpoint_tp_no_comm", False)
156156

157157
detached_inputs = detach_variable(tuple(inputs))

0 commit comments

Comments
 (0)