importance sampler value and gradient: fix nan issue and optimize