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

Make size entities consistent #10

Merged
merged 1 commit into from
Apr 8, 2024
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
4 changes: 2 additions & 2 deletions examples/arch/arm/cmsis-dsp/neuron/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ int main(int argc, char *argv[]) {
// Init vars
NNNeuron neuron;
NNError error;
size_t input_size = 3;
float inputs[NEURON_MAX_WEIGHTS] = {1, 2, 3};
float weights[NEURON_MAX_WEIGHTS] = {0.2f, 0.8f, -0.5f};
int n_weights = 3;
float bias = 2.0f;

// Compute the output
if (!nn_neuron_init(&neuron, weights, n_weights, bias, nn_activation_func_identity, nn_dot_product_cmsis, &error)) {
if (!nn_neuron_init(&neuron, weights, input_size, bias, nn_activation_func_identity, nn_dot_product_cmsis, &error)) {
printf("error: %s\n", error.message);
return 1;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/arch/arm/neon/neuron/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ int main(int argc, char *argv[]) {
// Init vars
NNNeuron neuron;
NNError error;
size_t input_size = 3;
float inputs[NEURON_MAX_WEIGHTS] = {1, 2, 3};
float weights[NEURON_MAX_WEIGHTS] = {0.2f, 0.8f, -0.5f};
int n_weights = 3;
float bias = 2.0f;

// Compute the output
if (!nn_neuron_init(&neuron, weights, n_weights, bias, nn_activation_func_identity, nn_dot_product_neon, &error)) {
if (!nn_neuron_init(&neuron, weights, input_size, bias, nn_activation_func_identity, nn_dot_product_neon, &error)) {
printf("error: %s\n", error.message);
return 1;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/arch/generic/neuron/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ int main(int argc, char *argv[]) {
// Init vars
NNNeuron neuron;
NNError error;
size_t input_size = 3;
float inputs[NEURON_MAX_WEIGHTS] = {1, 2, 3};
float weights[NEURON_MAX_WEIGHTS] = {0.2f, 0.8f, -0.5f};
int n_weights = 3;
float bias = 2.0f;

// Compute the output
if (!nn_neuron_init(&neuron, weights, n_weights, bias, nn_activation_func_identity, nn_dot_product, &error)) {
if (!nn_neuron_init(&neuron, weights, input_size, bias, nn_activation_func_identity, nn_dot_product, &error)) {
printf("error: %s\n", error.message);
return 1;
}
Expand Down
4 changes: 2 additions & 2 deletions include/nn_neuron.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
// real-world applications since it's not optimized for performance.
typedef struct {
float weights[NEURON_MAX_WEIGHTS];
size_t n_weights;
size_t input_size;
float bias;
NNActivationFunction act_func;
NNDotProductFunction dot_product_func;
} NNNeuron;

// nn_neuron_init initializes a neuron with the given arguments.
bool nn_neuron_init(NNNeuron *neuron, const float weights[NEURON_MAX_WEIGHTS], size_t n_weights, float bias, NNActivationFunction act_func, NNDotProductFunction dot_product_func, NNError *error);
bool nn_neuron_init(NNNeuron *neuron, const float weights[NEURON_MAX_WEIGHTS], size_t input_size, float bias, NNActivationFunction act_func, NNDotProductFunction dot_product_func, NNError *error);

// nn_neuron_set_weights sets the weights of the given neuron.
bool nn_neuron_set_weights(NNNeuron *neuron, const float weights[NEURON_MAX_WEIGHTS], NNError *error);
Expand Down
6 changes: 3 additions & 3 deletions src/arch/arm/cmsis-dsp/nn_dot_product.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#include <stddef.h>

// nn_dot_product_neon calculates the dot product of two vectors.
float nn_dot_product_cmsis(const float *a, const float *b, size_t length) {
NN_DEBUG_PRINT(5, "function %s called with length = %zu\n", __func__, length);
float nn_dot_product_cmsis(const float *a, const float *b, size_t vector_size) {
NN_DEBUG_PRINT(5, "function %s called with vector_size = %zu\n", __func__, vector_size);

float result = 0.0f;

// CMSIS-DSP provides arm_dot_prod_f32 for Cortex-M cores with FPU
arm_dot_prod_f32(a, b, length, &result);
arm_dot_prod_f32(a, b, vector_size, &result);

return result;
}
8 changes: 4 additions & 4 deletions src/arch/arm/neon/nn_dot_product.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
#include <stddef.h>

// nn_dot_product_neon calculates the dot product of two vectors.
float nn_dot_product_neon(const float *a, const float *b, size_t length) {
NN_DEBUG_PRINT(5, "function %s called with length = %zu\n", __func__, length);
float nn_dot_product_neon(const float *a, const float *b, size_t vector_size) {
NN_DEBUG_PRINT(5, "function %s called with vector_size = %zu\n", __func__, vector_size);

// Initialize vector sum to 0
float32x4_t sumVec = vdupq_n_f32(0.0);

// Process 4 elements at a time using NEON SIMD instructions
// NEON SIMD registers are 128 bits wide, which can hold 4 float32 values
size_t i;
for (i = 0; i + 3 < length; i += 4) {
for (i = 0; i + 3 < vector_size; i += 4) {
float32x4_t aVec = vld1q_f32(a + i); // load 4 elements from a
float32x4_t bVec = vld1q_f32(b + i); // load 4 elements from b
float32x4_t prodVec = vmulq_f32(aVec, bVec); // multiply elements
Expand All @@ -24,7 +24,7 @@ float nn_dot_product_neon(const float *a, const float *b, size_t length) {
float result = vgetq_lane_f32(sumVec, 0) + vgetq_lane_f32(sumVec, 1) + vgetq_lane_f32(sumVec, 2) + vgetq_lane_f32(sumVec, 3);

// Handle remaining elements
for (; i < length; ++i) {
for (; i < vector_size; ++i) {
result += a[i] * b[i];
}

Expand Down
6 changes: 3 additions & 3 deletions src/nn_dot_product.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
#include <stddef.h>

// nn_dot_product_neon calculates the dot product of two vectors.
float nn_dot_product(const float *a, const float *b, size_t length) {
NN_DEBUG_PRINT(5, "function %s called with length = %zu\n", __func__, length);
float nn_dot_product(const float *a, const float *b, size_t vector_size) {
NN_DEBUG_PRINT(5, "function %s called with vector_size = %zu\n", __func__, vector_size);

// Initialize vector sum to 0
float result = 0.0f;

// Iterate over the elements of the vectors
for (size_t i = 0; i < length; ++i) {
for (size_t i = 0; i < vector_size; ++i) {
result += a[i] * b[i];
}

Expand Down
10 changes: 5 additions & 5 deletions src/nn_neuron.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <stdio.h>

// nn_neuron_init initializes a neuron with the given arguments.
bool nn_neuron_init(NNNeuron *neuron, const float weights[NEURON_MAX_WEIGHTS], size_t n_weights, float bias, NNActivationFunction act_func, NNDotProductFunction dot_product_func, NNError *error) {
bool nn_neuron_init(NNNeuron *neuron, const float weights[NEURON_MAX_WEIGHTS], size_t input_size, float bias, NNActivationFunction act_func, NNDotProductFunction dot_product_func, NNError *error) {
nn_error_set(error, NN_ERROR_NONE, NULL);
if (neuron == NULL) {
nn_error_set(error, NN_ERROR_INVALID_INSTANCE, "neuron is NULL");
Expand All @@ -17,8 +17,8 @@ bool nn_neuron_init(NNNeuron *neuron, const float weights[NEURON_MAX_WEIGHTS], s
nn_error_set(error, NN_ERROR_INVALID_INSTANCE, "weights is NULL");
return false;
}
neuron->n_weights = n_weights;
for (size_t i = 0; i < neuron->n_weights; ++i) {
neuron->input_size = input_size;
for (size_t i = 0; i < neuron->input_size; ++i) {
neuron->weights[i] = weights[i];
}
neuron->bias = bias;
Expand All @@ -38,7 +38,7 @@ bool nn_neuron_set_weights(NNNeuron *neuron, const float weights[NEURON_MAX_WEIG
nn_error_set(error, NN_ERROR_INVALID_INSTANCE, "neuron is NULL");
return false;
}
for (size_t i = 0; i < neuron->n_weights; ++i) {
for (size_t i = 0; i < neuron->input_size; ++i) {
neuron->weights[i] = weights[i];
}
return true;
Expand Down Expand Up @@ -74,7 +74,7 @@ float nn_neuron_compute(const NNNeuron *neuron, const float inputs[NEURON_MAX_WE
// Compute the dot product
if (neuron->dot_product_func != NULL) {
// Sum the weighted inputs
result = neuron->dot_product_func(neuron->weights, inputs, neuron->n_weights);
result = neuron->dot_product_func(neuron->weights, inputs, neuron->input_size);
}
// Add the bias
result += neuron->bias;
Expand Down
24 changes: 12 additions & 12 deletions tests/arch/arm/cmsis-dsp/neuron/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
typedef struct {
float inputs[NEURON_MAX_WEIGHTS];
float weights[NEURON_MAX_WEIGHTS];
int n_weights;
size_t input_size;
float bias;
NNDotProductFunction dot_product_func;
float output_tolerance;
Expand All @@ -29,7 +29,7 @@ void run_test_cases(TestCase *test_cases, int n_cases, char *info, NNDotProductF
NNNeuron neuron;
NNError error;

nn_neuron_init(&neuron, tc.weights, tc.n_weights, tc.bias, nn_activation_func_identity, dot_product_func, &error);
nn_neuron_init(&neuron, tc.weights, tc.input_size, tc.bias, nn_activation_func_identity, dot_product_func, &error);
assert(error.code == NN_ERROR_NONE);
const float output = nn_neuron_compute(&neuron, tc.inputs, &error);
assert(error.code == NN_ERROR_NONE);
Expand All @@ -44,7 +44,7 @@ int main() {
{
.inputs = {0.5f, 1.2f, -0.8f},
.weights = {0.2f, 0.3f, -0.1f},
.n_weights = 3,
.input_size = 3,
.bias = 0.5f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = 1.04f,
Expand All @@ -53,7 +53,7 @@ int main() {
{
.inputs = {-0.6f, -1.1f, 0.9f},
.weights = {-0.2f, 0.5f, 0.3f},
.n_weights = 3,
.input_size = 3,
.bias = -0.5f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = -0.66f,
Expand All @@ -62,7 +62,7 @@ int main() {
{
.inputs = {1.5f, 2.0f, -1.0f},
.weights = {0.4f, 0.4f, -0.2f},
.n_weights = 3,
.input_size = 3,
.bias = 2.0f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = 3.6f,
Expand All @@ -71,7 +71,7 @@ int main() {
{
.inputs = {0.1f, -0.2f, 0.3f},
.weights = {0.3f, -0.2f, 0.1f},
.n_weights = 3,
.input_size = 3,
.bias = 0.05f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = 0.15f,
Expand All @@ -80,7 +80,7 @@ int main() {
{
.inputs = {-2.5f, 3.0f, -1.5f},
.weights = {0.5f, -0.5f, 0.75f},
.n_weights = 3,
.input_size = 3,
.bias = 1.0f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = -2.875f,
Expand All @@ -89,7 +89,7 @@ int main() {
{
.inputs = {0.0f, 0.0f, 0.0f},
.weights = {0.25f, -0.75f, 0.5f},
.n_weights = 3,
.input_size = 3,
.bias = 0.5f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = 0.5f,
Expand All @@ -98,7 +98,7 @@ int main() {
{
.inputs = {1.2f, -1.2f, 0.8f},
.weights = {0.0f, 0.0f, 0.0f},
.n_weights = 3,
.input_size = 3,
.bias = 0.25f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = 0.25f,
Expand All @@ -107,7 +107,7 @@ int main() {
{
.inputs = {1.0f, -1.0f, 1.0f},
.weights = {-1.0f, 1.0f, -1.0f},
.n_weights = 3,
.input_size = 3,
.bias = -0.5f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = -3.5f,
Expand All @@ -116,7 +116,7 @@ int main() {
{
.inputs = {0.123f, 0.456f, -0.789f},
.weights = {0.321f, -0.654f, 0.987f},
.n_weights = 3,
.input_size = 3,
.bias = 0.1f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = -0.937484,
Expand All @@ -125,7 +125,7 @@ int main() {
{
.inputs = {0.001f, -0.002f, 0.003f},
.weights = {0.004f, 0.005f, -0.006f},
.n_weights = 3,
.input_size = 3,
.bias = 0.0f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = 0.000012f,
Expand Down
24 changes: 12 additions & 12 deletions tests/arch/arm/neon/neuron/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
typedef struct {
float inputs[NEURON_MAX_WEIGHTS];
float weights[NEURON_MAX_WEIGHTS];
int n_weights;
size_t input_size;
float bias;
NNDotProductFunction dot_product_func;
float output_tolerance;
Expand All @@ -29,7 +29,7 @@ void run_test_cases(TestCase *test_cases, int n_cases, char *info, NNDotProductF
NNNeuron neuron;
NNError error;

nn_neuron_init(&neuron, tc.weights, tc.n_weights, tc.bias, nn_activation_func_identity, dot_product_func, &error);
nn_neuron_init(&neuron, tc.weights, tc.input_size, tc.bias, nn_activation_func_identity, dot_product_func, &error);
assert(error.code == NN_ERROR_NONE);
const float output = nn_neuron_compute(&neuron, tc.inputs, &error);
assert(error.code == NN_ERROR_NONE);
Expand All @@ -44,7 +44,7 @@ int main() {
{
.inputs = {0.5f, 1.2f, -0.8f},
.weights = {0.2f, 0.3f, -0.1f},
.n_weights = 3,
.input_size = 3,
.bias = 0.5f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = 1.04f,
Expand All @@ -53,7 +53,7 @@ int main() {
{
.inputs = {-0.6f, -1.1f, 0.9f},
.weights = {-0.2f, 0.5f, 0.3f},
.n_weights = 3,
.input_size = 3,
.bias = -0.5f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = -0.66f,
Expand All @@ -62,7 +62,7 @@ int main() {
{
.inputs = {1.5f, 2.0f, -1.0f},
.weights = {0.4f, 0.4f, -0.2f},
.n_weights = 3,
.input_size = 3,
.bias = 2.0f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = 3.6f,
Expand All @@ -71,7 +71,7 @@ int main() {
{
.inputs = {0.1f, -0.2f, 0.3f},
.weights = {0.3f, -0.2f, 0.1f},
.n_weights = 3,
.input_size = 3,
.bias = 0.05f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = 0.15f,
Expand All @@ -80,7 +80,7 @@ int main() {
{
.inputs = {-2.5f, 3.0f, -1.5f},
.weights = {0.5f, -0.5f, 0.75f},
.n_weights = 3,
.input_size = 3,
.bias = 1.0f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = -2.875f,
Expand All @@ -89,7 +89,7 @@ int main() {
{
.inputs = {0.0f, 0.0f, 0.0f},
.weights = {0.25f, -0.75f, 0.5f},
.n_weights = 3,
.input_size = 3,
.bias = 0.5f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = 0.5f,
Expand All @@ -98,7 +98,7 @@ int main() {
{
.inputs = {1.2f, -1.2f, 0.8f},
.weights = {0.0f, 0.0f, 0.0f},
.n_weights = 3,
.input_size = 3,
.bias = 0.25f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = 0.25f,
Expand All @@ -107,7 +107,7 @@ int main() {
{
.inputs = {1.0f, -1.0f, 1.0f},
.weights = {-1.0f, 1.0f, -1.0f},
.n_weights = 3,
.input_size = 3,
.bias = -0.5f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = -3.5f,
Expand All @@ -116,7 +116,7 @@ int main() {
{
.inputs = {0.123f, 0.456f, -0.789f},
.weights = {0.321f, -0.654f, 0.987f},
.n_weights = 3,
.input_size = 3,
.bias = 0.1f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = -0.937484,
Expand All @@ -125,7 +125,7 @@ int main() {
{
.inputs = {0.001f, -0.002f, 0.003f},
.weights = {0.004f, 0.005f, -0.006f},
.n_weights = 3,
.input_size = 3,
.bias = 0.0f,
.output_tolerance = DEFAULT_OUTPUT_TOLERANCE,
.expected_output = 0.000012f,
Expand Down
Loading
Loading