forked from kyegomez/LongNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
38 lines (27 loc) · 801 Bytes
/
example.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
import timeit
import torch
from LongNet.attention import DilatedAttention
#model config
d_model = 512
num_heads = 8
dilation_rate = 2
segment_size = 64
device = "cuda:0"
dtype=torch.float16
#input data
batch_size = 32
seq_len = 8192
#create model and data
model = DilatedAttention(d_model, num_heads, dilation_rate, segment_size).to(device)
x = torch.randn((batch_size, seq_len, d_model), device=device, dtype=dtype)
#test forward pass
with torch.no_grad():
output = model(x)
print(f"Output shape: {output.shape}") # expected (batch_size, seq_Len)
#benchmark model
num_runs = 1000
start_time = timeit.default_timer()
for _ in range(num_runs):
model(x)
elapsed_time = timeit.default_timer() - start_time
print(f"Average forward pass time: {elapsed_time / num_runs:.6f} seconds")