code: def backward(
      tensors: _TensorOrTensors,
      grad_tensors: Optional[_TensorOrTensors] = None,
      retain_graph: Optional[bool] = None,
      create_graph: bool = False,
      grad_variables: Optional[_TensorOrTensors] = None,
      inputs: Optional[_TensorOrTensorsOrGradEdge] = None,
  ) -> None:
      r"""Computes the sum of gradients of given tensors with respect to graph
      leaves.
  
      The graph is differentiated using the chain rule. If any of ``tensors``
      are non-scalar (i.e. their data has more than one element) and require
      gradient, then the Jacobian-vector product would be computed, in this
      case the function additionally requires specifying ``grad_tensors``.
      It should be a sequence of matching length, that contains the "vector"
      in the Jacobian-vector product, usually the gradient of the differentiated
      function w.r.t. corresponding tensors (``None`` is an acceptable value for
      all tensors that don't need gradient tensors).
  
      This function accumulates gradients in the leaves - you might need to zero
      ``.grad`` attributes or set them to ``None`` before calling it.
      See :ref:`Default gradient layouts<default-grad-layouts>`
      for details on the memory layout of accumulated gradients.
  
      .. note::
          Using this method with ``create_graph=True`` will create a reference cycle
          between the parameter and its gradient which can cause a memory leak.
          We recommend using ``autograd.grad`` when creating the graph to avoid this.
          If you have to use this function, make sure to reset the ``.grad`` fields of your
          parameters to ``None`` after use to break the cycle and avoid the leak.
  
      .. note::
  
          If you run any forward ops, create ``grad_tensors``, and/or call ``backward``
          in a user-specified CUDA stream context, see
          :ref:`Stream semantics of backward passes<bwd-cuda-stream-semantics>`.
  
      .. note::
  
          When ``inputs`` are provided and a given input is not a leaf,
          the current implementation will call its grad_fn (even though it is not strictly needed to get this gradients).
          It is an implementation detail on which the user should not rely.
          See https://github.com/pytorch/pytorch/pull/60521#issuecomment-867061780 for more details.
  
      Args:
          tensors (Sequence[Tensor] or Tensor): Tensors of which the derivative will be
              computed.
          grad_tensors (Sequence[Tensor or None] or Tensor, optional): The "vector" in
              the Jacobian-vector product, usually gradients w.r.t. each element of
              corresponding tensors. None values can be specified for scalar Tensors or
              ones that don't require grad. If a None value would be acceptable for all
              grad_tensors, then this argument is optional.
          retain_graph (bool, optional): If ``False``, the graph used to compute the grad
              will be freed. Note that in nearly all cases setting this option to ``True``
              is not needed and often can be worked around in a much more efficient
              way. Defaults to the value of ``create_graph``.
          create_graph (bool, optional): If ``True``, graph of the derivative will
              be constructed, allowing to compute higher order derivative products.
              Defaults to ``False``.
          inputs (Sequence[Tensor] or Tensor or Sequence[GradientEdge], optional): Inputs w.r.t. which the gradient
              be will accumulated into ``.grad``. All other Tensors will be ignored. If
              not provided, the gradient is accumulated into all the leaf Tensors that
              were used to compute the attr::tensors.
      """
      if torch._C._are_functorch_transforms_active():
          raise RuntimeError(
              "backward() called inside a functorch transform. This is not "
              "supported, please use functorch.grad or functorch.vjp instead "
              "or call backward() outside of functorch transforms."
          )
  
      if grad_variables is not None:
          warnings.warn(
              "'grad_variables' is deprecated. Use 'grad_tensors' instead."
          )
          if grad_tensors is None:
              grad_tensors = grad_variables
          else:
              raise RuntimeError(
                  "'grad_tensors' and 'grad_variables' (deprecated) "
                  "arguments both passed to backward(). Please only "
                  "use 'grad_tensors'."
              )
      if inputs is not None and len(inputs) == 0:
          raise RuntimeError(
              "'inputs' argument to backward() cannot be empty."
          )
  
      tensors = (
          (tensors,)
          if isinstance(tensors, torch.Tensor)
          else tuple(tensors)
      )
      inputs = (
          (inputs,)
          if isinstance(inputs, (torch.Tensor, graph.GradientEdge))
          else tuple(inputs)
          if inputs is not None
          else tuple()
      )
  
      grad_tensors_ = _tensor_or_tensors_to_tuple(
          grad_tensors, len(tensors)
      )
      grad_tensors_ = _make_grads(
          tensors, grad_tensors_, is_grads_batched=False
      )
      if retain_graph is None:
          retain_graph = create_graph
  
      # The reason we repeat the same comment below is that
      # some Python versions print out the first line of a multi-line function
      # calls in the traceback and some print out the last line
      _engine_run_backward(
          tensors,
          grad_tensors_,
          retain_graph,
          create_graph,
          inputs,
          allow_unreachable=True,
          accumulate_grad=True,
      )
  
imports: import warnings
  from typing import Any, Callable, cast, List, Optional, Sequence, Tuple, Union
  import torch
  from torch.types import _size, _TensorOrTensors, _TensorOrTensorsOrGradEdge
  from ..None import _vmap_internals
  from ..overrides import handle_torch_function, has_torch_function, is_tensor_like
  from .None import forward_ad, functional, graph
  from .anomaly_mode import detect_anomaly, set_detect_anomaly
  from .function import Function, NestedIOFunction
  from .grad_mode import _force_original_view_tracking, _unsafe_preserve_version_counter, enable_grad, inference_mode, no_grad, set_grad_enabled, set_multithreading_enabled
  from .gradcheck import gradcheck, gradgradcheck
  from .graph import _engine_run_backward
  from .variable import Variable
  from torch._C._autograd import _add_metadata_json, _disable_profiler, _disable_profiler_legacy, _enable_profiler, _enable_profiler_legacy, _enable_record_function, _get_sequence_nr, _kineto_step, _KinetoEvent, _pop_saved_tensors_default_hooks, _prepare_profiler, _profiler_enabled, _ProfilerResult, _push_saved_tensors_default_hooks, _record_function_with_args_enter, _record_function_with_args_exit, _set_empty_test_observer, _supported_activities, DeviceType, kineto_available, ProfilerEvent, SavedTensor
  from torch._C._profiler import ProfilerActivity, ProfilerConfig, ProfilerState
  from .None import profiler
label_desc: Use image node_id 4 for calling a global function with example usage: backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs) without return types