code: def vectorized_importance_weights(model, guide, *args, **kwargs):
      """
      :param model: probabilistic model defined as a function
      :param guide: guide used for sampling defined as a function
      :param num_samples: number of samples to draw from the guide (default 1)
      :param int max_plate_nesting: Bound on max number of nested :func:`pyro.plate` contexts.
      :param bool normalized: set to True to return self-normalized importance weights
      :returns: returns a ``(num_samples,)``-shaped tensor of importance weights
          and the model and guide traces that produced them
  
      Vectorized computation of importance weights for models with static structure::
  
          log_weights, model_trace, guide_trace = \\
              vectorized_importance_weights(model, guide, *args,
                                            num_samples=1000,
                                            max_plate_nesting=4,
                                            normalized=False)
      """
      num_samples = kwargs.pop("num_samples", 1)
      max_plate_nesting = kwargs.pop("max_plate_nesting", None)
      normalized = kwargs.pop("normalized", False)
  
      if max_plate_nesting is None:
          raise ValueError("must provide max_plate_nesting")
      max_plate_nesting += 1
  
      def vectorize(fn):
          def _fn(*args, **kwargs):
              with pyro.plate(
                  "num_particles_vectorized",
                  num_samples,
                  dim=-max_plate_nesting,
              ):
                  return fn(*args, **kwargs)
  
          return _fn
  
      model_trace, guide_trace = get_importance_trace(
          "flat",
          max_plate_nesting,
          vectorize(model),
          vectorize(guide),
          args,
          kwargs,
      )
  
      guide_trace.pack_tensors()
      model_trace.pack_tensors(guide_trace.plate_to_symbol)
  
      if num_samples == 1:
          log_weights = (
              model_trace.log_prob_sum() - guide_trace.log_prob_sum()
          )
      else:
          wd = guide_trace.plate_to_symbol["num_particles_vectorized"]
          log_weights = 0.0
          for site in model_trace.nodes.values():
              if site["type"] != "sample":
                  continue
              log_weights += torch.einsum(
                  site["packed"]["log_prob"]._pyro_dims + "->" + wd,
                  [site["packed"]["log_prob"]],
              )
  
          for site in guide_trace.nodes.values():
              if site["type"] != "sample":
                  continue
              log_weights -= torch.einsum(
                  site["packed"]["log_prob"]._pyro_dims + "->" + wd,
                  [site["packed"]["log_prob"]],
              )
  
      if normalized:
          log_weights = log_weights - torch.logsumexp(log_weights)
      return log_weights, model_trace, guide_trace
  
file_path: pyro/pyro/infer/importance.py
imports: import math
  import warnings
  import torch
  import pyro
  import pyro.poutine
  from pyro.ops.stats import fit_generalized_pareto
  from .abstract_infer import TracePosterior
  from .enum import get_importance_trace
label_desc: Use image node_id 1 for calling a global function with example usage: vectorized_importance_weights(model, guide) and returns: log_weights,  model_trace,  guide_trace, _fn, fn