-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_transformer.py
273 lines (226 loc) · 9.87 KB
/
test_transformer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import unittest
import argparse
import sys
import copy
# from your_model_file import ModelA, ModelB # Import your models from your model file
import numpy as np
import torch
import torch.nn as nn
import torch.distributed as dist
import torch.multiprocessing as mp
import torch.nn.functional as F
from fairscale.internal import torch_version
from models import transformer_lm_fsdp as transformer_lm
from fairscale.optim import GradScaler
from typing import TYPE_CHECKING, Any, Callable, Dict, Generator, List, Optional, Tuple, Union
RPC_PORT = 29501
from data_parallel import ParallelEmbedding, ParallelMultiheadAttention, ColumnParallelLinear
from data_parallel import WeightParallelEmbedding, WeightParallelLinear, WeightParallelMultiheadAttention
rtp_stream = torch.cuda.Stream()
def get_ColumnParallelLinear_model(args, device, config):
"""Get language model(based on GPT-2) used for sequence prediction."""
in_features = config["in_features"]
out_features = config["out_features"]
rank = torch.distributed.get_rank()
world_size = torch.distributed.get_world_size()
return ColumnParallelLinear(in_features, out_features, world_size=world_size, rank=rank).to(device)
def init_random_seed(seed: int):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
np.random.seed(seed)
def objects_are_equal(
a: Any,
b: Any,
raise_exception: bool = False,
dict_key: Optional[str] = None,
rtol: Optional[float] = None,
atol: Optional[float] = None,
) -> bool:
"""
Test that two objects are equal. Tensors are compared to ensure matching
size, dtype, device and values.
"""
if type(a) is not type(b):
if raise_exception:
raise ValueError(f"type mismatch {type(a)} vs. {type(b)}")
return False
if isinstance(a, dict):
if set(a.keys()) != set(b.keys()):
if raise_exception:
raise ValueError(f"keys mismatch {a.keys()} vs. {b.keys()}")
return False
for k in a.keys():
if not objects_are_equal(a[k], b[k], raise_exception, k):
return False
return True
elif isinstance(a, (list, tuple, set)):
if len(a) != len(b):
if raise_exception:
raise ValueError(f"length mismatch {len(a)} vs. {len(b)}")
return False
return all(objects_are_equal(x, y, raise_exception) for x, y in zip(a, b))
elif torch.is_tensor(a):
try:
# assert_close doesn't strictly test shape, dtype and device
shape_dtype_device_match = a.size() == b.size() and a.dtype == b.dtype and a.device == b.device
if not shape_dtype_device_match:
if raise_exception:
msg = f"sizes: {a.size()} vs. {b.size()}, "
msg += f"types: {a.dtype} vs. {b.dtype}, "
msg += f"device: {a.device} vs. {b.device}"
raise AssertionError(msg)
else:
return False
# assert_close.
if torch_version() < (1, 12, 0):
torch.testing.assert_allclose(a, b, rtol=rtol, atol=atol)
else:
torch.testing.assert_close(a, b, rtol=rtol, atol=atol)
return True
except (AssertionError, RuntimeError) as e:
if raise_exception:
if dict_key and isinstance(e, AssertionError):
# Add dict key to the assertion error.
msg = e.args[0]
new_msg = f"For dict key '{dict_key}': {msg}"
raise AssertionError(new_msg) from None
else:
raise e
else:
return False
else:
return a == b
def _gather(input_: torch.Tensor, dim) -> torch.Tensor:
# Bypass the function if we are using only 1 GPU.
if torch.distributed.get_world_size() == 1:
return input_
# Size and dimension.
last_dim = input_.dim() - 1
rank = torch.distributed.get_rank()
world_size = torch.distributed.get_world_size()
tensor_list = [torch.empty_like(input_) for _ in range(world_size)]
tensor_list[rank] = input_
torch.distributed.all_gather(tensor_list, input_)
# Note: torch.cat already creates a contiguous tensor.
output = torch.cat(tensor_list, dim=dim).contiguous()
return output
class MLP(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(MLP, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.fc2 = nn.Linear(hidden_size, num_classes)
def forward(self, x):
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
config = {
"vocab_size": 10000,
"ninp": 2048, # embedding dimension
"nhid": 2048, # the dimension of the feedforward network model in nn.TransformerEncoder
"nhead": 32, # the number of heads in the multiheadattention models
"dropout": 0,
"initrange": 0.1,
"scaler": GradScaler(),
"clip_value": 0.05,
"num_decoder_layers": 10,
"seq_len": 32,
}
class TestIdenticalOutputs(unittest.TestCase):
def setUp(self):
pass
def assert_grad(self, grad1, grad2):
assert objects_are_equal(grad1, grad2)
def test_identical_outputs(self):
# Example input for the models
num_samples = 32
num_embeddings = 32
embedding_dim = 32
learning_rate = 0.001
rank = torch.distributed.get_rank()
world_size = torch.distributed.get_world_size()
torch.cuda.set_device(args.local_rank)
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
init_random_seed(0)
data = torch.randint(low=0, high=7, size=(num_embeddings, num_samples), dtype=torch.long).cuda()
labels = torch.randint(low=0, high=7, size=(num_embeddings, num_samples), dtype=torch.long).cuda()
ninp = config["ninp"]
nhead = config["nhead"]
initrange = config["initrange"]
dropout = config["dropout"]
vocab_size = config["vocab_size"]
nhid = config["nhid"]
ndecoder = config["num_decoder_layers"] - 2
dropout = 0
sub_embedding_dim = ninp // world_size
model = transformer_lm.TransformerLM(vocab_size, ninp, nhead, nhid, dropout, initrange, ndecoder).to(device)
model.eval()
Col_model = copy.deepcopy(model)
Weight_model = copy.deepcopy(model)
outputs = model(data)
criterion = nn.CrossEntropyLoss().cuda()
loss = criterion(outputs.view(-1, vocab_size), labels.view(-1))
loss.backward()
ref_grads = [p.grad.detach().clone() for p in model.parameters()]
test_names = [n for n,_ in model.named_parameters()]
for param in model.parameters():
param.grad = None
# sub_sample = num_samples // world_size
# data_list = torch.split(data, sub_sample, dim=1)
# cur_data = data_list[rank]
# output_list = torch.split(outputs, sub_sample, dim=1)
# cur_output = output_list[rank]
# label_list = torch.split(labels, sub_sample, dim=1)
# cur_label = label_list[rank].contiguous()
# replace_embedding_layers(Weight_model, WeightParallelEmbedding, world_size, rank)
# replace_linear_layers(Weight_model, WeightParallelLinear, world_size, rank)
# replace_attention_layers(Weight_model, WeightParallelMultiheadAttention, world_size, rank)
# Weight_model.eval()
# Weight_model_output = Weight_model(cur_data)
# # print(Weight_model_output - cur_output)
# assert(torch.max(torch.abs(Weight_model_output - cur_output)) < 1e-4)
# Weight_loss = criterion(Weight_model_output.view(-1, vocab_size), cur_label.view(-1)) / 2
# Weight_loss.backward()
# Weight_grads = []
# orders = []
# for i in range(3):
# orders += [j * 3 + i for j in range(world_size)]
# tmp = []
# for param_name, param in Weight_model.named_parameters():
# tmp.append(param_name)
# if 'in_proj' in param_name:
# grad = param.grad.clone()
# grad = _gather(grad, dim=0)
# grad_list = torch.split(grad, sub_embedding_dim, dim=0)
# grad_list = [grad_list[i] for i in orders]
# grad = torch.cat(grad_list, dim=0).contiguous()
# Weight_grads.append(grad)
# elif 'out_proj' in param_name and 'weight' in param_name:
# grad = param.grad.clone()
# grad = _gather(grad, dim=1)
# Weight_grads.append(grad)
# elif 'out_proj' in param_name and 'bias' in param_name:
# grad = param.grad.clone()
# Weight_grads.append(grad)
# elif 'embedding' in param_name:
# grad = param.grad.clone()
# grad = _gather(grad, dim=1)
# Weight_grads.append(grad)
# else:
# grad = param.grad.clone()
# grad = _gather(grad, dim=0)
# Weight_grads.append(grad)
# for grad1, grad2, name, name2 in zip(ref_grads, Weight_grads, tmp, test_names):
# if grad1.shape == grad2.shape:
# assert(torch.max(torch.abs(grad1 - grad2)) < 1e-3)
parser = argparse.ArgumentParser()
parser.add_argument('--local-rank', type=int, default=0)
args, remaining = parser.parse_known_args()
sys.argv[1:] = remaining
# This allows the test to be run if the file is run as a script
if __name__ == '__main__':
num_devices = torch.cuda.device_count() if torch.cuda.is_available() else 1
world_size = num_devices
torch.distributed.init_process_group(
backend="nccl", rank=args.local_rank, world_size=world_size
)
unittest.main()