Skip to content

Commit

Permalink
generalize pooling
Browse files Browse the repository at this point in the history
  • Loading branch information
jmduarte committed Dec 21, 2023
1 parent 07c5bb6 commit f41951c
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions hls4ml/templates/vivado/nnet_utils/nnet_pooling.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,39 @@ template <int W, int N> ap_int<W> avg(ap_int<W> (&x)[N]) {
return tmp;
}

template <int W, int I, int N> ap_fixed<W, I> avg(ap_fixed<W, I> (&x)[N]) {
template <int W, int N> ap_int<W> avg(ap_uint<W> (&x)[N]) {
// Use a wider accumulator than the input to avoid overflow
ap_fixed<W + ceillog2(N), I + ceillog2(N)> tmp = 0;
ap_uint<W + ceillog2(N)> tmp = 0;
for (int i = 0; i < N; i++) {
tmp += x[i];
}
tmp /= N;
// Now cast back to original type
ap_fixed<W, I> y = tmp;
ap_uint<W> y = tmp;
return tmp;
}

template <int W, int I, int N, ap_q_mode Q, ap_o_mode O> ap_fixed<W, I, Q, O> avg(ap_fixed<W, I, Q, O> (&x)[N]) {
// Use a wider accumulator than the input to avoid overflow
ap_fixed<W + ceillog2(N), I + ceillog2(N), Q, O> tmp = 0;
for (int i = 0; i < N; i++) {
tmp += x[i];
}
tmp /= N;
// Now cast back to original type
ap_fixed<W, I, Q, O> y = tmp;
return y;
}

template <int W, int I, int N, ap_q_mode Q, ap_o_mode O> ap_ufixed<W, I, Q, O> avg(ap_ufixed<W, I, Q, O> (&x)[N]) {
// Use a wider accumulator than the input to avoid overflow
ap_ufixed<W + ceillog2(N), I + ceillog2(N), Q, O> tmp = 0;
for (int i = 0; i < N; i++) {
tmp += x[i];
}
tmp /= N;
// Now cast back to original type
ap_ufixed<W, I, Q, O> y = tmp;
return y;
}

Expand Down

0 comments on commit f41951c

Please sign in to comment.