fix grad calculation