Skip to content

Commit

Permalink
ADD: adding docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
T-K-233 committed Oct 15, 2024
1 parent 3ca85dc commit 67a6f15
Show file tree
Hide file tree
Showing 2 changed files with 265 additions and 6 deletions.
11 changes: 11 additions & 0 deletions nn/float16.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/**
* @file float16.h
* @brief Half-Precision Floating-Point (fp16) Definitions
*
* This header file provides a unified interface for half-precision floating-point numbers.
* It includes support for half-precision floating-point numbers on systems that do not natively support them.
*
* The half-precision floating-point number is represented using a 16-bit unsigned integer.
* The conversion functions between half-precision and single-precision floating-point numbers are provided.
*/

#ifndef __FLOAT16_H
#define __FLOAT16_H

Expand Down
260 changes: 254 additions & 6 deletions nn/nn.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* @file nn.h
* @brief The Baremetal-NN Library
*
* This file contains the declarations of the functions and structures for the Baremetal-NN Library.
*/

#ifndef __NN_H
#define __NN_H

Expand Down Expand Up @@ -25,35 +32,72 @@
".section \".text\"\n") /* Restore section */


/**
* Tensor0D_F16
*
* A 0D tensor (scalar) with a float16_t data type.
*/
typedef struct {
float16_t data;
} Tensor0D_F16;

/**
* Tensor0D_F32
*
* A 0D tensor (scalar) with a float data type.
*/
typedef struct {
float data;
} Tensor0D_F32;

/**
* Tensor1D_F16
*
* A 1D tensor with a float16_t data type.
*/
typedef struct {
size_t shape[1];
float16_t *data;
} Tensor1D_F16;

/**
* Tensor1D_F32
*
* A 1D tensor with a float data type.
*/
typedef struct {
size_t shape[1];
float *data;
} Tensor1D_F32;

/**
* Tensor2D_F16
*
* A 2D tensor with a float16_t data type.
*/
typedef struct {
size_t shape[2];
float16_t *data;
} Tensor2D_F16;

/**
* Tensor2D_F32
*
* A 2D tensor with a float data type.
*/
typedef struct {
size_t shape[2];
float *data;
} Tensor2D_F32;


