             // c = c'*g1 + u0*(1-g1) = (c'-u0)*g1 + g0
 
             // gradient with respect to values in the second gate g2
-            const auto gg2 = gh_val*(c_val*mask-x_val)*(g2*(1.f-g2));
+            const auto gg2 = gh_val*(c_val-x_val)*mask*(g2*(1.f-g2));
             gbias2 += gg2;
             gwc2 += gg2*prev_c_val;
             *gvp2 = gg2*prev_c_val;

            // c = c'*g1 + u0*(1-g1) = (c'-u0)*g1 + u0

            // gradient with respect to values in the second gate g2
            const auto gg2 = gh_val*(c_val*mask-x_val)*(g2*(1.f-g2));
            gbias2 += gg2;
            gwc2 += gg2*prev_c_val;
            *gvp2 = gg2*prev_c_val;

            const auto g2 = sigmoidf(u2 + wc2*cur + bias2);
            cur = (cur-u0)*g1 + u0;
            const auto val = calc_activation(activation_type, cur);
            *hp = skip_type ? ((val - x_val) * mask * g2 + x_val) : (val * mask * g2);
        } 
        //else {
        //    *hp = 0;  // ouptut 0 for a pad token
