class_docstr: Base class for all optimizers.
  
  .. warning::
      Parameters need to be specified as collections that have a deterministic
      ordering that is consistent between runs. Examples of objects that don't
      satisfy those properties are sets and iterators over values of dictionaries.
  
  Args:
      params (iterable): an iterable of :class:`torch.Tensor` s or
          :class:`dict` s. Specifies what Tensors should be optimized.
      defaults: (dict): a dict containing default values of optimization
          options (used when a parameter group doesn't specify them).
class_docstr_tok: ["Base","class","for","all","optimizers",".","..","warning",":",":","Parameters","need","to","be","specified","as","collections","that","have","a","deterministic","ordering","that","is","consistent","between","runs",".","Examples","of","objects","that","don't","satisfy","those","properties","are","sets","and","iterators","over","values","of","dictionaries",".","Args",":","params","(","iterable",")",":","an","iterable","of",":","class",":","`","torch.Tensor","`","s","or",":","class",":","`","dict","`","s.","Specifies","what","Tensors","should","be","optimized",".","defaults",":","(","dict",")",":","a","dict","containing","default","values","of","optimization","options","(","used","when","a","parameter","group","does","n't","specify","them",")","."]
docstr: Returns the state of the optimizer as a :class:`dict`.
  
  It contains two entries:
  
  * ``state``: a Dict holding current optimization state. Its content
      differs between optimizer classes, but some common characteristics
      hold. For example, state is saved per parameter, and the parameter
      itself is NOT saved. ``state`` is a Dictionary mapping parameter ids
      to a Dict with state corresponding to each parameter.
  * ``param_groups``: a List containing all parameter groups where each
      parameter group is a Dict. Each parameter group contains metadata
      specific to the optimizer, such as learning rate and weight decay,
      as well as a List of parameter IDs of the parameters in the group.
  
  NOTE: The parameter IDs may look like indices but they are just IDs
  associating state with param_group. When loading from a state_dict,
  the optimizer will zip the param_group ``params`` (int IDs) and the
  optimizer ``param_groups`` (actual ``nn.Parameter`` s) in order to
  match state WITHOUT additional verification.
  
  A returned state dict might look something like:
  
  .. code-block:: text
  
      {
          'state': {
              0: {'momentum_buffer': tensor(...), ...},
              1: {'momentum_buffer': tensor(...), ...},
              2: {'momentum_buffer': tensor(...), ...},
              3: {'momentum_buffer': tensor(...), ...}
          },
          'param_groups': [
              {
                  'lr': 0.01,
                  'weight_decay': 0,
                  ...
                  'params': [0]
              },
              {
                  'lr': 0.001,
                  'weight_decay': 0.5,
                  ...
                  'params': [1, 2, 3]
              }
          ]
      }
docstr_tok: ["Returns","the","state","of","the","optimizer","as","a",":","class",":","`","dict","`",".","It","contains","two","entries",":","*","``","state","``",":","a","Dict","holding","current","optimization","state",".","Its","content","differs","between","optimizer","classes",",","but","some","common","characteristics","hold",".","For","example",",","state","is","saved","per","parameter",",","and","the","parameter","itself","is","NOT","saved",".","``","state","``","is","a","Dictionary","mapping","parameter","ids","to","a","Dict","with","state","corresponding","to","each","parameter",".","*","``","param_groups","``",":","a","List","containing","all","parameter","groups","where","each","parameter","group","is","a","Dict",".","Each","parameter","group","contains","metadata","specific","to","the","optimizer",",","such","as","learning","rate","and","weight","decay",",","as","well","as","a","List","of","parameter","IDs","of","the","parameters","in","the","group",".","NOTE",":","The","parameter","IDs","may","look","like","indices","but","they","are","just","IDs","associating","state","with","param_group",".","When","loading","from","a","state_dict",",","the","optimizer","will","zip","the","param_group","``","params","``","(","int","IDs",")","and","the","optimizer","``","param_groups","``","(","actual","``","nn.Parameter","``","s",")","in","order","to","match","state","WITHOUT","additional","verification",".","A","returned","state","dict","might","look","something","like",":","..","code-block",":",":","text","{","'state","'",":","{","0",":","{","'momentum_buffer","'",":","tensor","(","...",")",",","...","}",",","1",":","{","'momentum_buffer","'",":","tensor","(","...",")",",","...","}",",","2",":","{","'momentum_buffer","'",":","tensor","(","...",")",",","...","}",",","3",":","{","'momentum_buffer","'",":","tensor","(","...",")",",","...","}","}",",","'param_groups","'",":","[","{","'lr","'",":","0.01",",","'weight_decay","'",":","0",",","...","'params","'",":","[","0","]","}",",","{","'lr","'",":","0.001",",","'weight_decay","'",":","0.5",",","...","'params","'",":","[","1",",","2",",","3","]","}","]","}"]
code: def state_dict(self) -> StateDict:
      r"""Returns the state of the optimizer as a :class:`dict`.
  
      It contains two entries:
  
      * ``state``: a Dict holding current optimization state. Its content
          differs between optimizer classes, but some common characteristics
          hold. For example, state is saved per parameter, and the parameter
          itself is NOT saved. ``state`` is a Dictionary mapping parameter ids
          to a Dict with state corresponding to each parameter.
      * ``param_groups``: a List containing all parameter groups where each
          parameter group is a Dict. Each parameter group contains metadata
          specific to the optimizer, such as learning rate and weight decay,
          as well as a List of parameter IDs of the parameters in the group.
  
      NOTE: The parameter IDs may look like indices but they are just IDs
      associating state with param_group. When loading from a state_dict,
      the optimizer will zip the param_group ``params`` (int IDs) and the
      optimizer ``param_groups`` (actual ``nn.Parameter`` s) in order to
      match state WITHOUT additional verification.
  
      A returned state dict might look something like:
  
      .. code-block:: text
  
          {
              'state': {
                  0: {'momentum_buffer': tensor(...), ...},
                  1: {'momentum_buffer': tensor(...), ...},
                  2: {'momentum_buffer': tensor(...), ...},
                  3: {'momentum_buffer': tensor(...), ...}
              },
              'param_groups': [
                  {
                      'lr': 0.01,
                      'weight_decay': 0,
                      ...
                      'params': [0]
                  },
                  {
                      'lr': 0.001,
                      'weight_decay': 0.5,
                      ...
                      'params': [1, 2, 3]
                  }
              ]
          }
  
      """
  
      for pre_hook in self._optimizer_state_dict_pre_hooks.values():
          pre_hook(self)
  
      # Save order indices instead of Tensors
      param_mappings: Dict[int, int] = {}
      start_index = 0
  
      def pack_group(group: Dict[str, Any]) -> Dict[str, Any]:
          nonlocal start_index
          packed = {k: v for k, v in group.items() if k != "params"}
          param_mappings.update(
              {
                  id(p): i
                  for i, p in enumerate(group["params"], start_index)
                  if id(p) not in param_mappings
              }
          )
          packed["params"] = [
              param_mappings[id(p)] for p in group["params"]
          ]
          start_index += len(packed["params"])
          return packed
  
      param_groups = [pack_group(g) for g in self.param_groups]
      # Remap state to use order indices as keys
      packed_state = {
          (
              param_mappings[id(k)]
              if isinstance(k, torch.Tensor)
              else k
          ): v
          for k, v in self.state.items()
      }
  
      state_dict = {
          "state": packed_state,
          "param_groups": param_groups,
      }
  
      for post_hook in self._optimizer_state_dict_post_hooks.values():
          hook_result = post_hook(self, state_dict)
          if hook_result is not None:
              state_dict = hook_result
      return state_dict
  
imports: import math
  import functools
  import warnings
  from collections import OrderedDict, defaultdict
  from copy import deepcopy
  from itertools import chain
  from typing import Any, Callable, DefaultDict, Dict, Hashable, Iterable, List, Optional, Set, Tuple, TypeVar, Union, cast, overload
  from typing_extensions import ParamSpec, Self, TypeAlias
  import torch
  import torch.utils.hooks
  from torch.utils.hooks import RemovableHandle
  from torch.utils._foreach_utils import Indices, TensorListList, _get_foreach_kernels_supported_devices, _get_fused_kernels_supported_devices
  from torch._utils import is_compiling
  from torch.utils._foreach_utils import _group_tensors_by_device_and_dtype
label_desc: Use image node_id 14 for calling the Optimizer obj's underlying member method code with example usage: obj.state_dict() and returns: state_dict, packed