From 9d2d61703d36fe2b121b8b08d708056c79df5407 Mon Sep 17 00:00:00 2001 From: julienChemillier Date: Thu, 9 Mar 2023 14:27:23 +0100 Subject: [PATCH] Replace min_float by fminf --- src/cnn/function.c | 13 ++++--------- src/cnn/include/function.h | 5 ----- src/cnn/include/initialisation.h | 1 + src/cnn/make.c | 14 ++------------ src/cnn/make.cu | 14 ++------------ src/scripts/convolution_benchmark.cu | 5 +---- src/scripts/matrix_multiplication_benchmark.cu | 6 +----- 7 files changed, 11 insertions(+), 47 deletions(-) diff --git a/src/cnn/function.c b/src/cnn/function.c index f576712..5fba576 100644 --- a/src/cnn/function.c +++ b/src/cnn/function.c @@ -5,12 +5,7 @@ #include "../include/colors.h" #include "include/function.h" - - -float max_float(float a, float b) { - return a < b ? b:a; -} - +#define BOUND_RELU 15 float identity(float x) { return x; @@ -33,7 +28,7 @@ float sigmoid_derivative(float x) { float relu(float x) { - return max_float(0, x); + return fmaxf(0, fminf(x, BOUND_RELU)); } float relu_derivative(float x) { @@ -45,7 +40,7 @@ float relu_derivative(float x) { float leaky_relu(float x) { if (x>0) - return x; + return fminf(x, BOUND_RELU); return x*LEAKER; } @@ -72,7 +67,7 @@ void apply_softmax_input(float ***input, int depth, int rows, int columns) { for (int i=0; i < depth; i++) { for (int j=0; j < rows; j++) { for (int k=0; k < columns; k++) { - m = max_float(m, input[i][j][k]); + m = fmaxf(m, input[i][j][k]); } } } diff --git a/src/cnn/include/function.h b/src/cnn/include/function.h index da44006..4e1b770 100644 --- a/src/cnn/include/function.h +++ b/src/cnn/include/function.h @@ -16,11 +16,6 @@ typedef float (*ptr)(float); typedef ptr (*pm)(); -/* -* Fonction max pour les floats -*/ -float max_float(float a, float b); - float identity(float x); float identity_derivative(float x); diff --git a/src/cnn/include/initialisation.h b/src/cnn/include/initialisation.h index ceef35d..20dd38a 100644 --- a/src/cnn/include/initialisation.h +++ b/src/cnn/include/initialisation.h @@ -8,6 +8,7 @@ #define ZERO 0 #define GLOROT 1 #define XAVIER 1 // Xavier et Glorot initialisations sont indentiques +#define NORMALIZED_GLOROT 2 #define NORMALIZED_XAVIER 2 #define HE 3 diff --git a/src/cnn/make.c b/src/cnn/make.c index eb8e096..bdd657f 100644 --- a/src/cnn/make.c +++ b/src/cnn/make.c @@ -1,5 +1,6 @@ #include #include +#include #include "include/convolution.h" #include "../include/colors.h" @@ -11,17 +12,6 @@ #define BLOCKSIZE_y 8 #define BLOCKSIZE_z 8 -float max_flt(float a, float b) { - // Return the max between the two floats - if (a > b) { - return a; - } - return b; -} - - - - /* * Average Pooling @@ -142,7 +132,7 @@ void make_max_pooling_cpu(float*** input, float*** output, int size, int output_ m = FLT_MIN; for (int a=0; a < size; a++) { for (int b=0; b < size; b++) { - m = max_flt(m, input[i][size*j +a][size*k +b]); + m = fmaxf(m, input[i][size*j +a][size*k +b]); } } output[i][j][k] = m; diff --git a/src/cnn/make.cu b/src/cnn/make.cu index eb8e096..bdd657f 100644 --- a/src/cnn/make.cu +++ b/src/cnn/make.cu @@ -1,5 +1,6 @@ #include #include +#include #include "include/convolution.h" #include "../include/colors.h" @@ -11,17 +12,6 @@ #define BLOCKSIZE_y 8 #define BLOCKSIZE_z 8 -float max_flt(float a, float b) { - // Return the max between the two floats - if (a > b) { - return a; - } - return b; -} - - - - /* * Average Pooling @@ -142,7 +132,7 @@ void make_max_pooling_cpu(float*** input, float*** output, int size, int output_ m = FLT_MIN; for (int a=0; a < size; a++) { for (int b=0; b < size; b++) { - m = max_flt(m, input[i][size*j +a][size*k +b]); + m = fmaxf(m, input[i][size*j +a][size*k +b]); } } output[i][j][k] = m; diff --git a/src/scripts/convolution_benchmark.cu b/src/scripts/convolution_benchmark.cu index bd40c4c..605b72d 100644 --- a/src/scripts/convolution_benchmark.cu +++ b/src/scripts/convolution_benchmark.cu @@ -77,9 +77,6 @@ void free_matrix(float*** matrix, int n, int p) { free(matrix); } -float max_float(float a, float b) { - return a > b ? a : b; -} bool check_matrices_equality(float*** m1, float*** m2, int n, int p, int q, int acceptation) { float err_max = 0.; @@ -93,7 +90,7 @@ bool check_matrices_equality(float*** m1, float*** m2, int n, int p, int q, int //return false; } err_percent = 2*fabs(m1[i][j][k] - m2[i][j][k])/fabs(m1[i][j][k] + m2[i][j][k]); - err_max = max_float(err_max, err_percent); + err_max = fmaxf(err_max, err_percent); err_moy += err_percent; } } diff --git a/src/scripts/matrix_multiplication_benchmark.cu b/src/scripts/matrix_multiplication_benchmark.cu index 213adb4..a611438 100644 --- a/src/scripts/matrix_multiplication_benchmark.cu +++ b/src/scripts/matrix_multiplication_benchmark.cu @@ -55,10 +55,6 @@ float** create_empty_matrix(int n, int p) { return matrix; } -float max_float(float a, float b) { - return a > b ? a : b; -} - bool check_matrices_equality(float** m1, float** m2, int n, int p) { float err_max = 0.; @@ -71,7 +67,7 @@ bool check_matrices_equality(float** m1, float** m2, int n, int p) { //return false; } err_percent = 2*fabs(m1[i][j] - m2[i][j])/fabs(m1[i][j] + m2[i][j]); - err_max = max_float(err_max, err_percent); + err_max = fmaxf(err_max, err_percent); err_moy += err_percent; } }