We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Fix nightly CPU test failures
https://github.com/pfnet/pytorch-pfn-extras/actions/workflows/nightly-test-cpu.yml

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pytorch_pfn_extras/distributed/_distributed_validation_sampler.py`
Content:
```
1 from typing import Iterator, Optional, Sized, TypeVar
2 
3 import numpy as np
4 import torch
5 import torch.distributed as dist
6 
7 T_co = TypeVar("T_co", covariant=True)
8 
9 
10 class DistributedValidationSampler(torch.utils.data.Sampler):
11     """Distributed sampler without duplication
12 
13     This sampler splits the input dataset to each worker process in distributed setup
14     without allowing repetition.
15     It is for evaluation purpose such as :class:`~DistributedEvaluator`.
16     This does not guarantee each worker to get the same number of samples,
17     so for training do not use this sampler (use PyTorch DistributedSampler instead).
18     """
19 
20     def __init__(
21         self,
22         dataset: Sized,
23         num_replicas: Optional[int] = None,
24         rank: Optional[int] = None,
25         shuffle: bool = True,
26         seed: int = 0,
27     ) -> None:
28         if num_replicas is None:
29             if not dist.is_available():  # type: ignore[no-untyped-call]
30                 raise RuntimeError(
31                     "Requires distributed package to be available"
32                 )
33             num_replicas = dist.get_world_size()  # type: ignore[no-untyped-call]
34         if rank is None:
35             if not dist.is_available():  # type: ignore[no-untyped-call]
36                 raise RuntimeError(
37                     "Requires distributed package to be available"
38                 )
39             rank = dist.get_rank()  # type: ignore[no-untyped-call]
40         if rank >= num_replicas or rank < 0:
41             raise ValueError(
42                 "Invalid rank {}, rank should be in the interval"
43                 " [0, {}]".format(rank, num_replicas - 1)
44             )
45         self.dataset = dataset
46         self.num_replicas = num_replicas
47         self.rank = rank
48         self.shuffle = shuffle
49         self.seed = seed
50 
51         self.dataset_len = len(dataset)
52         self.num_samples = len(
53             np.array_split(range(self.dataset_len), num_replicas)[rank]
54         )
55 
56     def __iter__(self) -> Iterator[T_co]:
57         if self.shuffle:
58             # deterministically shuffle based on epoch and seed
59             g = torch.Generator()
60             g.manual_seed(self.seed)
61             indices = torch.randperm(self.dataset_len, generator=g).tolist()
62         else:
63             indices = list(range(self.dataset_len))
64 
65         return iter(np.array_split(indices, self.num_replicas)[self.rank])
66 
67     def __len__(self) -> int:
68         return self.num_samples
69
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pytorch_pfn_extras/distributed/_distributed_validation_sampler.py b/pytorch_pfn_extras/distributed/_distributed_validation_sampler.py
--- a/pytorch_pfn_extras/distributed/_distributed_validation_sampler.py
+++ b/pytorch_pfn_extras/distributed/_distributed_validation_sampler.py
@@ -26,13 +26,13 @@
         seed: int = 0,
     ) -> None:
         if num_replicas is None:
-            if not dist.is_available():  # type: ignore[no-untyped-call]
+            if not dist.is_available() or not dist.is_initialized():  # type: ignore[no-untyped-call]
                 raise RuntimeError(
                     "Requires distributed package to be available"
                 )
             num_replicas = dist.get_world_size()  # type: ignore[no-untyped-call]
         if rank is None:
-            if not dist.is_available():  # type: ignore[no-untyped-call]
+            if not dist.is_available() or not dist.is_initialized():  # type: ignore[no-untyped-call]
                 raise RuntimeError(
                     "Requires distributed package to be available"
                 )