/**
* nn_assert
*
* Asserts that a condition is true. If the condition is false, it prints an error message and exits.
*
* @param condition The condition to assert.
* @param message The error message to print if the condition is false.
*/
static inline void nn_assert(int condition, char *message) {
if (!condition) {
printf("Assertion failed: ");
Expand All @@ -62,62 +106,266 @@ static inline void nn_assert(int condition, char *message) {
}
}

/**
* float_equal
*
* Checks if two floating-point numbers are equal within a relative error.
*
* @param golden The expected value.
* @param actual The actual value.
* @param rel_err The relative error tolerance.
* @return 1 if the numbers are equal within the relative error, 0 otherwise.
*/
static inline uint8_t float_equal(float golden, float actual, float rel_err) {
return (fabs(actual - golden) < rel_err) || (fabs((actual - golden) / actual) < rel_err);
}




/**
* nn_print_u8
*
* Prints an unsigned 8-bit integer.
*
* @param v The unsigned 8-bit integer to print.
*/
void nn_print_u8(uint8_t v);

/**
* nn_print_i8
*
* Prints an 8-bit integer.
*
* @param v The 8-bit integer to print.
*/
void nn_print_i8(int8_t v);

/**
* nn_print_u16
*
* Prints an unsigned 16-bit integer.
*
* @param v The unsigned 16-bit integer to print.
*/
void nn_print_u16(uint16_t v);

/**
* nn_print_i16
*
* Prints an 16-bit integer.
*
* @param v The 16-bit integer to print.
*/
void nn_print_i16(int16_t v);

/**
* nn_print_u32
*
* Prints an unsigned 32-bit integer.
*
* @param v The unsigned 32-bit integer to print.
*/
void nn_print_u32(uint32_t v);

/**
* nn_print_i32
*
* Prints a 32-bit integer.
*
* @param v The 32-bit integer to print.
*/
void nn_print_i32(int32_t v);

/**
* nn_print_f16
*
* Prints a float16_t.
*
* @param v The float16_t to print.
* @param num_digits The number of decimal digits to print.
*/
void nn_print_f16(float16_t v, int16_t num_digits);

/**
* nn_print_f32
*
* Prints a float.
*
* @param v The float to print.
* @param num_digits The number of decimal digits to print.
*/
void nn_print_f32(float v, int16_t num_digits);

/**
* nn_print_shape
*
* Prints the shape of the tensor.
*
* @param ndim The number of dimensions.
* @param shape The shape to print.
*/
void nn_print_shape(size_t ndim, const size_t *shape);

/**
* nn_print_tensor1d_f16
*
* Prints the content of a 1D tensor with type F16.
*
* @param tensor The 1D tensor to print.
*/
void nn_print_tensor1d_f16(const Tensor1D_F16 *tensor);

/**
* nn_print_tensor1d_f32
*
* Prints the content of a 1D tensor with type F32.
*
* @param tensor The 1D tensor to print.
*/
void nn_print_tensor1d_f32(const Tensor1D_F32 *tensor);

/**
* nn_print_tensor2d_f16
*
* Prints the content of a 2D tensor with type F16.
*
* @param tensor The 2D tensor to print.
*/
void nn_print_tensor2d_f16(const Tensor2D_F16 *tensor);

/**
* nn_print_tensor2d_f32
*
* Prints the content of a 2D tensor with type F32.
*
* @param tensor The 2D tensor to print.
*/
void nn_print_tensor2d_f32(const Tensor2D_F32 *tensor);


/**
* nn_equals0d_f16
*
* Checks if two 0D tensors with type F16 are equal.
*
* @param a The first 0D tensor.
* @param b The second 0D tensor.
* @param rel_err The relative error tolerance.
* @return 1 if the tensors are equal within the relative error, 0 otherwise.
*/
uint8_t nn_equals0d_f16(const Tensor0D_F16 *a, const Tensor0D_F16 *b, float rel_err);

/**
* nn_equals0d_f32
*
* Checks if two 0D tensors with type F32 are equal.
*
* @param a The first 0D tensor.
* @param b The second 0D tensor.
* @param rel_err The relative error tolerance.
* @return 1 if the tensors are equal within the relative error, 0 otherwise.
*/
uint8_t nn_equals0d_f32(const Tensor0D_F32 *a, const Tensor0D_F32 *b, float rel_err);

/**
* nn_equals1d_f16
*
* Checks if two 1D tensors with type F16 are equal.
*
* @param a The first 1D tensor.
* @param b The second 1D tensor.
* @param rel_err The relative error tolerance.
* @return 1 if the tensors are equal within the relative error, 0 otherwise.
*/
uint8_t nn_equals1d_f16(const Tensor1D_F16 *a, const Tensor1D_F16 *b, float rel_err);

/**
* nn_equals1d_f32
*
* Checks if two 1D tensors with type F32 are equal.
*
* @param a The first 1D tensor.
* @param b The second 1D tensor.
* @param rel_err The relative error tolerance.
* @return 1 if the tensors are equal within the relative error, 0 otherwise.
*/
uint8_t nn_equals1d_f32(const Tensor1D_F32 *a, const Tensor1D_F32 *b, float rel_err);

/**
* nn_equals2d_f16
*
* Checks if two 2D tensors with type F16 are equal.
*
* @param a The first 2D tensor.
* @param b The second 2D tensor.
* @param rel_err The relative error tolerance.
* @return 1 if the tensors are equal within the relative error, 0 otherwise.
*/
uint8_t nn_equals2d_f16(const Tensor2D_F16 *a, const Tensor2D_F16 *b, float rel_err);

/**
* nn_equals2d_f32
*
* Checks if two 2D tensors with type F32 are equal.
*
* @param a The first 2D tensor.
* @param b The second 2D tensor.
* @param rel_err The relative error tolerance.
* @return 1 if the tensors are equal within the relative error, 0 otherwise.
*/
uint8_t nn_equals2d_f32(const Tensor2D_F32 *a, const Tensor2D_F32 *b, float rel_err);



/**
* nn_add1d_f16
*
* Adds x1 and x2 element-wise and stores the result in y.
*
* y[i] = x1[i] + x2[i]
*
* @param y The result tensor.
* @param x1 The first tensor.
* @param x2 The second tensor.
*/
void nn_add1d_f16(Tensor1D_F16 *y, const Tensor1D_F16 *x1, const Tensor1D_F16 *x2);

/**
* nn_add1d_f32
*
* Adds x1 and x2 element-wise and stores the result in y.
*
* y[i] = x1[i] + x2[i]
*
* @param y The result tensor.
* @param x1 The first tensor.
* @param x2 The second tensor.
*/
void nn_add1d_f32(Tensor1D_F32 *y, const Tensor1D_F32 *x1, const Tensor1D_F32 *x2);

/**
* nn_add2d_f16
*
* Adds x1 and x2 element-wise and stores the result in y.
*
* y[i][j] = x1[i][j] + x2[i][j]
*
* @param y The result tensor.
* @param x1 The first tensor.
* @param x2 The second tensor.
*/
void nn_add2d_f16(Tensor2D_F16 *y, const Tensor2D_F16 *x1, const Tensor2D_F16 *x2);

/**
* nn_add2d_f32
*
* Adds x1 and x2 element-wise and stores the result in y.
*
* y[i][j] = x1[i][j] + x2[i][j]
*
* @param y The result tensor.
* @param x1 The first tensor.
* @param x2 The second tensor.
*/
void nn_add2d_f32(Tensor2D_F32 *y, const Tensor2D_F32 *x1, const Tensor2D_F32 *x2);


void nn_addscalar1d_f16(Tensor1D_F16 *y, const Tensor1D_F16 *x, float16_t scalar);

void nn_addscalar1d_f32(Tensor1D_F32 *y, const Tensor1D_F32 *x, float scalar);
Expand Down

0 comments on commit 67a6f15

Please sign in to comment.