From 7745445cb892249888607644e29c210edd57f9e3 Mon Sep 17 00:00:00 2001 From: Chunseok Lee Date: Thu, 26 Sep 2024 14:37:37 +0900 Subject: [PATCH 1/2] [onert_micro] workaround for tanh for tizenrt - TizenRT platform produces tanh(x) NaN for large x - reason behind this patch is that tanh(x) is almost 1 for x > 15 ONE-DCO-1.0-Signed-off-by: Chunseok Lee --- onert-micro/onert-micro/include/pal/common/PALGRUCommon.h | 4 ++++ onert-micro/onert-micro/include/pal/common/PALGRUWeightGrad.h | 3 +++ 2 files changed, 7 insertions(+) diff --git a/onert-micro/onert-micro/include/pal/common/PALGRUCommon.h b/onert-micro/onert-micro/include/pal/common/PALGRUCommon.h index 945122c32d7..6bffdc0c85d 100644 --- a/onert-micro/onert-micro/include/pal/common/PALGRUCommon.h +++ b/onert-micro/onert-micro/include/pal/common/PALGRUCommon.h @@ -144,7 +144,11 @@ void calculateGRU(const float *input_data, const float *weight_input_data, } for (int i = 0; i < num_elements; ++i) { + if(second_hidden_part[i] > 19) {second_hidden_part[i] = 1;} + else if(second_hidden_part[i] < -19) {second_hidden_part[i] = -1;} + else { second_hidden_part[i] = std::tanh(second_hidden_part[i]); + } } // If train mode - save mul input (left and right) diff --git a/onert-micro/onert-micro/include/pal/common/PALGRUWeightGrad.h b/onert-micro/onert-micro/include/pal/common/PALGRUWeightGrad.h index 1f3cd750c29..38c2bfb671c 100644 --- a/onert-micro/onert-micro/include/pal/common/PALGRUWeightGrad.h +++ b/onert-micro/onert-micro/include/pal/common/PALGRUWeightGrad.h @@ -62,6 +62,9 @@ void calculateGRUWeightGrads( float tanh_grad_value; { float tanh = std::tanh(tanh_data[i]); + if(tanh_data[i] > 19) {tanh = 1;} + else if(tanh_data[i] < -19) {tanh = -1;} + tanh_grad_value = (1 - tanh * tanh) * right_middle_mul; } From 42786393a5a0c0200440b23f3ee56784178542f7 Mon Sep 17 00:00:00 2001 From: Chunseok Lee Date: Thu, 26 Sep 2024 17:19:17 +0900 Subject: [PATCH 2/2] fix format --- .../onert-micro/include/pal/common/PALGRUCommon.h | 15 +++++++++++---- .../include/pal/common/PALGRUWeightGrad.h | 10 ++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/onert-micro/onert-micro/include/pal/common/PALGRUCommon.h b/onert-micro/onert-micro/include/pal/common/PALGRUCommon.h index 6bffdc0c85d..8206bda4328 100644 --- a/onert-micro/onert-micro/include/pal/common/PALGRUCommon.h +++ b/onert-micro/onert-micro/include/pal/common/PALGRUCommon.h @@ -144,10 +144,17 @@ void calculateGRU(const float *input_data, const float *weight_input_data, } for (int i = 0; i < num_elements; ++i) { - if(second_hidden_part[i] > 19) {second_hidden_part[i] = 1;} - else if(second_hidden_part[i] < -19) {second_hidden_part[i] = -1;} - else { - second_hidden_part[i] = std::tanh(second_hidden_part[i]); + if (second_hidden_part[i] > 19) + { + second_hidden_part[i] = 1; + } + else if (second_hidden_part[i] < -19) + { + second_hidden_part[i] = -1; + } + else + { + second_hidden_part[i] = std::tanh(second_hidden_part[i]); } } diff --git a/onert-micro/onert-micro/include/pal/common/PALGRUWeightGrad.h b/onert-micro/onert-micro/include/pal/common/PALGRUWeightGrad.h index 38c2bfb671c..e9194680d13 100644 --- a/onert-micro/onert-micro/include/pal/common/PALGRUWeightGrad.h +++ b/onert-micro/onert-micro/include/pal/common/PALGRUWeightGrad.h @@ -62,8 +62,14 @@ void calculateGRUWeightGrads( float tanh_grad_value; { float tanh = std::tanh(tanh_data[i]); - if(tanh_data[i] > 19) {tanh = 1;} - else if(tanh_data[i] < -19) {tanh = -1;} + if (tanh_data[i] > 19) + { + tanh = 1; + } + else if (tanh_data[i] < -19) + { + tanh = -1; + } tanh_grad_value = (1 - tanh * tanh) * right_middle_mul; }