code: def custom_op(
      qualname: str, manual_schema: typing.Optional[str] = None
  ) -> typing.Callable:
      r"""Creates a new CustomOp object.
  
      WARNING: if you're a user, please do not use this directly
      (instead use the torch._custom_ops APIs).
      Also please see the following for a detailed guide on custom ops.
      https://docs.google.com/document/d/1aGWtgxV3HppuxQAdddyPrs74_aEntpkYt9MalnCKnhk
  
      In PyTorch, defining an op (short for "operator") is a two step-process:
      - we need to define (create) the op
      - we need to implement behavior for how the operator interacts with
        various PyTorch subsystems, like CPU/CUDA Tensors, Autograd, etc.
  
      This entrypoint defines the CustomOp object (the first step);
      you must then perform the second step by calling various methods on
      the CustomOp object.
  
      This API is used as a decorator (see examples).
  
      Arguments:
          qualname (str): Should be a string that looks like
              "namespace::operator_name". Operators in PyTorch need a namespace to
              avoid name collisions; a given operator may only be created once.
              If you are writing a Python library, we recommend the namespace to
              be the name of your top-level module. The operator_name must be
              the same as the name of the function you pass to custom_op
              (see examples).
          manual_schema (Optional[str]): Each PyTorch operator needs a schema that
              tells PyTorch the types of the inputs/outputs. If None (default),
              we will infer the schema from the type annotations on the function
              (see examples). Otherwise, if you don't want to use type annotations,
              you may provide us the schema string.
  
      Example::
          >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
          >>> import numpy as np
          >>> from torch import Tensor
          >>>
          >>> # Step 1: define the CustomOp.
          >>> # We need to provide the decorator a "prototype function"
          >>> # (a function with Python ellipses as the body).
          >>> @custom_op("my_library::numpy_sin")
          >>> def numpy_sin(x: Tensor) -> Tensor:
          >>>     ...
          >>>
          >>> # numpy_sin is now an instance of class CustomOp
          >>> print(type(numpy_sin))
          >>>
          >>> # Step 2: Register an implementation for various PyTorch subsystems
          >>>
          >>> # Register an implementation for CPU tensors
          >>> @numpy_sin.impl('cpu')
          >>> def numpy_sin_impl_cpu(x):
          >>>     return torch.from_numpy(np.sin(x.numpy()))
          >>>
          >>> # Register an implementation for CUDA tensors
          >>> @numpy_sin.impl('cuda')
          >>> def numpy_sin_impl_cuda(x):
          >>>     return torch.from_numpy(np.sin(x.cpu().numpy())).to(x.device)
          >>>
          >>> x = torch.randn(3)
          >>> numpy_sin(x)  # calls numpy_sin_impl_cpu
          >>>
          >>> x_cuda = x.cuda()
          >>> numpy_sin(x)  # calls numpy_sin_impl_cuda
  
      """
  
      def inner(func):
          if not inspect.isfunction(func):
              raise ValueError(
                  f"custom_op(...)(func): Expected `func` to be a Python "
                  f"function, got: {type(func)}"
              )
  
          ns, name = parse_qualname(qualname)
          validate_namespace(ns)
          if func.__name__ != name:
              raise ValueError(
                  f"custom_op(qualname='{qualname}', ...)(func): expected `func` "
                  f"to have name '{name}' but got '{func.__name__}'. "
                  f"Please either change the name of `func` or the qualname that "
                  f"is passed to `custom_op`"
              )
  
          schema = (
              infer_schema(func)
              if manual_schema is None
              else manual_schema
          )
          schema_str = f"{name}{schema}"
          function_schema = FunctionSchema.parse(schema_str)
          validate_schema(function_schema)
          if manual_schema is not None:
              validate_function_matches_schema(function_schema, func)
  
          lib = library.Library(ns, "FRAGMENT")
          lib.define(schema_str)
          ophandle = find_ophandle_or_throw(ns, function_schema.name)
          result = CustomOp(
              lib,
              ns,
              function_schema,
              name,
              ophandle,
              _private_access=True,
          )
  
          result.__name__ = func.__name__
          result.__module__ = func.__module__
          result.__doc__ = func.__doc__
  
          library.impl(lib, result._opname, "Autograd")(
              autograd_kernel_indirection(weakref.proxy(result))
          )
  
          torch._C._dispatch_set_report_error_callback(
              ophandle,
              functools.partial(
                  report_error_callback, weakref.proxy(result)
              ),
          )
  
          return result
  
      return inner
  
imports: import dataclasses
  import functools
  import inspect
  import sys
  import typing
  import weakref
  from torchgen.model import FunctionSchema, OperatorName, SchemaKind, BaseType, ListType, BaseTy
  import torch
  import torch._C
  import torch.library
  from torch._library.abstract_impl import AbstractImplCtx
  from torch.library import get_ctx
  from .autograd import autograd_kernel_indirection, construct_autograd_kernel
label_desc: Use image node_id 1 for calling a global function with example usage: custom_op(qualname, manual_schema) and returns: inner, result