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 DIMC Software to chip_antwerp branch #110

Merged
merged 5 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion target/rtl/cfg/hemaia_ci.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
"snax_KUL_cluster", // snax_cgra_cluster
"snax_KUL_cluster",
"snax_hypercorex_cluster",
snax_dimc_cluster
"snax_dimc_cluster"
],

// peripherals
Expand Down
47 changes: 47 additions & 0 deletions target/sim/sw/device/apps/snax/snax-dimc-conv/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2024 KU Leuven.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
#
# Mace <ce.ma@esat.kuleuven.be>

APP = snax-dimc-conv

# Define the GitHub repository URL and target directory
GIT_REPO = https://github.com/KULeuven-MICAS/hemaia_dimc_data.git
DATA_DIR = data
CLONE_DIR = hemaia_dimc_data # The original repo name after cloning

# Ensure data directory is cloned before anything else
$(DATA_DIR):
@echo "Checking if data directory exists..."
@if [ ! -d "$(DATA_DIR)/.git" ]; then \
echo "Cloning repository..."; \
git clone $(GIT_REPO); \
echo "Renaming repository to $(DATA_DIR)..."; \
mv $(CLONE_DIR) $(DATA_DIR); \
else \
echo "Repository already cloned."; \
fi

# Ensure data.h is available before proceeding
$(DATA_DIR)/data.h: $(DATA_DIR)
@if [ ! -f "$(DATA_DIR)/data.h" ]; then \
echo "Error: data.h not found in $(DATA_DIR)!"; \
exit 1; \
fi

# Add INCDIRS only after cloning is complete
INCDIRS += $(DATA_DIR)
INCDIRS += ../../../snax/dimc/include

# Include this binary in the final build
RISCV_LDFLAGS += ../../../snax/dimc/build/snax-dimc-lib.o

SRCS = src/snax-dimc-conv.c

include ../../common.mk

$(DEP): $(DATA_DIR)/data.h

# Ensure cloning happens first before compiling
all: $(DATA_DIR)/data.h
177 changes: 177 additions & 0 deletions target/sim/sw/device/apps/snax/snax-dimc-conv/src/snax-dimc-conv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// Copyright 2024 KU Leuven.
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

#include "snrt.h"
#include "snax-dimc-csr.h"
#include "snax-dimc-lib.h"
#include "data_conv.h"

static int err = 0;

