Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FAST CI versions of tests #9

Merged
merged 4 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
34 changes: 34 additions & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ abs_top_srcdir := @abs_top_srcdir@
XLEN := @XLEN@
RISCVTOOLS := @RISCVTOOLS@
ROCC = examples
RUNNER := "spike --extension=gemmini "

.PHONY: all bareMetalC clean imagenet mlps
all: bareMetalC imagenet mlps
Expand Down Expand Up @@ -31,3 +32,36 @@ clean:
$(MAKE) -C imagenet -f $(abs_top_srcdir)/imagenet/Makefile abs_top_srcdir=$(abs_top_srcdir) PREFIX=$(ROCC)-imagenet clean
$(MAKE) -C mlps -f $(abs_top_srcdir)/mlps/Makefile abs_top_srcdir=$(abs_top_srcdir) PREFIX=$(ROCC)-mlps clean

test-baremetal-bareMetalC:
make -C bareMetalC \
-f $(abs_top_srcdir)/bareMetalC/Makefile \
TARGET_MAKEFILE=$(abs_top_srcdir)/bareMetalC/Makefile \
abs_top_srcdir=$(abs_top_srcdir) \
src_dir=$(abs_top_srcdir)/bareMetalC \
XLEN=$(XLEN) \
PREFIX=$(ROCC)-bareMetalC \
RISCVTOOLS=$(RISCVTOOLS) \
RUNNER=$(RUNNER) \
run-baremetal

test-baremetal: test-baremetal-bareMetalC
make -C mlps \
-f $(abs_top_srcdir)/mlps/Makefile \
TARGET_MAKEFILE=$(abs_top_srcdir)/mlps/Makefile \
abs_top_srcdir=$(abs_top_srcdir) \
src_dir=$(abs_top_srcdir)/mlps \
XLEN=$(XLEN) \
PREFIX=$(ROCC)-mlps \
RISCVTOOLS=$(RISCVTOOLS) \
RUNNER=$(RUNNER) \
run-baremetal
make -C imagenet \
-f $(abs_top_srcdir)/imagenet/Makefile \
TARGET_MAKEFILE=$(abs_top_srcdir)/imagenet/Makefile \
abs_top_srcdir=$(abs_top_srcdir) \
src_dir=$(abs_top_srcdir)/imagenet \
XLEN=$(XLEN) \
PREFIX=$(ROCC)-imagenet \
RISCVTOOLS=$(RISCVTOOLS) \
RUNNER=$(RUNNER) \
run-baremetal
8 changes: 8 additions & 0 deletions bareMetalC/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ tests = \
template

tests_baremetal = $(tests:=-baremetal)
# Currently don't support conv or conv-with-pool on spike
runs_baremetal = $(addsuffix .run,$(filter-out conv-baremetal conv_with_pool-baremetal,$(tests_baremetal)))

ifdef BAREMETAL_ONLY
tests_linux =
else
Expand Down Expand Up @@ -76,5 +79,10 @@ vpath %.c $(src_dir)
%-linux: %.c $(GEMMINI_HEADERS)
$(CC_LINUX) $(CFLAGS) $< $(LFLAGS) -o $@

run-baremetal: $(runs_baremetal)

%-baremetal.run: %-baremetal
$(RUNNER)$(abs_top_srcdir)/build/bareMetalC/$^

junk += $(tests_baremetal) $(tests_linux)

40 changes: 36 additions & 4 deletions bareMetalC/conv.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@
#define PADDING 1
#define STRIDE 2
#else
#define BATCH_SIZE 3
#ifdef FAST
#define IN_DIM 9
#define IN_CHANNELS 5
#define OUT_CHANNELS 7
#else
#define IN_DIM 23
#define IN_CHANNELS 17
#define OUT_CHANNELS 31
#endif

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

#endif

#define NO_BIAS false
Expand Down Expand Up @@ -112,16 +120,26 @@ bool vec_is_equal(elem_t * a, elem_t * b, int len) {
}

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;
*ptr = (rand() % 5) - 2;
#ifdef FAST
*ptr = 1;
#else
*ptr = (rand() % 5) - 2;
#endif
}
}

void init_random_acc(acc_t * buf, int len) {
elem_t i = 0;
for (acc_t * ptr = buf; ptr < buf + len; ptr++) {
// *ptr = (rand() % 32) - 16;
*ptr = (rand() % 5) - 2;
#ifdef FAST
*ptr = 1;
#else
*ptr = (rand() % 5) - 2;
#endif
}
}

