Skip to content

Commit

Permalink
Add neureka_v2
Browse files Browse the repository at this point in the history
  • Loading branch information
lukamac committed Nov 27, 2024
1 parent 232c6fd commit 8fc7802
Show file tree
Hide file tree
Showing 12 changed files with 1,091 additions and 6 deletions.
61 changes: 61 additions & 0 deletions inc/pulp_nnx_neureka_v2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Luka Macan <luka.macan@unibo.it>
*
* Copyright 2023 ETH Zurich and University of Bologna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "neureka_v2.h"
#include "neureka_v2_siracusa_bsp.h"
#include "neureka_v2_task.h"
#include <stdint.h>

/* PULP-NNX interface */

void neureka_v2_nnx_init(const neureka_v2_dev_t *dev, neureka_v2_siracusa_conf_t *conf);
void neureka_v2_nnx_term(const neureka_v2_dev_t *dev);

/** neureka_v2_nnx_dispatch_check
*
* Check whether you can dispatch to the accelerator.
*/
int neureka_v2_nnx_dispatch_check(const neureka_v2_dev_t *dev);

/** neureka_v2_nnx_dispatch_wait
*
* Block until you can dispatch to the accelerator.
*/
void neureka_v2_nnx_dispatch_wait(const neureka_v2_dev_t *dev);

/** neureka_v2_nnx_dispatch
*
* Dispatch a task to the accelerator.
* Fails with return code 1 if the task cannot be dispatched. Otherwise returns
* 0.
*/
int neureka_v2_nnx_dispatch(const neureka_v2_dev_t *dev, neureka_v2_task_t *task);

/** neureka_v2_nnx_resolve_check
*
* Check whether the task has been resolved.
*/
int neureka_v2_nnx_resolve_check(const neureka_v2_dev_t *dev, neureka_v2_task_t *task);

/** neureka_v2_nnx_resolve_wait
*
* Block until you can resolve the task.
*/
void neureka_v2_nnx_resolve_wait(const neureka_v2_dev_t *dev, neureka_v2_task_t *task);
34 changes: 34 additions & 0 deletions neureka_v2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Neureka

## Docs

