code: def invoke_with_optional_args(fn, *args, **kwargs):
      """
      Invokes a function with only the arguments that it
      is written to accept, giving priority to arguments
      that match by-name, using the following rules.
      (1) arguments with matching names are passed by name.
      (2) remaining non-name-matched args are passed by order.
      (3) extra caller arguments that the function cannot
          accept are not passed.
      (4) extra required function arguments that the caller
          cannot provide cause a TypeError to be raised.
      Ordinary python calling conventions are helpful for
      supporting a function that might be revised to accept
      extra arguments in a newer version, without requiring the
      caller to pass those new arguments.  This function helps
      support function callers that might be revised to supply
      extra arguments, without requiring the callee to accept
      those new arguments.
      """
      argspec = inspect.getfullargspec(fn)
      pass_args = []
      used_kw = set()
      unmatched_pos = []
      used_pos = 0
      defaulted_pos = len(argspec.args) - (
          0 if not argspec.defaults else len(argspec.defaults)
      )
      # Pass positional args that match name first, then by position.
      for i, n in enumerate(argspec.args):
          if n in kwargs:
              pass_args.append(kwargs[n])
              used_kw.add(n)
          elif used_pos < len(args):
              pass_args.append(args[used_pos])
              used_pos += 1
          else:
              unmatched_pos.append(len(pass_args))
              pass_args.append(
                  None
                  if i < defaulted_pos
                  else argspec.defaults[i - defaulted_pos]
              )
      # Fill unmatched positional args with unmatched keyword args in order.
      if len(unmatched_pos):
          for k, v in kwargs.items():
              if k in used_kw or k in argspec.kwonlyargs:
                  continue
              pass_args[unmatched_pos[0]] = v
              used_kw.add(k)
              unmatched_pos = unmatched_pos[1:]
              if len(unmatched_pos) == 0:
                  break
          else:
              if unmatched_pos[0] < defaulted_pos:
                  unpassed = ", ".join(
                      argspec.args[u]
                      for u in unmatched_pos
                      if u < defaulted_pos
                  )
                  raise TypeError(
                      f"{fn.__name__}() cannot be passed {unpassed}."
                  )
      # Pass remaining kw args if they can be accepted.
      pass_kw = {
          k: v
          for k, v in kwargs.items()
          if k not in used_kw
          and (k in argspec.kwonlyargs or argspec.varargs is not None)
      }
      # Pass remaining positional args if they can be accepted.
      if argspec.varargs is not None:
          pass_args += list(args[used_pos:])
      return fn(*pass_args, **pass_kw)
  
imports: import contextlib
  import copy
  import inspect
  from collections import OrderedDict
  import torch
label_desc: Use image node_id 8 for calling a global function with example usage: invoke_with_optional_args(fn) and returns: fn