Expand Down Expand Up @@ -164,6 +182,7 @@ int main() {

printf("CPU conv...\n");
uint64_t start_cpu = read_cycles();
#ifndef FAST
conv(BATCH_SIZE, IN_CHANNELS, IN_DIM,
OUT_CHANNELS, KERNEL_DIM,
OUT_DIM,
Expand All @@ -172,6 +191,7 @@ int main() {
weights,
bias,
output);
#endif
uint64_t end_cpu = read_cycles();
printf("CPU conv took %llu cycles\n", end_cpu - start_cpu);

Expand Down Expand Up @@ -204,7 +224,20 @@ int main() {

assert(sizeof(output_mat) == sizeof(output));

#ifdef FAST
bool success = true;
for (int orow = 0; orow < BATCH_SIZE * OUT_DIM * OUT_DIM; orow++) {
for (int ocol = 0; ocol < OUT_CHANNELS; ocol++) {
elem_t v = output_mat[orow][ocol];
if (v != 21 && v != 31 && v != 46) {
success = false;
break;
}
}
}
#else
bool success = vec_is_equal(&output[0][0][0][0], &output_mat[0][0], sizeof(output) / sizeof(elem_t));
#endif

if (!success) {
// return 1;
Expand Down Expand Up @@ -294,4 +327,3 @@ int main() {

return 0;
}

36 changes: 33 additions & 3 deletions bareMetalC/conv_with_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@

#else

#define BATCH_SIZE 3
#ifdef FAST
#define IN_DIM 9
#define IN_CHANNELS 5
#define OUT_CHANNELS 7
#else
#define IN_DIM 23
#define IN_CHANNELS 17
#define OUT_CHANNELS 31
#endif

#define BATCH_SIZE 3
#define KERNEL_DIM 3
#define PADDING 1
#define STRIDE 2
Expand All @@ -46,7 +53,7 @@

#define POOL_OUT_DIM ((OUT_DIM + 2*POOL_PADDING - POOL_SIZE) / POOL_STRIDE + 1)

#define NO_POOL false
#define NO_POOL false

#if NO_POOL == true && !(POOL_SIZE == 1 && POOL_STRIDE == 1 && POOL_PADDING == 0)
#error NO_POOL is not set correctly
Expand Down Expand Up @@ -274,16 +281,26 @@ bool vec_is_equal(elem_t * a, elem_t * b, int len) {
}

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
*ptr = 1;
#else
*ptr = (rand() % 5) - 2;
#endif
}
}

void init_random_acc(acc_t * buf, int len) {
elem_t i = 0;
for (acc_t * ptr = buf; ptr < buf + len; ptr++) {
// *ptr = (rand() % 32) - 16;
#ifdef FAST
*ptr = 1;
#else
*ptr = (rand() % 5) - 2;
#endif
}
}

Expand Down Expand Up @@ -327,6 +344,7 @@ int main() {
else
init_random_acc(&bias[0], sizeof(bias) / sizeof(acc_t));

#ifndef FAST
printf("CPU conv...\n");
uint64_t start_cpu = read_cycles();
conv(BATCH_SIZE, IN_CHANNELS, IN_DIM,
Expand All @@ -350,6 +368,7 @@ int main() {
printf("CPU pool took %llu cycles\n", end_cpu_pool - start_cpu_pool);

printf("CPU conv+pool took %llu cycles\n", end_cpu_pool - start_cpu_pool + end_cpu - start_cpu);
#endif

static elem_t weights_mat[PATCH_SIZE][OUT_CHANNELS];
static elem_t output_mat[N_PATCHES][OUT_CHANNELS];
Expand Down Expand Up @@ -388,7 +407,19 @@ int main() {

assert(sizeof(pool_output_mat) == sizeof(pool_output));

#ifdef FAST
bool success = true;
for (int orow = 0; orow < BATCH_SIZE * POOL_OUT_DIM * POOL_OUT_DIM; orow++) {
for (int ocol = 0; ocol < OUT_CHANNELS; ocol++) {
if (pool_output_mat[orow][ocol] != 46) {
success = false;
break;
}
}
}
#else
bool success = vec_is_equal(&pool_output[0][0][0][0], &pool_output_mat[0][0], sizeof(pool_output) / sizeof(elem_t));
#endif

if (!success) {
// return 1;
Expand Down Expand Up @@ -500,4 +531,3 @@ int main() {

return 0;
}

61 changes: 43 additions & 18 deletions bareMetalC/matmul.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,29 @@
#include <time.h>
#include "include/gemmini_testutils.h"

static elem_t ZERO[DIM][DIM];

#ifdef FAST
#define AINIT 2
#define SINIT 4
#define RAND (rand())
#define N 1
#else
#define AINIT 0
#define SINIT 0
#define RAND (rand())
#define N 2
#endif

static elem_t ZERO[DIM][DIM];

void operands(int c, int * a, int * b, int * d) {
*d = c % N;
*b = (c / N) % N;
*a = c / (N*N);
}



void test_os (bool A_transpose, bool B_transpose) {
// Output stationary
printf("Output-stationary\n");
Expand All @@ -43,8 +56,8 @@ void test_os (bool A_transpose, bool B_transpose) {
matmul_full_ptr = &matmul_full_AB_transposed;
}

for (int activation = 0; activation <= 2; ++activation) {
for (int shift = 0; shift <= 4; shift += 4) {
for (int activation = AINIT; activation <= 2; ++activation) {
for (int shift = SINIT; shift <= 4; shift += 4) {
// printf("activation: %d, shift: %d\n", activation, shift);

static elem_t A[N][DIM][DIM] row_align(1);
Expand Down Expand Up @@ -91,13 +104,19 @@ void test_os (bool A_transpose, bool B_transpose) {
for (size_t n = 0; n < N; ++n) {
for (size_t i = 0; i < DIM; ++i) {
for (size_t j = 0; j < DIM; ++j) {
A[n][i][j] = (rand() % 64) - 32;
B[n][i][j] = (rand() % 64) - 32;
D[n][i][j] = (rand() % 64) - 32;
A[n][i][j] = (RAND % 64) - 32;
B[n][i][j] = (RAND % 64) - 32;
D[n][i][j] = (RAND % 64) - 32;
}
}
}

#ifdef FAST1
for (size_t i = 0; i < DIM; ++i) {
for (size_t j = 0; j < DIM; ++j) {
gold[0][i][j] = 1;
}
}
#else
for (size_t g = 0; g < N*N*N; ++g) {
int a, b, d;
operands(g, &a, &b, &d);
Expand All @@ -118,7 +137,7 @@ void test_os (bool A_transpose, bool B_transpose) {
else if (activation == RELU6)
matrelu6(gold[g], gold[g], 1 << relu6_shift);
}

#endif
int A_addr = 0;
int B_addr = N*DIM;
int D_addr = 2*N*DIM;
Expand Down Expand Up @@ -222,8 +241,8 @@ void test_ws(bool A_transpose, bool B_transpose) {
return;
}

for (int activation = 0; activation <= 2; ++activation) {
for (int scale = 0; scale <= 4; scale += 4) {
for (int activation = AINIT; activation <= 2; ++activation) {
for (int scale = SINIT; scale <= 4; scale += 4) {
static elem_t A[N][DIM][DIM] row_align(1);
static elem_t B[N][DIM][DIM] row_align(1);
static elem_t D[N][DIM][DIM] row_align(1);
Expand All @@ -238,17 +257,17 @@ void test_ws(bool A_transpose, bool B_transpose) {
// ...taking into account whether we preload new weights or re-use the old ones
static int preload[N*N*N] = {1};
for (int i = 1; i < N*N*N; ++i)
preload[i] = rand() % 2;
preload[i] = RAND % 2;

// ...whether we pass in a D or just use zeros
static int add_to_zeros[N*N*N];
for (int i = 0; i < N*N*N; ++i)
add_to_zeros[i] = rand() % 2;
add_to_zeros[i] = RAND % 2;

// ...and whether we accumulate on top of the previous result
static int accumulate[N*N*N] = {0};
for (int i = 1; i < N*N*N; ++i)
accumulate[i] = rand() % 2;
accumulate[i] = RAND % 2;

static int no_output[N*N*N];
for (int i = 0; i < N*N*N-1; ++i)
Expand Down Expand Up @@ -276,13 +295,19 @@ void test_ws(bool A_transpose, bool B_transpose) {
for (size_t n = 0; n < N; ++n) {
for (size_t i = 0; i < DIM; ++i) {
for (size_t j = 0; j < DIM; ++j) {
A[n][i][j] = (rand() % 64) - 32;
B[n][i][j] = (rand() % 64) - 32;
D[n][i][j] = (rand() % 64) - 32;
A[n][i][j] = (RAND % 64) - 32;
B[n][i][j] = (RAND % 64) - 32;
D[n][i][j] = (RAND % 64) - 32;
}
}
}

#ifdef FAST1
for (size_t i = 0; i < DIM; ++i) {
for (size_t j = 0; j < DIM; ++j) {
gold[0][i][j] = 64;
}
}
#else
for (size_t g = 0; g < N*N*N; ++g) {
int a, b, d;
operands(g, &a, &b, &d);
Expand Down Expand Up @@ -313,7 +338,7 @@ void test_ws(bool A_transpose, bool B_transpose) {
else if (activation == RELU6)
matrelu6(gold[g], gold[g], 1 << relu6_shift);
}

#endif
uint32_t A_addr = 0;
uint32_t B_addr = N*DIM;
uint32_t D_addr = 2*N*DIM;
Expand Down
Loading