class_docstr: Subclass of :class:`torch.nn.Module` whose attributes can be modified by
  Pyro effects. Attributes can be set using helpers :class:`PyroParam` and
  :class:`PyroSample` , and methods can be decorated by :func:`pyro_method` .
  
  **Parameters**
  
  To create a Pyro-managed parameter attribute, set that attribute using
  either :class:`torch.nn.Parameter` (for unconstrained parameters) or
  :class:`PyroParam` (for constrained parameters). Reading that attribute
  will then trigger a :func:`pyro.param <pyro.primitives.param>` statement.
  For example::
  
      # Create Pyro-managed parameter attributes.
      my_module = PyroModule()
      my_module.loc = nn.Parameter(torch.tensor(0.))
      my_module.scale = PyroParam(torch.tensor(1.),
                                  constraint=constraints.positive)
      # Read the attributes.
      loc = my_module.loc  # Triggers a pyro.param statement.
      scale = my_module.scale  # Triggers another pyro.param statement.
  
  Note that, unlike normal :class:`torch.nn.Module` s, :class:`PyroModule` s
  should not be registered with :func:`pyro.module <pyro.primitives.module>`
  statements.  :class:`PyroModule` s can contain other :class:`PyroModule` s
  and normal :class:`torch.nn.Module` s.  Accessing a normal
  :class:`torch.nn.Module` attribute of a :class:`PyroModule` triggers a
  :func:`pyro.module <pyro.primitives.module>` statement.  If multiple
  :class:`PyroModule` s appear in a single Pyro model or guide, they should
  be included in a single root :class:`PyroModule` for that model.
  
  :class:`PyroModule` s synchronize data with the param store at each
  ``setattr``, ``getattr``, and ``delattr`` event, based on the nested name
  of an attribute:
  
  -   Setting ``mod.x = x_init`` tries to read ``x`` from the param store. If a
      value is found in the param store, that value is copied into ``mod``
      and ``x_init`` is ignored; otherwise ``x_init`` is copied into both
      ``mod`` and the param store.
  -   Reading ``mod.x`` tries to read ``x`` from the param store. If a
      value is found in the param store, that value is copied into ``mod``;
      otherwise ``mod``'s value is copied into the param store. Finally
      ``mod`` and the param store agree on a single value to return.
  -   Deleting ``del mod.x`` removes a value from both ``mod`` and the param
      store.
  
  Note two :class:`PyroModule` of the same name will both synchronize with
  the global param store and thus contain the same data.  When creating a
  :class:`PyroModule`, then deleting it, then creating another with the same
  name, the latter will be populated with the former's data from the param
  store. To avoid this persistence, either ``pyro.clear_param_store()`` or
  call :func:`clear` before deleting a :class:`PyroModule` .
  
  :class:`PyroModule` s can be saved and loaded either directly using
  :func:`torch.save` / :func:`torch.load` or indirectly using the param
  store's :meth:`~pyro.params.param_store.ParamStoreDict.save` /
  :meth:`~pyro.params.param_store.ParamStoreDict.load` . Note that
  :func:`torch.load` will be overridden by any values in the param store, so
  it is safest to ``pyro.clear_param_store()`` before loading.
  
  **Samples**
  
  To create a Pyro-managed random attribute, set that attribute using the
  :class:`PyroSample` helper, specifying a prior distribution. Reading that
  attribute will then trigger a :func:`pyro.sample <pyro.primitives.sample>`
  statement. For example::
  
      # Create Pyro-managed random attributes.
      my_module.x = PyroSample(dist.Normal(0, 1))
      my_module.y = PyroSample(lambda self: dist.Normal(self.loc, self.scale))
  
      # Sample the attributes.
      x = my_module.x  # Triggers a pyro.sample statement.
      y = my_module.y  # Triggers one pyro.sample + two pyro.param statements.
  
  Sampling is cached within each invocation of ``.__call__()`` or method
  decorated by :func:`pyro_method` .  Because sample statements can appear
  only once in a Pyro trace, you should ensure that traced access to sample
  attributes is wrapped in a single invocation of ``.__call__()`` or method
  decorated by :func:`pyro_method` .
  
  To make an existing module probabilistic, you can create a subclass and
  overwrite some parameters with :class:`PyroSample` s::
  
      class RandomLinear(nn.Linear, PyroModule):  # used as a mixin
          def __init__(self, in_features, out_features):
              super().__init__(in_features, out_features)
              self.weight = PyroSample(
                  lambda self: dist.Normal(0, 1)
                                   .expand([self.out_features,
                                            self.in_features])
                                   .to_event(2))
  
  **Mixin classes**
  
  :class:`PyroModule` can be used as a mixin class, and supports simple
  syntax for dynamically creating mixins, for example the following are
  equivalent::
  
      # Version 1. create a named mixin class
      class PyroLinear(nn.Linear, PyroModule):
          pass
  
      m.linear = PyroLinear(m, n)
  
      # Version 2. create a dynamic mixin class
      m.linear = PyroModule[nn.Linear](m, n)
  
  This notation can be used recursively to create Bayesian modules, e.g.::
  
      model = PyroModule[nn.Sequential](
          PyroModule[nn.Linear](28 * 28, 100),
          PyroModule[nn.Sigmoid](),
          PyroModule[nn.Linear](100, 100),
          PyroModule[nn.Sigmoid](),
          PyroModule[nn.Linear](100, 10),
      )
      assert isinstance(model, nn.Sequential)
      assert isinstance(model, PyroModule)
  
      # Now we can be Bayesian about weights in the first layer.
      model[0].weight = PyroSample(
          prior=dist.Normal(0, 1).expand([28 * 28, 100]).to_event(2))
      guide = AutoDiagonalNormal(model)
  
  Note that ``PyroModule[...]`` does not recursively mix in
  :class:`PyroModule` to submodules of the input ``Module``; hence we needed
  to wrap each submodule of the ``nn.Sequential`` above.
  
  :param str name: Optional name for a root PyroModule. This is ignored in
      sub-PyroModules of another PyroModule.
