internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: pyro-ppl/pyro

Issue:
[bug] CSIS error when parameters are not fully used
### Issue Description
For `pyro.infer.csis`
If some parameters are not used to produce the outputs, the gradients can be None. In this case, an error will be raised as following
```/usr/local/lib/python3.7/dist-packages/pyro/infer/csis.py in loss_and_grads(self, grads, batch, *args, **kwargs)
    114                 guide_grads = torch.autograd.grad(particle_loss, guide_params, allow_unused=True)
    115                 for guide_grad, guide_param in zip(guide_grads, guide_params):
--> 116                     guide_param.grad = guide_grad if guide_param.grad is None else guide_param.grad + guide_grad
    117 
    118             loss += torch_item(particle_loss)

TypeError: unsupported operand type(s) for +: 'Tensor' and 'NoneType'
```
I resolved this issue locally by checking None before updating gradients
```
if guide_grad is None:
    continue
guide_param.grad = guide_grad if guide_param.grad is None else guide_param.grad + guide_grad
```

### Environment
 - Python version: 3.7
 - PyTorch version: 1.8.1+cu101
 - Pyro version: 1.5.1


Discussion:

Resolving patch:
diff --git a/pyro/infer/csis.py b/pyro/infer/csis.py
--- a/pyro/infer/csis.py
+++ b/pyro/infer/csis.py
@@ -113,6 +113,8 @@ def loss_and_grads(self, grads, batch, *args, **kwargs):
                                    for site in particle_param_capture.trace.nodes.values())
                 guide_grads = torch.autograd.grad(particle_loss, guide_params, allow_unused=True)
                 for guide_grad, guide_param in zip(guide_grads, guide_params):
+                    if guide_grad is None:
+                        continue
                     guide_param.grad = guide_grad if guide_param.grad is None else guide_param.grad + guide_grad
 
             loss += torch_item(particle_loss)


Test patch:
Repository: pyro-ppl/pyro

Issue:
[bug] Runtime error during SVI inference when using poutine.do()
### Issue Description

Setting: a simple model with 2 latent Gaussians z1 and z2, giving rise to x ~ N( z1+z2, I).

In this setting p(z2 | x, z1) should be the same as p(z2 | x, do(z1)). 

I wanted to check whether the current Pyro interface reflects this and it seems it does not.

My initial thought is that there is a difference in how .do() and .condition() broadcast the constants across the plate context.

### Environment

 - OS and python version: MacOS 10.14.6, Python: 3.8.6
 - PyTorch version: 1.9.0.dev20210502 (nightly version)
 - Pyro version: 1.6.0.

### Code Snippet

Replication code:
https://pastebin.com/Ki2PYX7z



Discussion:

Resolving patch:
diff --git a/pyro/poutine/do_messenger.py b/pyro/poutine/do_messenger.py
--- a/pyro/poutine/do_messenger.py
+++ b/pyro/poutine/do_messenger.py
@@ -66,6 +66,7 @@ def _pyro_sample(self, msg):
 
             # split node, avoid reapplying self recursively to new node
             new_msg = msg.copy()
+            new_msg["cond_indep_stack"] = ()  # avoid entering plates twice
             apply_stack(new_msg)
 
             # apply intervention


Test patch:
diff --git a/tests/poutine/test_counterfactual.py b/tests/poutine/test_counterfactual.py
--- a/tests/poutine/test_counterfactual.py
+++ b/tests/poutine/test_counterfactual.py
@@ -73,3 +73,27 @@ def model():
                 assert_equal(interventions[name], actual_values[name])
             if interventions[name] != observations[name]:
                 assert_not_equal(interventions[name], tr.nodes[name]['value'])
