Skip to content

Commit

Permalink
REFACTOR: organize directory again
Browse files Browse the repository at this point in the history
  • Loading branch information
T-K-233 committed Jun 5, 2024
1 parent ed9ccb9 commit 8927f2d
Show file tree
Hide file tree
Showing 27 changed files with 408 additions and 292 deletions.
17 changes: 9 additions & 8 deletions nn/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@


set(INCLUDES
./
./add/
./matmul/
inc
)

set(SOURCES
./add/nn_add.c
./matmul/nn_matmul.c

./add/nn_add_rvv.c
./matmul/nn_matmul_rvv.c
src/nn_tensor.c
src/nn_print.c
src/add/nn_add.c
src/copy/nn_copy.c
src/matmul/nn_matmul.c

src/add/nn_add_rvv.c
src/matmul/nn_matmul_rvv.c
)

add_library(nn ${SOURCES})
Expand Down
39 changes: 39 additions & 0 deletions nn/inc/nn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef __NN_H
#define __NN_H

#include <assert.h>

#include "nn_tensor.h"
#include "nn_print.h"
#include "nn_add.h"
#include "nn_copy.h"
#include "nn_matmul.h"


// http://elm-chan.org/junk/32bit/binclude.html
#define INCLUDE_FILE(section, filename, symbol) asm (\
".section "#section"\n" /* Change section */\
".balign 4\n" /* Word alignment */\
".global "#symbol"_start\n" /* Export the object start address */\
".global "#symbol"_data\n" /* Export the object address */\
#symbol"_start:\n" /* Define the object start address label */\
#symbol"_data:\n" /* Define the object label */\
".incbin \""filename"\"\n" /* Import the file */\
".global "#symbol"_end\n" /* Export the object end address */\
#symbol"_end:\n" /* Define the object end address label */\
".balign 4\n" /* Word alignment */\
".section \".text\"\n") /* Restore section */



void NN_assert(int condition, char *message) {
if (!condition) {
printf("Assertion failed: ");
printf("%s\n", message);
exit(1);
}
}



#endif // __NN_H
2 changes: 2 additions & 0 deletions nn/add/nn_add.h → nn/inc/nn_add.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "nn_tensor.h"


/**
* Element-wise addition
*
Expand All @@ -25,4 +26,5 @@ void NN_add_INT(Tensor *out, Tensor *a, Tensor *b);

void NN_add_F32_RVV(Tensor *out, Tensor *a, Tensor *b);


#endif // __NN_ADD_H
File renamed without changes.
16 changes: 16 additions & 0 deletions nn/inc/nn_copy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef __NN_COPY_H
#define __NN_COPY_H

#include "nn_tensor.h"


/**
* Copies values from one tensor to another
*
* @param dst: destination tensor
* @param src: source tensor
*/
void NN_copy(Tensor *dst, Tensor *src);


#endif // __NN_COPY_H
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions nn/inc/nn_print.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef __NN_PRINT_H
#define __NN_PRINT_H

#include "nn_tensor.h"


void NN_printFloat(float v, int16_t num_digits);

void NN_printShape(Tensor *t);

void NN_printf(Tensor *t);


#endif // __NN_PRINT_H
4 changes: 3 additions & 1 deletion nn/relu/nn_relu.h → nn/inc/nn_relu.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@


/**
* Applies the rectified linear unit function element-wise: y = max(0, x)
* Applies the rectified linear unit function element-wise
*
* y = max(0, x)
*
*/
void NN_relu_F32(Tensor *y, Tensor *x);
Expand Down
56 changes: 51 additions & 5 deletions nn/nn_tensor.h → nn/inc/nn_tensor.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#ifndef __NN_TYPES
#define __NN_TYPES
#ifndef __NN_TENSOR
#define __NN_TENSOR

#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

#define MAX_DIMS 4
Expand All @@ -20,7 +21,6 @@ typedef enum {
DTYPE_F64,
} DataType;


typedef struct {
DataType dtype; /** datatype */
size_t ndim; /** number of dimensions */
Expand All @@ -42,12 +42,18 @@ static inline size_t NN_sizeof(DataType dtype) {
switch (dtype) {
case DTYPE_I8:
return sizeof(int8_t);
case DTYPE_I16:
return sizeof(int16_t);
case DTYPE_I32:
return sizeof(int32_t);
case DTYPE_I64:
return sizeof(int64_t);
case DTYPE_F32:
return sizeof(float);
case DTYPE_F64:
return sizeof(double);
default:
printf("Unsupported data type\n");
printf("[WARNING] Unsupported data type: %d\n", dtype);
return 0;
}
}
Expand All @@ -56,13 +62,53 @@ static inline const char *NN_getDataTypeName(DataType dtype) {
switch (dtype) {
case DTYPE_I8:
return "INT8";
case DTYPE_I16:
return "INT16";
case DTYPE_I32:
return "INT32";
case DTYPE_I64:
return "INT64";
case DTYPE_F32:
return "FLOAT32";
case DTYPE_F64:
return "FLOAT64";
default:
return "UNKNOWN";
}
}

#endif // __NN_TYPES
static inline void NN_freeTensorData(Tensor *t) {
free(t->data);
}

static inline void NN_deleteTensor(Tensor *t) {
free(t);
}


/**
* Initialize a tensor
*
* @param ndim: number of dimensions
* @param shape: shape of tensor
* @param dtype: DataType
* @param data: pointer to data, if NULL, the data will be allocated
*/
void NN_initTensor(Tensor *t, size_t ndim, size_t *shape, DataType dtype, void *data);

Tensor *NN_tensor(size_t ndim, size_t *shape, DataType dtype, void *data);

Tensor *NN_zeros(size_t ndim, size_t *shape, DataType dtype);

Tensor *NN_ones(size_t ndim, size_t *shape, DataType dtype);

/**
* Convert tensor data type
*
* @param t: input tensor
* @param dtype: target data type
*/
void NN_asType(Tensor *t, DataType dtype);


#endif // __NN_TENSOR
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 8927f2d

Please sign in to comment.