Files
light-petal-recognition/hls_cnn/src/hls_qcnn_accel.cpp
T

227 lines
8.8 KiB
C++

#include "hls_qcnn_accel.h"
static int clamp_relu_i16(long long value) {
#pragma HLS INLINE
long long scaled = value / HLS_QCNN_ACT_SCALE;
if (scaled < 0) {
return 0;
}
if (scaled > 32767) {
return 32767;
}
return (int)scaled;
}
static int feature_index(int ch, int row, int col, int size) {
#pragma HLS INLINE
return (ch * size + row) * size + col;
}
static int conv_weight_index(int offset, int oc, int ic, int kr, int kc, int in_ch) {
#pragma HLS INLINE
return offset + (((oc * in_ch + ic) * 3 + kr) * 3 + kc);
}
static int read_input_q(const unsigned int *image_words, int row, int col, int ch) {
#pragma HLS INLINE
int flat = (row * HLS_QCNN_INPUT_SIZE + col) * 3 + ch;
unsigned int word = image_words[flat >> 2];
unsigned int pixel = (word >> ((flat & 3) * 8)) & 0xff;
return (int)((pixel * HLS_QCNN_ACT_SCALE + 127) / 255);
}
static void conv_image_to_buf(
const unsigned int *image_words,
const short *weights,
short *dst,
int out_ch,
int w_offset,
int b_offset
) {
short w_cache[HLS_QCNN_INPUT_CHANNELS][3][3];
#pragma HLS ARRAY_PARTITION variable=w_cache complete dim=2
#pragma HLS ARRAY_PARTITION variable=w_cache complete dim=3
for (int oc = 0; oc < out_ch; oc++) {
short bias = weights[b_offset + oc];
for (int ic = 0; ic < HLS_QCNN_INPUT_CHANNELS; ic++) {
for (int kr = 0; kr < 3; kr++) {
for (int kc = 0; kc < 3; kc++) {
#pragma HLS PIPELINE II=1
w_cache[ic][kr][kc] = weights[conv_weight_index(w_offset, oc, ic, kr, kc, HLS_QCNN_INPUT_CHANNELS)];
}
}
}
for (int row = 0; row < HLS_QCNN_INPUT_SIZE; row++) {
for (int col = 0; col < HLS_QCNN_INPUT_SIZE; col++) {
long long acc = (long long)bias * HLS_QCNN_ACT_SCALE;
for (int ic = 0; ic < HLS_QCNN_INPUT_CHANNELS; ic++) {
for (int kr = 0; kr < 3; kr++) {
for (int kc = 0; kc < 3; kc++) {
int ir = row + kr - 1;
int ix = col + kc - 1;
if (ir >= 0 && ir < HLS_QCNN_INPUT_SIZE && ix >= 0 && ix < HLS_QCNN_INPUT_SIZE) {
int in_q = read_input_q(image_words, ir, ix, ic);
int w_q = w_cache[ic][kr][kc];
acc += (long long)in_q * w_q;
}
}
}
}
dst[feature_index(oc, row, col, HLS_QCNN_INPUT_SIZE)] = (short)clamp_relu_i16(acc);
}
}
}
}
static void conv_buf_to_buf(
const short *src,
const short *weights,
short *dst,
int in_ch,
int out_ch,
int size,
int w_offset,
int b_offset
) {
short w_cache[HLS_QCNN_C4][3][3];
#pragma HLS ARRAY_PARTITION variable=w_cache complete dim=2
#pragma HLS ARRAY_PARTITION variable=w_cache complete dim=3
for (int oc = 0; oc < out_ch; oc++) {
short bias = weights[b_offset + oc];
for (int ic = 0; ic < in_ch; ic++) {
for (int kr = 0; kr < 3; kr++) {
for (int kc = 0; kc < 3; kc++) {
#pragma HLS PIPELINE II=1
w_cache[ic][kr][kc] = weights[conv_weight_index(w_offset, oc, ic, kr, kc, in_ch)];
}
}
}
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
long long acc = (long long)bias * HLS_QCNN_ACT_SCALE;
for (int ic = 0; ic < in_ch; ic++) {
for (int kr = 0; kr < 3; kr++) {
for (int kc = 0; kc < 3; kc++) {
int ir = row + kr - 1;
int ix = col + kc - 1;
if (ir >= 0 && ir < size && ix >= 0 && ix < size) {
int in_q = src[feature_index(ic, ir, ix, size)];
int w_q = w_cache[ic][kr][kc];
acc += (long long)in_q * w_q;
}
}
}
}
dst[feature_index(oc, row, col, size)] = (short)clamp_relu_i16(acc);
}
}
}
}
static void maxpool2x2(
const short *src,
short *dst,
int channels,
int in_size
) {
int out_size = in_size / 2;
for (int ch = 0; ch < channels; ch++) {
for (int row = 0; row < out_size; row++) {
for (int col = 0; col < out_size; col++) {
int r = row * 2;
int c = col * 2;
short a = src[feature_index(ch, r, c, in_size)];
short b = src[feature_index(ch, r + 1, c, in_size)];
short d = src[feature_index(ch, r, c + 1, in_size)];
short e = src[feature_index(ch, r + 1, c + 1, in_size)];
short m0 = a > b ? a : b;
short m1 = d > e ? d : e;
dst[feature_index(ch, row, col, out_size)] = m0 > m1 ? m0 : m1;
}
}
}
}
static void gap_fc(
const short *src,
const short *weights,
int *logits
) {
int gap[HLS_QCNN_C4];
int hidden[HLS_QCNN_HIDDEN];
const int spatial = 4;
const int spatial_area = spatial * spatial;
for (int ch = 0; ch < HLS_QCNN_C4; ch++) {
long long sum = 0;
for (int row = 0; row < spatial; row++) {
for (int col = 0; col < spatial; col++) {
sum += src[feature_index(ch, row, col, spatial)];
}
}
gap[ch] = (int)(sum / spatial_area);
}
for (int oc = 0; oc < HLS_QCNN_HIDDEN; oc++) {
long long acc = (long long)weights[HLS_QCNN_FC0_B_OFFSET + oc] * HLS_QCNN_ACT_SCALE;
for (int ic = 0; ic < HLS_QCNN_C4; ic++) {
int w = weights[HLS_QCNN_FC0_W_OFFSET + oc * HLS_QCNN_C4 + ic];
acc += (long long)gap[ic] * w;
}
hidden[oc] = clamp_relu_i16(acc);
}
for (int oc = 0; oc < HLS_QCNN_NUM_CLASSES; oc++) {
long long acc = (long long)weights[HLS_QCNN_FC1_B_OFFSET + oc] * HLS_QCNN_ACT_SCALE;
for (int ic = 0; ic < HLS_QCNN_HIDDEN; ic++) {
int w = weights[HLS_QCNN_FC1_W_OFFSET + oc * HLS_QCNN_HIDDEN + ic];
acc += (long long)hidden[ic] * w;
}
logits[oc] = (int)(acc / HLS_QCNN_ACT_SCALE);
}
}
void hls_qcnn_accel(
const unsigned int *image_words,
const short *weights,
short *buf0,
short *buf1,
int *logits
) {
#pragma HLS INTERFACE m_axi port=image_words offset=slave bundle=gmem0 depth=12288 max_read_burst_length=16
#pragma HLS INTERFACE m_axi port=weights offset=slave bundle=gmem1 depth=502026 max_read_burst_length=16
#pragma HLS INTERFACE m_axi port=buf0 offset=slave bundle=gmem2 depth=262144 max_read_burst_length=16 max_write_burst_length=16
#pragma HLS INTERFACE m_axi port=buf1 offset=slave bundle=gmem3 depth=262144 max_read_burst_length=16 max_write_burst_length=16
#pragma HLS INTERFACE m_axi port=logits offset=slave bundle=gmem4 depth=10 max_write_burst_length=16
#pragma HLS INTERFACE s_axilite port=image_words bundle=control
#pragma HLS INTERFACE s_axilite port=weights bundle=control
#pragma HLS INTERFACE s_axilite port=buf0 bundle=control
#pragma HLS INTERFACE s_axilite port=buf1 bundle=control
#pragma HLS INTERFACE s_axilite port=logits bundle=control
#pragma HLS INTERFACE s_axilite port=return bundle=control
conv_image_to_buf(image_words, weights, buf0, HLS_QCNN_C0, HLS_QCNN_CONV0_W_OFFSET, HLS_QCNN_CONV0_B_OFFSET);
conv_buf_to_buf(buf0, weights, buf1, HLS_QCNN_C0, HLS_QCNN_C0, 128, HLS_QCNN_CONV1_W_OFFSET, HLS_QCNN_CONV1_B_OFFSET);
maxpool2x2(buf1, buf0, HLS_QCNN_C0, 128);
conv_buf_to_buf(buf0, weights, buf1, HLS_QCNN_C0, HLS_QCNN_C1, 64, HLS_QCNN_CONV2_W_OFFSET, HLS_QCNN_CONV2_B_OFFSET);
conv_buf_to_buf(buf1, weights, buf0, HLS_QCNN_C1, HLS_QCNN_C1, 64, HLS_QCNN_CONV3_W_OFFSET, HLS_QCNN_CONV3_B_OFFSET);
maxpool2x2(buf0, buf1, HLS_QCNN_C1, 64);
conv_buf_to_buf(buf1, weights, buf0, HLS_QCNN_C1, HLS_QCNN_C2, 32, HLS_QCNN_CONV4_W_OFFSET, HLS_QCNN_CONV4_B_OFFSET);
conv_buf_to_buf(buf0, weights, buf1, HLS_QCNN_C2, HLS_QCNN_C2, 32, HLS_QCNN_CONV5_W_OFFSET, HLS_QCNN_CONV5_B_OFFSET);
maxpool2x2(buf1, buf0, HLS_QCNN_C2, 32);
conv_buf_to_buf(buf0, weights, buf1, HLS_QCNN_C2, HLS_QCNN_C3, 16, HLS_QCNN_CONV6_W_OFFSET, HLS_QCNN_CONV6_B_OFFSET);
conv_buf_to_buf(buf1, weights, buf0, HLS_QCNN_C3, HLS_QCNN_C3, 16, HLS_QCNN_CONV7_W_OFFSET, HLS_QCNN_CONV7_B_OFFSET);
maxpool2x2(buf0, buf1, HLS_QCNN_C3, 16);
conv_buf_to_buf(buf1, weights, buf0, HLS_QCNN_C3, HLS_QCNN_C4, 8, HLS_QCNN_CONV8_W_OFFSET, HLS_QCNN_CONV8_B_OFFSET);
conv_buf_to_buf(buf0, weights, buf1, HLS_QCNN_C4, HLS_QCNN_C4, 8, HLS_QCNN_CONV9_W_OFFSET, HLS_QCNN_CONV9_B_OFFSET);
maxpool2x2(buf1, buf0, HLS_QCNN_C4, 8);
gap_fc(buf0, weights, logits);
}