mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-17 15:11:20 +00:00
Layer integration (#83)
* integrated parallel layers for ease of building models * integrated 2.5d layers * cleaned codes and unit tests * added log metric by step hook; updated imagenet benchmark; fixed some bugs * reworked initialization; cleaned codes Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
This commit is contained in:
@@ -4,7 +4,7 @@ from torch.nn import Parameter
|
||||
import time
|
||||
from colossalai.context.parallel_mode import ParallelMode
|
||||
from colossalai.core import global_context as gpc
|
||||
from colossalai.nn import Linear1D_Col, Linear1D_Row, TransformerMLP1D, TransformerSelfAttention1D, ViTMLP1D, ViTSelfAttention1D, ViTPatchEmbedding1D, ViTHead1D, ViTTokenFuser1D
|
||||
from colossalai.nn import Linear1D_Col, Linear1D_Row
|
||||
from colossalai.utils import get_current_device, print_rank_0
|
||||
from .common import HIDDEN_SIZE, DEPTH, BATCH_SIZE, SEQ_LENGTH, NUM_CLASSES, check_equal, IMG_SIZE
|
||||
|
||||
@@ -17,7 +17,7 @@ def check_linear_col():
|
||||
|
||||
i = gpc.get_local_rank(ParallelMode.PARALLEL_1D)
|
||||
|
||||
layer = Linear1D_Col(INPUT_SIZE, OUTPUT_SIZE, gather_output=True)
|
||||
layer = Linear1D_Col(INPUT_SIZE, OUTPUT_SIZE)
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
@@ -50,18 +50,20 @@ def check_linear_col():
|
||||
B_master = B_master.clone()
|
||||
B_master.requires_grad = True
|
||||
C_master = torch.matmul(A_master, W_master.transpose(0, 1)) + B_master
|
||||
C = C_master.clone()
|
||||
C = torch.chunk(C_master, DEPTH, dim=-1)[i]
|
||||
|
||||
check_equal(out, C)
|
||||
print_rank_0('linear_col gather_output forward: pass')
|
||||
print_rank_0('linear_col forward: pass')
|
||||
|
||||
grad_shape = C_master.shape
|
||||
grad_master = torch.randn(grad_shape, dtype=dtype, device=get_current_device())
|
||||
dist.broadcast(grad_master, src=0)
|
||||
grad = grad_master.detach()
|
||||
grad = torch.chunk(grad_master, DEPTH, dim=-1)[i]
|
||||
grad = grad.clone()
|
||||
out.backward(grad)
|
||||
|
||||
C_master.backward(grad)
|
||||
grad_master = grad_master.clone()
|
||||
C_master.backward(grad_master)
|
||||
A_grad = A_master.grad
|
||||
check_equal(A_grad, A.grad)
|
||||
|
||||
@@ -73,7 +75,7 @@ def check_linear_col():
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=0)[i]
|
||||
check_equal(B_grad, layer.bias.grad)
|
||||
|
||||
print_rank_0('linear_col gather_output backward: pass')
|
||||
print_rank_0('linear_col backward: pass')
|
||||
|
||||
|
||||
def check_linear_row():
|
||||
@@ -84,12 +86,13 @@ def check_linear_row():
|
||||
|
||||
i = gpc.get_local_rank(ParallelMode.PARALLEL_1D)
|
||||
|
||||
layer = Linear1D_Row(OUTPUT_SIZE, INPUT_SIZE, parallel_input=False)
|
||||
layer = Linear1D_Row(OUTPUT_SIZE, INPUT_SIZE)
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, OUTPUT_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
dist.broadcast(A_master, src=0)
|
||||
A = A_master.clone()
|
||||
A = torch.chunk(A_master, DEPTH, dim=-1)[i]
|
||||
A = A.clone()
|
||||
A.requires_grad = True
|
||||
|
||||
W_shape = (INPUT_SIZE, OUTPUT_SIZE)
|
||||
@@ -119,16 +122,18 @@ def check_linear_row():
|
||||
C = C_master.clone()
|
||||
|
||||
check_equal(out, C)
|
||||
print_rank_0('linear_row no parallel_input forward: pass')
|
||||
print_rank_0('linear_row forward: pass')
|
||||
|
||||
grad_shape = C_master.shape
|
||||
grad_master = torch.randn(grad_shape, dtype=dtype, device=get_current_device())
|
||||
dist.broadcast(grad_master, src=0)
|
||||
grad = grad_master.detach()
|
||||
grad = grad_master.clone()
|
||||
out.backward(grad)
|
||||
|
||||
C_master.backward(grad)
|
||||
grad_master = grad_master.clone()
|
||||
C_master.backward(grad_master)
|
||||
A_grad = A_master.grad
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=-1)[i]
|
||||
check_equal(A_grad, A.grad)
|
||||
|
||||
W_grad = W_master.grad
|
||||
@@ -138,276 +143,4 @@ def check_linear_row():
|
||||
B_grad = B_master.grad
|
||||
check_equal(B_grad, layer.bias.grad)
|
||||
|
||||
print_rank_0('linear_row no parallel_input backward: pass')
|
||||
|
||||
|
||||
class Testvithead(torch.nn.Module):
|
||||
def __init__(self, in_features, out_features, bias=True):
|
||||
super().__init__()
|
||||
self.linear = torch.nn.Linear(in_features, out_features, bias=bias)
|
||||
|
||||
def forward(self, x):
|
||||
x = x[:, 0]
|
||||
x = self.linear(x)
|
||||
return x
|
||||
|
||||
|
||||
def check_head():
|
||||
device = get_current_device()
|
||||
dtype = torch.float32
|
||||
INPUT_SIZE = HIDDEN_SIZE
|
||||
|
||||
i = gpc.get_local_rank(ParallelMode.PARALLEL_1D)
|
||||
|
||||
head = ViTHead1D(INPUT_SIZE, NUM_CLASSES, dtype=dtype)
|
||||
torch.nn.init.zeros_(head.linear.bias)
|
||||
torch.nn.init.ones_(head.linear.weight)
|
||||
head = head.to(device)
|
||||
|
||||
layer = Testvithead(INPUT_SIZE, NUM_CLASSES, bias=True)
|
||||
torch.nn.init.zeros_(layer.linear.bias)
|
||||
torch.nn.init.ones_(layer.linear.weight)
|
||||
layer = layer.to(device)
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = A_master.clone()
|
||||
A.requires_grad = True
|
||||
|
||||
fwd_start = time.time()
|
||||
out = head(A)
|
||||
fwd_end = time.time()
|
||||
print_rank_0(
|
||||
'head forward: pass | {0} --> {1} | {2:.3f} s'.format(
|
||||
tuple(A.shape), tuple(out.shape), fwd_end - fwd_start))
|
||||
A_master = A_master.clone()
|
||||
A_master.requires_grad = True
|
||||
C_master = layer(A_master)
|
||||
# C = torch.chunk(C_master, DEPTH, dim=0)[i]
|
||||
print_rank_0('Rank {} head forward: {}'.format(i, check_equal(out, C_master)))
|
||||
|
||||
grad_shape = C_master.shape
|
||||
grad_master = torch.randn(grad_shape,
|
||||
dtype=dtype,
|
||||
device=get_current_device())
|
||||
torch.distributed.broadcast(grad_master, src=0)
|
||||
# grad = torch.chunk(grad_master, DEPTH, dim=0)[i]
|
||||
|
||||
# bwd_start = time.time()
|
||||
out.backward(grad_master)
|
||||
# bwd_end = time.time()
|
||||
# print_rank_0('head backward: pass | {:.3f} s'.format(bwd_end - bwd_start),
|
||||
# logger)
|
||||
|
||||
C_master.backward(grad_master)
|
||||
A_grad = A_master.grad
|
||||
# if j == 0:
|
||||
print_rank_0('Rank {} head backward (input_grad): {}'.format(
|
||||
i, check_equal(A_grad, A.grad)))
|
||||
|
||||
|
||||
class Testvitembed(torch.nn.Module):
|
||||
def __init__(self, img_size: int, patch_size: int, in_chans: int,
|
||||
embed_size: int, drop_prob: float) -> None:
|
||||
super().__init__()
|
||||
self.proj = torch.nn.Conv2d(in_chans,
|
||||
embed_size,
|
||||
kernel_size=patch_size,
|
||||
stride=patch_size)
|
||||
num_patches = (img_size // patch_size)**2
|
||||
self.cls_token = torch.nn.Parameter(torch.zeros(1, 1, embed_size))
|
||||
self.pos_embed = torch.nn.Parameter(
|
||||
torch.zeros(1, num_patches + 1, embed_size))
|
||||
self.pos_drop = torch.nn.Dropout(drop_prob)
|
||||
|
||||
def forward(self, x):
|
||||
x = self.proj(x)
|
||||
x = x.flatten(2).transpose(1, 2)
|
||||
cls_token = self.cls_token.expand(x.shape[0], -1, -1)
|
||||
x = torch.cat((cls_token, x), dim=1)
|
||||
x = self.pos_drop(x + self.pos_embed)
|
||||
return x
|
||||
|
||||
|
||||
def check_embed():
|
||||
device = get_current_device()
|
||||
dtype = torch.float32
|
||||
i = gpc.get_local_rank(ParallelMode.PARALLEL_1D)
|
||||
|
||||
layer = ViTPatchEmbedding1D(IMG_SIZE, 4, HIDDEN_SIZE)
|
||||
layer2 = ViTTokenFuser1D(IMG_SIZE, 4, HIDDEN_SIZE)
|
||||
torch.nn.init.zeros_(layer.proj.bias)
|
||||
torch.nn.init.ones_(layer.proj.weight)
|
||||
torch.nn.init.ones_(layer2.cls_token)
|
||||
torch.nn.init.ones_(layer2.pos_embed)
|
||||
layer = layer.to(device)
|
||||
layer2 = layer2.to(device)
|
||||
|
||||
layer_master = Testvitembed(IMG_SIZE, 4, 3, HIDDEN_SIZE, 0.)
|
||||
torch.nn.init.zeros_(layer_master.proj.bias)
|
||||
torch.nn.init.ones_(layer_master.proj.weight)
|
||||
torch.nn.init.ones_(layer_master.cls_token)
|
||||
torch.nn.init.ones_(layer_master.pos_embed)
|
||||
layer_master = layer_master.to(device)
|
||||
|
||||
A_shape = (BATCH_SIZE, 3, IMG_SIZE, IMG_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = A_master.clone()
|
||||
A.requires_grad = True
|
||||
|
||||
fwd_start = time.time()
|
||||
out = layer2(layer(A))
|
||||
fwd_end = time.time()
|
||||
print_rank_0(
|
||||
'embedding forward: pass | {0} --> {1} | {2:.3f} s'.format(
|
||||
tuple(A.shape), tuple(out.shape), fwd_end - fwd_start))
|
||||
# out_cls = out[:, 0]
|
||||
# out_tensor = out[:, 1:]
|
||||
|
||||
A_master = A_master.clone()
|
||||
A_master.requires_grad = True
|
||||
C_master = layer_master(A_master)
|
||||
# if j == 0:
|
||||
# C_cls = C_master[:, 0]
|
||||
# C_cls = torch.chunk(C_cls, DEPTH, dim=0)[i]
|
||||
# C_cls = torch.chunk(C_cls, DEPTH, dim=-1)[k]
|
||||
# logger.info('Rank {} embed forward (cls): {}'.format(
|
||||
# rank, check_equal(out_cls, C_cls)))
|
||||
# C = C_master[:, 1:]
|
||||
print_rank_0('Rank {} embed forward: {}'.format(i, check_equal(out, C_master)))
|
||||
|
||||
grad_shape = C_master.shape
|
||||
grad_master = torch.randn(grad_shape,
|
||||
dtype=dtype,
|
||||
device=get_current_device())
|
||||
torch.distributed.broadcast(grad_master, src=0)
|
||||
# cls_grad = grad_master[:, 0]
|
||||
# cls_grad = torch.chunk(cls_grad, DEPTH, dim=0)[i]
|
||||
# cls_grad = torch.chunk(cls_grad, DEPTH, dim=-1)[k]
|
||||
# grad = grad_master[:, 1:]
|
||||
# grad = torch.cat((torch.unsqueeze(cls_grad, 1), grad), dim=1)
|
||||
bwd_start = time.time()
|
||||
out.backward(grad_master)
|
||||
bwd_end = time.time()
|
||||
print_rank_0(
|
||||
'embedding backward: pass | {:.3f} s'.format(bwd_end - bwd_start))
|
||||
|
||||
C_master.backward(grad_master)
|
||||
|
||||
A_grad = A_master.grad
|
||||
print_rank_0('Rank {} embed backward (input_grad): {}'.format(i, check_equal(A_grad, A.grad)))
|
||||
|
||||
print_rank_0('Rank {} embed backward (cls_grad): {}'.format(
|
||||
i, check_equal(layer_master.cls_token.grad, layer2.cls_token.grad)))
|
||||
|
||||
print_rank_0('Rank {} embed backward (pos_embed_grad): {}'.format(
|
||||
i, check_equal(layer_master.pos_embed.grad, layer2.pos_embed.grad)))
|
||||
|
||||
print_rank_0('Rank {} embed backward (proj_weight_grad): {}'.format(
|
||||
i, check_equal(layer_master.proj.weight.grad, layer.proj.weight.grad)))
|
||||
|
||||
print_rank_0('Rank {} embed backward (proj_bias_grad): {}'.format(
|
||||
i, check_equal(layer_master.proj.bias.grad, layer.proj.bias.grad)))
|
||||
|
||||
return fwd_end - fwd_start, bwd_end - bwd_start
|
||||
|
||||
|
||||
def check_attention():
|
||||
device = get_current_device()
|
||||
dtype = torch.float32
|
||||
INPUT_SIZE = HIDDEN_SIZE
|
||||
NUM_ATTENTION_HEADS = 2
|
||||
|
||||
i = gpc.get_local_rank(ParallelMode.PARALLEL_1D)
|
||||
|
||||
layer = ViTSelfAttention1D(
|
||||
HIDDEN_SIZE,
|
||||
NUM_ATTENTION_HEADS,
|
||||
0.5,
|
||||
0.5
|
||||
).to(device=device)
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = A_master.clone()
|
||||
A.requires_grad = True
|
||||
|
||||
mask_shape = (BATCH_SIZE, NUM_ATTENTION_HEADS // DEPTH, SEQ_LENGTH, SEQ_LENGTH)
|
||||
attention_mask = torch.zeros(mask_shape, dtype=dtype, device=device)
|
||||
|
||||
out = layer(A)
|
||||
assert out.shape == (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
print_rank_0('self attention forward: pass')
|
||||
|
||||
grad_shape = out.shape
|
||||
grad = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
|
||||
out.backward(grad)
|
||||
assert A.grad.shape == A.shape
|
||||
print_rank_0('self attention backward: pass')
|
||||
|
||||
|
||||
def check_mlp():
|
||||
device = get_current_device()
|
||||
dtype = torch.float32
|
||||
INPUT_SIZE = HIDDEN_SIZE
|
||||
|
||||
i = gpc.get_local_rank(ParallelMode.PARALLEL_1D)
|
||||
|
||||
layer = ViTMLP1D(
|
||||
HIDDEN_SIZE,
|
||||
4.0
|
||||
).to(device=device)
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = A_master.clone()
|
||||
A.requires_grad = True
|
||||
|
||||
out = layer(A)
|
||||
assert out.shape == (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
print_rank_0('mlp forward: pass')
|
||||
|
||||
grad_shape = out.shape
|
||||
grad = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
|
||||
out.backward(grad)
|
||||
assert A.grad.shape == A.shape
|
||||
print_rank_0('mlp backward: pass')
|
||||
|
||||
|
||||
def check_patch_embedding():
|
||||
device = get_current_device()
|
||||
dtype = torch.float32
|
||||
INPUT_SIZE = 4
|
||||
PATCH_SIZE = 2
|
||||
|
||||
i = gpc.get_local_rank(ParallelMode.PARALLEL_1D)
|
||||
|
||||
layer = ViTPatchEmbedding1D(
|
||||
INPUT_SIZE,
|
||||
PATCH_SIZE,
|
||||
HIDDEN_SIZE,
|
||||
).to(device=device)
|
||||
|
||||
A_shape = (BATCH_SIZE, 3, INPUT_SIZE, INPUT_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = A_master.clone()
|
||||
A.requires_grad = True
|
||||
|
||||
out = layer(A)
|
||||
print('output size: ', out.size())
|
||||
assert out.shape == (BATCH_SIZE, 4, HIDDEN_SIZE)
|
||||
print_rank_0('patch embedding forward: pass')
|
||||
|
||||
grad_shape = out.shape
|
||||
grad = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
|
||||
out.backward(grad)
|
||||
assert A.grad.shape == A.shape
|
||||
print_rank_0('patch embedding backward: pass')
|
||||
print_rank_0('linear_row backward: pass')
|
||||
|
@@ -3,12 +3,12 @@
|
||||
|
||||
import torch
|
||||
|
||||
DEPTH = 2
|
||||
DEPTH = 4
|
||||
BATCH_SIZE = 8
|
||||
SEQ_LENGTH = 8
|
||||
IMG_SIZE = 16
|
||||
HIDDEN_SIZE = 8
|
||||
NUM_CLASSES = 10
|
||||
NUM_CLASSES = 8
|
||||
|
||||
def check_equal(A, B):
|
||||
assert torch.allclose(A, B, rtol=1e-5, atol=1e-2) == True
|
||||
assert torch.allclose(A, B, rtol=1e-3, atol=1e-1) == True
|
||||
|
@@ -6,7 +6,7 @@ import torch
|
||||
import torch.multiprocessing as mp
|
||||
|
||||
from colossalai.core import global_context as gpc
|
||||
from colossalai.initialize import launch, get_default_parser
|
||||
from colossalai.initialize import launch
|
||||
from functools import partial
|
||||
from checks_1d.check_layer_1d import *
|
||||
|
||||
@@ -14,7 +14,7 @@ CONFIG = dict(
|
||||
parallel=dict(
|
||||
pipeline=dict(size=1),
|
||||
tensor=dict(
|
||||
size=2,
|
||||
size=4,
|
||||
mode='1d'
|
||||
)
|
||||
),
|
||||
@@ -31,11 +31,6 @@ def check_layer(rank, world_size):
|
||||
|
||||
check_linear_col()
|
||||
check_linear_row()
|
||||
check_attention()
|
||||
check_mlp()
|
||||
check_patch_embedding()
|
||||
check_embed()
|
||||
check_head()
|
||||
|
||||
gpc.destroy()
|
||||
torch.cuda.empty_cache()
|
||||
@@ -43,7 +38,7 @@ def check_layer(rank, world_size):
|
||||
|
||||
@pytest.mark.dist
|
||||
def test_1d():
|
||||
world_size = 2
|
||||
world_size = 4
|
||||
run_func = partial(check_layer, world_size=world_size)
|
||||
mp.spawn(run_func, nprocs=world_size)
|
||||
|
||||
|
@@ -3,16 +3,16 @@ from torch.nn import Parameter
|
||||
|
||||
from colossalai.context.parallel_mode import ParallelMode
|
||||
from colossalai.core import global_context as gpc
|
||||
from colossalai.nn import Linear2D, LayerNorm2D, TransformerSelfAttention2D, TransformerMLP2D, TransformerLayer2D
|
||||
from colossalai.nn import Linear2D, LayerNorm2D, Classifier2D
|
||||
from colossalai.utils import get_current_device, print_rank_0
|
||||
from .common import HIDDEN_SIZE, DEPTH, BATCH_SIZE, SEQ_LENGTH, check_equal
|
||||
from .common import HIDDEN_SIZE, DEPTH, BATCH_SIZE, SEQ_LENGTH, check_equal, NUM_CLASSES
|
||||
|
||||
|
||||
def check_linear():
|
||||
device = get_current_device()
|
||||
dtype = torch.float32
|
||||
INPUT_SIZE = HIDDEN_SIZE
|
||||
OUTPUT_SIZE = 2 * HIDDEN_SIZE
|
||||
OUTPUT_SIZE = HIDDEN_SIZE
|
||||
|
||||
j = gpc.get_local_rank(ParallelMode.PARALLEL_2D_ROW)
|
||||
i = gpc.get_local_rank(ParallelMode.PARALLEL_2D_COL)
|
||||
@@ -38,12 +38,13 @@ def check_linear():
|
||||
B_shape = (OUTPUT_SIZE)
|
||||
B_master = torch.randn(B_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(B_master, src=0)
|
||||
B = torch.chunk(B_master, DEPTH, dim=0)[j]
|
||||
B = torch.chunk(B_master, DEPTH, dim=-1)[j]
|
||||
B = torch.chunk(B, DEPTH, dim=-1)[i]
|
||||
B = B.clone()
|
||||
B.requires_grad = True
|
||||
|
||||
layer.weight = Parameter(W)
|
||||
layer.bias = Parameter(B)
|
||||
layer.weight.data.copy_(W)
|
||||
layer.bias.data.copy_(B)
|
||||
out = layer(A)
|
||||
|
||||
A_master = A_master.clone()
|
||||
@@ -56,6 +57,7 @@ def check_linear():
|
||||
C = torch.chunk(C_master, DEPTH, dim=0)[i]
|
||||
C = torch.chunk(C, DEPTH, dim=-1)[j]
|
||||
|
||||
# print(f'Rank {gpc.get_global_rank()} A:\n{A}\nRank {gpc.get_global_rank()} W:\n{W}\nRank {gpc.get_global_rank()} b:\n{B}\nRank {gpc.get_global_rank()} C:\n{C}\nRank {gpc.get_global_rank()} out:\n{out}')
|
||||
check_equal(out, C)
|
||||
print_rank_0('linear forward: pass')
|
||||
|
||||
@@ -64,8 +66,10 @@ def check_linear():
|
||||
torch.distributed.broadcast(grad_master, src=0)
|
||||
grad = torch.chunk(grad_master, DEPTH, dim=0)[i]
|
||||
grad = torch.chunk(grad, DEPTH, dim=-1)[j]
|
||||
grad = grad.clone()
|
||||
out.backward(grad)
|
||||
|
||||
grad_master = grad_master.clone()
|
||||
C_master.backward(grad_master)
|
||||
A_grad = A_master.grad
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=0)[i]
|
||||
@@ -78,13 +82,92 @@ def check_linear():
|
||||
check_equal(W_grad, layer.weight.grad)
|
||||
|
||||
B_grad = B_master.grad
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=0)[j]
|
||||
if i == 0:
|
||||
check_equal(B_grad, layer.bias.grad)
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=-1)[j]
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=-1)[i]
|
||||
# if i == 0:
|
||||
check_equal(B_grad, layer.bias.grad)
|
||||
|
||||
print_rank_0('linear backward: pass')
|
||||
|
||||
|
||||
def check_classifier():
|
||||
device = get_current_device()
|
||||
dtype = torch.float32
|
||||
INPUT_SIZE = HIDDEN_SIZE
|
||||
OUTPUT_SIZE = NUM_CLASSES
|
||||
|
||||
j = gpc.get_local_rank(ParallelMode.PARALLEL_2D_ROW)
|
||||
i = gpc.get_local_rank(ParallelMode.PARALLEL_2D_COL)
|
||||
|
||||
layer = Classifier2D(INPUT_SIZE, OUTPUT_SIZE)
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
A_master = torch.randint(5, A_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = torch.chunk(A_master, DEPTH, dim=0)[i]
|
||||
A = torch.chunk(A, DEPTH, dim=-1)[j]
|
||||
A = A.clone()
|
||||
A.requires_grad = True
|
||||
|
||||
W_shape = (OUTPUT_SIZE, INPUT_SIZE)
|
||||
W_master = torch.randint(5, W_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(W_master, src=0)
|
||||
W = torch.chunk(W_master, DEPTH, dim=-1)[j]
|
||||
W = torch.chunk(W, DEPTH, dim=-1)[i]
|
||||
W = W.clone()
|
||||
layer.weight.data.copy_(W)
|
||||
# W.requires_grad = True
|
||||
|
||||
B_shape = (OUTPUT_SIZE,)
|
||||
B_master = torch.randint(5, B_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(B_master, src=0)
|
||||
# B = torch.chunk(B_master, DEPTH, dim=0)[j]
|
||||
B = B_master.clone()
|
||||
layer.bias.data.copy_(B)
|
||||
|
||||
out = layer(A)
|
||||
|
||||
A_master = A_master.clone()
|
||||
A_master.requires_grad = True
|
||||
W_master = W_master.clone()
|
||||
W_master.requires_grad = True
|
||||
B_master = B_master.clone()
|
||||
B_master.requires_grad = True
|
||||
C_master = torch.matmul(A_master, W_master.transpose(0, 1)) + B_master
|
||||
C = torch.chunk(C_master, DEPTH, dim=0)[i]
|
||||
# C = torch.chunk(C, DEPTH, dim=-1)[j]
|
||||
|
||||
check_equal(out, C)
|
||||
print_rank_0('classifier forward: pass')
|
||||
|
||||
grad_shape = C_master.shape
|
||||
grad_master = torch.randn(grad_shape, dtype=dtype, device=get_current_device())
|
||||
torch.distributed.broadcast(grad_master, src=0)
|
||||
grad = torch.chunk(grad_master, DEPTH, dim=0)[i]
|
||||
# grad = torch.chunk(grad, DEPTH, dim=-1)[j]
|
||||
grad = grad.clone()
|
||||
out.backward(grad)
|
||||
|
||||
grad_master = grad_master.clone()
|
||||
C_master.backward(grad_master)
|
||||
A_grad = A_master.grad
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=0)[i]
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=-1)[j]
|
||||
check_equal(A_grad, A.grad)
|
||||
|
||||
W_grad = W_master.grad
|
||||
W_grad = torch.chunk(W_grad, DEPTH, dim=-1)[j]
|
||||
W_grad = torch.chunk(W_grad, DEPTH, dim=-1)[i]
|
||||
check_equal(W_grad, layer.weight.grad)
|
||||
|
||||
B_grad = B_master.grad
|
||||
# B_grad = torch.chunk(B_grad, DEPTH, dim=0)[j]
|
||||
# if i == 0:
|
||||
check_equal(B_grad, layer.bias.grad)
|
||||
|
||||
print_rank_0('classifier backward: pass')
|
||||
|
||||
|
||||
def check_layernorm():
|
||||
device = get_current_device()
|
||||
dtype = torch.float32
|
||||
@@ -136,113 +219,112 @@ def check_layernorm():
|
||||
print_rank_0('layer norm backward: pass')
|
||||
|
||||
|
||||
def check_attention():
|
||||
device = get_current_device()
|
||||
dtype = torch.float32
|
||||
INPUT_SIZE = HIDDEN_SIZE
|
||||
NUM_ATTENTION_HEADS = 2
|
||||
# def check_attention():
|
||||
# device = get_current_device()
|
||||
# dtype = torch.float32
|
||||
# INPUT_SIZE = HIDDEN_SIZE
|
||||
# NUM_ATTENTION_HEADS = 2
|
||||
|
||||
j = gpc.get_local_rank(ParallelMode.PARALLEL_2D_ROW)
|
||||
i = gpc.get_local_rank(ParallelMode.PARALLEL_2D_COL)
|
||||
# j = gpc.get_local_rank(ParallelMode.PARALLEL_2D_ROW)
|
||||
# i = gpc.get_local_rank(ParallelMode.PARALLEL_2D_COL)
|
||||
|
||||
layer = TransformerSelfAttention2D(
|
||||
HIDDEN_SIZE,
|
||||
NUM_ATTENTION_HEADS,
|
||||
attention_dropout_prob=0.5,
|
||||
hidden_dropout_prob=0.5,
|
||||
)
|
||||
# layer = TransformerSelfAttention2D(
|
||||
# HIDDEN_SIZE,
|
||||
# NUM_ATTENTION_HEADS,
|
||||
# attention_dropout_prob=0.5,
|
||||
# hidden_dropout_prob=0.5,
|
||||
# )
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = torch.chunk(A_master, DEPTH, dim=0)[i]
|
||||
A = torch.chunk(A, DEPTH, dim=-1)[j]
|
||||
A = A.clone()
|
||||
A.requires_grad = True
|
||||
# A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
# A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
# torch.distributed.broadcast(A_master, src=0)
|
||||
# A = torch.chunk(A_master, DEPTH, dim=0)[i]
|
||||
# A = torch.chunk(A, DEPTH, dim=-1)[j]
|
||||
# A = A.clone()
|
||||
# A.requires_grad = True
|
||||
|
||||
mask_shape = (BATCH_SIZE // DEPTH, NUM_ATTENTION_HEADS // DEPTH, SEQ_LENGTH, SEQ_LENGTH)
|
||||
attention_mask = torch.zeros(mask_shape, dtype=dtype, device=device)
|
||||
# mask_shape = (BATCH_SIZE // DEPTH, NUM_ATTENTION_HEADS // DEPTH, SEQ_LENGTH, SEQ_LENGTH)
|
||||
# attention_mask = torch.zeros(mask_shape, dtype=dtype, device=device)
|
||||
|
||||
out = layer(A, attention_mask)
|
||||
assert out.shape == (BATCH_SIZE // DEPTH, SEQ_LENGTH, INPUT_SIZE // DEPTH)
|
||||
print_rank_0('self attention forward: pass')
|
||||
# out = layer(A, attention_mask)
|
||||
# assert out.shape == (BATCH_SIZE // DEPTH, SEQ_LENGTH, INPUT_SIZE // DEPTH)
|
||||
# print_rank_0('self attention forward: pass')
|
||||
|
||||
grad_shape = out.shape
|
||||
grad = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
# grad_shape = out.shape
|
||||
# grad = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
|
||||
out.backward(grad)
|
||||
assert A.grad.shape == A.shape
|
||||
print_rank_0('self attention backward: pass')
|
||||
# out.backward(grad)
|
||||
# assert A.grad.shape == A.shape
|
||||
# print_rank_0('self attention backward: pass')
|
||||
|
||||
|
||||
def check_mlp():
|
||||
device = get_current_device()
|
||||
dtype = torch.float32
|
||||
INPUT_SIZE = HIDDEN_SIZE
|
||||
# def check_mlp():
|
||||
# device = get_current_device()
|
||||
# dtype = torch.float32
|
||||
# INPUT_SIZE = HIDDEN_SIZE
|
||||
|
||||
j = gpc.get_local_rank(ParallelMode.PARALLEL_2D_ROW)
|
||||
i = gpc.get_local_rank(ParallelMode.PARALLEL_2D_COL)
|
||||
# j = gpc.get_local_rank(ParallelMode.PARALLEL_2D_ROW)
|
||||
# i = gpc.get_local_rank(ParallelMode.PARALLEL_2D_COL)
|
||||
|
||||
layer = TransformerMLP2D(
|
||||
HIDDEN_SIZE,
|
||||
dropout_prob=0.5,
|
||||
act_func='gelu',
|
||||
)
|
||||
# layer = TransformerMLP2D(
|
||||
# HIDDEN_SIZE,
|
||||
# dropout_prob=0.5,
|
||||
# act_func='gelu',
|
||||
# )
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = torch.chunk(A_master, DEPTH, dim=0)[i]
|
||||
A = torch.chunk(A, DEPTH, dim=-1)[j]
|
||||
A = A.clone()
|
||||
A.requires_grad = True
|
||||
# A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
# A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
# torch.distributed.broadcast(A_master, src=0)
|
||||
# A = torch.chunk(A_master, DEPTH, dim=0)[i]
|
||||
# A = torch.chunk(A, DEPTH, dim=-1)[j]
|
||||
# A = A.clone()
|
||||
# A.requires_grad = True
|
||||
|
||||
out = layer(A)
|
||||
assert out.shape == (BATCH_SIZE // DEPTH, SEQ_LENGTH, INPUT_SIZE // DEPTH)
|
||||
print_rank_0('mlp forward: pass')
|
||||
# out = layer(A)
|
||||
# assert out.shape == (BATCH_SIZE // DEPTH, SEQ_LENGTH, INPUT_SIZE // DEPTH)
|
||||
# print_rank_0('mlp forward: pass')
|
||||
|
||||
grad_shape = out.shape
|
||||
grad = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
# grad_shape = out.shape
|
||||
# grad = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
|
||||
out.backward(grad)
|
||||
assert A.grad.shape == A.shape
|
||||
print_rank_0('mlp backward: pass')
|
||||
# out.backward(grad)
|
||||
# assert A.grad.shape == A.shape
|
||||
# print_rank_0('mlp backward: pass')
|
||||
|
||||
|
||||
def check_transformerlayer():
|
||||
device = get_current_device()
|
||||
dtype = torch.float32
|
||||
INPUT_SIZE = HIDDEN_SIZE
|
||||
NUM_ATTENTION_HEADS = 2
|
||||
# def check_transformerlayer():
|
||||
# device = get_current_device()
|
||||
# dtype = torch.float32
|
||||
# INPUT_SIZE = HIDDEN_SIZE
|
||||
# NUM_ATTENTION_HEADS = 2
|
||||
|
||||
j = gpc.get_local_rank(ParallelMode.PARALLEL_2D_ROW)
|
||||
i = gpc.get_local_rank(ParallelMode.PARALLEL_2D_COL)
|
||||
# j = gpc.get_local_rank(ParallelMode.PARALLEL_2D_ROW)
|
||||
# i = gpc.get_local_rank(ParallelMode.PARALLEL_2D_COL)
|
||||
|
||||
layer = TransformerLayer2D(
|
||||
HIDDEN_SIZE,
|
||||
NUM_ATTENTION_HEADS,
|
||||
act_func='gelu',
|
||||
attention_dropout_prob=0.5,
|
||||
hidden_dropout_prob=0.5)
|
||||
# layer = TransformerLayer2D(HIDDEN_SIZE,
|
||||
# NUM_ATTENTION_HEADS,
|
||||
# act_func='gelu',
|
||||
# attention_dropout_prob=0.5,
|
||||
# hidden_dropout_prob=0.5)
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = torch.chunk(A_master, DEPTH, dim=0)[i]
|
||||
A = torch.chunk(A, DEPTH, dim=-1)[j]
|
||||
A = A.clone()
|
||||
A.requires_grad = True
|
||||
# A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
# A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
# torch.distributed.broadcast(A_master, src=0)
|
||||
# A = torch.chunk(A_master, DEPTH, dim=0)[i]
|
||||
# A = torch.chunk(A, DEPTH, dim=-1)[j]
|
||||
# A = A.clone()
|
||||
# A.requires_grad = True
|
||||
|
||||
mask_shape = (BATCH_SIZE // DEPTH, NUM_ATTENTION_HEADS // DEPTH, SEQ_LENGTH, SEQ_LENGTH)
|
||||
attention_mask = torch.zeros(mask_shape, dtype=dtype, device=device)
|
||||
# mask_shape = (BATCH_SIZE // DEPTH, NUM_ATTENTION_HEADS // DEPTH, SEQ_LENGTH, SEQ_LENGTH)
|
||||
# attention_mask = torch.zeros(mask_shape, dtype=dtype, device=device)
|
||||
|
||||
out = layer(A, attention_mask)
|
||||
assert out.shape == (BATCH_SIZE // DEPTH, SEQ_LENGTH, INPUT_SIZE // DEPTH)
|
||||
print_rank_0('transformerlayer forward: pass')
|
||||
# out = layer(A, attention_mask)
|
||||
# assert out.shape == (BATCH_SIZE // DEPTH, SEQ_LENGTH, INPUT_SIZE // DEPTH)
|
||||
# print_rank_0('transformerlayer forward: pass')
|
||||
|
||||
grad_shape = out.shape
|
||||
grad = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
# grad_shape = out.shape
|
||||
# grad = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
|
||||
out.backward(grad)
|
||||
assert A.grad.shape == A.shape
|
||||
print_rank_0('transformerlayer backward: pass')
|
||||
# out.backward(grad)
|
||||
# assert A.grad.shape == A.shape
|
||||
# print_rank_0('transformerlayer backward: pass')
|
||||
|
@@ -5,7 +5,7 @@ import torch
|
||||
|
||||
from colossalai.context.parallel_mode import ParallelMode
|
||||
from colossalai.core import global_context as gpc
|
||||
from colossalai.nn.layer.parallel_2d import Matmul_AB_2D, Matmul_ABT_2D, Matmul_ATB_2D
|
||||
from colossalai.nn.layer.parallel_2d._operation import Matmul_AB_2D, Matmul_ABT_2D, Matmul_ATB_2D
|
||||
from colossalai.utils import get_current_device
|
||||
from colossalai.utils import print_rank_0
|
||||
from .common import check_equal, BATCH_SIZE, SEQ_LENGTH, HIDDEN_SIZE, DEPTH
|
||||
|
@@ -7,7 +7,7 @@ DEPTH = 2
|
||||
BATCH_SIZE = 8
|
||||
SEQ_LENGTH = 8
|
||||
HIDDEN_SIZE = 8
|
||||
|
||||
NUM_CLASSES = 8
|
||||
|
||||
def check_equal(A, B):
|
||||
assert torch.allclose(A, B, rtol=1e-5, atol=1e-2) == True
|
||||
assert torch.allclose(A, B, rtol=1e-3, atol=1e-2) == True
|
||||
|
@@ -6,9 +6,9 @@ import torch
|
||||
import torch.multiprocessing as mp
|
||||
|
||||
from colossalai.core import global_context as gpc
|
||||
from colossalai.initialize import launch, get_default_parser
|
||||
from checks_2d.check_layer_2d import check_linear, check_layernorm, check_attention, check_mlp, check_transformerlayer
|
||||
from checks_2d.check_operation_2d import check_AB, check_ABT, check_ATB
|
||||
from colossalai.initialize import launch
|
||||
from checks_2d.check_layer_2d import *
|
||||
from checks_2d.check_operation_2d import *
|
||||
from functools import partial
|
||||
|
||||
|
||||
@@ -32,10 +32,7 @@ def check_operations():
|
||||
def check_layer():
|
||||
check_linear()
|
||||
check_layernorm()
|
||||
check_attention()
|
||||
check_mlp()
|
||||
check_transformerlayer()
|
||||
|
||||
check_classifier()
|
||||
|
||||
def check_layer_and_operation(rank, world_size):
|
||||
launch(config=CONFIG,
|
||||
@@ -45,7 +42,7 @@ def check_layer_and_operation(rank, world_size):
|
||||
port=29921,
|
||||
backend='nccl')
|
||||
|
||||
check_operations()
|
||||
# check_operations()
|
||||
check_layer()
|
||||
gpc.destroy()
|
||||
torch.cuda.empty_cache()
|
||||
|
@@ -1,9 +1,9 @@
|
||||
import torch
|
||||
from torch.nn import Parameter
|
||||
|
||||
from colossalai.context.parallel_mode import ParallelMode
|
||||
from colossalai.core import global_context as gpc
|
||||
from colossalai.nn import (Linear2p5D, LayerNorm2p5D, TransformerSelfAttention2p5D, TransformerMLP2p5D,
|
||||
TransformerLayer2p5D)
|
||||
from colossalai.nn import Linear2p5D, LayerNorm2p5D, Classifier2p5D
|
||||
from colossalai.utils import get_current_device
|
||||
from colossalai.utils import print_rank_0
|
||||
from .common import *
|
||||
@@ -71,8 +71,10 @@ def check_linear():
|
||||
torch.distributed.broadcast(grad_master, src=0)
|
||||
grad = torch.chunk(grad_master, TESSERACT_DIM, dim=0)[i]
|
||||
grad = torch.chunk(grad, TESSERACT_DIM, dim=-1)[j]
|
||||
grad = grad.clone()
|
||||
out.backward(grad)
|
||||
|
||||
grad_master = grad_master.clone()
|
||||
C_master.backward(grad_master)
|
||||
A_grad = A_master.grad
|
||||
A_grad = torch.chunk(A_grad, TESSERACT_DIM, dim=0)[i]
|
||||
@@ -92,6 +94,86 @@ def check_linear():
|
||||
print_rank_0('linear backward: pass')
|
||||
|
||||
|
||||
def check_classifier():
|
||||
device = get_current_device()
|
||||
dtype = torch.float32
|
||||
INPUT_SIZE = HIDDEN_SIZE
|
||||
OUTPUT_SIZE = NUM_CLASSES
|
||||
|
||||
j = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_ROW)
|
||||
i = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_COL)
|
||||
|
||||
layer = Classifier2p5D(INPUT_SIZE, OUTPUT_SIZE)
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
A_master = torch.randint(5, A_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = torch.chunk(A_master, TESSERACT_DIM, dim=0)[i]
|
||||
A = torch.chunk(A, TESSERACT_DIM, dim=-1)[j]
|
||||
A = A.clone()
|
||||
A.requires_grad = True
|
||||
|
||||
W_shape = (OUTPUT_SIZE, INPUT_SIZE)
|
||||
W_master = torch.randint(5, W_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(W_master, src=0)
|
||||
# W = torch.chunk(W_master, TESSERACT_DIM, dim=-1)[j]
|
||||
W = torch.chunk(W_master, TESSERACT_DIM, dim=-1)[j]
|
||||
W = torch.chunk(W, TESSERACT_DIM, dim=-1)[i]
|
||||
W = W.clone()
|
||||
layer.weight.data.copy_(W)
|
||||
# W.requires_grad = True
|
||||
|
||||
B_shape = (OUTPUT_SIZE,)
|
||||
B_master = torch.randint(5, B_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(B_master, src=0)
|
||||
# B = torch.chunk(B_master, TESSERACT_DIM, dim=0)[j]
|
||||
B = B_master.clone()
|
||||
layer.bias.data.copy_(B)
|
||||
|
||||
|
||||
out = layer(A)
|
||||
|
||||
A_master = A_master.clone()
|
||||
A_master.requires_grad = True
|
||||
W_master = W_master.clone()
|
||||
W_master.requires_grad = True
|
||||
B_master = B_master.clone()
|
||||
B_master.requires_grad = True
|
||||
C_master = torch.matmul(A_master, W_master.transpose(0, 1)) + B_master
|
||||
C = torch.chunk(C_master, TESSERACT_DIM, dim=0)[i]
|
||||
# C = torch.chunk(C, TESSERACT_DIM, dim=-1)[j]
|
||||
|
||||
check_equal(out, C)
|
||||
print_rank_0('classifier forward: pass')
|
||||
|
||||
grad_shape = C_master.shape
|
||||
grad_master = torch.randn(grad_shape, dtype=dtype, device=get_current_device())
|
||||
torch.distributed.broadcast(grad_master, src=0)
|
||||
grad = torch.chunk(grad_master, TESSERACT_DIM, dim=0)[i]
|
||||
# grad = torch.chunk(grad, TESSERACT_DIM, dim=-1)[j]
|
||||
grad = grad.clone()
|
||||
out.backward(grad)
|
||||
|
||||
grad_master = grad_master.clone()
|
||||
C_master.backward(grad_master)
|
||||
A_grad = A_master.grad
|
||||
A_grad = torch.chunk(A_grad, TESSERACT_DIM, dim=0)[i]
|
||||
A_grad = torch.chunk(A_grad, TESSERACT_DIM, dim=-1)[j]
|
||||
check_equal(A_grad, A.grad)
|
||||
|
||||
W_grad = W_master.grad
|
||||
W_grad = torch.chunk(W_grad, TESSERACT_DIM, dim=-1)[j]
|
||||
W_grad = torch.chunk(W_grad, TESSERACT_DIM, dim=-1)[i]
|
||||
check_equal(W_grad, layer.weight.grad)
|
||||
|
||||
B_grad = B_master.grad
|
||||
# B_grad = torch.chunk(B_grad, TESSERACT_DIM, dim=0)[j]
|
||||
# if i == 0:
|
||||
check_equal(B_grad, layer.bias.grad)
|
||||
|
||||
print_rank_0('classifier backward: pass')
|
||||
|
||||
|
||||
def check_layernorm():
|
||||
device = get_current_device()
|
||||
dtype = torch.float32
|
||||
@@ -146,120 +228,120 @@ def check_layernorm():
|
||||
print_rank_0('layer norm backward: pass')
|
||||
|
||||
|
||||
def check_attention():
|
||||
device = get_current_device()
|
||||
dtype = torch.float32
|
||||
INPUT_SIZE = HIDDEN_SIZE
|
||||
NUM_ATTENTION_HEADS = 2
|
||||
# def check_attention():
|
||||
# device = get_current_device()
|
||||
# dtype = torch.float32
|
||||
# INPUT_SIZE = HIDDEN_SIZE
|
||||
# NUM_ATTENTION_HEADS = 2
|
||||
|
||||
i = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_COL)
|
||||
j = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_ROW)
|
||||
k = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_DEP)
|
||||
# i = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_COL)
|
||||
# j = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_ROW)
|
||||
# k = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_DEP)
|
||||
|
||||
layer = TransformerSelfAttention2p5D(
|
||||
HIDDEN_SIZE, NUM_ATTENTION_HEADS,
|
||||
attention_dropout_prob=0.5,
|
||||
hidden_dropout_prob=0.5,
|
||||
dtype=dtype,
|
||||
)
|
||||
# layer = TransformerSelfAttention2p5D(
|
||||
# HIDDEN_SIZE, NUM_ATTENTION_HEADS,
|
||||
# attention_dropout_prob=0.5,
|
||||
# hidden_dropout_prob=0.5,
|
||||
# dtype=dtype,
|
||||
# )
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = torch.chunk(A_master, TESSERACT_DIM, dim=0)[i]
|
||||
A = torch.chunk(A, TESSERACT_DIM, dim=-1)[j]
|
||||
A = A.clone()
|
||||
A.requires_grad = True
|
||||
# A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
# A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
# torch.distributed.broadcast(A_master, src=0)
|
||||
# A = torch.chunk(A_master, TESSERACT_DIM, dim=0)[i]
|
||||
# A = torch.chunk(A, TESSERACT_DIM, dim=-1)[j]
|
||||
# A = A.clone()
|
||||
# A.requires_grad = True
|
||||
|
||||
mask_shape = (BATCH_SIZE // TESSERACT_DIM, NUM_ATTENTION_HEADS // TESSERACT_DIM, SEQ_LENGTH, SEQ_LENGTH)
|
||||
attention_mask = torch.zeros(mask_shape, dtype=dtype, device=device)
|
||||
# mask_shape = (BATCH_SIZE // TESSERACT_DIM, NUM_ATTENTION_HEADS // TESSERACT_DIM, SEQ_LENGTH, SEQ_LENGTH)
|
||||
# attention_mask = torch.zeros(mask_shape, dtype=dtype, device=device)
|
||||
|
||||
out = layer(A, attention_mask)
|
||||
assert out.shape == (BATCH_SIZE // TESSERACT_DIM, SEQ_LENGTH, INPUT_SIZE // TESSERACT_DIM)
|
||||
print_rank_0('self attention forward: pass')
|
||||
# out = layer(A, attention_mask)
|
||||
# assert out.shape == (BATCH_SIZE // TESSERACT_DIM, SEQ_LENGTH, INPUT_SIZE // TESSERACT_DIM)
|
||||
# print_rank_0('self attention forward: pass')
|
||||
|
||||
grad_shape = out.shape
|
||||
grad = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
# grad_shape = out.shape
|
||||
# grad = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
|
||||
out.backward(grad)
|
||||
assert A.grad.shape == A.shape
|
||||
print_rank_0('self attention backward: pass')
|
||||
# out.backward(grad)
|
||||
# assert A.grad.shape == A.shape
|
||||
# print_rank_0('self attention backward: pass')
|
||||
|
||||
|
||||
def check_mlp():
|
||||
device = get_current_device()
|
||||
dtype = torch.float32
|
||||
INPUT_SIZE = HIDDEN_SIZE
|
||||
# def check_mlp():
|
||||
# device = get_current_device()
|
||||
# dtype = torch.float32
|
||||
# INPUT_SIZE = HIDDEN_SIZE
|
||||
|
||||
i = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_COL)
|
||||
j = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_ROW)
|
||||
k = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_DEP)
|
||||
# i = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_COL)
|
||||
# j = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_ROW)
|
||||
# k = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_DEP)
|
||||
|
||||
layer = TransformerMLP2p5D(
|
||||
HIDDEN_SIZE,
|
||||
mlp_ratio=1,
|
||||
dropout_prob=0.5,
|
||||
act_func='gelu',
|
||||
dtype=dtype,
|
||||
)
|
||||
# layer = TransformerMLP2p5D(
|
||||
# HIDDEN_SIZE,
|
||||
# mlp_ratio=1,
|
||||
# dropout_prob=0.5,
|
||||
# act_func='gelu',
|
||||
# dtype=dtype,
|
||||
# )
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = torch.chunk(A_master, TESSERACT_DIM, dim=0)[i]
|
||||
A = torch.chunk(A, TESSERACT_DIM, dim=-1)[j]
|
||||
A = A.clone()
|
||||
A.requires_grad = True
|
||||
# A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
# A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
# torch.distributed.broadcast(A_master, src=0)
|
||||
# A = torch.chunk(A_master, TESSERACT_DIM, dim=0)[i]
|
||||
# A = torch.chunk(A, TESSERACT_DIM, dim=-1)[j]
|
||||
# A = A.clone()
|
||||
# A.requires_grad = True
|
||||
|
||||
out = layer(A)
|
||||
assert out.shape == (BATCH_SIZE // TESSERACT_DIM, SEQ_LENGTH, INPUT_SIZE // TESSERACT_DIM)
|
||||
print_rank_0('mlp forward: pass')
|
||||
# out = layer(A)
|
||||
# assert out.shape == (BATCH_SIZE // TESSERACT_DIM, SEQ_LENGTH, INPUT_SIZE // TESSERACT_DIM)
|
||||
# print_rank_0('mlp forward: pass')
|
||||
|
||||
grad_shape = out.shape
|
||||
grad = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
# grad_shape = out.shape
|
||||
# grad = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
|
||||
out.backward(grad)
|
||||
assert A.grad.shape == A.shape
|
||||
print_rank_0('mlp backward: pass')
|
||||
# out.backward(grad)
|
||||
# assert A.grad.shape == A.shape
|
||||
# print_rank_0('mlp backward: pass')
|
||||
|
||||
|
||||
def check_transformerlayer():
|
||||
device = get_current_device()
|
||||
dtype = torch.float32
|
||||
INPUT_SIZE = HIDDEN_SIZE
|
||||
NUM_ATTENTION_HEADS = 2
|
||||
# def check_transformerlayer():
|
||||
# device = get_current_device()
|
||||
# dtype = torch.float32
|
||||
# INPUT_SIZE = HIDDEN_SIZE
|
||||
# NUM_ATTENTION_HEADS = 2
|
||||
|
||||
i = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_COL)
|
||||
j = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_ROW)
|
||||
k = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_DEP)
|
||||
# i = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_COL)
|
||||
# j = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_ROW)
|
||||
# k = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_DEP)
|
||||
|
||||
layer = TransformerLayer2p5D(
|
||||
HIDDEN_SIZE,
|
||||
NUM_ATTENTION_HEADS,
|
||||
act_func='gelu',
|
||||
attention_dropout_prob=0.5,
|
||||
hidden_dropout_prob=0.5,
|
||||
dtype=dtype,
|
||||
)
|
||||
# layer = TransformerLayer2p5D(
|
||||
# HIDDEN_SIZE,
|
||||
# NUM_ATTENTION_HEADS,
|
||||
# act_func='gelu',
|
||||
# attention_dropout_prob=0.5,
|
||||
# hidden_dropout_prob=0.5,
|
||||
# dtype=dtype,
|
||||
# )
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = torch.chunk(A_master, TESSERACT_DIM, dim=0)[i]
|
||||
A = torch.chunk(A, TESSERACT_DIM, dim=-1)[j]
|
||||
A = A.clone()
|
||||
A.requires_grad = True
|
||||
# A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
# A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
# torch.distributed.broadcast(A_master, src=0)
|
||||
# A = torch.chunk(A_master, TESSERACT_DIM, dim=0)[i]
|
||||
# A = torch.chunk(A, TESSERACT_DIM, dim=-1)[j]
|
||||
# A = A.clone()
|
||||
# A.requires_grad = True
|
||||
|
||||
mask_shape = (BATCH_SIZE // TESSERACT_DIM, NUM_ATTENTION_HEADS // TESSERACT_DIM, SEQ_LENGTH, SEQ_LENGTH)
|
||||
attention_mask = torch.zeros(mask_shape, dtype=dtype, device=device)
|
||||
# mask_shape = (BATCH_SIZE // TESSERACT_DIM, NUM_ATTENTION_HEADS // TESSERACT_DIM, SEQ_LENGTH, SEQ_LENGTH)
|
||||
# attention_mask = torch.zeros(mask_shape, dtype=dtype, device=device)
|
||||
|
||||
out = layer(A, attention_mask)
|
||||
assert out.shape == (BATCH_SIZE // TESSERACT_DIM, SEQ_LENGTH, INPUT_SIZE // TESSERACT_DIM)
|
||||
print_rank_0('transformerlayer forward: pass')
|
||||
# out = layer(A, attention_mask)
|
||||
# assert out.shape == (BATCH_SIZE // TESSERACT_DIM, SEQ_LENGTH, INPUT_SIZE // TESSERACT_DIM)
|
||||
# print_rank_0('transformerlayer forward: pass')
|
||||
|
||||
grad_shape = out.shape
|
||||
grad = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
# grad_shape = out.shape
|
||||
# grad = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
|
||||
out.backward(grad)
|
||||
assert A.grad.shape == A.shape
|
||||
print_rank_0('transformerlayer backward: pass')
|
||||
# out.backward(grad)
|
||||
# assert A.grad.shape == A.shape
|
||||
# print_rank_0('transformerlayer backward: pass')
|
@@ -5,7 +5,8 @@ TESSERACT_DEP = 2
|
||||
BATCH_SIZE = 8
|
||||
SEQ_LENGTH = 8
|
||||
HIDDEN_SIZE = 8
|
||||
NUM_CLASSES = 3
|
||||
|
||||
|
||||
def check_equal(A, B):
|
||||
assert torch.allclose(A, B, rtol=1e-5, atol=1e-2) == True
|
||||
assert torch.allclose(A, B, rtol=1e-5, atol=1e-2) == True
|
@@ -4,7 +4,7 @@ import torch.multiprocessing as mp
|
||||
|
||||
from colossalai.core import global_context as gpc
|
||||
from colossalai.initialize import launch
|
||||
from checks_2p5d.check_layer_2p5d import check_linear, check_layernorm, check_attention, check_mlp, check_transformerlayer
|
||||
from checks_2p5d.check_layer_2p5d import check_linear, check_layernorm, check_classifier
|
||||
from checks_2p5d.check_operation_2p5d import check_AB, check_ABT, check_ATB
|
||||
from functools import partial
|
||||
|
||||
@@ -12,7 +12,7 @@ from functools import partial
|
||||
CONFIG = dict(
|
||||
parallel=dict(
|
||||
pipeline=dict(size=1),
|
||||
tensor=dict(size=8, mode='2.5d', depth=2),
|
||||
tensor=dict(size=4, mode='2.5d', depth=1),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -26,9 +26,7 @@ def check_operations():
|
||||
def check_layer():
|
||||
check_linear()
|
||||
check_layernorm()
|
||||
check_attention()
|
||||
check_mlp()
|
||||
check_transformerlayer()
|
||||
check_classifier()
|
||||
|
||||
|
||||
def check_layer_and_operation(rank, world_size):
|
||||
@@ -47,7 +45,7 @@ def check_layer_and_operation(rank, world_size):
|
||||
|
||||
@pytest.mark.dist
|
||||
def test_2p5d():
|
||||
world_size = 8
|
||||
world_size = 4
|
||||
run_func = partial(check_layer_and_operation, world_size=world_size)
|
||||
mp.spawn(run_func, nprocs=world_size)
|
||||
|
||||
|
@@ -1,34 +0,0 @@
|
||||
import time
|
||||
|
||||
import torch
|
||||
import torch.distributed as dist
|
||||
from colossalai.communication import all_gather, reduce_scatter, all_reduce
|
||||
from colossalai.context import ParallelMode
|
||||
from colossalai.core import global_context as gpc
|
||||
from colossalai.initialize import init_dist, parse_args
|
||||
from colossalai.utils import get_current_device, print_rank_0
|
||||
|
||||
# ARGS = parse_args()
|
||||
# size = ARGS.world_size
|
||||
# rank = ARGS.rank
|
||||
|
||||
# init_method = f'tcp://{ARGS.host}:{ARGS.port}'
|
||||
# dist.init_process_group(backend='nccl', rank=rank, world_size=size, init_method=init_method)
|
||||
CONFIG = dict(parallel=dict(data=8, pipeline=1, tensor=dict(mode=None, size=1)))
|
||||
init_dist(CONFIG)
|
||||
|
||||
assert dist.get_rank() == gpc.get_global_rank()
|
||||
|
||||
print('Rank {} / {}'.format(dist.get_rank(), dist.get_world_size()))
|
||||
|
||||
SIZE = 8
|
||||
tensor = torch.randn(SIZE)
|
||||
tensor = tensor.to(get_current_device())
|
||||
print('Before: Rank {0} - {1}'.format(dist.get_rank(), tensor))
|
||||
time.sleep(1)
|
||||
# tensor, op = all_gather(tensor, 0, ParallelMode.GLOBAL, async_op=True)
|
||||
# tensor, op = reduce_scatter(tensor, 0, ParallelMode.GLOBAL, async_op=True)
|
||||
tensor, op = all_reduce(tensor, ParallelMode.GLOBAL, async_op=True)
|
||||
print_rank_0('After: Rank {0} - {1}'.format(dist.get_rank(), tensor))
|
||||
op.wait()
|
||||
print_rank_0('Complete: Rank {0} - {1}'.format(dist.get_rank(), tensor))
|
@@ -1,19 +1,18 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
import math
|
||||
import time
|
||||
|
||||
import numpy as np
|
||||
from colossalai.context.parallel_mode import ParallelMode
|
||||
from colossalai.constants import (INPUT_GROUP_3D, OUTPUT_GROUP_3D, WEIGHT_GROUP_3D)
|
||||
from colossalai.core import global_context
|
||||
from colossalai.logging import get_dist_logger
|
||||
from colossalai.registry import LAYERS, LOSSES
|
||||
from colossalai.utils import get_current_device, print_rank_0
|
||||
from colossalai.nn import (Classifier3D, CrossEntropyLoss3D, LayerNorm3D, Linear3D, PatchEmbedding3D, VanillaClassifier,
|
||||
VanillaPatchEmbedding)
|
||||
from colossalai.nn.layer.parallel_3d._utils import get_parallel_mode_from_env
|
||||
from colossalai.constants import INPUT_GROUP_3D, WEIGHT_GROUP_3D, OUTPUT_GROUP_3D
|
||||
from colossalai.utils import get_current_device, print_rank_0
|
||||
|
||||
from .common import *
|
||||
import torch
|
||||
|
||||
|
||||
def check_linear():
|
||||
@@ -32,29 +31,20 @@ def check_linear():
|
||||
i = B_rank = global_context.get_local_rank(weight_parallel_mode)
|
||||
k = C_rank = global_context.get_local_rank(output_parallel_mode)
|
||||
|
||||
layer = LAYERS.get_module('Linear3D')(INPUT_SIZE,
|
||||
OUTPUT_SIZE,
|
||||
# ParallelMode.PARALLEL_3D_INPUT,
|
||||
# ParallelMode.PARALLEL_3D_WEIGHT,
|
||||
dtype=dtype,
|
||||
bias=True)
|
||||
# torch.nn.init.zeros_(layer.bias)
|
||||
# torch.nn.init.ones_(layer.weight)
|
||||
layer = Linear3D(INPUT_SIZE, OUTPUT_SIZE, dtype=dtype, bias=True)
|
||||
layer = layer.to(device)
|
||||
layer_master = torch.nn.Linear(INPUT_SIZE, OUTPUT_SIZE)
|
||||
# torch.nn.init.zeros_(layer_master.bias)
|
||||
# torch.nn.init.ones_(layer_master.weight)
|
||||
layer_master = layer_master.to(device)
|
||||
|
||||
weight_master = layer_master.weight.data.transpose(0, 1)
|
||||
torch.distributed.broadcast(weight_master, src=0)
|
||||
weight = torch.chunk(weight_master, DEPTH, dim=0)[k]
|
||||
weight = torch.chunk(weight, DEPTH, dim=-1)[j]
|
||||
layer.weight = torch.nn.Parameter(weight)
|
||||
layer.weight.data.copy_(weight)
|
||||
bias_master = layer_master.bias.data
|
||||
torch.distributed.broadcast(bias_master, src=0)
|
||||
bias = torch.chunk(bias_master, DEPTH)[j]
|
||||
layer.bias = torch.nn.Parameter(bias)
|
||||
layer.bias.data.copy_(bias)
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
@@ -67,10 +57,10 @@ def check_linear():
|
||||
|
||||
fwd_start = time.time()
|
||||
out = layer(A)
|
||||
torch.cuda.synchronize()
|
||||
fwd_end = time.time()
|
||||
print_rank_0(
|
||||
'linear forward: {0} --> {1} | {2:.3f} s'.format(
|
||||
tuple(A.shape), tuple(out.shape), fwd_end - fwd_start), logger)
|
||||
'linear forward: {0} --> {1} | {2:.3f} s'.format(tuple(A.shape), tuple(out.shape), fwd_end - fwd_start), logger)
|
||||
A_master = A_master.clone()
|
||||
A_master.requires_grad = True
|
||||
C_master = layer_master(A_master)
|
||||
@@ -80,9 +70,7 @@ def check_linear():
|
||||
logger.info('Rank {} linear forward: {}'.format(rank, check_equal(out, C)))
|
||||
|
||||
grad_shape = C_master.shape
|
||||
grad_master = torch.randn(grad_shape,
|
||||
dtype=dtype,
|
||||
device=get_current_device())
|
||||
grad_master = torch.randn(grad_shape, dtype=dtype, device=get_current_device())
|
||||
torch.distributed.broadcast(grad_master, src=0)
|
||||
grad = torch.chunk(grad_master, DEPTH, dim=0)[i]
|
||||
grad = torch.chunk(grad, DEPTH, dim=-1)[j]
|
||||
@@ -90,30 +78,25 @@ def check_linear():
|
||||
|
||||
bwd_start = time.time()
|
||||
out.backward(grad)
|
||||
torch.cuda.synchronize()
|
||||
bwd_end = time.time()
|
||||
print_rank_0('linear backward: {:.3f} s'.format(bwd_end - bwd_start),
|
||||
logger)
|
||||
print_rank_0('linear backward: {:.3f} s'.format(bwd_end - bwd_start), logger)
|
||||
|
||||
C_master.backward(grad_master)
|
||||
A_grad = A_master.grad
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=0)[i]
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=-1)[k]
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=0)[j]
|
||||
logger.info('Rank {} linear backward (input_grad): {}'.format(
|
||||
rank, check_equal(A_grad, A.grad)))
|
||||
logger.info('Rank {} linear backward (input_grad): {}'.format(rank, check_equal(A_grad, A.grad)))
|
||||
|
||||
B_grad = layer_master.weight.grad.transpose(0, 1)
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=0)[k]
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=-1)[j]
|
||||
# B_grad = torch.chunk(B_grad, DEPTH, dim=-1)[i]
|
||||
logger.info('Rank {} linear backward (weight_grad): {}'.format(
|
||||
rank, check_equal(B_grad, layer.weight.grad)))
|
||||
logger.info('Rank {} linear backward (weight_grad): {}'.format(rank, check_equal(B_grad, layer.weight.grad)))
|
||||
|
||||
bias_grad = layer_master.bias.grad
|
||||
bias_grad = torch.chunk(bias_grad, DEPTH)[j]
|
||||
logger.info('Rank {} linear backward (bias_grad): {}'.format(
|
||||
rank, check_equal(bias_grad, layer.bias.grad)))
|
||||
# logger.info(f'\nRank {rank} Master:\n{layer_master.bias.grad}\nRank {rank} True:\n{bias_grad}\nRank {rank} Out:\n{layer.bias.grad}')
|
||||
logger.info('Rank {} linear backward (bias_grad): {}'.format(rank, check_equal(bias_grad, layer.bias.grad)))
|
||||
|
||||
return fwd_end - fwd_start, bwd_end - bwd_start
|
||||
|
||||
@@ -133,11 +116,7 @@ def check_layernorm():
|
||||
i = B_rank = global_context.get_local_rank(weight_parallel_mode)
|
||||
k = C_rank = global_context.get_local_rank(output_parallel_mode)
|
||||
|
||||
norm = LAYERS.get_module('LayerNorm3D')(INPUT_SIZE,
|
||||
# ParallelMode.PARALLEL_3D_INPUT,
|
||||
# ParallelMode.PARALLEL_3D_WEIGHT,
|
||||
eps=1e-6,
|
||||
dtype=dtype)
|
||||
norm = LayerNorm3D(INPUT_SIZE, eps=1e-6, dtype=dtype)
|
||||
norm = norm.to(device)
|
||||
norm_master = torch.nn.LayerNorm(INPUT_SIZE, eps=1e-6)
|
||||
norm_master = norm_master.to(device)
|
||||
@@ -145,11 +124,11 @@ def check_layernorm():
|
||||
weight_master = norm_master.weight.data
|
||||
torch.distributed.broadcast(weight_master, src=0)
|
||||
weight = torch.chunk(weight_master, DEPTH)[k]
|
||||
norm.weight = torch.nn.Parameter(weight)
|
||||
norm.weight.data.copy_(weight)
|
||||
bias_master = norm_master.bias.data
|
||||
torch.distributed.broadcast(bias_master, src=0)
|
||||
bias = torch.chunk(bias_master, DEPTH)[k]
|
||||
norm.bias = torch.nn.Parameter(bias)
|
||||
norm.bias.data.copy_(bias)
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
@@ -162,10 +141,11 @@ def check_layernorm():
|
||||
|
||||
fwd_start = time.time()
|
||||
out = norm(A)
|
||||
torch.cuda.synchronize()
|
||||
fwd_end = time.time()
|
||||
print_rank_0(
|
||||
'layer norm forward: pass | {0} --> {1} | {2:.3f} s'.format(
|
||||
tuple(A.shape), tuple(out.shape), fwd_end - fwd_start), logger)
|
||||
'layer norm forward: pass | {0} --> {1} | {2:.3f} s'.format(tuple(A.shape), tuple(out.shape),
|
||||
fwd_end - fwd_start), logger)
|
||||
|
||||
A_master = A_master.clone()
|
||||
A_master.requires_grad = True
|
||||
@@ -173,14 +153,7 @@ def check_layernorm():
|
||||
C = torch.chunk(C_master, DEPTH, dim=0)[i]
|
||||
C = torch.chunk(C, DEPTH, dim=-1)[k]
|
||||
C = torch.chunk(C, DEPTH, dim=0)[j]
|
||||
logger.info('Rank {} layernorm forward: {}'.format(rank,
|
||||
check_equal(out, C)))
|
||||
# time.sleep(rank)
|
||||
# logger.info('Rank {0} master:\n{1}\nRank {0} out:\n{2}\nRank {0} true:\n{3}\n'.
|
||||
# format(rank,
|
||||
# C_master.detach().cpu().numpy().tolist(),
|
||||
# out.detach().cpu().numpy().tolist(),
|
||||
# C.detach().cpu().numpy().tolist()))
|
||||
logger.info('Rank {} layernorm forward: {}'.format(rank, check_equal(out, C)))
|
||||
|
||||
grad_shape = C_master.shape
|
||||
grad_master = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
@@ -191,39 +164,34 @@ def check_layernorm():
|
||||
|
||||
bwd_start = time.time()
|
||||
out.backward(grad)
|
||||
torch.cuda.synchronize()
|
||||
bwd_end = time.time()
|
||||
print_rank_0(
|
||||
'layer norm backward: pass | {:.3f} s'.format(bwd_end - bwd_start),
|
||||
logger)
|
||||
print_rank_0('layer norm backward: pass | {:.3f} s'.format(bwd_end - bwd_start), logger)
|
||||
|
||||
C_master.backward(grad_master)
|
||||
A_grad = A_master.grad
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=0)[i]
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=-1)[k]
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=0)[j]
|
||||
logger.info('Rank {} layernorm backward (input_grad): {}'.format(
|
||||
rank, check_equal(A_grad, A.grad)))
|
||||
logger.info('Rank {} layernorm backward (input_grad): {}'.format(rank, check_equal(A_grad, A.grad)))
|
||||
|
||||
bias_grad = norm_master.weight.grad
|
||||
bias_grad = torch.chunk(bias_grad, DEPTH)[k]
|
||||
logger.info('Rank {} layernorm backward (weight_grad): {}'.format(
|
||||
rank, check_equal(bias_grad, norm.weight.grad)))
|
||||
logger.info('Rank {} layernorm backward (weight_grad): {}'.format(rank, check_equal(bias_grad, norm.weight.grad)))
|
||||
|
||||
bias_grad = norm_master.bias.grad
|
||||
bias_grad = torch.chunk(bias_grad, DEPTH)[k]
|
||||
logger.info('Rank {} layernorm backward (bias_grad): {}'.format(
|
||||
rank, check_equal(bias_grad, norm.bias.grad)))
|
||||
logger.info('Rank {} layernorm backward (bias_grad): {}'.format(rank, check_equal(bias_grad, norm.bias.grad)))
|
||||
|
||||
return fwd_end - fwd_start, bwd_end - bwd_start
|
||||
|
||||
|
||||
def check_attention():
|
||||
def check_classifier():
|
||||
rank = torch.distributed.get_rank()
|
||||
device = get_current_device()
|
||||
logger = get_dist_logger()
|
||||
device = get_current_device()
|
||||
dtype = torch.float32
|
||||
INPUT_SIZE = HIDDEN_SIZE
|
||||
NUM_ATTENTION_HEADS = 2
|
||||
|
||||
input_parallel_mode = get_parallel_mode_from_env(INPUT_GROUP_3D)
|
||||
weight_parallel_mode = get_parallel_mode_from_env(WEIGHT_GROUP_3D)
|
||||
@@ -233,145 +201,19 @@ def check_attention():
|
||||
i = B_rank = global_context.get_local_rank(weight_parallel_mode)
|
||||
k = C_rank = global_context.get_local_rank(output_parallel_mode)
|
||||
|
||||
layer = LAYERS.get_module('ViTSelfAttention3D')(HIDDEN_SIZE,
|
||||
NUM_ATTENTION_HEADS,
|
||||
0.,
|
||||
0.1,
|
||||
dtype=dtype,
|
||||
bias=True)
|
||||
layer = Classifier3D(INPUT_SIZE, NUM_CLASSES, dtype=dtype, bias=True)
|
||||
layer = layer.to(device)
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = torch.chunk(A_master, DEPTH, dim=0)[i]
|
||||
A = torch.chunk(A, DEPTH, dim=-1)[k]
|
||||
A = torch.chunk(A, DEPTH, dim=0)[j]
|
||||
A = A.clone()
|
||||
A.requires_grad = True
|
||||
layer_master = VanillaClassifier(INPUT_SIZE, NUM_CLASSES, bias=True, dtype=dtype)
|
||||
layer_master = layer_master.to(device)
|
||||
|
||||
mask_shape = (BATCH_SIZE // DEPTH, NUM_ATTENTION_HEADS // DEPTH,
|
||||
SEQ_LENGTH // DEPTH, SEQ_LENGTH // DEPTH)
|
||||
attention_mask = torch.zeros(mask_shape, dtype=dtype, device=device)
|
||||
|
||||
fwd_start = time.time()
|
||||
out = layer(A)
|
||||
fwd_end = time.time()
|
||||
print_rank_0(
|
||||
'self attention forward: pass | {0} --> {1} | {2:.3f} s'.format(
|
||||
tuple(A.shape), tuple(out.shape), fwd_end - fwd_start), logger)
|
||||
|
||||
grad_shape = out.shape
|
||||
grad = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
|
||||
bwd_start = time.time()
|
||||
out.backward(grad)
|
||||
bwd_end = time.time()
|
||||
print_rank_0(
|
||||
'self attention backward: pass | {:.3f} s'.format(bwd_end - bwd_start),
|
||||
logger)
|
||||
|
||||
return fwd_end - fwd_start, bwd_end - bwd_start
|
||||
|
||||
|
||||
def check_mlp():
|
||||
rank = torch.distributed.get_rank()
|
||||
device = get_current_device()
|
||||
logger = get_dist_logger()
|
||||
dtype = torch.float32
|
||||
INPUT_SIZE = HIDDEN_SIZE
|
||||
|
||||
input_parallel_mode = get_parallel_mode_from_env(INPUT_GROUP_3D)
|
||||
weight_parallel_mode = get_parallel_mode_from_env(WEIGHT_GROUP_3D)
|
||||
output_parallel_mode = get_parallel_mode_from_env(OUTPUT_GROUP_3D)
|
||||
|
||||
j = A_rank = global_context.get_local_rank(input_parallel_mode)
|
||||
i = B_rank = global_context.get_local_rank(weight_parallel_mode)
|
||||
k = C_rank = global_context.get_local_rank(output_parallel_mode)
|
||||
|
||||
layer = LAYERS.get_module('ViTMLP3D')(HIDDEN_SIZE,
|
||||
1,
|
||||
0.1,
|
||||
'gelu',
|
||||
dtype=dtype,
|
||||
bias=True)
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = torch.chunk(A_master, DEPTH, dim=0)[i]
|
||||
A = torch.chunk(A, DEPTH, dim=-1)[k]
|
||||
A = torch.chunk(A, DEPTH, dim=0)[j]
|
||||
A = A.clone()
|
||||
A.requires_grad = True
|
||||
|
||||
fwd_start = time.time()
|
||||
out = layer(A)
|
||||
fwd_end = time.time()
|
||||
print_rank_0(
|
||||
'mlp forward: pass | {0} --> {1} | {2:.3f} s'.format(
|
||||
tuple(A.shape), tuple(out.shape), fwd_end - fwd_start), logger)
|
||||
|
||||
grad_shape = out.shape
|
||||
grad = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
|
||||
bwd_start = time.time()
|
||||
out.backward(grad)
|
||||
bwd_end = time.time()
|
||||
print_rank_0('mlp backward: pass | {:.3f} s'.format(bwd_end - bwd_start),
|
||||
logger)
|
||||
|
||||
return fwd_end - fwd_start, bwd_end - bwd_start
|
||||
|
||||
|
||||
class Testvithead(torch.nn.Module):
|
||||
def __init__(self, in_features, out_features, bias=True):
|
||||
super().__init__()
|
||||
self.linear = torch.nn.Linear(in_features, out_features, bias=bias)
|
||||
|
||||
def forward(self, x):
|
||||
x = x[:, 0]
|
||||
x = self.linear(x)
|
||||
return x
|
||||
|
||||
|
||||
def check_head():
|
||||
rank = torch.distributed.get_rank()
|
||||
logger = get_dist_logger()
|
||||
device = get_current_device()
|
||||
dtype = torch.float32
|
||||
INPUT_SIZE = HIDDEN_SIZE
|
||||
|
||||
input_parallel_mode = get_parallel_mode_from_env(INPUT_GROUP_3D)
|
||||
weight_parallel_mode = get_parallel_mode_from_env(WEIGHT_GROUP_3D)
|
||||
output_parallel_mode = get_parallel_mode_from_env(OUTPUT_GROUP_3D)
|
||||
|
||||
j = A_rank = global_context.get_local_rank(input_parallel_mode)
|
||||
i = B_rank = global_context.get_local_rank(weight_parallel_mode)
|
||||
k = C_rank = global_context.get_local_rank(output_parallel_mode)
|
||||
|
||||
head = LAYERS.get_module('ViTHead3D')(INPUT_SIZE,
|
||||
NUM_CLASSES,
|
||||
dtype=dtype,
|
||||
bias=True)
|
||||
# torch.nn.init.zeros_(head.linear.bias)
|
||||
# torch.nn.init.ones_(head.linear.weight)
|
||||
head = head.to(device)
|
||||
|
||||
layer = Testvithead(INPUT_SIZE, NUM_CLASSES, bias=True)
|
||||
# torch.nn.init.zeros_(layer.linear.bias)
|
||||
# torch.nn.init.ones_(layer.linear.weight)
|
||||
layer = layer.to(device)
|
||||
|
||||
weight_master = layer.linear.weight.data.transpose(0, 1)
|
||||
weight_master = layer_master.weight.data
|
||||
torch.distributed.broadcast(weight_master, src=0)
|
||||
weight = torch.chunk(weight_master, DEPTH, dim=0)[k]
|
||||
weight = torch.chunk(weight, DEPTH, dim=-1)[j]
|
||||
head.linear.weight = torch.nn.Parameter(weight)
|
||||
bias_master = layer.linear.bias.data
|
||||
weight = torch.chunk(weight_master, DEPTH, dim=-1)[k]
|
||||
layer.weight.data.copy_(weight)
|
||||
bias_master = layer_master.bias.data
|
||||
torch.distributed.broadcast(bias_master, src=0)
|
||||
bias = torch.chunk(bias_master, DEPTH)[j]
|
||||
head.linear.bias = torch.nn.Parameter(bias)
|
||||
layer.bias.data.copy_(bias_master)
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
@@ -383,115 +225,54 @@ def check_head():
|
||||
A.requires_grad = True
|
||||
|
||||
fwd_start = time.time()
|
||||
out = head(A)
|
||||
out = layer(A)
|
||||
torch.cuda.synchronize()
|
||||
fwd_end = time.time()
|
||||
print_rank_0(
|
||||
'head forward: pass | {0} --> {1} | {2:.3f} s'.format(
|
||||
tuple(A.shape), tuple(out.shape), fwd_end - fwd_start), logger)
|
||||
'head forward: pass | {0} --> {1} | {2:.3f} s'.format(tuple(A.shape), tuple(out.shape), fwd_end - fwd_start),
|
||||
logger)
|
||||
A_master = A_master.clone()
|
||||
A_master.requires_grad = True
|
||||
C_master = layer(A_master)
|
||||
C_master = layer_master(A_master)
|
||||
C = torch.chunk(C_master, DEPTH, dim=0)[i]
|
||||
C = torch.chunk(C, DEPTH, dim=-1)[j]
|
||||
C = torch.chunk(C, DEPTH, dim=0)[k]
|
||||
C = torch.chunk(C, DEPTH, dim=0)[j]
|
||||
logger.info('Rank {} head forward: {}'.format(rank, check_equal(out, C)))
|
||||
|
||||
grad_shape = C_master.shape
|
||||
grad_master = torch.randn(grad_shape,
|
||||
dtype=dtype,
|
||||
device=get_current_device())
|
||||
grad_master = torch.randn(grad_shape, dtype=dtype, device=get_current_device())
|
||||
torch.distributed.broadcast(grad_master, src=0)
|
||||
grad = torch.chunk(grad_master, DEPTH, dim=0)[i]
|
||||
grad = torch.chunk(grad, DEPTH, dim=-1)[j]
|
||||
grad = torch.chunk(grad, DEPTH, dim=0)[k]
|
||||
grad = torch.chunk(grad, DEPTH, dim=0)[j]
|
||||
grad = grad.clone()
|
||||
|
||||
bwd_start = time.time()
|
||||
out.backward(grad)
|
||||
torch.cuda.synchronize()
|
||||
bwd_end = time.time()
|
||||
print_rank_0('head backward: pass | {:.3f} s'.format(bwd_end - bwd_start),
|
||||
logger)
|
||||
print_rank_0('head backward: pass | {:.3f} s'.format(bwd_end - bwd_start), logger)
|
||||
|
||||
grad_master = grad_master.clone()
|
||||
C_master.backward(grad_master)
|
||||
A_grad = A_master.grad
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=0)[i]
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=-1)[k]
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=0)[j]
|
||||
# if j == 0:
|
||||
logger.info('Rank {} head backward (input_grad): {}'.format(
|
||||
rank, check_equal(A_grad, A.grad)))
|
||||
# else:
|
||||
# logger.info('Rank {} head backward (input_grad): {}'.format(
|
||||
# # rank, check_equal(A_grad, A.grad)))
|
||||
# rank,
|
||||
# A.grad is None))
|
||||
logger.info('Rank {} head backward (input_grad): {}'.format(rank, check_equal(A_grad, A.grad)))
|
||||
|
||||
B_grad = layer.linear.weight.grad.transpose(0, 1)
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=0)[k]
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=-1)[j]
|
||||
# B_grad = torch.chunk(B_grad, DEPTH, dim=-1)[i]
|
||||
logger.info('Rank {} head backward (weight_grad): {}'.format(
|
||||
rank, check_equal(B_grad, head.linear.weight.grad)))
|
||||
B_grad = layer_master.weight.grad
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=-1)[k]
|
||||
if j == k:
|
||||
logger.info('Rank {} head backward (weight_grad): {}'.format(rank,
|
||||
check_equal(B_grad, layer.weight.grad)))
|
||||
else:
|
||||
logger.info('Rank {} head backward (weight_grad): {}'.format(rank, layer.weight.grad is None))
|
||||
|
||||
bias_grad = layer.linear.bias.grad
|
||||
bias_grad = torch.chunk(bias_grad, DEPTH)[j]
|
||||
logger.info('Rank {} head backward (bias_grad): {}'.format(
|
||||
rank, check_equal(bias_grad, head.linear.bias.grad)))
|
||||
|
||||
# B_grad = layer.linear.weight.grad.transpose(0, 1)
|
||||
# B_grad = torch.chunk(B_grad, DEPTH, dim=0)[k]
|
||||
# B_grad = torch.chunk(B_grad, DEPTH, dim=-1)[j]
|
||||
# pad_shape = (B_grad.shape[0], math.ceil(B_grad.shape[-1] / DEPTH) * DEPTH -
|
||||
# B_grad.shape[-1])
|
||||
# B_grad = torch.cat(
|
||||
# [B_grad, torch.zeros(pad_shape, dtype=dtype, device=device)], dim=-1)
|
||||
# B_grad = torch.chunk(B_grad, DEPTH, dim=-1)[i]
|
||||
# logger.info('Rank {} head backward (weight_grad): {}'.format(
|
||||
# rank, check_equal(B_grad, head.linear.weight.grad)))
|
||||
|
||||
# if j == k:
|
||||
# bias_grad = layer.linear.bias.grad
|
||||
# bias_grad = torch.chunk(bias_grad, DEPTH)[j]
|
||||
# pad_shape = (math.ceil(bias_grad.shape[0] / DEPTH) * DEPTH -
|
||||
# bias_grad.shape[0], )
|
||||
# bias_grad = torch.cat(
|
||||
# [bias_grad,
|
||||
# torch.zeros(pad_shape, dtype=dtype, device=device)])
|
||||
# bias_grad = torch.chunk(bias_grad, DEPTH)[i]
|
||||
# logger.info('Rank {} head backward (bias_grad): {}'.format(
|
||||
# rank, check_equal(bias_grad, head.linear.bias.grad)))
|
||||
# else:
|
||||
# logger.info('Rank {} head backward (bias_grad): {}'.format(
|
||||
# rank,
|
||||
# # np.count_nonzero(
|
||||
# # head.linear.bias.grad.detach().cpu().numpy()) == 0))
|
||||
# head.linear.bias.grad is None))
|
||||
bias_grad = layer_master.bias.grad
|
||||
logger.info('Rank {} head backward (bias_grad): {}'.format(rank, check_equal(bias_grad, layer.bias.grad)))
|
||||
|
||||
return fwd_end - fwd_start, bwd_end - bwd_start
|
||||
|
||||
|
||||
class Testvitembed(torch.nn.Module):
|
||||
def __init__(self, img_size: int, patch_size: int, in_chans: int,
|
||||
embed_size: int, drop_prob: float) -> None:
|
||||
super().__init__()
|
||||
self.proj = torch.nn.Conv2d(in_chans,
|
||||
embed_size,
|
||||
kernel_size=patch_size,
|
||||
stride=patch_size)
|
||||
num_patches = (img_size // patch_size)**2
|
||||
self.cls_token = torch.nn.Parameter(torch.zeros(1, 1, embed_size))
|
||||
self.pos_embed = torch.nn.Parameter(
|
||||
torch.zeros(1, num_patches + 1, embed_size))
|
||||
self.pos_drop = torch.nn.Dropout(drop_prob)
|
||||
|
||||
def forward(self, x):
|
||||
x = self.proj(x)
|
||||
x = x.flatten(2).transpose(1, 2)
|
||||
cls_token = self.cls_token.expand(x.shape[0], -1, -1)
|
||||
x = torch.cat((cls_token, x), dim=1)
|
||||
x = self.pos_drop(x + self.pos_embed)
|
||||
return x
|
||||
|
||||
|
||||
def check_embed():
|
||||
rank = torch.distributed.get_rank()
|
||||
device = get_current_device()
|
||||
@@ -506,21 +287,25 @@ def check_embed():
|
||||
i = B_rank = global_context.get_local_rank(weight_parallel_mode)
|
||||
k = C_rank = global_context.get_local_rank(output_parallel_mode)
|
||||
|
||||
layer = LAYERS.get_module('ViTPatchEmbedding3D')(IMG_SIZE, 4, 3,
|
||||
HIDDEN_SIZE, 0.)
|
||||
torch.nn.init.zeros_(layer.proj.bias)
|
||||
torch.nn.init.ones_(layer.proj.weight)
|
||||
layer = PatchEmbedding3D(IMG_SIZE, 4, 3, HIDDEN_SIZE, dtype=dtype)
|
||||
torch.nn.init.ones_(layer.cls_token)
|
||||
torch.nn.init.ones_(layer.pos_embed)
|
||||
layer = layer.to(device)
|
||||
|
||||
layer_master = Testvitembed(IMG_SIZE, 4, 3, HIDDEN_SIZE, 0.)
|
||||
torch.nn.init.zeros_(layer_master.proj.bias)
|
||||
torch.nn.init.ones_(layer_master.proj.weight)
|
||||
layer_master = VanillaPatchEmbedding(IMG_SIZE, 4, 3, HIDDEN_SIZE, dtype=dtype)
|
||||
torch.nn.init.ones_(layer_master.cls_token)
|
||||
torch.nn.init.ones_(layer_master.pos_embed)
|
||||
layer_master = layer_master.to(device)
|
||||
|
||||
proj_weight_master = layer_master.weight.data
|
||||
torch.distributed.broadcast(proj_weight_master, src=0)
|
||||
proj_weight = torch.chunk(proj_weight_master, DEPTH, dim=0)[k]
|
||||
layer.weight.data.copy_(proj_weight)
|
||||
proj_bias_master = layer_master.bias.data
|
||||
torch.distributed.broadcast(proj_bias_master, src=0)
|
||||
proj_bias = torch.chunk(proj_bias_master, DEPTH)[k]
|
||||
layer.bias.data.copy_(proj_bias)
|
||||
|
||||
A_shape = (BATCH_SIZE, 3, IMG_SIZE, IMG_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
@@ -529,103 +314,55 @@ def check_embed():
|
||||
|
||||
fwd_start = time.time()
|
||||
out = layer(A)
|
||||
torch.cuda.synchronize()
|
||||
fwd_end = time.time()
|
||||
print_rank_0(
|
||||
'embedding forward: pass | {0} --> {1} | {2:.3f} s'.format(
|
||||
tuple(A.shape), tuple(out.shape), fwd_end - fwd_start), logger)
|
||||
# out_cls = out[:, 0]
|
||||
# out_tensor = out[:, 1:]
|
||||
'embedding forward: pass | {0} --> {1} | {2:.3f} s'.format(tuple(A.shape), tuple(out.shape),
|
||||
fwd_end - fwd_start), logger)
|
||||
|
||||
A_master = A_master.clone()
|
||||
A_master.requires_grad = True
|
||||
C_master = layer_master(A_master)
|
||||
# if j == 0:
|
||||
# C_cls = C_master[:, 0]
|
||||
# C_cls = torch.chunk(C_cls, DEPTH, dim=0)[i]
|
||||
# C_cls = torch.chunk(C_cls, DEPTH, dim=-1)[k]
|
||||
# logger.info('Rank {} embed forward (cls): {}'.format(
|
||||
# rank, check_equal(out_cls, C_cls)))
|
||||
# C = C_master[:, 1:]
|
||||
C = torch.chunk(C_master, DEPTH, dim=0)[i]
|
||||
C = torch.chunk(C, DEPTH, dim=-1)[k]
|
||||
C = torch.chunk(C, DEPTH, dim=0)[j]
|
||||
logger.info('Rank {} embed forward: {}'.format(rank, check_equal(out, C)))
|
||||
|
||||
grad_shape = C_master.shape
|
||||
grad_master = torch.randn(grad_shape,
|
||||
dtype=dtype,
|
||||
device=get_current_device())
|
||||
grad_master = torch.randn(grad_shape, dtype=dtype, device=get_current_device())
|
||||
torch.distributed.broadcast(grad_master, src=0)
|
||||
# cls_grad = grad_master[:, 0]
|
||||
# cls_grad = torch.chunk(cls_grad, DEPTH, dim=0)[i]
|
||||
# cls_grad = torch.chunk(cls_grad, DEPTH, dim=-1)[k]
|
||||
# grad = grad_master[:, 1:]
|
||||
grad = torch.chunk(grad_master, DEPTH, dim=0)[i]
|
||||
grad = torch.chunk(grad, DEPTH, dim=-1)[k]
|
||||
grad = torch.chunk(grad, DEPTH, dim=0)[j]
|
||||
# grad = torch.cat((torch.unsqueeze(cls_grad, 1), grad), dim=1)
|
||||
grad = grad.clone()
|
||||
bwd_start = time.time()
|
||||
out.backward(grad)
|
||||
torch.cuda.synchronize()
|
||||
bwd_end = time.time()
|
||||
print_rank_0(
|
||||
'embedding backward: pass | {:.3f} s'.format(bwd_end - bwd_start),
|
||||
logger)
|
||||
print_rank_0('embedding backward: pass | {:.3f} s'.format(bwd_end - bwd_start), logger)
|
||||
|
||||
grad_master = grad_master.clone()
|
||||
C_master.backward(grad_master)
|
||||
# A_grad = A_master.grad
|
||||
# logger.info('Rank {} embed backward (input_grad): {}'.format(
|
||||
# rank, check_equal(A_grad, A.grad)))
|
||||
# time.sleep(0.1 * rank)
|
||||
# logger.info(
|
||||
# 'Rank {0} master:\n{1}\nRank {0} out:\n{2}\nRank {0} true:\n{3}\n'.
|
||||
# format(rank,
|
||||
# A_master.grad.detach().cpu().numpy().tolist(),
|
||||
# A.grad.detach().cpu().numpy().tolist(),
|
||||
# A_grad.detach().cpu().numpy().tolist()), ranks=[0])
|
||||
|
||||
cls_grad_master = layer_master.cls_token.grad
|
||||
cls_grad = torch.chunk(cls_grad_master, DEPTH, dim=-1)[k]
|
||||
# if j == 0:
|
||||
logger.info('Rank {} embed backward (cls_grad): {}'.format(
|
||||
rank, check_equal(cls_grad, layer.cls_token.grad)))
|
||||
# else:.
|
||||
# logger.info('Rank {} embed backward (cls_grad): {}'.format(
|
||||
# rank,
|
||||
# layer.cls_token.grad is None or np.count_nonzero(
|
||||
# layer.cls_token.grad.detach().cpu().numpy()) == 0))
|
||||
logger.info('Rank {} embed backward (cls_grad): {}'.format(rank, check_equal(cls_grad, layer.cls_token.grad)))
|
||||
|
||||
pos_grad_master = layer_master.pos_embed.grad
|
||||
pos_grad = torch.chunk(pos_grad_master, DEPTH, dim=-1)[k]
|
||||
logger.info('Rank {} embed backward (pos_embed_grad): {}'.format(
|
||||
rank, check_equal(pos_grad, layer.pos_embed.grad)))
|
||||
# if i == 0:
|
||||
# pos_cls_grad = pos_grad[:, 0]
|
||||
# pos_tensor_grad = pos_grad[:, 1:]
|
||||
# pos_tensor_grad = torch.chunk(pos_tensor_grad, DEPTH, dim=1)[j]
|
||||
# if j == 0:
|
||||
# logger.info('Rank {} embed backward (pos_embed_grad): {}'.format(
|
||||
# rank,
|
||||
# check_equal(
|
||||
# torch.cat(
|
||||
# (torch.unsqueeze(pos_cls_grad, 1), pos_tensor_grad),
|
||||
# dim=1), layer.pos_embed.grad)))
|
||||
# else:
|
||||
# logger.info('Rank {} embed backward (pos_embed_grad): {}'.format(
|
||||
# rank, check_equal(pos_tensor_grad, layer.pos_embed.grad[:,
|
||||
# 1:])))
|
||||
# else:
|
||||
# logger.info('Rank {} embed backward (pos_embed_grad): {}'.format(
|
||||
# rank, layer.pos_embed.grad is None))
|
||||
logger.info('Rank {} embed backward (pos_embed_grad): {}'.format(rank, check_equal(pos_grad, layer.pos_embed.grad)))
|
||||
|
||||
B_grad = layer_master.proj.weight.grad
|
||||
B_grad = layer_master.weight.grad
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=0)[k]
|
||||
logger.info('Rank {} embed backward (proj_weight_grad): {}'.format(
|
||||
rank, check_equal(B_grad, layer.proj.weight.grad)))
|
||||
if j == k:
|
||||
logger.info('Rank {} embed backward (proj_weight_grad): {}'.format(rank, check_equal(B_grad,
|
||||
layer.weight.grad)))
|
||||
else:
|
||||
logger.info('Rank {} embed backward (proj_weight_grad): {}'.format(rank, layer.weight.grad is None))
|
||||
|
||||
bias_grad = layer_master.proj.bias.grad
|
||||
bias_grad = layer_master.bias.grad
|
||||
bias_grad = torch.chunk(bias_grad, DEPTH)[k]
|
||||
logger.info('Rank {} embed backward (proj_bias_grad): {}'.format(
|
||||
rank, check_equal(bias_grad, layer.proj.bias.grad)))
|
||||
logger.info('Rank {} embed backward (proj_bias_grad): {}'.format(rank, check_equal(bias_grad, layer.bias.grad)))
|
||||
|
||||
return fwd_end - fwd_start, bwd_end - bwd_start
|
||||
|
||||
@@ -644,19 +381,15 @@ def check_loss():
|
||||
i = B_rank = global_context.get_local_rank(weight_parallel_mode)
|
||||
k = C_rank = global_context.get_local_rank(output_parallel_mode)
|
||||
|
||||
criterion = LOSSES.get_module('CrossEntropyLoss3D')()
|
||||
# ParallelMode.PARALLEL_3D_INPUT, ParallelMode.PARALLEL_3D_WEIGHT)
|
||||
criterion = CrossEntropyLoss3D()
|
||||
criterion_master = torch.nn.CrossEntropyLoss()
|
||||
|
||||
out_shape = (BATCH_SIZE, NUM_CLASSES)
|
||||
out_master = torch.randn(out_shape, dtype=dtype, device=device)
|
||||
target_master = torch.randint(NUM_CLASSES, (BATCH_SIZE, ),
|
||||
dtype=torch.long,
|
||||
device=device)
|
||||
target_master = torch.randint(NUM_CLASSES, (BATCH_SIZE, ), dtype=torch.long, device=device)
|
||||
torch.distributed.broadcast(out_master, src=0)
|
||||
torch.distributed.broadcast(target_master, src=0)
|
||||
out = torch.chunk(out_master, DEPTH, dim=0)[i]
|
||||
out = torch.chunk(out, DEPTH, dim=-1)[k]
|
||||
out = torch.chunk(out, DEPTH, dim=0)[j]
|
||||
out = out.clone()
|
||||
out.requires_grad = True
|
||||
@@ -665,27 +398,23 @@ def check_loss():
|
||||
loss = criterion(out, target_master)
|
||||
fwd_end = time.time()
|
||||
print_rank_0(
|
||||
'loss forward: pass | {0} --> {1} | {2:.3f} s'.format(
|
||||
tuple(out.shape), tuple(loss.shape), fwd_end - fwd_start), logger)
|
||||
'loss forward: pass | {0} --> {1} | {2:.3f} s'.format(tuple(out.shape), tuple(loss.shape), fwd_end - fwd_start),
|
||||
logger)
|
||||
|
||||
out_master = out_master.clone()
|
||||
out_master.requires_grad = True
|
||||
loss_master = criterion_master(out_master, target_master)
|
||||
logger.info('Rank {} CrossEntropyLoss forward: {}'.format(
|
||||
rank, check_equal(loss, loss_master)))
|
||||
logger.info('Rank {} CrossEntropyLoss forward: {}'.format(rank, check_equal(loss, loss_master)))
|
||||
|
||||
bwd_start = time.time()
|
||||
loss.backward()
|
||||
bwd_end = time.time()
|
||||
print_rank_0('loss backward: pass | {:.3f} s'.format(bwd_end - bwd_start),
|
||||
logger)
|
||||
print_rank_0('loss backward: pass | {:.3f} s'.format(bwd_end - bwd_start), logger)
|
||||
|
||||
loss_master.backward()
|
||||
out_grad = out_master.grad
|
||||
out_grad = torch.chunk(out_grad, DEPTH, dim=0)[i]
|
||||
out_grad = torch.chunk(out_grad, DEPTH, dim=-1)[k]
|
||||
out_grad = torch.chunk(out_grad, DEPTH, dim=0)[j]
|
||||
logger.info('Rank {} CrossEntropyLoss backward: {}'.format(
|
||||
rank, check_equal(out_grad, out.grad)))
|
||||
logger.info('Rank {} CrossEntropyLoss backward: {}'.format(rank, check_equal(out_grad, out.grad)))
|
||||
|
||||
return fwd_end - fwd_start, bwd_end - bwd_start
|
||||
|
@@ -1,465 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
from colossalai.context import ParallelMode
|
||||
from colossalai.core import global_context
|
||||
from colossalai.logging import get_dist_logger
|
||||
from colossalai.nn.layer.parallel_3d._operation import *
|
||||
from colossalai.utils import get_current_device
|
||||
|
||||
from .common import *
|
||||
|
||||
|
||||
def check_AB():
|
||||
rank = torch.distributed.get_rank()
|
||||
logger = get_dist_logger()
|
||||
dtype = torch.float
|
||||
j = global_context.get_local_rank(ParallelMode.PARALLEL_3D_INPUT)
|
||||
i = global_context.get_local_rank(ParallelMode.PARALLEL_3D_WEIGHT)
|
||||
k = global_context.get_local_rank(ParallelMode.PARALLEL_3D_OUTPUT)
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, HIDDEN_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=get_current_device())
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = torch.chunk(A_master, DEPTH, dim=0)[i]
|
||||
A = torch.chunk(A, DEPTH, dim=-1)[k]
|
||||
A = torch.chunk(A, DEPTH, dim=0)[j]
|
||||
A = A.clone()
|
||||
A.requires_grad = True
|
||||
|
||||
B_shape = (HIDDEN_SIZE, 4 * HIDDEN_SIZE)
|
||||
B_master = torch.randn(B_shape, dtype=dtype, device=get_current_device())
|
||||
torch.distributed.broadcast(B_master, src=0)
|
||||
B = torch.chunk(B_master, DEPTH, dim=0)[k]
|
||||
B = torch.chunk(B, DEPTH, dim=-1)[j]
|
||||
B = torch.chunk(B, DEPTH, dim=-1)[i]
|
||||
B = B.clone()
|
||||
B.requires_grad = True
|
||||
|
||||
out = Matmul_AB_3D.apply(A, B, DEPTH, ParallelMode.PARALLEL_3D_INPUT,
|
||||
ParallelMode.PARALLEL_3D_WEIGHT,
|
||||
ParallelMode.PARALLEL_3D_OUTPUT)
|
||||
|
||||
C_shape = (BATCH_SIZE, SEQ_LENGTH, 4 * HIDDEN_SIZE)
|
||||
A_master = A_master.clone()
|
||||
A_master.requires_grad = True
|
||||
B_master = B_master.clone()
|
||||
B_master.requires_grad = True
|
||||
C_master = torch.matmul(A_master, B_master)
|
||||
C = torch.chunk(C_master, DEPTH, dim=0)[i]
|
||||
C = torch.chunk(C, DEPTH, dim=-1)[j]
|
||||
C = torch.chunk(C, DEPTH, dim=0)[k]
|
||||
# check forward correctness
|
||||
logger.info('Rank {} AB forward: {}'.format(rank, check_equal(out, C)))
|
||||
|
||||
grad_shape = C_master.shape
|
||||
grad_master = torch.randn(grad_shape,
|
||||
dtype=dtype,
|
||||
device=get_current_device())
|
||||
torch.distributed.broadcast(grad_master, src=0)
|
||||
grad = torch.chunk(grad_master, DEPTH, dim=0)[i]
|
||||
grad = torch.chunk(grad, DEPTH, dim=-1)[j]
|
||||
grad = torch.chunk(grad, DEPTH, dim=0)[k]
|
||||
|
||||
out.backward(grad)
|
||||
|
||||
C_master.backward(grad_master)
|
||||
A_grad = A_master.grad
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=0)[i]
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=-1)[k]
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=0)[j]
|
||||
# check backward correctness
|
||||
logger.info('Rank {} AB backward (A_grad): {}'.format(
|
||||
rank, check_equal(A_grad, A.grad)))
|
||||
|
||||
B_grad = B_master.grad
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=0)[k]
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=-1)[j]
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=-1)[i]
|
||||
# check backward correctness
|
||||
logger.info('Rank {} AB backward (B_grad): {}'.format(
|
||||
rank, check_equal(B_grad, B.grad)))
|
||||
|
||||
|
||||
def check_ABT():
|
||||
rank = torch.distributed.get_rank()
|
||||
logger = get_dist_logger()
|
||||
dtype = torch.float
|
||||
|
||||
j = A_rank = global_context.get_local_rank(ParallelMode.PARALLEL_3D_INPUT)
|
||||
i = B_rank = global_context.get_local_rank(ParallelMode.PARALLEL_3D_WEIGHT)
|
||||
k = C_rank = global_context.get_local_rank(ParallelMode.PARALLEL_3D_OUTPUT)
|
||||
device = get_current_device()
|
||||
|
||||
C_shape = (BATCH_SIZE, SEQ_LENGTH, 4 * HIDDEN_SIZE)
|
||||
C_master = torch.randn(C_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(C_master, src=0)
|
||||
C = torch.chunk(C_master, DEPTH, dim=0)[i]
|
||||
C = torch.chunk(C, DEPTH, dim=-1)[j]
|
||||
C = torch.chunk(C, DEPTH, dim=0)[k]
|
||||
C = C.clone()
|
||||
C.requires_grad = True
|
||||
|
||||
B_shape = (HIDDEN_SIZE, 4 * HIDDEN_SIZE)
|
||||
B_master = torch.randn(B_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(B_master, src=0)
|
||||
B = torch.chunk(B_master, DEPTH, dim=0)[k]
|
||||
B = torch.chunk(B, DEPTH, dim=-1)[j]
|
||||
B = torch.chunk(B, DEPTH, dim=-1)[i]
|
||||
B = B.clone()
|
||||
B.requires_grad = True
|
||||
|
||||
out = Matmul_ABT_3D.apply(C, B, DEPTH, ParallelMode.PARALLEL_3D_OUTPUT,
|
||||
ParallelMode.PARALLEL_3D_WEIGHT,
|
||||
ParallelMode.PARALLEL_3D_INPUT)
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, HIDDEN_SIZE)
|
||||
C_master = C_master.clone()
|
||||
C_master.requires_grad = True
|
||||
B_master = B_master.clone()
|
||||
B_master.requires_grad = True
|
||||
A_master = torch.matmul(C_master, B_master.transpose(0, 1))
|
||||
A = torch.chunk(A_master, DEPTH, dim=0)[i]
|
||||
A = torch.chunk(A, DEPTH, dim=-1)[k]
|
||||
A = torch.chunk(A, DEPTH, dim=0)[j]
|
||||
logger.info('Rank {} ABT forward: {}'.format(rank, check_equal(out, A)))
|
||||
|
||||
grad_shape = A_master.shape
|
||||
grad_master = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(grad_master, src=0)
|
||||
grad = torch.chunk(grad_master, DEPTH, dim=0)[i]
|
||||
grad = torch.chunk(grad, DEPTH, dim=-1)[k]
|
||||
grad = torch.chunk(grad, DEPTH, dim=0)[j]
|
||||
|
||||
# backward
|
||||
out.backward(grad)
|
||||
|
||||
A_master.backward(grad_master)
|
||||
C_grad = C_master.grad
|
||||
C_grad = torch.chunk(C_grad, DEPTH, dim=0)[i]
|
||||
C_grad = torch.chunk(C_grad, DEPTH, dim=-1)[j]
|
||||
C_grad = torch.chunk(C_grad, DEPTH, dim=0)[k]
|
||||
logger.info('Rank {} ABT backward (A_grad): {}'.format(
|
||||
rank, check_equal(C_grad, C.grad)))
|
||||
|
||||
B_grad = B_master.grad
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=0)[k]
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=-1)[j]
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=-1)[i]
|
||||
logger.info('Rank {} ABT backward (B_grad): {}'.format(
|
||||
rank, check_equal(B_grad, B.grad)))
|
||||
|
||||
|
||||
def check_ATB():
|
||||
rank = torch.distributed.get_rank()
|
||||
logger = get_dist_logger()
|
||||
device = get_current_device()
|
||||
dtype = torch.float
|
||||
|
||||
j = A_rank = global_context.get_local_rank(ParallelMode.PARALLEL_3D_INPUT)
|
||||
i = B_rank = global_context.get_local_rank(ParallelMode.PARALLEL_3D_WEIGHT)
|
||||
k = C_rank = global_context.get_local_rank(ParallelMode.PARALLEL_3D_OUTPUT)
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, HIDDEN_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = torch.chunk(A_master, DEPTH, dim=0)[i]
|
||||
A = torch.chunk(A, DEPTH, dim=-1)[k]
|
||||
A = torch.chunk(A, DEPTH, dim=0)[j]
|
||||
A = A.clone()
|
||||
A.requires_grad = True
|
||||
|
||||
C_shape = (BATCH_SIZE, SEQ_LENGTH, 4 * HIDDEN_SIZE)
|
||||
C_master = torch.randn(C_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(C_master, src=0)
|
||||
C = torch.chunk(C_master, DEPTH, dim=0)[i]
|
||||
C = torch.chunk(C, DEPTH, dim=-1)[j]
|
||||
C = torch.chunk(C, DEPTH, dim=0)[k]
|
||||
C = C.clone()
|
||||
C.requires_grad = True
|
||||
|
||||
out = Matmul_ATB_3D.apply(A, C, DEPTH, ParallelMode.PARALLEL_3D_INPUT,
|
||||
ParallelMode.PARALLEL_3D_OUTPUT,
|
||||
ParallelMode.PARALLEL_3D_WEIGHT)
|
||||
|
||||
B_shape = (HIDDEN_SIZE, 4 * HIDDEN_SIZE)
|
||||
A_master = A_master.clone()
|
||||
A_master.requires_grad = True
|
||||
C_master = C_master.clone()
|
||||
C_master.requires_grad = True
|
||||
B_master = torch.matmul(
|
||||
A_master.view(-1, A_master.shape[-1]).transpose(0, 1),
|
||||
C_master.view(-1, C_master.shape[-1]))
|
||||
B = torch.chunk(B_master, DEPTH, dim=0)[k]
|
||||
B = torch.chunk(B, DEPTH, dim=-1)[j]
|
||||
B = torch.chunk(B, DEPTH, dim=-1)[i]
|
||||
logger.info('Rank {} ATB forward: {}'.format(rank, check_equal(out, B)))
|
||||
|
||||
grad_shape = B_master.shape
|
||||
grad_master = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(grad_master, src=0)
|
||||
grad = torch.chunk(grad_master, DEPTH, dim=0)[k]
|
||||
grad = torch.chunk(grad, DEPTH, dim=-1)[j]
|
||||
grad = torch.chunk(grad, DEPTH, dim=-1)[i]
|
||||
|
||||
out.backward(grad)
|
||||
|
||||
B_master.backward(grad_master)
|
||||
A_grad = A_master.grad
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=0)[i]
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=-1)[k]
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=0)[j]
|
||||
logger.info('Rank {} ATB backward (A_grad): {}'.format(
|
||||
rank, check_equal(A_grad, A.grad)))
|
||||
|
||||
C_grad = C_master.grad
|
||||
C_grad = torch.chunk(C_grad, DEPTH, dim=0)[i]
|
||||
C_grad = torch.chunk(C_grad, DEPTH, dim=-1)[j]
|
||||
C_grad = torch.chunk(C_grad, DEPTH, dim=0)[k]
|
||||
logger.info('Rank {} ATB backward (B_grad): {}'.format(
|
||||
rank, check_equal(C_grad, C.grad)))
|
||||
|
||||
|
||||
def check_add():
|
||||
rank = torch.distributed.get_rank()
|
||||
logger = get_dist_logger()
|
||||
dtype = torch.float
|
||||
|
||||
j = A_rank = global_context.get_local_rank(ParallelMode.PARALLEL_3D_INPUT)
|
||||
i = B_rank = global_context.get_local_rank(ParallelMode.PARALLEL_3D_WEIGHT)
|
||||
k = C_rank = global_context.get_local_rank(ParallelMode.PARALLEL_3D_OUTPUT)
|
||||
device = get_current_device()
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, HIDDEN_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=get_current_device())
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = torch.chunk(A_master, DEPTH, dim=0)[i]
|
||||
A = torch.chunk(A, DEPTH, dim=-1)[k]
|
||||
A = torch.chunk(A, DEPTH, dim=0)[j]
|
||||
A = A.clone()
|
||||
A.requires_grad = True
|
||||
|
||||
bias_shape = (HIDDEN_SIZE, )
|
||||
bias_master = torch.randn(bias_shape,
|
||||
dtype=dtype,
|
||||
device=get_current_device())
|
||||
torch.distributed.broadcast(bias_master, src=0)
|
||||
bias = torch.chunk(bias_master, DEPTH)[j]
|
||||
bias = torch.chunk(bias, DEPTH)[i]
|
||||
bias = bias.clone()
|
||||
bias.requires_grad = True
|
||||
|
||||
out = Add_3D.apply(A, bias, DEPTH, ParallelMode.PARALLEL_3D_INPUT,
|
||||
ParallelMode.PARALLEL_3D_WEIGHT,
|
||||
ParallelMode.PARALLEL_3D_OUTPUT)
|
||||
|
||||
A_master = A_master.clone()
|
||||
A_master.requires_grad = True
|
||||
bias_master = bias_master.clone()
|
||||
bias_master.requires_grad = True
|
||||
C_master = A_master + bias_master
|
||||
C = torch.chunk(C_master, DEPTH, dim=0)[i]
|
||||
C = torch.chunk(C, DEPTH, dim=-1)[k]
|
||||
C = torch.chunk(C, DEPTH, dim=0)[j]
|
||||
|
||||
logger.info('Rank {} Add forward: {}'.format(rank, check_equal(out, C)))
|
||||
|
||||
grad_shape = C_master.shape
|
||||
grad_master = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(grad_master, src=0)
|
||||
grad = torch.chunk(grad_master, DEPTH, dim=0)[i]
|
||||
grad = torch.chunk(grad, DEPTH, dim=-1)[k]
|
||||
grad = torch.chunk(grad, DEPTH, dim=0)[j]
|
||||
|
||||
out.backward(grad)
|
||||
|
||||
C_master.backward(grad_master)
|
||||
A_grad = A_master.grad
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=0)[i]
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=-1)[k]
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=0)[j]
|
||||
logger.info('Rank {} Add backward (A_grad): {}'.format(
|
||||
rank, check_equal(A_grad, A.grad)))
|
||||
|
||||
if j == k:
|
||||
bias_grad = bias_master.grad
|
||||
bias_grad = torch.chunk(bias_grad, DEPTH)[j]
|
||||
bias_grad = torch.chunk(bias_grad, DEPTH)[i]
|
||||
logger.info('Rank {} Add backward (b_grad): {}'.format(
|
||||
rank, check_equal(bias_grad, bias.grad)))
|
||||
else:
|
||||
logger.info('Rank {} Add backward (b_grad): {}'.format(
|
||||
rank,
|
||||
# np.count_nonzero(bias.grad.detach().cpu().numpy()) == 0))
|
||||
bias.grad is None))
|
||||
|
||||
|
||||
def check_mul():
|
||||
rank = torch.distributed.get_rank()
|
||||
logger = get_dist_logger()
|
||||
dtype = torch.float
|
||||
|
||||
j = A_rank = global_context.get_local_rank(ParallelMode.PARALLEL_3D_INPUT)
|
||||
i = B_rank = global_context.get_local_rank(ParallelMode.PARALLEL_3D_WEIGHT)
|
||||
k = C_rank = global_context.get_local_rank(ParallelMode.PARALLEL_3D_OUTPUT)
|
||||
device = get_current_device()
|
||||
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, HIDDEN_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=get_current_device())
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = torch.chunk(A_master, DEPTH, dim=0)[i]
|
||||
A = torch.chunk(A, DEPTH, dim=-1)[k]
|
||||
A = torch.chunk(A, DEPTH, dim=0)[j]
|
||||
A = A.clone()
|
||||
A.requires_grad = True
|
||||
|
||||
bias_shape = (HIDDEN_SIZE, )
|
||||
bias_master = torch.randn(bias_shape,
|
||||
dtype=dtype,
|
||||
device=get_current_device())
|
||||
torch.distributed.broadcast(bias_master, src=0)
|
||||
bias = torch.chunk(bias_master, DEPTH)[j]
|
||||
bias = torch.chunk(bias, DEPTH)[i]
|
||||
bias = bias.clone()
|
||||
bias.requires_grad = True
|
||||
|
||||
out = Mul_3D.apply(A, bias, DEPTH, ParallelMode.PARALLEL_3D_INPUT,
|
||||
ParallelMode.PARALLEL_3D_WEIGHT,
|
||||
ParallelMode.PARALLEL_3D_OUTPUT)
|
||||
|
||||
A_master = A_master.clone()
|
||||
A_master.requires_grad = True
|
||||
bias_master = bias_master.clone()
|
||||
bias_master.requires_grad = True
|
||||
C_master = torch.mul(A_master, bias_master)
|
||||
C = torch.chunk(C_master, DEPTH, dim=0)[i]
|
||||
C = torch.chunk(C, DEPTH, dim=-1)[k]
|
||||
C = torch.chunk(C, DEPTH, dim=0)[j]
|
||||
|
||||
logger.info('Rank {} Mul forward: {}'.format(rank, check_equal(out, C)))
|
||||
|
||||
grad_shape = C_master.shape
|
||||
grad_master = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(grad_master, src=0)
|
||||
grad = torch.chunk(grad_master, DEPTH, dim=0)[i]
|
||||
grad = torch.chunk(grad, DEPTH, dim=-1)[k]
|
||||
grad = torch.chunk(grad, DEPTH, dim=0)[j]
|
||||
|
||||
out.backward(grad)
|
||||
|
||||
C_master.backward(grad_master)
|
||||
A_grad = A_master.grad
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=0)[i]
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=-1)[k]
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=0)[j]
|
||||
logger.info('Rank {} Mul backward (A_grad): {}'.format(
|
||||
rank, check_equal(A_grad, A.grad)))
|
||||
|
||||
if j == k:
|
||||
bias_grad = bias_master.grad
|
||||
bias_grad = torch.chunk(bias_grad, DEPTH)[j]
|
||||
bias_grad = torch.chunk(bias_grad, DEPTH)[i]
|
||||
logger.info('Rank {} Mul backward (b_grad): {}'.format(
|
||||
rank, check_equal(bias_grad, bias.grad)))
|
||||
else:
|
||||
logger.info('Rank {} Mul backward (b_grad): {}'.format(
|
||||
rank,
|
||||
# np.count_nonzero(bias.grad.detach().cpu().numpy()) == 0))
|
||||
bias.grad is None))
|
||||
|
||||
|
||||
def check_sum():
|
||||
rank = torch.distributed.get_rank()
|
||||
logger = get_dist_logger()
|
||||
dtype = torch.float
|
||||
|
||||
j = A_rank = global_context.get_local_rank(ParallelMode.PARALLEL_3D_INPUT)
|
||||
i = B_rank = global_context.get_local_rank(ParallelMode.PARALLEL_3D_WEIGHT)
|
||||
k = C_rank = global_context.get_local_rank(ParallelMode.PARALLEL_3D_OUTPUT)
|
||||
device = get_current_device()
|
||||
|
||||
# tensor
|
||||
A_shape = (BATCH_SIZE, SEQ_LENGTH, HIDDEN_SIZE)
|
||||
A_master = torch.randn(A_shape, dtype=dtype, device=get_current_device())
|
||||
torch.distributed.broadcast(A_master, src=0)
|
||||
A = torch.chunk(A_master, DEPTH, dim=0)[i]
|
||||
A = torch.chunk(A, DEPTH, dim=-1)[k]
|
||||
A = torch.chunk(A, DEPTH, dim=0)[j]
|
||||
A = A.clone()
|
||||
A.requires_grad = True
|
||||
|
||||
out_tensor = Sum_3D.apply(A, -1, DEPTH, ParallelMode.PARALLEL_3D_OUTPUT)
|
||||
|
||||
A_master = A_master.clone()
|
||||
A_master.requires_grad = True
|
||||
C_master = torch.sum(A_master, dim=-1)
|
||||
C = torch.chunk(C_master, DEPTH, dim=0)[i]
|
||||
C = torch.chunk(C, DEPTH, dim=0)[j]
|
||||
logger.info('Rank {} Sum forward: {}'.format(rank,
|
||||
check_equal(out_tensor, C)))
|
||||
|
||||
grad_shape = C_master.shape
|
||||
grad_master = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(grad_master, src=0)
|
||||
grad = torch.chunk(grad_master, DEPTH, dim=0)[i]
|
||||
grad = torch.chunk(grad, DEPTH, dim=0)[j]
|
||||
|
||||
out_tensor.backward(grad / DEPTH)
|
||||
|
||||
C_master.backward(grad_master)
|
||||
A_grad = A_master.grad
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=0)[i]
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=-1)[k]
|
||||
A_grad = torch.chunk(A_grad, DEPTH, dim=0)[j]
|
||||
logger.info('Rank {} Sum backward: {}'.format(rank,
|
||||
check_equal(A_grad, A.grad)))
|
||||
|
||||
|
||||
def check_reduce():
|
||||
rank = torch.distributed.get_rank()
|
||||
logger = get_dist_logger()
|
||||
dtype = torch.float
|
||||
|
||||
j = A_rank = global_context.get_local_rank(ParallelMode.PARALLEL_3D_INPUT)
|
||||
i = B_rank = global_context.get_local_rank(ParallelMode.PARALLEL_3D_WEIGHT)
|
||||
k = C_rank = global_context.get_local_rank(ParallelMode.PARALLEL_3D_OUTPUT)
|
||||
device = get_current_device()
|
||||
|
||||
# scaler
|
||||
B_shape = (DEPTH * DEPTH, DEPTH)
|
||||
B_master = torch.randn(B_shape, dtype=dtype, device=get_current_device())
|
||||
torch.distributed.broadcast(B_master, src=0)
|
||||
B = torch.chunk(B_master, DEPTH, dim=0)[i]
|
||||
B = torch.chunk(B, DEPTH, dim=-1)[k]
|
||||
B = torch.chunk(B, DEPTH, dim=0)[j]
|
||||
B = torch.squeeze(B)
|
||||
B = B.clone()
|
||||
B.requires_grad = True
|
||||
|
||||
out_scaler = Reduce_3D.apply(B, 0, DEPTH, ParallelMode.PARALLEL_3D_OUTPUT)
|
||||
out_scaler = Reduce_3D.apply(out_scaler, 0, DEPTH,
|
||||
ParallelMode.PARALLEL_3D_INPUT)
|
||||
out_scaler = Reduce_3D.apply(out_scaler, 0, DEPTH,
|
||||
ParallelMode.PARALLEL_3D_WEIGHT)
|
||||
|
||||
B_master = B_master.clone()
|
||||
B_master.requires_grad = True
|
||||
D = torch.sum(B_master)
|
||||
logger.info('Rank {} Reduce forward: {}'.format(rank,
|
||||
check_equal(out_scaler,
|
||||
D)))
|
||||
|
||||
grad_shape = D.shape
|
||||
grad_master = torch.randn(grad_shape, dtype=dtype, device=device)
|
||||
torch.distributed.broadcast(grad_master, src=0)
|
||||
|
||||
out_scaler.backward(grad_master)
|
||||
|
||||
D.backward(grad_master)
|
||||
B_grad = B_master.grad
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=0)[i]
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=-1)[k]
|
||||
B_grad = torch.chunk(B_grad, DEPTH, dim=0)[j]
|
||||
B_grad = torch.squeeze(B_grad)
|
||||
logger.info('Rank {} Reduce backward: {}'.format(
|
||||
rank, check_equal(B_grad, B.grad)))
|
@@ -4,12 +4,14 @@
|
||||
import torch
|
||||
|
||||
DEPTH = 2
|
||||
BATCH_SIZE = 512
|
||||
SEQ_LENGTH = 128
|
||||
HIDDEN_SIZE = 512
|
||||
NUM_CLASSES = 1000
|
||||
NUM_BLOCKS = 6
|
||||
IMG_SIZE = 224
|
||||
BATCH_SIZE = 8
|
||||
SEQ_LENGTH = 8
|
||||
HIDDEN_SIZE = 8
|
||||
NUM_CLASSES = 8
|
||||
NUM_BLOCKS = 2
|
||||
IMG_SIZE = 16
|
||||
|
||||
def check_equal(A, B):
|
||||
return torch.allclose(A, B, rtol=1e-4, atol=1e-2)
|
||||
eq = torch.allclose(A, B, rtol=1e-3, atol=1e-2)
|
||||
assert eq
|
||||
return eq
|
||||
|
@@ -1,54 +1,34 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- encoding: utf-8 -*-
|
||||
from functools import partial
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
import torch.multiprocessing as mp
|
||||
from colossalai.initialize import launch, get_default_parser
|
||||
from colossalai.core import global_context as gpc
|
||||
from colossalai.initialize import launch
|
||||
|
||||
from checks_3d.check_layer_3d import *
|
||||
from checks_3d.check_operation_3d import *
|
||||
from colossalai.logging import get_dist_logger
|
||||
from functools import partial
|
||||
|
||||
CONFIG = dict(parallel=dict(pipeline=1, tensor=dict(mode='3d', size=8)),
|
||||
seed=0)
|
||||
|
||||
|
||||
# def check_operations():
|
||||
# check_AB()
|
||||
# check_ABT()
|
||||
# check_ATB()
|
||||
# check_add()
|
||||
# check_mul()
|
||||
# check_sum()
|
||||
CONFIG = dict(
|
||||
parallel=dict(
|
||||
pipeline=1,
|
||||
tensor=dict(mode='3d', size=8),
|
||||
),
|
||||
seed=42,
|
||||
)
|
||||
|
||||
|
||||
def check_layer():
|
||||
logger = get_dist_logger()
|
||||
liear_fwd_time, linear_bwd_time = check_linear()
|
||||
norm_fwd_time, norm_bwd_time = check_layernorm()
|
||||
attn_fwd_time, attn_bwd_time = check_attention()
|
||||
mlp_fwd_time, mlp_bwd_time = check_mlp()
|
||||
head_fwd_time, head_bwd_time = check_head()
|
||||
embed_fwd_time, embed_bwd_time = check_embed()
|
||||
loss_fwd_time, loss_bwd_time = check_loss()
|
||||
block_fwd_time = norm_fwd_time + attn_fwd_time + norm_fwd_time + mlp_fwd_time
|
||||
block_bwd_time = norm_bwd_time + attn_bwd_time + norm_bwd_time + mlp_bwd_time
|
||||
fwd_time = embed_fwd_time + NUM_BLOCKS * block_fwd_time + norm_fwd_time + head_fwd_time + loss_fwd_time
|
||||
bwd_time = embed_bwd_time + NUM_BLOCKS * block_bwd_time + norm_bwd_time + head_bwd_time + loss_bwd_time
|
||||
logger.info('ViT forward time: {:.3f} s | backward time: {:.3f} s'.format(
|
||||
fwd_time, bwd_time),
|
||||
ranks=[0])
|
||||
check_linear()
|
||||
check_layernorm()
|
||||
check_classifier()
|
||||
# check_embed()
|
||||
# check_loss()
|
||||
|
||||
|
||||
def check_layer_and_operation(rank, world_size):
|
||||
launch(config=CONFIG,
|
||||
rank=rank,
|
||||
world_size=world_size,
|
||||
host='localhost',
|
||||
port=29923,
|
||||
backend='nccl')
|
||||
|
||||
launch(config=CONFIG, rank=rank, world_size=world_size, host='localhost', port=29923, backend='nccl')
|
||||
check_layer()
|
||||
gpc.destroy()
|
||||
torch.cuda.empty_cache()
|
||||
|
Reference in New Issue
Block a user