parameters: (pyfunc)
return_statement: return _CLASS_FUNCTION(handle, False)
docstring: Convert a python function to TVM function
  
      Parameters
      ----------
      pyfunc : python function
          The python function to be converted.
  
      Returns
      -------
      tvmfunc: tvm.nd.Function
          The converted tvm function.
docstring_summary: Convert a python function to TVM function
docstring_tokens: Convert a python function to TVM function
function: def convert_to_tvm_func(pyfunc):
      """Convert a python function to TVM function
  
      Parameters
      ----------
      pyfunc : python function
          The python function to be converted.
  
      Returns
      -------
      tvmfunc: tvm.nd.Function
          The converted tvm function.
      """
      local_pyfunc = pyfunc
      def cfun(args, type_codes, num_args, ret, _):
          """ ctypes function """
          num_args = num_args.value if isinstance(num_args, ctypes.c_int) else num_args
          pyargs = (C_TO_PY_ARG_SWITCH[type_codes[i]](args[i]) for i in range(num_args))
          # pylint: disable=broad-except
          try:
              rv = local_pyfunc(*pyargs)
          except Exception:
              msg = traceback.format_exc()
              _LIB.TVMAPISetLastError(c_str(msg))
              return -1
  
          if rv is not None:
              if isinstance(rv, tuple):
                  raise ValueError("PackedFunction can only support one return value")
              temp_args = []
              values, tcodes, _ = _make_tvm_args((rv,), temp_args)
              if not isinstance(ret, TVMRetValueHandle):
                  ret = TVMRetValueHandle(ret)
              check_call(_LIB.TVMCFuncSetReturn(ret, values, tcodes, ctypes.c_int(1)))
              _ = temp_args
              _ = rv
          return 0
  
      handle = FunctionHandle()
      f = TVMPackedCFunc(cfun)
      # NOTE: We will need to use python-api to increase ref count of the f
      # TVM_FREE_PYOBJ will be called after it is no longer needed.
      pyobj = ctypes.py_object(f)
      ctypes.pythonapi.Py_IncRef(pyobj)
      check_call(_LIB.TVMFuncCreateFromCFunc(
          f, pyobj, TVM_FREE_PYOBJ, ctypes.byref(handle)))
      return _CLASS_FUNCTION(handle, False)
function_tokens: def convert_to_tvm_func ( pyfunc ) : local_pyfunc = pyfunc def cfun ( args , type_codes , num_args , ret , _ ) : """ ctypes function """ num_args = num_args . value if isinstance ( num_args , ctypes . c_int ) else num_args pyargs = ( C_TO_PY_ARG_SWITCH [ type_codes [ i ] ] ( args [ i ] ) for i in range ( num_args ) ) # pylint: disable=broad-except try : rv = local_pyfunc ( * pyargs ) except Exception : msg = traceback . format_exc ( ) _LIB . TVMAPISetLastError ( c_str ( msg ) ) return - 1 if rv is not None : if isinstance ( rv , tuple ) : raise ValueError ( "PackedFunction can only support one return value" ) temp_args = [ ] values , tcodes , _ = _make_tvm_args ( ( rv , ) , temp_args ) if not isinstance ( ret , TVMRetValueHandle ) : ret = TVMRetValueHandle ( ret ) check_call ( _LIB . TVMCFuncSetReturn ( ret , values , tcodes , ctypes . c_int ( 1 ) ) ) _ = temp_args _ = rv return 0 handle = FunctionHandle ( ) f = TVMPackedCFunc ( cfun ) # NOTE: We will need to use python-api to increase ref count of the f # TVM_FREE_PYOBJ will be called after it is no longer needed. pyobj = ctypes . py_object ( f ) ctypes . pythonapi . Py_IncRef ( pyobj ) check_call ( _LIB . TVMFuncCreateFromCFunc ( f , pyobj , TVM_FREE_PYOBJ , ctypes . byref ( handle ) ) ) return _CLASS_FUNCTION ( handle , False )