Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bareMetalC/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ tests = \
mvin_scale \
conv \
conv_rect \
conv_rect_kernel \
conv_rect_kernel_trans_weight_0132 \
conv_rect_kernel_trans_weight_1203 \
conv_rect_kernel_and_input \
conv_with_pool \
conv_with_rot180 \
conv_with_kernel_dilation \
Expand Down
50 changes: 26 additions & 24 deletions bareMetalC/conv.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
#define IN_COL_DIM 224
#define IN_CHANNELS 3
#define OUT_CHANNELS 32
#define KERNEL_DIM 3
#define KERNEL_ROW_DIM 3
#define KERNEL_COL_DIM 3
#define PADDING 1
#define STRIDE 2

Expand All @@ -38,36 +39,37 @@
#endif

#define BATCH_SIZE 2
#define KERNEL_DIM 3
#define KERNEL_ROW_DIM 3
#define KERNEL_COL_DIM 3
#define PADDING 1
#define STRIDE 2

#endif

#define NO_BIAS false

#define OUT_ROW_DIM ((IN_ROW_DIM + 2*PADDING - KERNEL_DIM) / STRIDE + 1)
#define OUT_COL_DIM ((IN_COL_DIM + 2*PADDING - KERNEL_DIM) / STRIDE + 1)
#define PATCH_SIZE (KERNEL_DIM * KERNEL_DIM * IN_CHANNELS)
#define OUT_ROW_DIM ((IN_ROW_DIM + 2*PADDING - KERNEL_ROW_DIM) / STRIDE + 1)
#define OUT_COL_DIM ((IN_COL_DIM + 2*PADDING - KERNEL_COL_DIM) / STRIDE + 1)
#define PATCH_SIZE (KERNEL_ROW_DIM * KERNEL_COL_DIM * IN_CHANNELS)
#define N_PATCHES (BATCH_SIZE * OUT_ROW_DIM * OUT_COL_DIM)

