code: def _weak_memoize(fn):
      cache = {}
  
      @functools.wraps(fn)
      def memoized_fn(*args):
          key = tuple(map(id, args))
  
          # Allow cache hit only when tensors have not since been mutated.
          version = tuple(arg._version for arg in args)
          if key in cache:
              old_version, result = cache[key]
              if old_version == version:
                  return result
  
          result = fn(*args)
          cache[key] = version, result
          for arg in args:
              weakref.finalize(arg, cache.pop, key, None)
          return result
  
      return memoized_fn
  
imports: import functools
  import weakref
  from collections import namedtuple
  import torch
  from torch.distributions import constraints
  from pyro.distributions.util import broadcast_shape, is_validation_enabled
  from pyro.ops.special import safe_log
  from .torch_distribution import TorchDistribution
label_desc: Use image node_id 5 for calling a global function with example usage: _weak_memoize(fn) and returns: memoized_fn, result, result