code: def memoize(f):
      """
      Memoize decorator
  
      Uses a cache provided through `memoize.bind`
      """
  
      @functools.wraps(f)
      def wrapper(
          *args,
          _private_cache_kwarg: Cache = None,
          _expect_cache_hit=None,
          **kwargs,
      ):
          cache = _private_cache_kwarg
  
          if cache is None:  # no caching
              assert _expect_cache_hit in [False, None]
              return f(*args, **kwargs)
  
          result_cache, hit_counter = cache
  
          # Retrieve this functions (local) cache from the global cache.
          result_cache = result_cache[f]
          hit_counter = hit_counter[f]
  
          # Construct the cache key from `args` and `kwargs`.
  
          # there could be nested memoize functions, so need to pass around
          # _Cache objects to bind the inner function -- explicitly ignore
          # such objects from the key.
          key_args = [x for x in args if not isinstance(x, _Cache)]
          key_kwargs = [
              (k, v)
              for (k, v) in sorted(kwargs.items())
              if not isinstance(v, _Cache)
          ]
          key = (tuple(key_args), tuple(key_kwargs))
  
          key = _ensure_hashable(key)
  
          if key in result_cache:
              assert _expect_cache_hit in [True, None]
              hit_counter[key] += 1
              result = result_cache[key]
          else:
              assert _expect_cache_hit in [False, None]
              result = f(*args, **kwargs)
              result_cache[key] = result
          return result
  
      # for inspection/testing:
      wrapper.get_cache_hits = lambda cache, key: cache.hits[f][key]
      wrapper.get_total_cache_hits = lambda cache: sum(
          cache.hits[f].values()
      )
      return wrapper
  
file_path: NeuralCompression/projects/torch_vct/utils/memoize.py
imports: import collections
  import functools
  from typing import Callable, Optional, Tuple, TypeVar, Union
  from torch import Tensor
label_desc: Use image node_id 3 for calling a global function with example usage: memoize(f) and returns: wrapper, result, f