-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
108 lines (95 loc) · 3.82 KB
/
test.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
import time
import torch
from tqdm.auto import tqdm
from model import GPT, GPTConfig
from data.prepare_data import get_dataloader
def test_model(config_path="config/medium.yaml"):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Instantiating model from {config_path}.")
config = GPTConfig.from_yaml(config_path)
model = GPT(config)
model.to(device)
print("Testing model forward pass.")
example_input = torch.randint(0, config.vocab_size, (4, config.seq_len))
example_input = example_input.to(device)
start = time.time()
out1 = model(example_input)
print("Forwarded initial batch in", time.time() - start, "seconds")
assert out1.shape == (4, config.seq_len, config.vocab_size), "Output shape is incorrect."
# delete to save memory
del out1
print("Model forward pass test passed.")
def test_compiled_model(config_path="config/medium.yaml"):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Instantiating model from {config_path}.")
config = GPTConfig.from_yaml(config_path)
model = GPT(config)
model.to(device)
compiled = torch.compile(model, mode="max-autotune")
print("torch.compile() ran successfully.")
print("Device of compiled model:", compiled.named_parameters().__next__()[1].device)
print("Testing initial model forward pass.")
example_input = torch.randint(0, config.vocab_size, (4, config.seq_len))
example_input = example_input.to(device)
start = time.time()
out1 = compiled(example_input)
print("Forwarded first batch in", time.time() - start, "seconds")
assert out1.shape == (4, config.seq_len, config.vocab_size), "Output shape is incorrect."
# delete to save memory
del out1
print("Compiled model test passed.")
# from pytorch docs
def timed(fn):
start = torch.cuda.Event(enable_timing=True)
end = torch.cuda.Event(enable_timing=True)
start.record()
result = fn()
end.record()
torch.cuda.synchronize()
return result, start.elapsed_time(end) / 1000
def run_forward_passes(config, model, device, num_passes=100):
times = []
for i in tqdm(range(num_passes)):
random_tensor = torch.randint(0, config.vocab_size, (4, config.seq_len))
random_tensor = random_tensor.to(device)
out, time = timed(lambda: model(random_tensor))
times.append(time)
del out
return times
def test_compiled_speedup(config_path="config/medium.yaml"):
# Create model and compiled model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Instantiating model from {config_path}.")
config = GPTConfig.from_yaml(config_path)
model = GPT(config)
model.to(device)
compiled = torch.compile(model, mode="max-autotune")
print("torch.compile() ran successfully.")
# Run forward passes on normal model
print("Warming up...")
run_forward_passes(config, model, device, num_passes=100)
print("Testing normal forward pass.")
normal_times = run_forward_passes(config, model, device)
print(torch.mean(torch.tensor(normal_times)).item())
# Run forward passes on compiled model
print("Warming up...")
run_forward_passes(config, compiled, device, num_passes=100)
print("Testing compiled forward pass.")
compiled_times = run_forward_passes(config, compiled, device)
print(torch.mean(torch.tensor(compiled_times)).item())
def test_dataloader():
loader = get_dataloader()
i = 0
for X, Y in loader:
print(X.shape, Y.shape)
i += 1
if i > 10:
break
if __name__ == "__main__":
#test_model()
if torch.cuda.is_available():
test_compiled_model()
test_compiled_speedup()
else:
print("Skipping compiled model tests because CUDA is not available.")
test_dataloader()