-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_fetcher_test.py
More file actions
112 lines (86 loc) · 3.55 KB
/
data_fetcher_test.py
File metadata and controls
112 lines (86 loc) · 3.55 KB
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
"""
Create tests for the data_fetcher
Usage:
python3 data_fetcher_test.py <n_inputs> <n_outputs> <bit_width> <data_width>
Output format:
The .dat file has the following format (example with two inputs, i_n n=1,2):
<fetch> <i_1> <i_2> <w> <b> <raddr> <out_count> <i_1exp> <i_2exp> <wexp> <bexp>
"""
import argparse
import random
from test_module import write_dat # noqa: E402
random.seed(7357)
parser = argparse.ArgumentParser(description="Testing script for data_fetcher")
parser.add_argument("-inp", help="Number of inputs", type=int)
parser.add_argument("-node", help="Number of nodes in the layer", type=int)
parser.add_argument("-bw", help="Bit width", type=int)
parser.add_argument("-dw", help="Data width", type=int)
args = parser.parse_args()
PREV_TEST = 3
AFTER_TEST = 2
N_TESTS = PREV_TEST + args.inp * args.node + AFTER_TEST
RANGE = int(2 ** (args.bw - 1) - 1)
# Construct fetch
# Start after two cycles with fetch = 0
fetch = [0, 0, 1]
# The rest of the fetches is 0
fetch.extend([0 for _ in range(args.node * args.dw)])
fetch.extend([0, 0])
# Construct data
# Generate a random data block with the dimensions (args.dw, args.inp)
data_block = [[random.randint(-RANGE, RANGE) for _ in range(args.inp)] for _ in range(args.dw)]
# Data before the fetching
data = [[random.randint(-RANGE, RANGE) for _ in range(args.inp)] for _ in range(PREV_TEST)]
# Add the data block args.node times
for _ in range(args.node):
data.extend(data_block)
# Data after the fetching process has finished
data.extend([[random.randint(-RANGE, RANGE) for _ in range(args.inp)] for _ in range(AFTER_TEST)])
# Construct weights and biases
# Model of the weights and biases in the ROM
weight = [[random.randint(-RANGE, RANGE) for _ in range(args.inp)] for _ in range(PREV_TEST)]
bias = [random.randint(-RANGE, RANGE) for _ in range(PREV_TEST)]
# Cycle through the weights and biases
for _ in range(args.node):
rand_list = [random.randint(-RANGE, RANGE) for _ in range(args.inp)]
rand_n = random.randint(-RANGE, RANGE)
for _ in range(args.dw):
weight.append(rand_list)
bias.append(rand_n)
# Weights and biases after the fetching process
weight.extend([[random.randint(-RANGE, RANGE) for _ in range(args.inp)] for _ in range(AFTER_TEST)])
bias.extend([random.randint(-RANGE, RANGE) for _ in range(AFTER_TEST)])
# Construct raddr_exp
# raddr before the fetching
raddr_exp = [0 for _ in range(PREV_TEST - 1)]
# Cycle through the range of args.dw to fetch every sample
raddr_exp.extend([i % args.dw for i in range(args.dw * args.node)])
# raddr after the fetching process
raddr_exp.extend([0 for _ in range(AFTER_TEST + 1)])
# Construct out_count_exp
# out_count before the fetching
out_count_exp = [0 for _ in range(PREV_TEST - 1)]
# Cycle through args.node with args.dw times the current output number
for i in range(args.node):
out_count_exp.extend([i for j in range(args.dw)])
# out_count after the fetching process
out_count_exp.extend([0 for _ in range(AFTER_TEST + 1)])
# Output data and weights before the fetching
zeros = [[0 for _ in range(args.inp)] for _ in range(PREV_TEST + 1)]
# Take only the parts that should be the outputs
data_exp = zeros + data[3:-2] + [[0 for _ in range(args.inp)] for _ in range(AFTER_TEST - 1)]
weight_exp = zeros + weight[3:-2] + [[0 for _ in range(args.inp)] for _ in range(AFTER_TEST - 1)]
bias_exp = [0 for _ in range(PREV_TEST + 1)] + bias[3:-2] + [0 for _ in range(AFTER_TEST - 1)]
write_dat(
"data_fetcher.dat",
N_TESTS,
fetch,
data,
weight,
bias,
raddr_exp,
out_count_exp,
data_exp,
weight_exp,
bias_exp,
)