+
+
+def test_plate_duplication_smoke():
+
+    def model(N):
+
+        with pyro.plate("x_plate", N):
+            z1 = pyro.sample("z1", dist.MultivariateNormal(torch.zeros(2), torch.eye(2)))
+            z2 = pyro.sample("z2", dist.MultivariateNormal(torch.zeros(2), torch.eye(2)))
+            return pyro.sample("x", dist.MultivariateNormal(z1+z2, torch.eye(2)))
+
+    fix_z1 = torch.tensor([[-6.1258, -6.1524],
+                           [-4.1513, -4.3080]])
+
+    obs_x = torch.tensor([[-6.1258, -6.1524],
+                          [-4.1513, -4.3080]])
+
+    do_model = poutine.do(model, data={"z1": fix_z1})
+    do_model = poutine.condition(do_model, data={"x": obs_x})
+    do_auto = pyro.infer.autoguide.AutoMultivariateNormal(do_model)
+    optim = pyro.optim.Adam({"lr": 0.05})
+
+    svi = pyro.infer.SVI(do_model, do_auto, optim, pyro.infer.Trace_ELBO())
+    svi.step(len(obs_x))
Repository: pyro-ppl/pyro

Issue:
model_7 in `examples/contrib/funsor/hmm.py` may have a bug
In the hmm example using funsor, model_7 may have a bug, `x_prev` is initialized but never updated.
https://github.com/pyro-ppl/pyro/blame/38facc126103ef77604d0d47dfde94839ad9dc69/examples/contrib/funsor/hmm.py#L610




Discussion:
Nice catch! Would you like to submit a PR with the fix? If so could you also run `python examples/hmm.py -m 7 --funsor -n 1 --batch-size=10 --print-shapes` to see if there are any changes in the shapes of tensors?

Resolving patch:
diff --git a/examples/contrib/funsor/hmm.py b/examples/contrib/funsor/hmm.py
--- a/examples/contrib/funsor/hmm.py
+++ b/examples/contrib/funsor/hmm.py
@@ -626,6 +626,7 @@ def model_7(sequences, lengths, args, batch_size=None, include_prior=True):
                         dist.Bernoulli(probs_y[x_curr.squeeze(-1)]),
                         obs=Vindex(sequences)[batch, t],
                     )
+                x_prev = x_curr
 
 
 # Let's see how vectorizing time dimension changes the shapes of sample sites:
@@ -644,11 +645,11 @@ def model_7(sequences, lengths, args, batch_size=None, include_prior=True):
 #                      value       16  1  1  1 |
 #                   y_0 dist       16 10  1 51 |
 #                      value          10  1 51 |
-#  x_slice(0, 71, None) dist          10 71  1 |
+#  x_slice(0, 71, None) dist       16 10 71  1 |
 #                      value    16  1  1  1  1 |
 #  y_slice(0, 71, None) dist    16  1 10 71 51 |
 #                      value          10 71 51 |
-#  x_slice(1, 72, None) dist          10 71  1 |
+#  x_slice(1, 72, None) dist    16  1 10 71  1 |
 #                      value 16  1  1  1  1  1 |
 #  y_slice(1, 72, None) dist 16  1  1 10 71 51 |
 #                      value          10 71 51 |


Test patch:
Repository: pyro-ppl/pyro

