Skip to content

Commit

Permalink
ADD: add a bunch of operators
Browse files Browse the repository at this point in the history
  • Loading branch information
T-K-233 committed Jun 9, 2024
1 parent e33d7c5 commit 3fee8ec
Show file tree
Hide file tree
Showing 27 changed files with 635 additions and 45 deletions.
12 changes: 12 additions & 0 deletions nn/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ set(INCLUDES
set(SOURCES
src/nn_tensor.c
src/nn_print.c
src/abs/nn_abs.c
src/add/nn_add.c
src/batchnorm2d/nn_batchnorm2d.c
src/conv2d/nn_conv2d.c
src/copy/nn_copy.c
src/linear/nn_linear.c
src/matmul/nn_matmul.c
src/matrixnorm/nn_matrixnorm.c
src/max/nn_max.c
src/maximum/nn_maximum.c
src/maxpool2d/nn_maxpool2d.c
src/min/nn_min.c
src/minimum/nn_minimum.c
src/mul/nn_mul.c
src/multiply/nn_multiply.c
src/neg/nn_neg.c
src/relu/nn_relu.c
src/relu6/nn_relu6.c
src/sub/nn_sub.c
Expand All @@ -26,12 +32,18 @@ set(SOURCES
)

set(RVV_SOURCE
src/abs/nn_abs_rvv.c
src/add/nn_add_rvv.c
src/linear/nn_linear_rvv.c
src/matmul/nn_matmul_rvv.c
src/matrixnorm/nn_matrixnorm_rvv.c
src/max/nn_max_rvv.c
src/maximum/nn_maximum_rvv.c
src/min/nn_min_rvv.c
src/minimum/nn_minimum_rvv.c
src/mul/nn_mul_rvv.c
src/multiply/nn_multiply_rvv.c
src/neg/nn_neg_rvv.c
src/sub/nn_sub_rvv.c
)

Expand Down
4 changes: 4 additions & 0 deletions nn/inc/nn.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@

#include "nn_tensor.h"
#include "nn_print.h"
#include "nn_abs.h"
#include "nn_add.h"
#include "nn_batchnorm2d.h"
#include "nn_conv2d.h"
#include "nn_copy.h"
#include "nn_linear.h"
#include "nn_matmul.h"
#include "nn_matrixnorm.h"
#include "nn_max.h"
#include "nn_min.h"
#include "nn_mul.h"
#include "nn_multiply.h"
#include "nn_neg.h"
#include "nn_relu.h"
#include "nn_relu6.h"
#include "nn_sub.h"
Expand Down
26 changes: 26 additions & 0 deletions nn/inc/nn_abs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef __NN_ABS_H
#define __NN_ABS_H

#include <assert.h>
#include <math.h>

#include "nn_tensor.h"


/**
* Computes the absolute value of each element in input.
*
* out_i = |input_i|
*
* @param out: the output tensor
* @param input: the input tensor
*/
void NN_abs(Tensor *out, Tensor *input);

void NN_abs_F32(Tensor *out, Tensor *input);


void NN_abs_F32_RVV(Tensor *out, Tensor *input);


#endif // __NN_ABS_H
21 changes: 21 additions & 0 deletions nn/inc/nn_matrixnorm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef __NN_MATRIXNORM_H
#define __NN_MATRIXNORM_H

#include <assert.h>
#include <math.h>

#include "nn_tensor.h"


/**
* Computes the Frobenius norm of a matrix.
*
* @param tensor: the input tensor of shape (m, n)
*/
float NN_matrixNorm_F32(Tensor *tensor);


float NN_matrixNorm_F32_RVV(Tensor *tensor);


#endif // __NN_MATRIXNORM_H
25 changes: 25 additions & 0 deletions nn/inc/nn_maximum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef __NN_MAXIMUM_H
#define __NN_MAXIMUM_H

#include <assert.h>
#include <float.h>

#include "nn_tensor.h"


/**
* Computes the element-wise maximum of two tensors.
*
* @param out: the output tensor
* @param a: the input tensor
* @param b: the input tensor
*/
void NN_maximum(Tensor *out, Tensor *a, Tensor *b);

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


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


#endif // __NN_MAXIMUM_H
1 change: 1 addition & 0 deletions nn/inc/nn_min.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ float NN_min(Tensor *tensor);

float NN_min_F32(Tensor *tensor);


float NN_min_F32_RVV(Tensor *tensor);


Expand Down
24 changes: 24 additions & 0 deletions nn/inc/nn_minimum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef __NN_MINIMUM_H
#define __NN_MINIMUM_H

#include <assert.h>

#include "nn_tensor.h"


/**
* Computes the element-wise minimum of two tensors.
*
* @param out: the output tensor
* @param a: the input tensor
* @param b: the input tensor
*/
void NN_minimum(Tensor *out, Tensor *a, Tensor *b);

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


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


#endif // __NN_MINIMUM_H
15 changes: 9 additions & 6 deletions nn/inc/nn_mul.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@


/**
* Returns the element-wise multiplication of the input tensor with a scalar.
* Returns the element-wise multiplication of two tensors.
*
* out_i = a_i * b_i
*
* @param out: the output tensor
* @param in: the input tensor
* @param scalar: scalar value
* @param a: the input tensor
* @param b: the input tensor
*/
void NN_mul(Tensor *out, Tensor *in, float scalar);
void NN_mul(Tensor *out, Tensor *a, Tensor *b);

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

void NN_mul_F32(Tensor *out, Tensor *in, float scalar);

void NN_mul_F32_RVV(Tensor *out, Tensor *in, float scalar);
void NN_mul_F32_RVV(Tensor *out, Tensor *a, Tensor *b);


#endif // __NN_MUL_H
24 changes: 24 additions & 0 deletions nn/inc/nn_multiply.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef __NN_MULTIPLY_H
#define __NN_MULTIPLY_H

#include <assert.h>
#include <float.h>

#include "nn_tensor.h"


/**
* Returns the element-wise multiplication of the input tensor with a scalar.
*
* @param out: the output tensor
* @param in: the input tensor
* @param scalar: scalar value
*/
void NN_multiply(Tensor *out, Tensor *in, float scalar);

void NN_multiply_F32(Tensor *out, Tensor *in, float scalar);

void NN_multiply_F32_RVV(Tensor *out, Tensor *in, float scalar);


#endif // __NN_MULTIPLY_H
25 changes: 25 additions & 0 deletions nn/inc/nn_neg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef __NN_NEG_H
#define __NN_NEG_H

#include <assert.h>

#include "nn_tensor.h"


/**
* Returns a tensor with the negative of the elements of input.
*
* out = -1 x input
*
* @param out: the output tensor
* @param input: the input tensor
*/
void NN_neg(Tensor *out, Tensor *input);

void NN_neg_F32(Tensor *out, Tensor *input);


void NN_neg_F32_RVV(Tensor *out, Tensor *input);


#endif // __NN_NEG_H
63 changes: 63 additions & 0 deletions nn/inc/nn_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#define MAX_DIMS 4

Expand Down Expand Up @@ -74,40 +75,102 @@ static inline const char *NN_getDataTypeName(DataType dtype) {
}
}

/**
* Returns if the tensor is a scalar
*
* A scalar is a 1D tensor with a single element, i.e., shape = (1, )
*
* @param tensor: the target tensor
*/
static inline uint8_t NN_isScalar(Tensor *tensor) {
return tensor->ndim == 1 && tensor->shape[0] == 1;
}

/**
* Returns if the tensor is a vector
*
* @param tensor: the target tensor
*/
static inline uint8_t NN_isVector(Tensor *tensor) {
return tensor->ndim == 1;
}

/**
* Returns if the tensor is a matrix
*
* @param tensor: the target tensor
*/
static inline uint8_t NN_isMatrix(Tensor *tensor) {
return tensor->ndim == 2;
}

/**
* Returns if the tensor is a 3D tensor
*
* @param tensor: the target tensor
*/
static inline uint8_t NN_is3D(Tensor *tensor) {
return tensor->ndim == 3;
}

/**
* Returns if the tensor is a 4D tensor
*
* @param tensor: the target tensor
*/
static inline uint8_t NN_is4D(Tensor *tensor) {
return tensor->ndim == 4;
}

/**
* Frees the memory allocated for the tensor data
*
* @param tensor: the target tensor
*/
static inline void NN_freeTensorData(Tensor *tensor) {
free(tensor->data);
}

/**
* Frees the memory allocated for the tensor
*
* @param tensor: the target tensor
*/
static inline void NN_deleteTensor(Tensor *tensor) {
free(tensor);
}

/**
* Fills the tensor with the specified value.
*
* @param tensor: the input tensor
* @param value: scalar value
*/
static inline void NN_fill_F32(Tensor *tensor, float value) {
assert(tensor->dtype == DTYPE_F32);

for (size_t i = 0; i < tensor->size; i += 1) {
((float *)tensor->data)[i] = value;
}
}

static inline void NN_fill_I32(Tensor *tensor, int32_t value) {
assert(tensor->dtype == DTYPE_I32);

for (size_t i = 0; i < tensor->size; i += 1) {
((int32_t *)tensor->data)[i] = value;
}
}

static inline void NN_fill_I8(Tensor *tensor, int8_t value) {
assert(tensor->dtype == DTYPE_I8);

for (size_t i = 0; i < tensor->size; i += 1) {
((int8_t *)tensor->data)[i] = value;
}
}


/**
* Initialize a given tensor
*
Expand Down
14 changes: 14 additions & 0 deletions nn/src/abs/nn_abs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

#include "nn_abs.h"


void NN_abs_F32(Tensor *out, Tensor *input) {
assert(out->ndim == input->ndim);
assert(input->dtype == DTYPE_F32);
assert(out->dtype == DTYPE_F32);
assert(out->size == input->size);

for (size_t i = 0; i < out->size; i += 1) {
((float *)out->data)[i] = fabs(((float *)input->data)[i]);
}
}
25 changes: 25 additions & 0 deletions nn/src/abs/nn_abs_rvv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

#include "nn_abs.h"
#include "riscv_vector.h"

void NN_abs_F32_RVV(Tensor *out, Tensor *input) {
assert(out->ndim == input->ndim);
assert(input->dtype == DTYPE_F32);
assert(out->dtype == DTYPE_F32);
assert(out->size == input->size);

float *ptr_out = out->data;
float *ptr_in = input->data;

size_t n = out->shape[0] * out->shape[1];
while (n > 0) {
size_t vl = __riscv_vsetvl_e32m1(n);
vfloat32m1_t vec_in = __riscv_vle32_v_f32m1(ptr_in, vl);
vfloat32m1_t vec_out = __riscv_vfabs_v_f32m1(vec_in, vl);
__riscv_vse32_v_f32m1(ptr_out, vec_out, vl);
ptr_in += vl;
ptr_out += vl;
n -= vl;
}
}

Loading

0 comments on commit 3fee8ec

Please sign in to comment.