int main() {

// printf("Starting the DIMC CONV test\r\n");

// Set err value for checking
err = 0;
if(snrt_cluster_idx() == 3) {

uint64_t kernel_length = 256;
uint64_t qkv_activation_length = Q_LENGTH;
uint64_t qkt_activation_length = Q1K1T_LENGTH;

uint8_t *local_kernel;
uint64_t *local_activation_qkv, *local_activation_qkt, *local_final_res;

local_kernel = (uint8_t *)snrt_l1_next();
local_activation_qkv = (uint64_t *)(local_kernel + kernel_length);
local_activation_qkt = (uint64_t *)(local_activation_qkv + qkv_activation_length);
local_final_res = (uint64_t *)(local_activation_qkt + qkt_activation_length);

if(snrt_is_dm_core()) {
printf("DMA core will be configured\r\n");

// This measures the start of cycle count
// for preloading the data to the L1 memory
uint32_t start_dma_load = snrt_mcycle();
/**********************************************************************/
// initialize TCDM by DMA
/**********************************************************************/
printf("INITIALIZING TCDM\r\n");

// read matrix Q from data.h
int kernel_size = kernel_length * sizeof(uint8_t);
int activation_size_qkv = qkv_activation_length * sizeof(uint64_t);
int activation_size_qkt = qkt_activation_length * sizeof(uint64_t);

snrt_dma_start_1d(local_kernel, kernel, kernel_size);
snrt_dma_start_1d(local_activation_qkv, Q, activation_size_qkv);
snrt_dma_start_1d(local_activation_qkt, Q1K1T_G, activation_size_qkt);

// Measure the end of the transfer process
snrt_dma_wait_all();

uint32_t end_dma_load = snrt_mcycle();

printf("DMA core exits\r\n");
}

// Wait until DMA transfer is done
snrt_cluster_hw_barrier();

if (snrt_is_compute_core()){
printf("COMPUTE CORE\r\n");

printf("ENTERING CONV MODE\r\n");

uint32_t busy = dimc_query_busy();
printf("%d: busy\r\n", busy);
printf("QUERYING BUSY SUCCEEDED\r\n");

configure_accelerator();

printf("CONFIGURING ACCELERATOR SUCCEEDED\r\n");

busy = dimc_query_busy();
printf("%d: busy\r\n", busy);
printf("QUERYING BUSY SUCCEEDED\r\n");

printf("CONFIGURING STREAMERS for KERNEL\r\n");
dimc_set_streamer_dim_w((64*16), 1, 64, 0, 8, (uint32_t)(local_final_res));
dimc_set_streamer_dim_r0(145, 1, 256, 0, 8, (uint32_t)(local_kernel));
dimc_set_streamer_dim_r1(145, 1, 256, 0, 8, (uint32_t)(local_kernel + 64));
dimc_set_streamer_dim_r2(145, 1, 256, 0, 8, (uint32_t)(local_kernel + 128));
dimc_set_streamer_dim_r3(145, 1, 256, 0, 8, (uint32_t)(local_kernel + 192));
dimc_start_streamer();

dimc_start_conv();

// LOAD KERNELS
/*
printf("CONFIGURING STREAMERS for KERNEL\r\n");
dimc_set_streamer_dim_w(0, 0, 0, 0, 0, 0);
dimc_set_streamer_dim_r0(1, 1, 256, 0, 8, (uint32_t)(local_kernel));
dimc_set_streamer_dim_r1(1, 1, 256, 0, 8, (uint32_t)(local_kernel + 64));
dimc_set_streamer_dim_r2(1, 1, 256, 0, 8, (uint32_t)(local_kernel + 128));
dimc_set_streamer_dim_r3(1, 1, 256, 0, 8, (uint32_t)(local_kernel + 192));

dimc_start_streamer();

dimc_start_conv();

while (dimc_is_streamer_busy()) {
// printf("STREAMER BUSY\r\n");
}

// LOAD ACTIVATION
printf("CONFIGURING STREAMERS for ACTIVATION\r\n");
dimc_set_streamer_dim_w((64*16), 1, 64, 0, 8, (uint32_t)(local_final_res));
dimc_set_streamer_dim_r0(144, 1, 256, 0, 8, (uint32_t)(local_activation_qkv));
dimc_set_streamer_dim_r1(144, 1, 256, 0, 8, (uint32_t)(local_activation_qkv + 8));
dimc_set_streamer_dim_r2(144, 1, 256, 0, 8, (uint32_t)(local_activation_qkv + 16));
dimc_set_streamer_dim_r3(144, 1, 256, 0, 8, (uint32_t)(local_activation_qkv + 24));
// printf("STREAMER CONFIGURED for ACTIVATION\r\n");

dimc_start_streamer();
*/

busy = dimc_query_busy();
printf("%d: busy\r\n", busy);
printf("QUERYING BUSY SUCCEEDED\r\n");


// Wait until the accelerator is done
while (dimc_is_streamer_busy()) { }

printf("GO CHECK FINAL RESULT\r\n");

int mismatch = 0;

// const int iterations = 8192 / 8;
for (int i = 0; i < (8192 / 8); i++) {
// Pointers to the current chunks
uint64_t* res_chunk = &local_final_res[i * 8];
uint8_t* gold_chunk = &gold_conv[i * 36];

// Buffer to hold bits from local_final_res
uint8_t res_bits[64]; // 512 bits / 8 bits per byte = 64 bytes

// Convert local_final_res chunk to bytes
for (int j = 0; j < 8; j++) {
uint64_t val = res_chunk[j];
for (int k = 0; k < 8; ++k) {
res_bits[j * 8 + k] = (val >> (k * 8)) & 0xFF; // Little-endian
}
}

// Buffer to hold bits from gold_conv with padding

volatile uint8_t gold_bits[64];

for (int j = 0; j < 36; j++) {
gold_bits[j] = gold_chunk[j];
}

for (int j = 36; j < 64; j++) {
gold_bits[j] = 0;
}

// Compare the two byte arrays
bool match = true;
for (int j = 0; j < 64; j++) {
if (res_bits[j] != gold_bits[j]) {
match = false;
err += 1;
printf("fuck\r\n");
break;
}
}

}
printf("MISMATCHES: %d\r\n", err);
}
}

return err;
}
47 changes: 47 additions & 0 deletions target/sim/sw/device/apps/snax/snax-dimc-mha/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2024 KU Leuven.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
#
# Mace <ce.ma@esat.kuleuven.be>

APP = snax-dimc-mha

# Define the updated GitHub repository URL and target directory
GIT_REPO = https://github.com/KULeuven-MICAS/hemaia_dimc_data.git
DATA_DIR = data
CLONE_DIR = hemaia_dimc_data # The original repo name after cloning

# Ensure data directory is cloned before anything else
$(DATA_DIR):
@echo "Checking if data directory exists..."
@if [ ! -d "$(DATA_DIR)/.git" ]; then \
echo "Cloning repository..."; \
git clone $(GIT_REPO); \
echo "Renaming repository to $(DATA_DIR)..."; \
mv $(CLONE_DIR) $(DATA_DIR); \
else \
echo "Repository already cloned."; \
fi

# Ensure data.h is available before proceeding
$(DATA_DIR)/data.h: $(DATA_DIR)
@if [ ! -f "$(DATA_DIR)/data.h" ]; then \
echo "Error: data.h not found in $(DATA_DIR)!"; \
exit 1; \
fi

# Ensure data directory is available before adding it to INCDIRS
INCDIRS += $(DATA_DIR)
INCDIRS += ../../../snax/dimc/include

# Include this binary in the final build
RISCV_LDFLAGS += ../../../snax/dimc/build/snax-dimc-lib.o

SRCS = src/snax-dimc-mha.c

include ../../common.mk

$(DEP): $(DATA_DIR)/data.h

# Ensure cloning happens first before compiling
all: $(DATA_DIR)/data.h
Loading
Loading