Issue:
PyTorch 2.0 compatibility: Explicit PyTorch 1.x check causing issues with packages that depend on PyTorch / pyro (e.g. BoTorch)
### Issue Description
The explicit check for PyTorch 1.x here (https://github.com/pyro-ppl/pyro/blob/dev/pyro/distributions/torch_patch.py#L10) is causing problems when another package has a dependency on PyTorch + Pyro, since PyTorch is now at 2.0. For example, it is causing BoTorch tests to fail here (https://github.com/pytorch/botorch/pull/1551).

Could this check be removed to allow for PyTorch 2.0?

### Environment
Mac OS 11.7.1
Python 3.10
PyTorch 2.0
Pyro 1.8.3

### Code Snippet
https://github.com/pytorch/botorch/actions/runs/3659534850/jobs/6185642011


Discussion:

Resolving patch:
diff --git a/pyro/distributions/torch_patch.py b/pyro/distributions/torch_patch.py
--- a/pyro/distributions/torch_patch.py
+++ b/pyro/distributions/torch_patch.py
@@ -7,8 +7,6 @@
 
 import torch
 
-assert torch.__version__.startswith("1.")
-
 
 def patch_dependency(target, root_module=torch):
     parts = target.split(".")


Test patch:
Repository: pyro-ppl/pyro

Issue:
bug with OneCycleLR on Apple Silicone 
### Guidelines

**NOTE:** Issues are for bugs and feature requests only. If you have a question about using Pyro or general modeling questions, please post it on the [forum](https://forum.pyro.ai/).

If you would like to address any minor bugs in the documentation or source, please feel free to contribute a Pull Request without creating an issue first. 

Please tag the issue appropriately in the title e.g. [bug], [feature request], [discussion], etc.

Please provide the following details:
--------------------------------------------------------------------------------------------------
### Issue Description
Provide a brief description of the issue.

### Environment
For any bugs, please provide the following:
 - OS and python version.
 - PyTorch version, or if relevant, output of `pip freeze`.
 - Pyro version: output of `python -c 'import pyro; print pyro.__version__'`

### Code Snippet
Provide any relevant code snippets and commands run to replicate the issue.



Discussion:
Hi,

I got stuck on this for a couple of weeks now. The bug appears on a Apple Silicone install and the same code runs fine on a Ubuntu box.

MacOS Ventura 13.1
pyro-ppl    1.8.3+3422c3a4
pytorch     2.0.0.dev2022121
(I get the error on several pytorch releases tested over the last month)

code:
Python 3.8.15 | packaged by conda-forge | (default, Nov 22 2022, 08:53:40)
[Clang 14.0.6 ] on darwin
>>> from torch.optim.lr_scheduler import OneCycleLR    # This works and I can use the scheduler on some test data

# If I trie instead:
from pyro.optim import OneCycleLR
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'OneCycleLR' from 'pyro.optim' (/Users/JA/anaconda3/envs/torch-gpu/lib/python3.8/site-packages/pyro/optim/__init__.py)

Thank you for looking into this.


Resolving patch:
diff --git a/pyro/optim/pytorch_optimizers.py b/pyro/optim/pytorch_optimizers.py
--- a/pyro/optim/pytorch_optimizers.py
+++ b/pyro/optim/pytorch_optimizers.py
@@ -34,13 +34,16 @@
     del _PyroOptim
 
 # Load all schedulers from PyTorch
+# breaking change in torch >= 1.14: LRScheduler is new base class
+if hasattr(torch.optim.lr_scheduler, "LRScheduler"):
+    _torch_scheduler_base = torch.optim.lr_scheduler.LRScheduler
+else:  # for torch < 1.13, _LRScheduler is base class
+    _torch_scheduler_base = torch.optim.lr_scheduler._LRScheduler
+
 for _name, _Optim in torch.optim.lr_scheduler.__dict__.items():
     if not isinstance(_Optim, type):
         continue
-    if (
-        not issubclass(_Optim, torch.optim.lr_scheduler._LRScheduler)
-        and _name != "ReduceLROnPlateau"
-    ):
+    if not issubclass(_Optim, _torch_scheduler_base) and _name != "ReduceLROnPlateau":
         continue
     if _Optim is torch.optim.Optimizer:
         continue


Test patch:
Repository: pyro-ppl/pyro

Issue:
Docs builds are failing
Docs builds seem to have been failing since 1.8.5 release. We should fix this before the 1.8.6 release.

https://readthedocs.org/projects/pyro-ppl/builds/20847164/
<img width="812" alt="image" src="https://github.com/pyro-ppl/pyro/assets/648532/45149fae-a72d-481a-aaf9-73262d50aa92">



Discussion:

Resolving patch:
diff --git a/docs/source/conf.py b/docs/source/conf.py
--- a/docs/source/conf.py
+++ b/docs/source/conf.py
@@ -164,7 +164,9 @@
 # (source start file, target name, title,
 #  author, documentclass [howto, manual, or own class]).
 latex_documents = [
-    (master_doc, "Pyro.tex", u"Pyro Documentation", u"Uber AI Labs", "manual"),
+    # Disabled pdf builds to unblock readthedocs failed builds;
+    # see https://github.com/pyro-ppl/pyro/issues/3248
+    # (master_doc, "Pyro.tex", u"Pyro Documentation", u"Uber AI Labs", "manual"),
 ]
 
 # -- Options for manual page output ---------------------------------------


Test patch:
Repository: pyro-ppl/pyro

Issue:
Docs builds are failing
Docs builds seem to have been failing since 1.8.5 release. We should fix this before the 1.8.6 release.

https://readthedocs.org/projects/pyro-ppl/builds/20847164/
<img width="812" alt="image" src="https://github.com/pyro-ppl/pyro/assets/648532/45149fae-a72d-481a-aaf9-73262d50aa92">



Discussion:
hmm, [still failing...](https://readthedocs.org/projects/pyro-ppl/builds/21409058/)
<img width="812" alt="image" src="https://github.com/pyro-ppl/pyro/assets/648532/57fd947d-5f98-432c-91ce-c03901634284">

have you seen some of this discussion? 
https://github.com/readthedocs/readthedocs.org/issues/10549

Resolving patch:
diff --git a/docs/source/conf.py b/docs/source/conf.py
--- a/docs/source/conf.py
+++ b/docs/source/conf.py
@@ -164,9 +164,7 @@
 # (source start file, target name, title,
 #  author, documentclass [howto, manual, or own class]).
 latex_documents = [
-    # Disabled pdf builds to unblock readthedocs failed builds;
-    # see https://github.com/pyro-ppl/pyro/issues/3248
-    # (master_doc, "Pyro.tex", u"Pyro Documentation", u"Uber AI Labs", "manual"),
+    (master_doc, "Pyro.tex", u"Pyro Documentation", u"Uber AI Labs", "manual"),
 ]
 
 # -- Options for manual page output ---------------------------------------


Test patch:
Repository: pyro-ppl/pyro

Issue:
Switch to ruff for linting
Many projects in the Python community are moving to [ruff](https://github.com/astral-sh/ruff) for their linting, due to its speed and ability to fix some of the surfaced errors.  The speed is especially nice when integrated into IDEs like VS code.

It would be great if we could incrementally adopt ruff's checks, starting with 
- [ ] `E`, `F` for flake8
- [ ] `I` for isort
- [ ] see other checks [here](https://beta.ruff.rs/docs/rules/)


Discussion:
hi @fritzo,
Is this issue open to contribute ?
Absolutely @Sameer-Pal, any help is appreciated!

Resolving patch:
diff --git a/pyro/distributions/__init__.py b/pyro/distributions/__init__.py
--- a/pyro/distributions/__init__.py
+++ b/pyro/distributions/__init__.py
@@ -53,8 +53,6 @@
 except ImportError:
     pass
 
-# isort: split
-
 from pyro.distributions.affine_beta import AffineBeta
 from pyro.distributions.asymmetriclaplace import (
     AsymmetricLaplace,
diff --git a/pyro/distributions/constraints.py b/pyro/distributions/constraints.py
--- a/pyro/distributions/constraints.py
+++ b/pyro/distributions/constraints.py
@@ -42,8 +42,6 @@
 except ImportError:
     pass
 
-# isort: split
-
 import torch
 from torch.distributions.constraints import __all__ as torch_constraints
 
diff --git a/pyro/distributions/transforms/__init__.py b/pyro/distributions/transforms/__init__.py
--- a/pyro/distributions/transforms/__init__.py
+++ b/pyro/distributions/transforms/__init__.py
@@ -31,8 +31,6 @@
 except ImportError:
     pass
 
-# isort: split
-
 from torch.distributions import biject_to, transform_to
 from torch.distributions.transforms import __all__ as torch_transforms
 


Test patch:
Repository: pyro-ppl/pyro

Issue:
Use of the outdated 'jupyter' metapackage
### Issue Description
You depend [here](https://github.com/pyro-ppl/pyro/blob/dev/setup.py#L68) on the outdated jupyter package.
It hasn't been updated since 2015.
This dependency would be probably correct if the Jupyter project would update this PYPI package.
See [this discussion](https://discourse.jupyter.org/t/the-jupyter-metapackage-is-outdated-on-pypi/25034).




Discussion:
@yurivict could you contribute or recommend a fix?
@fritzo 
I am only an occasional jupyter user, and don't know what are the parts that are required.
I guess we should instead depend on [notebook](https://pypi.org/project/notebook)

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -65,7 +65,8 @@
 
 # examples/tutorials
 EXTRAS_REQUIRE = [
-    "jupyter>=1.0.0",
+    "notebook",
+    "ipywidgets",
     "graphviz>=0.8",
     "matplotlib>=1.3",
     "torchvision>=0.15.0",


Test patch:
Repository: avocado-framework/avocado-vt

Issue:
vt-boostrap asks me twice if I want to download JeOS 19 x86_64
vt-bootstrap seems to be asking me the same question twice (just with different wording):

```
11:50:50 INFO | 6 - Verifying (and possibly downloading) guest image
File JeOS 19 x86_64 not present. Do you want to download it? (y/n) y
11:50:52 INFO | Verifying expected SHA1 sum from http://lmr.fedorapeople.org/jeos/SHA1SUM_JEOS19
11:50:52 INFO | Expected SHA1 sum: f5141c3f9d51ee340660f2b96b2b842ceab9128f
11:50:52 WARNI| File /usr/share/avocado/data/avocado-vt/images/jeos-19-64.qcow2.7z not found
Would you like to download it from http://lmr.fedorapeople.org/jeos/jeos-19-64.qcow2.7z? (y/n) y
```



Discussion:
Oh yeah, this has been there pretty much forever. I agree that it's necessary to cut one of the questions.


Resolving patch:
diff --git a/virttest/asset.py b/virttest/asset.py
--- a/virttest/asset.py
+++ b/virttest/asset.py
@@ -576,14 +576,6 @@ def download_asset(asset, interactive=True, restore_image=False):
                           in order to restore the image.
     """
     asset_info = get_asset_info(asset)
-    destination = asset_info['destination']
-
-    if (interactive and not os.path.isfile(destination)):
-        answer = genio.ask("File %s not present. Do you want to download it?" %
-                           asset_info['title'])
-    else:
-        answer = "y"
 
-    if answer == "y":
-        download_file(asset_info=asset_info, interactive=interactive,
-                      force=restore_image)
+    download_file(asset_info=asset_info, interactive=interactive,
+                  force=restore_image)


Test patch:
Repository: pytorch/tnt

Issue:
PyTorch 0.4 test errors
Need to fix these:

```
..........
----------------------------------------------------------------------
Ran 10 tests in 0.015s

OK
E.../Users/szagoruyko/anaconda3/lib/python3.6/site-packages/numpy/core/_methods.py:135: RuntimeWarning: Degrees of freedom <= 0 for slice
  keepdims=keepdims)
/Users/szagoruyko/anaconda3/lib/python3.6/site-packages/numpy/core/_methods.py:127: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)
.....E
======================================================================
ERROR: testAPMeter (__main__.TestMeters)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_meters.py", line 208, in testAPMeter
    ap = mtr.value()
  File "/Users/szagoruyko/anaconda3/lib/python3.6/site-packages/torchnet/meter/apmeter.py", line 137, in value
    ap[k] = precision[truth.byte()].sum() / max(truth.sum(), 1)
RuntimeError: Expected object of type torch.FloatTensor but found type torch.LongTensor for argument #2 'other'

======================================================================
ERROR: testmAPMeter (__main__.TestMeters)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_meters.py", line 329, in testmAPMeter
    ap = mtr.value()
  File "/Users/szagoruyko/anaconda3/lib/python3.6/site-packages/torchnet/meter/mapmeter.py", line 30, in value
    return self.apmeter.value().mean()
  File "/Users/szagoruyko/anaconda3/lib/python3.6/site-packages/torchnet/meter/apmeter.py", line 137, in value
    ap[k] = precision[truth.byte()].sum() / max(truth.sum(), 1)
RuntimeError: Expected object of type torch.FloatTensor but found type torch.LongTensor for argument #2 'other'

----------------------------------------------------------------------
Ran 10 tests in 0.118s

FAILED (errors=2)
```


Discussion:

Resolving patch:
diff --git a/torchnet/meter/apmeter.py b/torchnet/meter/apmeter.py
--- a/torchnet/meter/apmeter.py
+++ b/torchnet/meter/apmeter.py
@@ -134,5 +134,5 @@ def value(self):
             precision = tp.div(rg)
 
             # compute average precision
-            ap[k] = precision[truth.byte()].sum() / max(truth.sum(), 1)
+            ap[k] = precision[truth.byte()].sum() / max(float(truth.sum()), 1)
         return ap


Test patch:
Repository: pytorch/tnt

Issue:
AverageValueMeter returns incorrect results when `tensor` is passed
Based on [this thread](https://discuss.pytorch.org/t/confusing-result-about-meter-averagevaluemeter/21819) it seems as if the internal members of the class hold references to the `tensors`, thus yielding wrong results.
When the `tensor` value is passed by `.item()` the result is correct.
A simple fix would be to add this condition to `add`:
```python
def add(self, value, n=1):
    if isinstance(value, torch.Tensor):
        value = value.item()
    self.val = value
```

I can submit a PR, if that makes sense to you.


Discussion:

Resolving patch:
diff --git a/torchnet/meter/averagevaluemeter.py b/torchnet/meter/averagevaluemeter.py
--- a/torchnet/meter/averagevaluemeter.py
+++ b/torchnet/meter/averagevaluemeter.py
@@ -18,14 +18,15 @@ def add(self, value, n=1):
         if self.n == 0:
             self.mean, self.std = np.nan, np.nan
         elif self.n == 1:
-            self.mean, self.std = self.sum, np.inf
+            self.mean = 0.0 + self.sum  # This is to force a copy in torch/numpy
+            self.std = np.inf
             self.mean_old = self.mean
             self.m_s = 0.0
         else:
             self.mean = self.mean_old + (value - n * self.mean_old) / float(self.n)
             self.m_s += (value - self.mean_old) * (value - self.mean)
             self.mean_old = self.mean
-            self.std = math.sqrt(self.m_s / (self.n - 1.0))
+            self.std = np.sqrt(self.m_s / (self.n - 1.0))
 
     def value(self):
         return self.mean, self.std


Test patch:
diff --git a/test/test_meters.py b/test/test_meters.py
--- a/test/test_meters.py
+++ b/test/test_meters.py
@@ -18,6 +18,30 @@ def testAverageValueMeter(self):
 
         self.assertTrue(np.isnan(mean))
 
+    def testAverageValueMeter_np_2d(self):
+        m = meter.AverageValueMeter()
+        for i in range(1, 10):
+            m.add(np.float32([[i, i + 1]]))
+        mean, std = m.value()
+        self.assertTrue(np.allclose(mean, [[5.0, 6.0]]))
+        self.assertTrue(np.allclose(std, [[2.738613, 2.738613]]))
+        m.reset()
+        mean, std = m.value()
+
+        self.assertTrue(np.isnan(mean))
+
+    def testAverageValueMeter_torch_2d(self):
+        m = meter.AverageValueMeter()
+        for i in range(1, 10):
+            m.add(torch.Tensor([[i, i + 1]]))
+        mean, std = m.value()
+        self.assertTrue(np.allclose(mean, [[5.0, 6.0]]))
+        self.assertTrue(np.allclose(std, [[2.738613, 2.738613]]))
+        m.reset()
+        mean, std = m.value()
+
+        self.assertTrue(np.isnan(mean))
+
     def testAverageValueMeter_n(self):
         """Test the case of adding more than 1 value.
         """