prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  AcceleratedOptimizer `zero_grad` argument not supported: `set_to_none`
  Currently the `AcceleratedOptimizer` class doesn't support the argument `set_to_none`, is this an intentional exclusion?
  
  </issue>
  <code>
  [start of src/accelerate/optimizer.py]
  1 # Copyright 2021 The HuggingFace Team. All rights reserved.
  2 #
  3 # Licensed under the Apache License, Version 2.0 (the "License");
  4 # you may not use this file except in compliance with the License.
  5 # You may obtain a copy of the License at
  6 #
  7 #     http://www.apache.org/licenses/LICENSE-2.0
  8 #
  9 # Unless required by applicable law or agreed to in writing, software
  10 # distributed under the License is distributed on an "AS IS" BASIS,
  11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12 # See the License for the specific language governing permissions and
  13 # limitations under the License.
  14 
  15 import torch
  16 
  17 from .state import AcceleratorState, DistributedType, is_tpu_available
  18 
  19 
  20 if is_tpu_available():
  21     import torch_xla.core.xla_model as xm
  22 
  23 
  24 def move_to_device(state, device):
  25     if isinstance(state, (list, tuple)):
  26         return type(state)(move_to_device(t, device) for t in state)
  27     elif isinstance(state, dict):
  28         return type(state)({k: move_to_device(v, device) for k, v in state.items()})
  29     elif isinstance(state, torch.Tensor):
  30         return state.to(device)
  31     return state
  32 
  33 
  34 class AcceleratedOptimizer(torch.optim.Optimizer):
  35     """
  36     Internal wrapper around a torch optimizer.
  37 
  38     Args:
  39         optimizer (:obj:`torch.optim.optimizer.Optimizer`):
  40             The optimizer to wrap.
  41         device_placement (:obj:`bool`, `optional`, defaults to :obj:`True`):
  42             Whether or not the optimizer should handle device placement. If so, it will place the state dictionary of
  43             :obj:`optimizer` on the right device.
  44         scaler (:obj:`torch.cuda.amp.grad_scaler.GradScaler`, `optional`):
  45             The scaler to use in the step function if training with mixed precision.
  46     """
  47 
  48     def __init__(self, optimizer, device_placement=True, scaler=None):
  49         self.optimizer = optimizer
  50         self.scaler = scaler
  51         self.state = AcceleratorState()
  52 
  53         # Handle device placement
  54         if device_placement:
  55             state_dict = self.optimizer.state_dict()
  56             if self.state.distributed_type == DistributedType.TPU:
  57                 xm.send_cpu_data_to_device(state_dict, self.state.device)
  58             else:
  59                 state_dict = move_to_device(state_dict, self.state.device)
  60             self.optimizer.load_state_dict(state_dict)
  61 
  62     @property
  63     def param_groups(self):
  64         return self.optimizer.param_groups
  65 
  66     @param_groups.setter
  67     def param_groups(self, param_groups):
  68         self.optimizer.param_groups = param_groups
  69 
  70     @property
  71     def defaults(self):
  72         return self.optimizer.defaults
  73 
  74     @defaults.setter
  75     def defaults(self, defaults):
  76         self.optimizer.defaults = defaults
  77 
  78     def add_param_group(self, param_group):
  79         self.optimizer.add_param_group(param_group)
  80 
  81     def load_state_dict(self, state_dict):
  82         if self.state.distributed_type == DistributedType.TPU and self.device_placement:
  83             xm.send_cpu_data_to_device(state_dict, self.state.device)
  84         self.optimizer.load_state_dict(state_dict)
  85 
  86     def state_dict(self):
  87         return self.optimizer.state_dict()
  88 
  89     def zero_grad(self):
  90         self.optimizer.zero_grad()
  91 
  92     def step(self):
  93         if self.state.distributed_type == DistributedType.TPU:
  94             xm.optimizer_step(self.optimizer)
  95         elif self.scaler is not None:
  96             self.scaler.step(self.optimizer)
  97             self.scaler.update()
  98         else:
  99             self.optimizer.step()
  100 
  101     def _switch_parameters(self, parameters_map):
  102         for param_group in self.optimizer.param_groups:
  103             param_group["params"] = [parameters_map.get(p, p) for p in param_group["params"]]
  104
  [end of src/accelerate/optimizer.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/src/accelerate/optimizer.py b/src/accelerate/optimizer.py
  --- a/src/accelerate/optimizer.py
  +++ b/src/accelerate/optimizer.py
  @@ -14,6 +14,8 @@
   
   import torch
   
  +from packaging import version
  +
   from .state import AcceleratorState, DistributedType, is_tpu_available
   
   
  @@ -86,8 +88,18 @@
       def state_dict(self):
           return self.optimizer.state_dict()
   
  -    def zero_grad(self):
  -        self.optimizer.zero_grad()
  +    def zero_grad(self, set_to_none=None):
  +        if version.parse(torch.__version__) < version.parse("1.7.0"):
  +            if set_to_none is not None:
  +                raise ValueError(
  +                    "`set_to_none` for Optimizer.zero_grad` was introduced in PyTorch 1.7.0 and can't be used for "
  +                    f"earlier versions (found version {torch.__version__})."
  +                )
  +            self.optimizer.zero_grad()
  +        else:
  +            if set_to_none is not None:
  +                set_to_none = False
  +            self.optimizer.zero_grad(set_to_none=set_to_none)
   
       def step(self):
           if self.state.distributed_type == DistributedType.TPU:
  
verification_info: {"golden_diff": "diff --git a/src/accelerate/optimizer.py b/src/accelerate/optimizer.py\n--- a/src/accelerate/optimizer.py\n+++ b/src/accelerate/optimizer.py\n@@ -14,6 +14,8 @@\n \n import torch\n \n+from packaging import version\n+\n from .state import AcceleratorState, DistributedType, is_tpu_available\n \n \n@@ -86,8 +88,18 @@\n     def state_dict(self):\n         return self.optimizer.state_dict()\n \n-    def zero_grad(self):\n-        self.optimizer.zero_grad()\n+    def zero_grad(self, set_to_none=None):\n+        if version.parse(torch.__version__) < version.parse(\"1.7.0\"):\n+            if set_to_none is not None:\n+                raise ValueError(\n+                    \"`set_to_none` for Optimizer.zero_grad` was introduced in PyTorch 1.7.0 and can't be used for \"\n+                    f\"earlier versions (found version {torch.__version__}).\"\n+                )\n+            self.optimizer.zero_grad()\n+        else:\n+            if set_to_none is not None:\n+                set_to_none = False\n+            self.optimizer.zero_grad(set_to_none=set_to_none)\n \n     def step(self):\n         if self.state.distributed_type == DistributedType.TPU:\n", "issue": "AcceleratedOptimizer `zero_grad` argument not supported: `set_to_none`\nCurrently the `AcceleratedOptimizer` class doesn't support the argument `set_to_none`, is this an intentional exclusion?\n", "before_files": [{"content": "# Copyright 2021 The HuggingFace Team. All rights reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport torch\n\nfrom .state import AcceleratorState, DistributedType, is_tpu_available\n\n\nif is_tpu_available():\n    import torch_xla.core.xla_model as xm\n\n\ndef move_to_device(state, device):\n    if isinstance(state, (list, tuple)):\n        return type(state)(move_to_device(t, device) for t in state)\n    elif isinstance(state, dict):\n        return type(state)({k: move_to_device(v, device) for k, v in state.items()})\n    elif isinstance(state, torch.Tensor):\n        return state.to(device)\n    return state\n\n\nclass AcceleratedOptimizer(torch.optim.Optimizer):\n    \"\"\"\n    Internal wrapper around a torch optimizer.\n\n    Args:\n        optimizer (:obj:`torch.optim.optimizer.Optimizer`):\n            The optimizer to wrap.\n        device_placement (:obj:`bool`, `optional`, defaults to :obj:`True`):\n            Whether or not the optimizer should handle device placement. If so, it will place the state dictionary of\n            :obj:`optimizer` on the right device.\n        scaler (:obj:`torch.cuda.amp.grad_scaler.GradScaler`, `optional`):\n            The scaler to use in the step function if training with mixed precision.\n    \"\"\"\n\n    def __init__(self, optimizer, device_placement=True, scaler=None):\n        self.optimizer = optimizer\n        self.scaler = scaler\n        self.state = AcceleratorState()\n\n        # Handle device placement\n        if device_placement:\n            state_dict = self.optimizer.state_dict()\n            if self.state.distributed_type == DistributedType.TPU:\n                xm.send_cpu_data_to_device(state_dict, self.state.device)\n            else:\n                state_dict = move_to_device(state_dict, self.state.device)\n            self.optimizer.load_state_dict(state_dict)\n\n    @property\n    def param_groups(self):\n        return self.optimizer.param_groups\n\n    @param_groups.setter\n    def param_groups(self, param_groups):\n        self.optimizer.param_groups = param_groups\n\n    @property\n    def defaults(self):\n        return self.optimizer.defaults\n\n    @defaults.setter\n    def defaults(self, defaults):\n        self.optimizer.defaults = defaults\n\n    def add_param_group(self, param_group):\n        self.optimizer.add_param_group(param_group)\n\n    def load_state_dict(self, state_dict):\n        if self.state.distributed_type == DistributedType.TPU and self.device_placement:\n            xm.send_cpu_data_to_device(state_dict, self.state.device)\n        self.optimizer.load_state_dict(state_dict)\n\n    def state_dict(self):\n        return self.optimizer.state_dict()\n\n    def zero_grad(self):\n        self.optimizer.zero_grad()\n\n    def step(self):\n        if self.state.distributed_type == DistributedType.TPU:\n            xm.optimizer_step(self.optimizer)\n        elif self.scaler is not None:\n            self.scaler.step(self.optimizer)\n            self.scaler.update()\n        else:\n            self.optimizer.step()\n\n    def _switch_parameters(self, parameters_map):\n        for param_group in self.optimizer.param_groups:\n            param_group[\"params\"] = [parameters_map.get(p, p) for p in param_group[\"params\"]]\n", "path": "src/accelerate/optimizer.py"}]}