gradient computed using caching