code: def generic_func(fdefault):
      """Wrap a target generic function.
  
      Generic function allows registration of further functions
      that can be dispatched on current target context.
      If no registered dispatch is matched, the fdefault will be called.
  
      Parameters
      ----------
      fdefault : function
          The default function.
  
      Returns
      -------
      fgeneric : function
          A wrapped generic function.
  
      Example
      -------
      .. code-block:: python
  
        import tvm
        # wrap function as target generic
        @tvm.target.generic_func
        def my_func(a):
            return a + 1
        # register specialization of my_func under target cuda
        @my_func.register("cuda")
        def my_func_cuda(a):
            return a + 2
        # displays 3, because my_func is called
        print(my_func(2))
        # displays 4, because my_func_cuda is called
        with tvm.target.cuda():
            print(my_func(2))
      """
      dispatch_dict = {}
      func_name = fdefault.__name__
  
      def register(key, func=None, override=False):
          """Register function to be the dispatch function.
  
          Parameters
          ----------
          key : str or list of str
              The key to be registered.
  
          func : function
              The function to be registered.
  
          override : bool
              Whether override existing registration.
  
          Returns
          -------
          The register function is necessary.
          """
  
          def _do_reg(myf):
              key_list = [key] if isinstance(key, str) else key
              for k in key_list:
                  if k in dispatch_dict and not override:
                      raise ValueError(
                          "Key is already registered for %s" % func_name
                      )
                  dispatch_dict[k] = myf
              return myf
  
          if func:
              return _do_reg(func)
          return _do_reg
  
      def dispatch_func(func, *args, **kwargs):
          """The wrapped dispatch function"""
          target = Target.current()
          if target is None:
              return func(*args, **kwargs)
          for k in target.keys:
              if k in dispatch_dict:
                  return dispatch_dict[k](*args, **kwargs)
          return func(*args, **kwargs)
  
      def get_packed_func():
          """The wrapped to get dispatched function"""
          target = Target.current()
          if target is None:
              return fdefault
          for k in target.keys:
              if k in dispatch_dict:
                  return dispatch_dict[k]
          return fdefault
  
      fdecorate = decorate(fdefault, dispatch_func)
      fdecorate.register = register
      fdecorate.fdefault = fdefault
      fdecorate.dispatch_dict = dispatch_dict
      fdecorate.get_packed_func = get_packed_func
      return fdecorate
  
file_path: tvm/python/tvm/target/generic_func.py
imports: import tvm._ffi
  from tvm.runtime import Object
  from .target import Target
  from .None import _ffi_api
label_desc: Use image node_id 3 for calling a global function with example usage: generic_func(fdefault) and returns: fdecorate, _do_reg, func, fdefault, myf, _do_reg, func, fdefault, dispatch_dict, dispatch_dict