Skip to content

Commit

Permalink
Add XDMA Software (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
IveanEx committed Nov 2, 2024
1 parent 56088bd commit 17cafd0
Show file tree
Hide file tree
Showing 14 changed files with 816 additions and 4 deletions.
6 changes: 3 additions & 3 deletions target/fpga/sw/send_uart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ else
FILE=$1
fi

stty -F /dev/ttyUSB1 cs8 1000000 ignbrk -brkint -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon crtscts
stty -F /dev/ttyUSB2 cs8 1000000 ignbrk -brkint -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon crtscts

echo -n 2 > /dev/ttyUSB1
echo -n 2 > /dev/ttyUSB2

sx -k "$FILE" < /dev/ttyUSB1 > /dev/ttyUSB1
sx -k "$FILE" < /dev/ttyUSB2 > /dev/ttyUSB2

fi
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ include ./data/Makefile
include ../../common.mk


$(DEP): $(DATA_H)
$(DEP): $(DATA_H)
20 changes: 20 additions & 0 deletions target/sim/sw/device/apps/snax/snax-xdma-maxpool/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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
#
# Yunhao Deng <[email protected]>

APP = snax-xdma-maxpool

INCDIRS += data
INCDIRS += INCDIRS += ../../../snax/xdma/include/

# Add binary to final build
RISCV_LDFLAGS += ../../../snax/xdma/build/snax-xdma-lib.o

SRCS = src/snax-xdma-maxpool.c

include ./data/Makefile
include ../../common.mk

$(DEP): $(DATA_H)
23 changes: 23 additions & 0 deletions target/sim/sw/device/apps/snax/snax-xdma-maxpool/data/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2023 KU Leuven.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
#
# Xiaoling Yi <[email protected]>

# Usage of absolute paths is required to externally include this Makefile
MK_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
DATA_DIR := $(realpath $(MK_DIR))

DATA_CFG ?= $(DATA_DIR)/params.hjson

DATA_H = $(DATA_DIR)/data.h

$(DATA_H): $(DATA_DIR)/datagen.py $(DATA_CFG)
$< -c $(DATA_CFG) > $@

.PHONY: clean-data clean

clean-data:
rm -f $(DATA_H)

clean: clean-data
133 changes: 133 additions & 0 deletions target/sim/sw/device/apps/snax/snax-xdma-maxpool/data/datagen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/usr/bin/env python3

# Copyright 2024 KU Leuven.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
#
# Fanchen Kong <[email protected]>

import numpy as np
import argparse
import pathlib
import hjson
import sys
import os
import subprocess

# Add data utility path
sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../../../../../util/sim/"))
from data_utils import format_scalar_definition, format_vector_definition # noqa E402

bender_command = subprocess.run(['bender', 'path', 'snitch_cluster'],
capture_output=True, text=True)
snax_utils_path = bender_command.stdout.strip()

sys.path.append(snax_utils_path + "/util/sim/")

from snax_utils import max_pooling # noqa E402
np.random.seed(42)


# Add stdint.h header
def emit_header_file(**kwargs):
emit_str = "#include <stdint.h>\n\n"
emit_str += "#include <stdbool.h> \n\n"
emit_str += emit_data(**kwargs)
return emit_str


def emit_data(**kwargs):
MIN = -128
MAX = 127

data_str = ""
data_str += format_scalar_definition("int8_t",
"H",
kwargs["H"]) + "\n"
data_str += format_scalar_definition("int8_t",
"W",
kwargs["W"]) + "\n"
data_str += format_scalar_definition("int8_t",
"Cin",
kwargs["Cin"]) + "\n"
data_str += format_scalar_definition("int8_t",
"Kh",
kwargs["Kh"]) + "\n"
data_str += format_scalar_definition("int8_t", "Kw", kwargs["Kw"]) + "\n"
data_str += format_scalar_definition("int8_t",
"pad_h", kwargs["pad_h"]) + "\n"
data_str += format_scalar_definition("int8_t",
"pad_w", kwargs["pad_w"]) + "\n"
data_str += format_scalar_definition("int8_t",
"stride_h", kwargs["stride_h"]) + "\n"
data_str += format_scalar_definition("int8_t",
"stride_w", kwargs["stride_w"]) + "\n"
padded_h = kwargs["H"] + 2 * kwargs["pad_h"]
padded_w = kwargs["W"] + 2 * kwargs["pad_w"]
out_h = (kwargs["H"] + 2 * kwargs["pad_h"] -
kwargs["Kh"]) // kwargs["stride_h"] + 1
out_w = (kwargs["W"] + 2 * kwargs["pad_w"] -
kwargs["Kw"]) // kwargs["stride_w"] + 1

data_str += format_scalar_definition("int8_t", "out_H", out_h) + "\n"
data_str += format_scalar_definition("int8_t", "out_W", out_w) + "\n"
data_str += format_scalar_definition("int8_t", "padded_H", padded_h) + "\n"
data_str += format_scalar_definition("int8_t", "padded_W", padded_w) + "\n"

# Generating random input data vector
data_in = np.random.randint(
MIN, MAX, (1, kwargs["H"], kwargs["W"], kwargs["Cin"])
)
padded_data_in = np.pad(
data_in,
(
(0, 0),
(kwargs["pad_h"], kwargs["pad_h"]),
(kwargs["pad_w"], kwargs["pad_w"]),
(0, 0),
),
"constant",
)
# Generating golden data
c_golden = max_pooling(
data_in,
kwargs["Kw"],
kwargs["Kh"],
kwargs["stride_w"],
kwargs["stride_h"],
kwargs["pad_w"],
kwargs["pad_h"],
"HWC",
)
data_str += format_vector_definition("int8_t",
"padded_data_in",
padded_data_in.reshape(-1)) + "\n"
data_str += format_vector_definition("int8_t",
"golden_data_out",
c_golden.reshape(-1)) + "\n"

return data_str


def main():
# Parsing cmd args
parser = argparse.ArgumentParser(description="Generating data for kernels")
parser.add_argument(
"-c",
"--cfg",
type=pathlib.Path,
required=True,
help="Select param config file kernel",
)
args = parser.parse_args()

# Load param config file
with args.cfg.open() as f:
param = hjson.loads(f.read())

# Emit header file
print(emit_header_file(**param))


if __name__ == "__main__":
main()
16 changes: 16 additions & 0 deletions target/sim/sw/device/apps/snax/snax-xdma-maxpool/data/params.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2023 KU Leuven.
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0
//
// Fanchen Kong <[email protected]>
{
H: 32,
W: 32,
Cin: 8,
Kh: 3,
Kw: 3,
pad_h: 1,
pad_w: 1,
stride_h: 1,
stride_w: 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright 2024 KU Leuven.
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0
//
// Fanchen Kong <[email protected]>

#include "data.h"
#include "snax-xdma-lib.h"
#include "snrt.h"

int main() {
// Set err value for checking
int err = 0;
// Obtain the start address of the TCDM memory
uint32_t dma_load_input_start;
uint32_t dma_load_input_end;
uint32_t *tcdm_baseaddress = (uint32_t *)snrt_l1_next();
// Put the input at the starting of tcdm
uint8_t *tcdm_in = tcdm_baseaddress;
// Put the output at the middle of tcdm
uint8_t *tcdm_out = tcdm_in + 0x10000 * sizeof(uint8_t);

if (snrt_is_dm_core()) {
// The xdma core is the last compute core in the cluster
uint32_t sstride_src[1] = {8};
uint32_t sstride_dst[1] = {8};
uint32_t tstride_src[2] = {8, 512};
uint32_t tbound_src[2] = {3, 3};

// First we need to transfer the input data from L3->TCDM
// Here we use the 2d iDMA transfer
dma_load_input_start = snrt_mcycle();
snrt_dma_start_2d(
tcdm_in, padded_data_in, padded_W * Cin * sizeof(uint8_t),
512 * sizeof(uint8_t), padded_W * Cin * sizeof(uint8_t),
padded_H * sizeof(uint8_t));
snrt_dma_wait_all();
dma_load_input_end = snrt_mcycle();

// --------------------- Configure the Ext --------------------- //

// There are three extensions in xdma
// VerilogMemset, Maxpool, Transposer
// 0 , 1 , 2
// We want to only use Maxpool
// Hence we need to disable the 0 and 2
// and we set the maxpool csr to 9 since we need 3x3 pooling
if (xdma_disable_dst_ext(0) != 0) {
printf("Error in disabling xdma extension 0 \r\n");
err++;
} else {
printf("The xdma extension 0 is disabled \r\n");
}

uint32_t ext_param_maxpool_size[1] = {9};
if (xdma_enable_dst_ext(1, ext_param_maxpool_size) != 0) {
printf("Error in enabling xdma extension 1 \r\n");
err++;
} else {
printf("The xdma extension 1 is enabled \r\n");
}

if (xdma_disable_dst_ext(2) != 0) {
printf("Error in disabling xdma extension 2 \r\n");
err++;
} else {
printf("The xdma extension 2 is disabled \r\n");
}

// --------------------- Configure the AGU --------------------- //
uint8_t *local_src_pointer;
uint8_t *local_dst_pointer;
int task_id;
for (int i = 0; i < out_H; i++) {
for (int j = 0; j < out_W / 8; j++) {
local_src_pointer = tcdm_in + j * 64 + i * 512;
local_dst_pointer = tcdm_out + j * 64 + i * 256;
if (xdma_memcpy_nd(local_src_pointer, local_dst_pointer,
sstride_src, sstride_dst, 2, tstride_src,
tbound_src, 0, NULL, NULL, 0xFFFFFFFF,
0xFFFFFFFF, 0xFFFFFFFF) != 0) {
printf("Error in xdma agu configuration \r\n");
err++;
} else {
printf("The xdma agu is configured \r\n");
}
int task_id = xdma_start();
xdma_wait(task_id);
printf("i = %d, j = %d is done \r\n", i, j);
}
}

// --------------------- Checking the Results --------------------- //
printf("Checking the results \r\n");
for (int i = 0; i < out_H * out_W * Cin; i++) {
if ((int8_t)tcdm_out[i] != golden_data_out[i]) {
printf("The maxpool is incorrect! \r\n");
printf("tcdm_out[%d]=%d, golden_data_out[%d]=%d", i,
(int8_t)tcdm_out[i], i, golden_data_out[i]);
}
}
printf("Checking is done. All values are right \r\n");
}

return 0;
}
16 changes: 16 additions & 0 deletions target/sim/sw/device/apps/snax/snax-xdma-memset/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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
#
# Yunhao Deng <[email protected]>

APP = snax-xdma-memset

INCDIRS += ../../../snax/xdma/include/

# Add binary to final build
RISCV_LDFLAGS += ../../../snax/xdma/build/snax-xdma-lib.o

SRCS = src/snax-xdma-memset.c

include ../../common.mk
Loading

0 comments on commit 17cafd0

Please sign in to comment.