| 785 | } |
| 786 | |
| 787 | void ecall_softplus_grad(float *input,float *grad,int N,float *output){ |
| 788 | float *x = (float*)malloc(sizeof(float)*N); |
| 789 | memset(x, 0, sizeof(float)*N); |
| 790 | for(int j=0;j<N;j++){ |
| 791 | x[j] = 0.0f; |
| 792 | float sum = input[insert*N+j]; |
| 793 | float c = 0.0f; |
| 794 | for(int i=0;i<Nt;i++){ |
| 795 | float y = input[indexs[i]*N+j] - c; |
| 796 | float t = sum + y; |
| 797 | c = (t - sum) - y; |
| 798 | sum = t; |
| 799 | } |
| 800 | x[j] = sum; |
| 801 | } |
| 802 | |
| 803 | float *g = (float*)malloc(sizeof(float)*N); |
| 804 | memset(g, 0, sizeof(float)*N); |
| 805 | for(int j=0;j<N;j++){ |
| 806 | g[j] = 0.0f; |
| 807 | float sum = grad[insert*N+j]; |
| 808 | float c = 0.0f; |
| 809 | for(int i=0;i<Nt;i++){ |
| 810 | float y = grad[indexs[i]*N+j] - c; |
| 811 | float t = sum + y; |
| 812 | c = (t - sum) - y; |
| 813 | sum = t; |
| 814 | } |
| 815 | g[j] = sum; |
| 816 | } |
| 817 | |
| 818 | for (int j = 0; j < N; j++) { |
| 819 | float tmp; |
| 820 | if(x[j] > 50.0f){ |
| 821 | tmp = 1.0f; |
| 822 | } |
| 823 | else if(x[j] < -50.0f){ |
| 824 | tmp = 0.0f; |
| 825 | } |
| 826 | else{ |
| 827 | tmp = static_cast<float>(1)/(static_cast<float>(1) + expf(-x[j])); |
| 828 | } |
| 829 | g[j] = g[j]*tmp; |
| 830 | } |
| 831 | |
| 832 | float mean = 0.0f, std, mean_sqr = 0.0f; |
| 833 | float c_mean = 0.0f, c_mean_sqr = 0.0f; |
| 834 | for (int j = 0; j < N; j++) { |
| 835 | float y = (g[j] /static_cast<float>(N)) - c_mean; |
| 836 | float t = mean + y; |
| 837 | c_mean = (t - mean) - y; |
| 838 | mean = t; |
| 839 | |
| 840 | float y2 = (g[j]*g[j]/static_cast<float>(N)) - c_mean_sqr; |
| 841 | float t2 = mean_sqr + y2; |
| 842 | c_mean_sqr = (t2 - mean_sqr) - y2; |
| 843 | mean_sqr = t2; |
| 844 | } |
no test coverage detected