Gvsoc model repo [link](https://github.com/gvsoc/gvsoc-pulp/tree/prasadar/siracusa/pulp/neureka).

## Implemented features

- [x] Convolution w/ kernel shape 1x1
- [x] Convolution w/ kernel shape 3x3
- [x] Depthwise convolution w/ kernel shape 3x3
- [ ] Normalization and quantization
- [x] With
- [x] Without
- [x] Relu (w/ and w/o)
- [x] Bias (w/ and w/o)
- [ ] Per-channel shift
- [x] Per-layer shift
- [x] Input type
- [x] uint8
- [x] int8
- [x] Output type
- [x] int8
- [x] uint8 (only w/ Relu)
- [x] int32
- [x] Scale type
- [x] uint8
- [x] uint32
- [x] Bias type
- [x] int32
- [ ] Weight type
- [x] int8
- [ ] int2-7
- [x] Dedicated weight memory
78 changes: 78 additions & 0 deletions neureka_v2/bsp/neureka_v2_siracusa_bsp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Luka Macan <luka.macan@unibo.it>
*
* Copyright 2023 ETH Zurich and University of Bologna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "neureka_v2_siracusa_bsp.h"
#include <pmsis.h>

#define NEUREKA_V2_SIRACUSA_CLUSTER_CTRL_BASE_ADDR (0x00200000)
#define NEUREKA_V2_SIRACUSA_CLUSTER_CTRL_HWPE_OFFS 0x18
#define NEUREKA_V2_SIRACUSA_CLUSTER_CTRL_HWPE_ADDR \
(NEUREKA_V2_SIRACUSA_CLUSTER_CTRL_BASE_ADDR + \
NEUREKA_V2_SIRACUSA_CLUSTER_CTRL_HWPE_OFFS)
#define NEUREKA_V2_SIRACUSA_CLUSTER_CTRL_HWPE_MASK_HCI_PRIO 0x100
#define NEUREKA_V2_SIRACUSA_CLUSTER_CTRL_HWPE_MASK_HCI_MAXSTALL 0xff
#define NEUREKA_V2_SIRACUSA_MAX_STALL (8)
#define NEUREKA_V2_SIRACUSA_EVENT (1 << 12)
#define NEUREKA_V2_SIRACUSA_BASE_ADDR (0x00201000)
#define NEUREKA_V2_SIRACUSA_WEIGHT_MEM_BASE_ADDR (0x10400000)
#define NEUREKA_V2_SIRACUSA_WEIGHT_MEM_MRAM_OFFSET (0x00000000)
#define NEUREKA_V2_SIRACUSA_WEIGHT_MEM_SRAM_OFFSET (0x00400000)

void neureka_v2_siracusa_hci_setpriority_neureka_v2() {
*(volatile uint32_t *)NEUREKA_V2_SIRACUSA_CLUSTER_CTRL_HWPE_ADDR |=
NEUREKA_V2_SIRACUSA_CLUSTER_CTRL_HWPE_MASK_HCI_PRIO;
}

void neureka_v2_siracusa_hci_setpriority_core() {
*(volatile uint32_t *)NEUREKA_V2_SIRACUSA_CLUSTER_CTRL_HWPE_ADDR &=
~NEUREKA_V2_SIRACUSA_CLUSTER_CTRL_HWPE_MASK_HCI_PRIO;
}

void neureka_v2_siracusa_hci_reset_max_stall() {
*(volatile uint32_t *)NEUREKA_V2_SIRACUSA_CLUSTER_CTRL_HWPE_ADDR &=
~NEUREKA_V2_SIRACUSA_CLUSTER_CTRL_HWPE_MASK_HCI_MAXSTALL;
}

void neureka_v2_siracusa_hci_set_max_stall(uint32_t max_stall) {
*(volatile uint32_t *)NEUREKA_V2_SIRACUSA_CLUSTER_CTRL_HWPE_ADDR |=
max_stall & NEUREKA_V2_SIRACUSA_CLUSTER_CTRL_HWPE_MASK_HCI_MAXSTALL;
}

void neureka_v2_siracusa_open(neureka_v2_siracusa_conf_t *conf) {
neureka_v2_siracusa_hci_setpriority_neureka_v2();
neureka_v2_siracusa_hci_set_max_stall(conf->max_stall);
}

void neureka_v2_siracusa_close() {
neureka_v2_siracusa_hci_reset_max_stall();
neureka_v2_siracusa_hci_setpriority_core();
}

void neureka_v2_siracusa_event_wait_and_clear() {
eu_evt_maskWaitAndClr(NEUREKA_V2_SIRACUSA_EVENT);
}

static const neureka_v2_dev_t neureka_v2_siracusa_dev = {
.hwpe_dev = (struct hwpe_dev_t){
.base_addr = (volatile uint32_t *)NEUREKA_V2_SIRACUSA_BASE_ADDR}};

const neureka_v2_dev_t *neureka_v2_siracusa_get_dev() {
return &neureka_v2_siracusa_dev;
}
67 changes: 67 additions & 0 deletions neureka_v2/bsp/neureka_v2_siracusa_bsp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Luka Macan <luka.macan@unibo.it>
*
* Copyright 2023 ETH Zurich and University of Bologna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef __NEUREKA_V2_SIRACUSA_BSP_H__
#define __NEUREKA_V2_SIRACUSA_BSP_H__

#include "neureka_v2.h"
#include <stdint.h>

/**
* neureka_v2_siracusa_setpriority_neureka_v2
*
* Set HCI interconnect bus priority to prioritize neureka_v2.
*/
void neureka_v2_siracusa_hci_setpriority_neureka_v2();

/**
* neureka_v2_siracusa_setpriority_core
*
* Set HCI bus priority to prioritize cores.
*/
void neureka_v2_siracusa_hci_setpriority_core();

/**
* neureka_v2_siracusa_hci_reset_maxstall
*
* Reset the HCI bus maxstall parameter.
* TODO: Check if it disables it also or just resets?
*/
void neureka_v2_siracusa_hci_reset_max_stall();

/**
* neureka_v2_siracusa_hci_set_maxstall
*
* Set the HCI bus maxstall. Maxstall defines how many cycles
* will the HCI bus stall the lower priority master, i.e. neureka_v2 or core,
* before letting it do a transaction.
*/
void neureka_v2_siracusa_hci_set_max_stall(uint32_t max_stall);

typedef struct neureka_v2_siracusa_conf_t {
int max_stall;
} neureka_v2_siracusa_conf_t;

void neureka_v2_siracusa_open(neureka_v2_siracusa_conf_t *conf);
void neureka_v2_siracusa_close();
void neureka_v2_siracusa_event_wait_and_clear();
const neureka_v2_dev_t *neureka_v2_siracusa_get_dev();

#endif // !__NEUREKA_V2_SIRACUSA_BSP_H__
55 changes: 55 additions & 0 deletions neureka_v2/gvsoc/neureka_v2_gvsoc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Luka Macan <luka.macan@unibo.it>
*
* Copyright 2023 ETH Zurich and University of Bologna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef __NEUREKA_V2_GVSOC_H__
#define __NEUREKA_V2_GVSOC_H__

#include "neureka_v2.h"
#include "neureka_v2_task.h"

#define NEUREKA_V2_REG_GVSOC_LOG_LEVEL 25
#define NEUREKA_V2_REG_GVSOC_LOG_FORMAT 26
#define NEUREKA_V2_REG_GVSOC_FRAME_REG 27

typedef enum neureka_v2_gvsoc_log_format_e {
NEUREKA_V2_GVSOC_LOG_FORMAT_DECIMAL = 0,
NEUREKA_V2_GVSOC_LOG_FORMAT_HEXADECIMAL = 3
} neureka_v2_gvsoc_log_format_e;

typedef enum neureka_v2_gvsoc_log_level_e {
NEUREKA_V2_GVSOC_LOG_LEVEL_JOB_START_END = 0,
NEUREKA_V2_GVSOC_LOG_LEVEL_CONFIG = 1,
NEUREKA_V2_GVSOC_LOG_LEVEL_ACTIV_INOUT = 2,
NEUREKA_V2_GVSOC_LOG_LEVEL_ALL = 3
} neureka_v2_gvsoc_log_level_e;

static void neureka_v2_gvsoc_log_activate(const neureka_v2_dev_t *dev,
neureka_v2_gvsoc_log_level_e log_level,
neureka_v2_gvsoc_log_format_e format) {
hwpe_task_reg_write(&dev->hwpe_dev, NEUREKA_V2_REG_GVSOC_LOG_LEVEL, log_level);
hwpe_task_reg_write(&dev->hwpe_dev, NEUREKA_V2_REG_GVSOC_LOG_FORMAT, format);
}

static void neureka_v2_gvsoc_log_deactivate(const neureka_v2_dev_t *dev) {
hwpe_task_reg_write(&dev->hwpe_dev, NEUREKA_V2_REG_GVSOC_LOG_LEVEL,
NEUREKA_V2_GVSOC_LOG_LEVEL_JOB_START_END);
}

#endif // __NEUREKA_V2_GVSOC_H__
37 changes: 37 additions & 0 deletions neureka_v2/hal/neureka_v2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Luka Macan <luka.macan@unibo.it>
*
* Copyright 2023 ETH Zurich and University of Bologna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "neureka_v2.h"

#define NEUREKA_V2_STATUS_EMPTY (0x000)
#define NEUREKA_V2_STATUS_FULL (0x101)

inline int neureka_v2_task_queue_tasks_in_flight(const neureka_v2_dev_t *dev) {
uint32_t status = hwpe_task_queue_status(&dev->hwpe_dev);
return (status & 0x1) + ((status >> 8) & 0x1);
}

inline int neureka_v2_task_queue_empty(const neureka_v2_dev_t *dev) {
return hwpe_task_queue_status(&dev->hwpe_dev) == NEUREKA_V2_STATUS_EMPTY;
}

inline int neureka_v2_task_queue_full(const neureka_v2_dev_t *dev) {
return hwpe_task_queue_status(&dev->hwpe_dev) == NEUREKA_V2_STATUS_FULL;
}
37 changes: 37 additions & 0 deletions neureka_v2/hal/neureka_v2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Luka Macan <luka.macan@unibo.it>
*
* Copyright 2023 ETH Zurich and University of Bologna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef __NEUREKA_V2_H__
#define __NEUREKA_V2_H__

#include "hwpe.h"
#include <stdint.h>

#define NEUREKA_V2_TASK_QUEUE_SIZE (2)

typedef struct neureka_v2_dev_t {
hwpe_dev_t hwpe_dev; /* Implements the HWPE device interface */
} neureka_v2_dev_t;

int neureka_v2_task_queue_tasks_in_flight(const neureka_v2_dev_t *dev);
int neureka_v2_task_queue_empty(const neureka_v2_dev_t *dev);
int neureka_v2_task_queue_full(const neureka_v2_dev_t *dev);

#endif // __NEUREKA_V2_H__
Loading

0 comments on commit 8fc7802

Please sign in to comment.