class_docstr_tok: ["Subclass","of",":","class",":","`","torch.nn.Module","`","whose","attributes","can","be","modified","by","Pyro","effects",".","Attributes","can","be","set","using","helpers",":","class",":","`","PyroParam","`","and",":","class",":","`","PyroSample","`",",","and","methods","can","be","decorated","by",":","func",":","`","pyro_method","`",".","*","*","Parameters","*","*","To","create","a","Pyro-managed","parameter","attribute",",","set","that","attribute","using","either",":","class",":","`","torch.nn.Parameter","`","(","for","unconstrained","parameters",")","or",":","class",":","`","PyroParam","`","(","for","constrained","parameters",")",".","Reading","that","attribute","will","then","trigger","a",":","func",":","`","pyro.param","<","pyro.primitives.param",">","`","statement",".","For","example",":",":","#","Create","Pyro-managed","parameter","attributes",".","my_module","=","PyroModule","(",")","my_module.loc","=","nn.Parameter","(","torch.tensor","(","0",".",")",")","my_module.scale","=","PyroParam","(","torch.tensor","(","1",".",")",",","constraint=constraints.positive",")","#","Read","the","attributes",".","loc","=","my_module.loc","#","Triggers","a","pyro.param","statement",".","scale","=","my_module.scale","#","Triggers","another","pyro.param","statement",".","Note","that",",","unlike","normal",":","class",":","`","torch.nn.Module","`","s",",",":","class",":","`","PyroModule","`","s","should","not","be","registered","with",":","func",":","`","pyro.module","<","pyro.primitives.module",">","`","statements",".",":","class",":","`","PyroModule","`","s","can","contain","other",":","class",":","`","PyroModule","`","s","and","normal",":","class",":","`","torch.nn.Module","`","s.","Accessing","a","normal",":","class",":","`","torch.nn.Module","`","attribute","of","a",":","class",":","`","PyroModule","`","triggers","a",":","func",":","`","pyro.module","<","pyro.primitives.module",">","`","statement",".","If","multiple",":","class",":","`","PyroModule","`","s","appear","in","a","single","Pyro","model","or","guide",",","they","should","be","included","in","a","single","root",":","class",":","`","PyroModule","`","for","that","model",".",":","class",":","`","PyroModule","`","s","synchronize","data","with","the","param","store","at","each","``","setattr","``",",","``","getattr","``",",","and","``","delattr","``","event",",","based","on","the","nested","name","of","an","attribute",":","-","Setting","``","mod.x","=","x_init","``","tries","to","read","``","x","``","from","the","param","store",".","If","a","value","is","found","in","the","param","store",",","that","value","is","copied","into","``","mod","``","and","``","x_init","``","is","ignored",";","otherwise","``","x_init","``","is","copied","into","both","``","mod","``","and","the","param","store",".","-","Reading","``","mod.x","``","tries","to","read","``","x","``","from","the","param","store",".","If","a","value","is","found","in","the","param","store",",","that","value","is","copied","into","``","mod","``",";","otherwise","``","mod","``","'s","value","is","copied","into","the","param","store",".","Finally","``","mod","``","and","the","param","store","agree","on","a","single","value","to","return",".","-","Deleting","``","del","mod.x","``","removes","a","value","from","both","``","mod","``","and","the","param","store",".","Note","two",":","class",":","`","PyroModule","`","of","the","same","name","will","both","synchronize","with","the","global","param","store","and","thus","contain","the","same","data",".","When","creating","a",":","class",":","`","PyroModule","`",",","then","deleting","it",",","then","creating","another","with","the","same","name",",","the","latter","will","be","populated","with","the","former","'s","data","from","the","param","store",".","To","avoid","this","persistence",",","either","``","pyro.clear_param_store","(",")","``","or","call",":","func",":","`","clear","`","before","deleting","a",":","class",":","`","PyroModule","`",".",":","class",":","`","PyroModule","`","s","can","be","saved","and","loaded","either","directly","using",":","func",":","`","torch.save","`","\/",":","func",":","`","torch.load","`","or","indirectly","using","the","param","store","'s",":","meth",":","`","~pyro.params.param_store.ParamStoreDict.save","`","\/",":","meth",":","`","~pyro.params.param_store.ParamStoreDict.load","`",".","Note","that",":","func",":","`","torch.load","`","will","be","overridden","by","any","values","in","the","param","store",",","so","it","is","safest","to","``","pyro.clear_param_store","(",")","``","before","loading",".","*","*","Samples","*","*","To","create","a","Pyro-managed","random","attribute",",","set","that","attribute","using","the",":","class",":","`","PyroSample","`","helper",",","specifying","a","prior","distribution",".","Reading","that","attribute","will","then","trigger","a",":","func",":","`","pyro.sample","<","pyro.primitives.sample",">","`","statement",".","For","example",":",":","#","Create","Pyro-managed","random","attributes",".","my_module.x","=","PyroSample","(","dist.Normal","(","0",",","1",")",")","my_module.y","=","PyroSample","(","lambda","self",":","dist.Normal","(","self.loc",",","self.scale",")",")","#","Sample","the","attributes",".","x","=","my_module.x","#","Triggers","a","pyro.sample","statement",".","y","=","my_module.y","#","Triggers","one","pyro.sample","+","two","pyro.param","statements",".","Sampling","is","cached","within","each","invocation","of","``",".__call__","(",")","``","or","method","decorated","by",":","func",":","`","pyro_method","`",".","Because","sample","statements","can","appear","only","once","in","a","Pyro","trace",",","you","should","ensure","that","traced","access","to","sample","attributes","is","wrapped","in","a","single","invocation","of","``",".__call__","(",")","``","or","method","decorated","by",":","func",":","`","pyro_method","`",".","To","make","an","existing","module","probabilistic",",","you","can","create","a","subclass","and","overwrite","some","parameters","with",":","class",":","`","PyroSample","`","s",":",":","class","RandomLinear","(","nn.Linear",",","PyroModule",")",":","#","used","as","a","mixin","def","__init__","(","self",",","in_features",",","out_features",")",":","super","(",")",".__init__","(","in_features",",","out_features",")","self.weight","=","PyroSample","(","lambda","self",":","dist.Normal","(","0",",","1",")",".expand","(","[","self.out_features",",","self.in_features","]",")",".to_event","(","2",")",")","*","*","Mixin","classes","*","*",":","class",":","`","PyroModule","`","can","be","used","as","a","mixin","class",",","and","supports","simple","syntax","for","dynamically","creating","mixins",",","for","example","the","following","are","equivalent",":",":","#","Version","1.","create","a","named","mixin","class","class","PyroLinear","(","nn.Linear",",","PyroModule",")",":","pass","m.linear","=","PyroLinear","(","m",",","n",")","#","Version","2.","create","a","dynamic","mixin","class","m.linear","=","PyroModule","[","nn.Linear","]","(","m",",","n",")","This","notation","can","be","used","recursively","to","create","Bayesian","modules",",","e.g",".",":",":","model","=","PyroModule","[","nn.Sequential","]","(","PyroModule","[","nn.Linear","]","(","28","*","28",",","100",")",",","PyroModule","[","nn.Sigmoid","]","(",")",",","PyroModule","[","nn.Linear","]","(","100",",","100",")",",","PyroModule","[","nn.Sigmoid","]","(",")",",","PyroModule","[","nn.Linear","]","(","100",",","10",")",",",")","assert","isinstance","(","model",",","nn.Sequential",")","assert","isinstance","(","model",",","PyroModule",")","#","Now","we","can","be","Bayesian","about","weights","in","the","first","layer",".","model","[","0","]",".weight","=","PyroSample","(","prior=dist.Normal","(","0",",","1",")",".expand","(","[","28","*","28",",","100","]",")",".to_event","(","2",")",")","guide","=","AutoDiagonalNormal","(","model",")","Note","that","``","PyroModule","[","...","]","``","does","not","recursively","mix","in",":","class",":","`","PyroModule","`","to","submodules","of","the","input","``","Module","``",";","hence","we","needed","to","wrap","each","submodule","of","the","``","nn.Sequential","``","above",".",":","param","str","name",":","Optional","name","for","a","root","PyroModule",".","This","is","ignored","in","sub-PyroModules","of","another","PyroModule","."]
code: def _pyro_set_supermodule(self, name, context):
      if _is_module_local_param_enabled() and pyro.settings.get(
          "validate_poutine"
      ):
          self._check_module_local_param_usage()
      self._pyro_name = name
      self._pyro_context = context
      for key, value in self._modules.items():
          if isinstance(value, PyroModule):
              assert (
                  not value._pyro_context.used
              ), "submodule {} has executed outside of supermodule".format(
                  name
              )
              value._pyro_set_supermodule(
                  _make_name(name, key), context
              )
  
imports: import functools
  import inspect
  import weakref
  from collections import OrderedDict, namedtuple
  import torch
  from torch.distributions import constraints, transform_to
  import pyro
  from pyro.ops.provenance import detach_provenance
  from pyro.poutine.runtime import _PYRO_PARAM_STORE
label_desc: Use image node_id 4 for calling the PyroModule obj's underlying member method code with example usage: obj._pyro_set_supermodule(name, context) without return types