void conv(int batch_size, int in_channels,
int in_row_dim, int in_col_dim,
int out_channels, int kernel_dim,
int out_channels, int kernel_row_dim, int kernel_col_dim,
int out_row_dim, int out_col_dim,
int stride, int padding,
elem_t input[batch_size][in_row_dim][in_col_dim][in_channels],
elem_t weights[out_channels][kernel_dim][kernel_dim][in_channels],
elem_t weights[out_channels][kernel_row_dim][kernel_col_dim][in_channels],
acc_t bias[out_channels],
elem_t output[batch_size][out_row_dim][out_col_dim][out_channels]) {

#ifdef GEMMINI_ASSERTIONS
if (out_row_dim != (in_row_dim + 2 * padding - kernel_dim) / stride + 1) {
if (out_row_dim != (in_row_dim + 2 * padding - kernel_row_dim) / stride + 1) {
printf("conv out_row_dim is not correct\n");
exit(1);
}

if (out_col_dim != (in_col_dim + 2 * padding - kernel_dim) / stride + 1) {
if (out_col_dim != (in_col_dim + 2 * padding - kernel_col_dim) / stride + 1) {
printf("conv out_col_dim is not correct\n");
exit(1);
}
Expand All @@ -79,8 +81,8 @@ void conv(int batch_size, int in_channels,
for (int och = 0; och < out_channels; och++) {
acc_t result = bias[och];

for (int krow = 0; krow < kernel_dim; krow++) {
for (int kcol = 0; kcol < kernel_dim; kcol++) {
for (int krow = 0; krow < kernel_row_dim; krow++) {
for (int kcol = 0; kcol < kernel_col_dim; kcol++) {
for (int kch = 0; kch < in_channels; kch++) {
int irow = orow * stride + krow - padding;
int icol = ocol * stride + kcol - padding;
Expand All @@ -106,18 +108,18 @@ void conv(int batch_size, int in_channels,
}
}

void flatten_weights(int out_channels, int kernel_dim, int in_channels,
void flatten_weights(int out_channels, int kernel_row_dim, int kernel_col_dim, int in_channels,
int patch_size,
elem_t weights[out_channels][kernel_dim][kernel_dim][in_channels],
elem_t weights[out_channels][kernel_row_dim][kernel_col_dim][in_channels],
elem_t weights_mat[patch_size][out_channels]) {

assert(patch_size == kernel_dim * kernel_dim * in_channels);
assert(patch_size == kernel_row_dim * kernel_col_dim * in_channels);

for (int outc = 0; outc < out_channels; outc++) {
for (int krow = 0; krow < kernel_dim; krow++) {
for (int kcol = 0; kcol < kernel_dim; kcol++) {
for (int krow = 0; krow < kernel_row_dim; krow++) {
for (int kcol = 0; kcol < kernel_col_dim; kcol++) {
for (int inc = 0; inc < in_channels; inc++) {
int wmatrow = krow * kernel_dim * in_channels +
int wmatrow = krow * kernel_col_dim * in_channels +
kcol * in_channels +
inc;

Expand Down Expand Up @@ -182,7 +184,7 @@ int main() {
printf("Output dimensions (rows by columns): %u by %u\n\n", OUT_ROW_DIM, OUT_COL_DIM);

static elem_t input[BATCH_SIZE][IN_ROW_DIM][IN_COL_DIM][IN_CHANNELS];
static elem_t weights[OUT_CHANNELS][KERNEL_DIM][KERNEL_DIM][IN_CHANNELS];
static elem_t weights[OUT_CHANNELS][KERNEL_ROW_DIM][KERNEL_COL_DIM][IN_CHANNELS];
static acc_t bias[OUT_CHANNELS];
static elem_t output[BATCH_SIZE][OUT_ROW_DIM][OUT_COL_DIM][OUT_CHANNELS];

Expand All @@ -203,7 +205,7 @@ int main() {
#ifndef FAST
conv(BATCH_SIZE, IN_CHANNELS,
IN_ROW_DIM, IN_COL_DIM,
OUT_CHANNELS, KERNEL_DIM,
OUT_CHANNELS, KERNEL_ROW_DIM, KERNEL_COL_DIM,
OUT_ROW_DIM, OUT_COL_DIM,
STRIDE, PADDING,
input,
Expand All @@ -218,7 +220,7 @@ int main() {
static elem_t output_mat[N_PATCHES][OUT_CHANNELS];

printf("Flatten weights...\n");
flatten_weights(OUT_CHANNELS, KERNEL_DIM, IN_CHANNELS,
flatten_weights(OUT_CHANNELS, KERNEL_ROW_DIM, KERNEL_COL_DIM, IN_CHANNELS,
PATCH_SIZE,
weights,
weights_mat);
Expand All @@ -228,7 +230,7 @@ int main() {
tiled_conv_auto(
BATCH_SIZE, IN_ROW_DIM, IN_COL_DIM, IN_CHANNELS,
OUT_CHANNELS, OUT_ROW_DIM, OUT_COL_DIM,
STRIDE, 1, 1, PADDING, KERNEL_DIM,
STRIDE, 1, 1, PADDING, KERNEL_ROW_DIM, KERNEL_COL_DIM,
false, false, false, false, false,

(elem_t*)input,
Expand Down Expand Up @@ -271,9 +273,9 @@ int main() {
printf("weights:\n");
for (int och = 0; och < OUT_CHANNELS; och++) {
printf("[");
for (int wrow = 0; wrow < KERNEL_DIM; wrow++) {
for (int wrow = 0; wrow < KERNEL_ROW_DIM; wrow++) {
printf("[");
for (int wcol = 0; wcol < KERNEL_DIM; wcol++) {
for (int wcol = 0; wcol < KERNEL_COL_DIM; wcol++) {
printf("[");
for (int ich = 0; ich < IN_CHANNELS; ich++) {
printf("%d,", weights[och][wrow][wcol][ich]);
Expand All @@ -287,7 +289,7 @@ int main() {
printf("\b\n\n");

printf("weights_mat:\n");
for (int wrow = 0; wrow < KERNEL_DIM * KERNEL_DIM * IN_CHANNELS; wrow++) {
for (int wrow = 0; wrow < KERNEL_ROW_DIM * KERNEL_COL_DIM * IN_CHANNELS; wrow++) {
printf("[");
for (int wcol = 0; wcol < OUT_CHANNELS; wcol++) {
printf("%d,", weights_mat[wrow][wcol]);
Expand Down
48 changes: 34 additions & 14 deletions bareMetalC/conv_dw.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@
#endif
#include "include/gemmini_testutils.h"

#define RECT_KERNEL

#ifndef BAREMETAL

#define BATCH_SIZE 3
#define IN_ROW_DIM 112
#define IN_COL_DIM 112
#define CHANNELS 17
#define KERNEL_DIM 3

#ifdef RECT_KERNEL
#define KERNEL_ROW_DIM 4
#define KERNEL_COL_DIM 2
#else
#define KERNEL_ROW_DIM 3
#define KERNEL_COL_DIM 3
#endif

#define PADDING 1
#define STRIDE 2

Expand All @@ -30,21 +40,31 @@

#define IN_ROW_DIM 17
#define IN_COL_DIM 17
#define CHANNELS 15
#define CHANNELS 2

#endif

#define BATCH_SIZE 3
#define KERNEL_DIM 3

#ifdef RECT_KERNEL
#define KERNEL_ROW_DIM 4
#define KERNEL_COL_DIM 2
#else
#define KERNEL_ROW_DIM 3
#define KERNEL_COL_DIM 3
#endif

#define PADDING 1
#define STRIDE 2

#endif



#define NO_BIAS false

#define OUT_ROW_DIM ((IN_ROW_DIM + 2*PADDING - KERNEL_DIM) / STRIDE + 1)
#define OUT_COL_DIM ((IN_COL_DIM + 2*PADDING - KERNEL_DIM) / STRIDE + 1)
#define OUT_ROW_DIM ((IN_ROW_DIM + 2*PADDING - KERNEL_ROW_DIM) / STRIDE + 1)
#define OUT_COL_DIM ((IN_COL_DIM + 2*PADDING - KERNEL_COL_DIM) / STRIDE + 1)

bool vec_is_equal(elem_t * a, elem_t * b, int len) {
for (int i = 0; i < len; i++)
Expand All @@ -57,11 +77,11 @@ void init_random(elem_t * buf, int len) {
elem_t i = 0;
for (elem_t * ptr = buf; ptr < buf + len; ptr++) {
// *ptr = (rand() % 32) - 16;
#ifdef FAST
//#ifdef FAST
*ptr = 1;
#else
*ptr = (rand() % 5) - 2;
#endif
//#else
// *ptr = (rand() % 5) - 2;
//#endif
}
}

Expand Down Expand Up @@ -99,7 +119,7 @@ int main() {
printf("Output dimensions: %u by %u\n\n", OUT_ROW_DIM, OUT_COL_DIM);

static elem_t input[BATCH_SIZE][IN_ROW_DIM][IN_COL_DIM][CHANNELS];
static elem_t weights[CHANNELS][KERNEL_DIM][KERNEL_DIM];
static elem_t weights[CHANNELS][KERNEL_ROW_DIM][KERNEL_COL_DIM];
static acc_t bias[CHANNELS];
static elem_t output[BATCH_SIZE][OUT_ROW_DIM][OUT_COL_DIM][CHANNELS];

Expand All @@ -120,7 +140,7 @@ int main() {
#ifndef FAST
tiled_conv_dw_auto(BATCH_SIZE, IN_ROW_DIM, IN_COL_DIM,
CHANNELS, OUT_ROW_DIM, OUT_COL_DIM,
STRIDE, PADDING, KERNEL_DIM,
STRIDE, PADDING, KERNEL_ROW_DIM, KERNEL_COL_DIM,

(elem_t*)input,
(elem_t*)weights,
Expand All @@ -140,7 +160,7 @@ int main() {
uint64_t start_gemmini = read_cycles();
tiled_conv_dw_auto(BATCH_SIZE, IN_ROW_DIM, IN_COL_DIM,
CHANNELS, OUT_ROW_DIM, OUT_COL_DIM,
STRIDE, PADDING, KERNEL_DIM,
STRIDE, PADDING, KERNEL_ROW_DIM, KERNEL_COL_DIM,

(elem_t*)input,
(elem_t*)weights,
Expand Down Expand Up @@ -180,9 +200,9 @@ int main() {
printf("weights:\n");
for (int och = 0; och < CHANNELS; och++) {
printf("[");
for (int wrow = 0; wrow < KERNEL_DIM; wrow++) {
for (int wrow = 0; wrow < KERNEL_ROW_DIM; wrow++) {
printf("[");
for (int wcol = 0; wcol < KERNEL_DIM; wcol++) {
for (int wcol = 0; wcol < KERNEL_COL_DIM; wcol++) {
printf("%d,", weights[och][wrow][wcol]);
}
printf("\b],\n");
Expand Down
2 changes: 1 addition & 1 deletion bareMetalC/conv_dw_perf.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ int main (int argc, char * argv[]) {
uint64_t start_gemmini = read_cycles();

tiled_conv_dw_auto(BATCH_SIZE, IN_DIM, IN_DIM, CHANNELS, OUT_DIM, OUT_DIM,
STRIDE, PADDING, KERNEL_DIM,
STRIDE, PADDING, KERNEL_DIM, KERNEL_DIM,

(elem_t*)input,
(elem_t*)weights,
Expand Down
Loading