def memoize_autodiff(func):
    """
    Memoize to avoid rebuilding of the gradient tree.
    """
    cache = {}

    @wraps(func)
    def memoizer(op_tree, be, next_error=None):
        """
        If params in the caches, return results directly. Othewise, add to cache
        and return the results.
        """
        key = (op_tree.key(), be, next_error)
        if key not in cache:
            cache[key] = func(op_tree, be, next_error)
            # print 'created grad_tree cache'
        return cache[key]
    return memoizer
dataset/ETHPy150Open NervanaSystems/neon/neon/backends/autodiff.py/memoize_autodiff/original