{"golden_diff": "diff --git a/pytorch_pfn_extras/distributed/_distributed_validation_sampler.py b/pytorch_pfn_extras/distributed/_distributed_validation_sampler.py\n--- a/pytorch_pfn_extras/distributed/_distributed_validation_sampler.py\n+++ b/pytorch_pfn_extras/distributed/_distributed_validation_sampler.py\n@@ -26,13 +26,13 @@\n         seed: int = 0,\n     ) -> None:\n         if num_replicas is None:\n-            if not dist.is_available():  # type: ignore[no-untyped-call]\n+            if not dist.is_available() or not dist.is_initialized():  # type: ignore[no-untyped-call]\n                 raise RuntimeError(\n                     \"Requires distributed package to be available\"\n                 )\n             num_replicas = dist.get_world_size()  # type: ignore[no-untyped-call]\n         if rank is None:\n-            if not dist.is_available():  # type: ignore[no-untyped-call]\n+            if not dist.is_available() or not dist.is_initialized():  # type: ignore[no-untyped-call]\n                 raise RuntimeError(\n                     \"Requires distributed package to be available\"\n                 )\n", "issue": "Fix nightly CPU test failures\nhttps://github.com/pfnet/pytorch-pfn-extras/actions/workflows/nightly-test-cpu.yml\n", "before_files": [{"content": "from typing import Iterator, Optional, Sized, TypeVar\n\nimport numpy as np\nimport torch\nimport torch.distributed as dist\n\nT_co = TypeVar(\"T_co\", covariant=True)\n\n\nclass DistributedValidationSampler(torch.utils.data.Sampler):\n    \"\"\"Distributed sampler without duplication\n\n    This sampler splits the input dataset to each worker process in distributed setup\n    without allowing repetition.\n    It is for evaluation purpose such as :class:`~DistributedEvaluator`.\n    This does not guarantee each worker to get the same number of samples,\n    so for training do not use this sampler (use PyTorch DistributedSampler instead).\n    \"\"\"\n\n    def __init__(\n        self,\n        dataset: Sized,\n        num_replicas: Optional[int] = None,\n        rank: Optional[int] = None,\n        shuffle: bool = True,\n        seed: int = 0,\n    ) -> None:\n        if num_replicas is None:\n            if not dist.is_available():  # type: ignore[no-untyped-call]\n                raise RuntimeError(\n                    \"Requires distributed package to be available\"\n                )\n            num_replicas = dist.get_world_size()  # type: ignore[no-untyped-call]\n        if rank is None:\n            if not dist.is_available():  # type: ignore[no-untyped-call]\n                raise RuntimeError(\n                    \"Requires distributed package to be available\"\n                )\n            rank = dist.get_rank()  # type: ignore[no-untyped-call]\n        if rank >= num_replicas or rank < 0:\n            raise ValueError(\n                \"Invalid rank {}, rank should be in the interval\"\n                \" [0, {}]\".format(rank, num_replicas - 1)\n            )\n        self.dataset = dataset\n        self.num_replicas = num_replicas\n        self.rank = rank\n        self.shuffle = shuffle\n        self.seed = seed\n\n        self.dataset_len = len(dataset)\n        self.num_samples = len(\n            np.array_split(range(self.dataset_len), num_replicas)[rank]\n        )\n\n    def __iter__(self) -> Iterator[T_co]:\n        if self.shuffle:\n            # deterministically shuffle based on epoch and seed\n            g = torch.Generator()\n            g.manual_seed(self.seed)\n            indices = torch.randperm(self.dataset_len, generator=g).tolist()\n        else:\n            indices = list(range(self.dataset_len))\n\n        return iter(np.array_split(indices, self.num_replicas)[self.rank])\n\n    def __len__(self) -> int:\n        return self.num_samples\n", "path": "pytorch_pfn_extras/distributed/_distributed_validation_sampler.py"}], "after_files": [{"content": "from typing import Iterator, Optional, Sized, TypeVar\n\nimport numpy as np\nimport torch\nimport torch.distributed as dist\n\nT_co = TypeVar(\"T_co\", covariant=True)\n\n\nclass DistributedValidationSampler(torch.utils.data.Sampler):\n    \"\"\"Distributed sampler without duplication\n\n    This sampler splits the input dataset to each worker process in distributed setup\n    without allowing repetition.\n    It is for evaluation purpose such as :class:`~DistributedEvaluator`.\n    This does not guarantee each worker to get the same number of samples,\n    so for training do not use this sampler (use PyTorch DistributedSampler instead).\n    \"\"\"\n\n    def __init__(\n        self,\n        dataset: Sized,\n        num_replicas: Optional[int] = None,\n        rank: Optional[int] = None,\n        shuffle: bool = True,\n        seed: int = 0,\n    ) -> None:\n        if num_replicas is None:\n            if not dist.is_available() or not dist.is_initialized():  # type: ignore[no-untyped-call]\n                raise RuntimeError(\n                    \"Requires distributed package to be available\"\n                )\n            num_replicas = dist.get_world_size()  # type: ignore[no-untyped-call]\n        if rank is None:\n            if not dist.is_available() or not dist.is_initialized():  # type: ignore[no-untyped-call]\n                raise RuntimeError(\n                    \"Requires distributed package to be available\"\n                )\n            rank = dist.get_rank()  # type: ignore[no-untyped-call]\n        if rank >= num_replicas or rank < 0:\n            raise ValueError(\n                \"Invalid rank {}, rank should be in the interval\"\n                \" [0, {}]\".format(rank, num_replicas - 1)\n            )\n        self.dataset = dataset\n        self.num_replicas = num_replicas\n        self.rank = rank\n        self.shuffle = shuffle\n        self.seed = seed\n\n        self.dataset_len = len(dataset)\n        self.num_samples = len(\n            np.array_split(range(self.dataset_len), num_replicas)[rank]\n        )\n\n    def __iter__(self) -> Iterator[T_co]:\n        if self.shuffle:\n            # deterministically shuffle based on epoch and seed\n            g = torch.Generator()\n            g.manual_seed(self.seed)\n            indices = torch.randperm(self.dataset_len, generator=g).tolist()\n        else:\n            indices = list(range(self.dataset_len))\n\n        return iter(np.array_split(indices, self.num_replicas)[self.rank])\n\n    def __len__(self) -> int:\n        return self.num_samples\n", "path": "pytorch_pfn_extras/distributed/_distributed_validation_sampler.py"}]}