Skip to content

Commit

Permalink
Add GEMM sw tests (pulp-platform#27)
Browse files Browse the repository at this point in the history
* sw: Add GEMM sw test

* sw: Lint codes

* lint: Add license

* sw: Add base test without the GEMM engine

* sw: Add engine and base test to apps Makefile

* sw: Add GEMM tests to test list

* lint: Exclude GEMM tests from license check

* sw: Add new lines at the end of each file
  • Loading branch information
rgantonio authored and JosseVanDelm committed Dec 11, 2023
1 parent eb67e8a commit 1396a4a
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ jobs:
hw/snax_gemm/src/*
target/snitch_cluster/sw/apps/snax-mac/*
target/snitch_cluster/sw/apps/snax-mac-simple/*
target/snitch_cluster/sw/apps/snax-gemm-base/*
target/snitch_cluster/sw/apps/snax-gemm-engine/*
##################
# Lint YML Files #
Expand Down
2 changes: 2 additions & 0 deletions target/snitch_cluster/sw/apps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ SUBDIRS += dnn/softmax
SUBDIRS += montecarlo/pi_estimation
SUBDIRS += snax-mac
SUBDIRS += snax-mac-simple
SUBDIRS += snax-gemm-engine
SUBDIRS += snax-gemm-base

.PHONY: all clean $(SUBDIRS)

Expand Down
11 changes: 11 additions & 0 deletions target/snitch_cluster/sw/apps/snax-gemm-base/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2023 ETH Zurich and University of Bologna.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
#
# Ryan Antonio <[email protected]>

APP = snax-gemm-base
SRCS = src/snax-gemm-base.c
INCDIRS = data

include ../common.mk
19 changes: 19 additions & 0 deletions target/snitch_cluster/sw/apps/snax-gemm-base/data/data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdint.h>

uint32_t m = 8;
uint32_t n = 8;
uint32_t k = 8;

uint8_t A[] = {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};
uint8_t B[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
uint32_t C_golden[] = {36, 36, 36, 36, 36, 36, 36, 36, 100, 100, 100,
100, 100, 100, 100, 100, 164, 164, 164, 164, 164, 164,
164, 164, 228, 228, 228, 228, 228, 228, 228, 228, 292,
292, 292, 292, 292, 292, 292, 292, 356, 356, 356, 356,
356, 356, 356, 356, 420, 420, 420, 420, 420, 420, 420,
420, 484, 484, 484, 484, 484, 484, 484, 484};
72 changes: 72 additions & 0 deletions target/snitch_cluster/sw/apps/snax-gemm-base/src/snax-gemm-base.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

// Copyright 2020 ETH Zurich and University of Bologna.
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

#include "data.h"
#include "snrt.h"

int main() {
// Set err value for checking
int err = 0;

// Prepare addresses in TCDM
uint8_t *local_a, *local_b;
uint32_t *local_c;

uint32_t tic, toc;

// Allocate space in TCDM
local_a = (uint8_t *)snrt_l1_next();
local_b = local_a + m * k * sizeof(uint8_t);
local_c = (uint32_t *)(local_b + n * k * sizeof(uint8_t));

// Transfer data from L3 to L1
// Using DMA only
if (snrt_is_dm_core()) {
tic = snrt_mcycle();
tic = snrt_mcycle();

snrt_dma_start_1d(local_a, A, m * k * sizeof(uint8_t));
snrt_dma_start_1d(local_b, B, n * k * sizeof(uint8_t));

toc = snrt_mcycle();

printf("DMA transfer cycles: %d \n", toc - tic);
}

// Wait for DMA to finish
snrt_cluster_hw_barrier();

// Base MM calculation
if (snrt_is_compute_core()) {
tic = snrt_mcycle();
uint32_t temp_accumulator;

for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
temp_accumulator = 0;
for (int s = 0; s < k; s++) {
temp_accumulator += (uint32_t)(*(local_a + i * k + s)) *
(uint32_t)(*(local_b + s + j * k));
}
*(local_c + i * k + j) = temp_accumulator;
}
}

toc = snrt_mcycle();

printf("Cycles: %d \n", toc - tic);

// Check if result is not equal to golden result
for (uint32_t i = 0; i < m; i++) {
for (uint32_t j = 0; j < n; j++) {
if (C_golden[i * n + j] != *(local_c + (i * n + j))) {
err += 1;
};
}
}
};

return err;
}
11 changes: 11 additions & 0 deletions target/snitch_cluster/sw/apps/snax-gemm-engine/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2023 ETH Zurich and University of Bologna.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
#
# Ryan Antonio <[email protected]>

APP = snax-gemm-engine
SRCS = src/snax-gemm-engine.c
INCDIRS = data

include ../common.mk
19 changes: 19 additions & 0 deletions target/snitch_cluster/sw/apps/snax-gemm-engine/data/data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdint.h>

uint32_t m = 8;
uint32_t n = 8;
uint32_t k = 8;

uint8_t A[] = {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};
uint8_t B[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
uint32_t C_golden[] = {36, 36, 36, 36, 36, 36, 36, 36, 100, 100, 100,
100, 100, 100, 100, 100, 164, 164, 164, 164, 164, 164,
164, 164, 228, 228, 228, 228, 228, 228, 228, 228, 292,
292, 292, 292, 292, 292, 292, 292, 356, 356, 356, 356,
356, 356, 356, 356, 420, 420, 420, 420, 420, 420, 420,
420, 484, 484, 484, 484, 484, 484, 484, 484};
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

// Copyright 2020 ETH Zurich and University of Bologna.
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

#include "data.h"
#include "snrt.h"

int main() {
// Set err value for checking
int err = 0;

// Prepare addresses in TCDM
uint8_t *local_a, *local_b;
uint32_t *local_c;

uint32_t tic, toc;

// Allocate space in TCDM
local_a = (uint8_t *)snrt_l1_next();
local_b = local_a + m * k * sizeof(uint8_t);
local_c = (uint32_t *)(local_b + n * k * sizeof(uint8_t));

// Transfer data from L3 to L1
// Using DMA only
if (snrt_is_dm_core()) {
tic = snrt_mcycle();
tic = snrt_mcycle();

snrt_dma_start_1d(local_a, A, m * k * sizeof(uint8_t));
snrt_dma_start_1d(local_b, B, n * k * sizeof(uint8_t));

toc = snrt_mcycle();

printf("DMA transfer cycles: %d \n", toc - tic);
}

// Wait for DMA to finish
snrt_cluster_hw_barrier();

// Base MM calculation
if (snrt_is_compute_core()) {
// Setting of CSRs
tic = snrt_mcycle();

// Set addresses
write_csr(0x3c0, (uint32_t)local_a);
write_csr(0x3c1, (uint32_t)local_b);
write_csr(0x3c2, (uint32_t)local_c);

// CSR start
write_csr(0x3c3, 0);

toc = snrt_mcycle();

printf("Cycles: %d \n", toc - tic);

// Check if result is not equal to golden result
for (uint32_t i = 0; i < m; i++) {
for (uint32_t j = 0; j < n; j++) {
if (C_golden[i * n + j] != *(local_c + (i * n + j))) {
err += 1;
};
}
}
};

return err;
}
2 changes: 2 additions & 0 deletions target/snitch_cluster/sw/apps/snax-gemm-run.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ runs:
- app: dnn/linear
- app: dnn/maxpool
- app: dnn/gemm
- app: snax-gemm-base
- app: snax-gemm-engine
# dnn/gelu # seems like it stalls
# dnn/conv2d # fails with exit code 32
# dnn/fusedconv # fails newly
Expand Down

0 comments on commit 1396a4a

Please sign in to comment.