forked from altera-fpga/hls-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreaming_covariance_matrix.hpp
executable file
·311 lines (266 loc) · 11.9 KB
/
streaming_covariance_matrix.hpp
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#ifndef __STREAMING_COVARIANCE_MATRIX_HPP__
#define __STREAMING_COVARIANCE_MATRIX_HPP__
namespace fpga_linalg {
// This functor computes the columns x columns covariance matrix of a rows x
// columns input matrix A
// It uses the following formula:
// COV[i][j] = (T[i][j] - rows*mean[i]*mean[j]) /
// (sqrt(T[i][i] - rows*mean[i]*mean[i]) *
// sqrt(T[j][j] - rows*mean[j]*mean[j]))
// Where T is transpose(A)*A and mean[k] is the mean of the column k of the A
// matrix
template <typename T, // The datatype for the computation
unsigned rows, // Number of rows in the A matrices
unsigned columns, // Number of columns in the A matrices
unsigned pipe_size, // Number of elements read/write per pipe
// operation, the matrix is received through the
// pipe
// by blocks of size columns*columns.
typename InputPipe, // A matrix input pipe, receive pipe_size
// elements from the pipe with each read
typename OutputPipe, // Q matrix output pipe, send pipe_size
// elements to the pipe with each write
bool standardized=true
>
struct StreamingCovarianceMatrix {
void operator()() const {
static_assert(rows % columns == 0,
"The feature count must be a multiple of the samples count."
"This can be artificially achieved by increasing the number"
"of samples with no data.");
// Type used to store the matrices in the compute loop
using row_tuple = fpga_tools::NTuple<T, columns>;
// Number of matrix blocks to read from the pipe
constexpr int block_count = rows / columns;
// Break memories up to store 8 float numbers (32 bytes) per bank
constexpr short kBankwidth = pipe_size * sizeof(T);
constexpr unsigned short kNumBanks = columns / pipe_size;
// When specifying numbanks for a memory, it must be a power of 2.
// Unused banks will be automatically optimized away.
constexpr short kNumBanksNextPow2 =
fpga_tools::Pow2(fpga_tools::CeilLog2(kNumBanks));
// Copy a matrix from the pipe to a local memory
// Number of pipe reads of pipe_size required to read a full column
constexpr int kExtraIteration = (columns % pipe_size) != 0 ? 1 : 0;
constexpr int kLoopIterationPerRow = columns / pipe_size + kExtraIteration;
// Number of pipe reads of pipe_size to read all the matrices
constexpr int kLoopIterations = kLoopIterationPerRow * columns;
// We keep a replicate of the diagonal of T for improved memory access
// pattern over T
T t_matrix_diagonal_replicate[columns];
// Array to keep the means of all the A matrix columns
T means[columns];
// Array to keep the T matrix
[[intel::max_replicates(1)]] // NO-FORMAT: Attribute
[[intel::private_copies(4)]] // NO-FORMAT: Attribute
T t_matrix[columns * columns];
// We keep count of the current block number
int block = 0;
while (1) {
// Read the next matrix block into the a_load local memory
[[intel::numbanks(kNumBanksNextPow2)]] // NO-FORMAT: Attribute
[[intel::bankwidth(kBankwidth)]] // NO-FORMAT: Attribute
[[intel::max_replicates(1)]] // NO-FORMAT: Attribute
[[intel::private_copies(2)]] // NO-FORMAT: Attribute
row_tuple a_load[columns];
[[intel::initiation_interval(1)]] // NO-FORMAT: Attribute
for (int li = 0; li < kLoopIterations; li++) {
fpga_tools::NTuple<T, pipe_size> pipe_read = InputPipe::read();
int write_idx = li % kLoopIterationPerRow;
int a_col_index = li / kLoopIterationPerRow;
fpga_tools::UnrolledLoop<kLoopIterationPerRow>([&](auto k) {
fpga_tools::UnrolledLoop<pipe_size>([&](auto t) {
if (write_idx == k) {
if constexpr (k * pipe_size + t < columns) {
a_load[a_col_index].template get<k * pipe_size + t>() =
pipe_read.template get<t>();
}
}
// Delay data signals to create a vine-based data distribution
// to lower signal fanout.
pipe_read.template get<t>() =
sycl::ext::intel::fpga_reg(pipe_read.template get<t>());
});
write_idx = sycl::ext::intel::fpga_reg(write_idx);
});
} // for:li
// We are going to reuse the same column of the matrix multiple
// iterations in a row, so we keep it locally
row_tuple current_base_column;
row_tuple next_base_column;
// Compute the block T matrix and the partial means
// Arrays to keep all the data of the current block being computed
[[intel::max_replicates(1)]] // NO-FORMAT: Attribute
[[intel::private_copies(2)]] // NO-FORMAT: Attribute
T t_matrix_compute[columns * columns];
T t_matrix_diagonal_replicate_partial[columns];
T means_partial[columns];
int row = 0;
int column = 0;
for (int it = 0; it < columns * columns; it++) {
// Load the current column of the block
row_tuple current_column = a_load[column];
// Keep the current column in the local cache for future reuse
if (column == 0) {
if (column == row) {
current_base_column = current_column;
} else {
current_base_column = next_base_column;
}
} else if (column == (row + 1)) {
next_base_column = current_column;
}
// Compute the partial T value and the partial mean
T dot_product = 0;
T mean = 0;
fpga_tools::UnrolledLoop<columns>([&](auto t) {
dot_product += current_column.template get<t>() *
current_base_column.template get<t>();
mean += current_column.template get<t>();
});
// Update the partial result T matrix
t_matrix_compute[it] = dot_product;
// Adjust the mean as we only need to compute it once
if (row != 0) {
mean = 0;
}
mean /= rows;
// Update the partial means results
if (row == 0) {
means_partial[column] = mean;
}
// Update the partial diagonal replicates results
if (row == column) {
t_matrix_diagonal_replicate_partial[column] = dot_product;
}
// Update the current row and column indexes
if (column == columns - 1) {
column = 0;
row++;
} else {
column++;
}
}
// Update the global mean array and the diagonal replicates array with the
// partial results
#pragma unroll
for (size_t t = 0; t < columns; ++t) {
T mean_to_add = block == 0 ? 0 : means[t];
means[t] = means_partial[t] + mean_to_add;
T t_matrix_diagonal_replicate_to_add =
block == 0 ? 0 : t_matrix_diagonal_replicate[t];
t_matrix_diagonal_replicate[t] =
t_matrix_diagonal_replicate_partial[t] +
t_matrix_diagonal_replicate_to_add;
}
// For the computation of COV
[[intel::max_replicates(1)]] // NO-FORMAT: Attribute
[[intel::private_copies(2)]] // NO-FORMAT: Attribute
T cov_matrix_consume[columns][columns];
// Update the global T matrix with the partial results and copy the result
// to the cov_matrix_consume array for better memory structure in the
// computation of COV
for (row = 0; row < columns; row++) {
#pragma unroll
for (size_t column = 0; column < columns; ++column) {
T t_matrix_to_add = block == 0 ? 0 : t_matrix[row * columns + column];
T sum = t_matrix_compute[row * columns + column] + t_matrix_to_add;
t_matrix[row * columns + column] = sum;
cov_matrix_consume[row][column] = sum;
}
}
if (standardized) {
// cov_matrix_consume now contains the full matrix product of the transpose
// of A times A. mean now contains the mean of all the columns of A. We
// now need to compose all of these results to get the covariance matrix
[[intel::numbanks(kNumBanksNextPow2)]] // NO-FORMAT: Attribute
[[intel::bankwidth(kBankwidth)]] // NO-FORMAT: Attribute
[[intel::max_replicates(1)]] // NO-FORMAT: Attribute
[[intel::private_copies(2)]] // NO-FORMAT: Attribute
T cov_matrix_std[columns][columns];
int row = 0;
int column = 0;
for (int it = 0; it < columns * columns; it++) {
T numerator = cov_matrix_consume[row][column] -
(rows * means[row] * means[column]);
T denominator = sycl::sqrt((t_matrix_diagonal_replicate[row] -
(rows * means[row] * means[row])) *
(t_matrix_diagonal_replicate[column] -
(rows * means[column] * means[column])));
cov_matrix_std[row][column] = numerator / denominator;
if (column == columns - 1) {
column = 0;
row++;
} else {
column++;
}
}
// Write the standardized covariance matrix to the output pipe
// [[intel::initiation_interval(1)]] // NO-FORMAT: Attribute
for (int li = 0; li < kLoopIterations; li++) {
int column_iter = li % kLoopIterationPerRow;
bool get[kLoopIterationPerRow];
#pragma unroll
for (size_t k = 0; k < kLoopIterationPerRow; ++k) {
get[k] = column_iter == k;
column_iter = sycl::ext::intel::fpga_reg(column_iter);
}
fpga_tools::NTuple<T, pipe_size> pipe_write;
fpga_tools::UnrolledLoop<kLoopIterationPerRow>([&](auto t) {
fpga_tools::UnrolledLoop<pipe_size>([&](auto k) {
if constexpr (t * pipe_size + k < columns) {
pipe_write.template get<k>() =
get[t]
? cov_matrix_std[li / kLoopIterationPerRow][t * pipe_size + k]
: sycl::ext::intel::fpga_reg(
pipe_write.template get<k>());
}
});
});
if (block == block_count - 1) {
OutputPipe::write(pipe_write);
}
}
if (block == block_count - 1) {
block = 0;
} else {
block++;
}
} else {
// Write the standardized covariance matrix to the output pipe
// [[intel::initiation_interval(1)]] // NO-FORMAT: Attribute
for (int li = 0; li < kLoopIterations; li++) {
int column_iter = li % kLoopIterationPerRow;
bool get[kLoopIterationPerRow];
#pragma unroll
for (size_t k = 0; k < kLoopIterationPerRow; ++k) {
get[k] = column_iter == k;
column_iter = sycl::ext::intel::fpga_reg(column_iter);
}
fpga_tools::NTuple<T, pipe_size> pipe_write;
fpga_tools::UnrolledLoop<kLoopIterationPerRow>([&](auto t) {
fpga_tools::UnrolledLoop<pipe_size>([&](auto k) {
if constexpr (t * pipe_size + k < columns) {
pipe_write.template get<k>() =
get[t]
? cov_matrix_consume[li / kLoopIterationPerRow][t * pipe_size + k]
: sycl::ext::intel::fpga_reg(
pipe_write.template get<k>());
}
});
});
if (block == block_count - 1) {
OutputPipe::write(pipe_write);
}
}
if (block == block_count - 1) {
block = 0;
} else {
block++;
}
} // end of if else (standardized)
} // end of while
}; // end of operator()
}; // end of struct{}
} // namespace fpga_linalg
#endif /* __STREAMING_COVARIANCE_MATRIX_HPP__ */