Used CachedDiffFunction for caching the gradient result.