class_docstr: Dimension allocator for internal use by :class:`plate`.
  There is a single global instance.
  
  Note that dimensions are indexed from the right, e.g. -1, -2.
docstr: Allocate a dimension to an :class:`plate` with given name.
  Dim should be either None for automatic allocation or a negative
  integer for manual allocation.
docstr_tok: ["Allocate","a","dimension","to","an",":","class",":","`","plate","`","with","given","name",".","Dim","should","be","either","None","for","automatic","allocation","or","a","negative","integer","for","manual","allocation","."]
code: def allocate(self, name: str, dim: Optional[int]) -> int:
      """
      Allocate a dimension to an :class:`plate` with given name.
      Dim should be either None for automatic allocation or a negative
      integer for manual allocation.
      """
      if name in self._stack:
          raise ValueError(f"duplicate plate '{name}'")
      if dim is None:
          # Automatically designate the rightmost available dim for allocation.
          dim = -1
          while (
              -dim <= len(self._stack)
              and self._stack[-1 - dim] is not None
          ):
              dim -= 1
      elif dim >= 0:
          raise ValueError(
              f"Expected dim < 0 to index from the right, actual {dim}"
          )
  
      # Allocate the requested dimension.
      while dim < -len(self._stack):
          self._stack.append(None)
      if self._stack[-1 - dim] is not None:
          raise ValueError(
              "\n".join(
                  [
                      'at plates "{}" and "{}", collide at dim={}'.format(
                          name, self._stack[-1 - dim], dim
                      ),
                      "\nTry moving the dim of one plate to the left, e.g. dim={}".format(
                          dim - 1
                      ),
                  ]
              )
          )
      self._stack[-1 - dim] = name
      return dim
  
imports: import functools
  from typing import TYPE_CHECKING, Callable, Dict, Generic, List, Optional, Set, Tuple, TypeVar, Union, overload
  import torch
  from typing_extensions import Literal, ParamSpec, TypedDict
  from pyro.params.param_store import _MODULE_NAMESPACE_DIVIDER, ParamStoreDict
label_desc: Use image node_id 2 for calling the _DimAllocator obj's underlying member method code with example usage: obj.allocate(name, dim) and returns: dim