-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore.py
129 lines (96 loc) · 4.59 KB
/
Core.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
import numpy as np
class ColumnRunner:
def __init__(self, x_stream, w_stream, prev_prefix_y_stream):
self._x_stream = x_stream
self._w_stream = w_stream
self._prev_prefix_y_stream = prev_prefix_y_stream
self._combinatorial_value = -1
self._prefix_y_value = -1
def propagate_combinatorial_logic(self):
self._combinatorial_value = sum(self._x_stream() ^ self._w_stream()) +\
self._prev_prefix_y_stream()
def tick(self):
self._prefix_y_value = self._combinatorial_value
def prefix_y_stream(self):
return lambda: self._prefix_y_value
class ColumnTrain:
def __init__(self, runners, word_size, matrix_shape, ram_access,
x_bus_stream, offset_stream):
assert len(x_bus_stream()) == runners * word_size
self.matrix_shape = matrix_shape
self.runners = runners
self.word_size = word_size
self._offset_stream = offset_stream
self._runners = [None] * runners
self._w_streams = [None] * runners
for i in range(runners):
x_stream = self._make_x_stream(x_bus_stream, i)
matrix_index = i*(runners + 1)*word_size
self._w_streams[i] = self._make_w_stream(ram_access, i)
prev_prefix_y_stream = \
self._runners[i-1].prefix_y_stream() if i > 0 else lambda: 0
self._runners[i] = ColumnRunner(
x_stream,
self._w_streams[i],
prev_prefix_y_stream
)
def _make_x_stream(self, x_bus_stream, i):
return lambda: x_bus_stream()[i*self.word_size:(i+1)*self.word_size]
def _make_w_stream(self, ram_access, i):
shard_length = self.matrix_shape[0]*self.matrix_shape[1] // self.runners
return lambda: ram_access(
((self._offset_stream()*self.word_size) % shard_length) + i*shard_length,
self.word_size)
def propagate_combinatorial_logic(self):
for runner in self._runners:
runner.propagate_combinatorial_logic()
def tick(self):
for runner in self._runners:
runner.tick()
def y_stream(self):
return self._runners[-1].prefix_y_stream()
class ColumnCore:
def __init__(self, runners, word_size, matrix_shape,
ram, ram_offset, x_bus_stream, reset_stream):
assert matrix_shape[0] % (runners * word_size) == 0
self.runners = runners
self.word_size = word_size
self._ram = ram
self._matrix_shape = matrix_shape
self._cycle_time = matrix_shape[0] * (matrix_shape[1] // (runners * word_size))
self._reset_stream = reset_stream
self._tick_counter = -1
self._next_tick_counter = -1
def rolling_x_bus_stream():
rolling_x_bus = []
for i in range(runners):
step = ((self._cycle_time + self._tick_counter - i) % self._cycle_time) // matrix_shape[0]
rolling_x_bus.append(x_bus_stream()[(step*runners + i)*word_size:(step*runners + i + 1)*word_size])
return np.array(rolling_x_bus).flatten()
self._train = ColumnTrain(
runners=runners,
word_size=word_size,
matrix_shape=matrix_shape,
ram_access=ram.get_values,
x_bus_stream=rolling_x_bus_stream,
offset_stream=lambda: ram_offset + self._tick_counter
)
self._y_accumulator_ram_offset = ram.add_content(np.zeros(matrix_shape[0], dtype=np.int))
def propagate_combinatorial_logic(self):
self._train.propagate_combinatorial_logic()
self._next_tick_counter = (self._tick_counter + 1) % self._cycle_time
def tick(self):
self._train.tick()
y_acc_offset = (self._tick_counter + self._matrix_shape[0] - self.runners + 1) % self._matrix_shape[0]
next_y_accumulator = 0
if (self._tick_counter + self._cycle_time - self.runners + 1) % self._cycle_time >= self._matrix_shape[0]:
next_y_accumulator += self._ram.get_values(self._y_accumulator_ram_offset + y_acc_offset, 1)[0]
next_y_accumulator += self._train.y_stream()()
self._ram.put_values(self._y_accumulator_ram_offset + y_acc_offset, np.array([next_y_accumulator]))
if self._reset_stream() == 1:
self._tick_counter = 0
else:
self._tick_counter = self._next_tick_counter
def y_stream(self):
y_acc_offset = ((self._tick_counter + self._matrix_shape[0] - self.runners) % self._matrix_shape[0])
return lambda: self._ram.get_values(self._y_accumulator_ram_offset + y_acc_offset, 1)