prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Fix nightly CPU test failures
  https://github.com/pfnet/pytorch-pfn-extras/actions/workflows/nightly-test-cpu.yml
  
  </issue>
  <code>
  [start of pytorch_pfn_extras/distributed/_distributed_validation_sampler.py]
  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 of pytorch_pfn_extras/distributed/_distributed_validation_sampler.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: 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"
                   )
  
verification_info: {"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"}]}