prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Error when loading optimizer state
  Thanks for this awesome product!!
  
  When I try to load optimizer state on TPUs, I get an error, since self.device_placement is never initialized in AcceleratedOptimizer..!
  https://github.com/huggingface/accelerate/blob/e0a420f7cb32124cadeeae690b56e463f8fc598f/src/accelerate/optimizer.py#L83-L86
  Error when loading optimizer state
  Thanks for this awesome product!!
  
  When I try to load optimizer state on TPUs, I get an error, since self.device_placement is never initialized in AcceleratedOptimizer..!
  https://github.com/huggingface/accelerate/blob/e0a420f7cb32124cadeeae690b56e463f8fc598f/src/accelerate/optimizer.py#L83-L86
  
  </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 packaging import version
  18 
  19 from .state import AcceleratorState, DistributedType, is_tpu_available
  20 from .utils import honor_type
  21 
  22 
  23 if is_tpu_available():
  24     import torch_xla.core.xla_model as xm
  25 
  26 
  27 def move_to_device(state, device):
  28     if isinstance(state, (list, tuple)):
  29         return honor_type(state, (move_to_device(t, device) for t in state))
  30     elif isinstance(state, dict):
  31         return type(state)({k: move_to_device(v, device) for k, v in state.items()})
  32     elif isinstance(state, torch.Tensor):
  33         return state.to(device)
  34     return state
  35 
  36 
  37 class AcceleratedOptimizer(torch.optim.Optimizer):
  38     """
  39     Internal wrapper around a torch optimizer.
  40 
  41     Args:
  42         optimizer (:obj:`torch.optim.optimizer.Optimizer`):
  43             The optimizer to wrap.
  44         device_placement (:obj:`bool`, `optional`, defaults to :obj:`True`):
  45             Whether or not the optimizer should handle device placement. If so, it will place the state dictionary of
  46             :obj:`optimizer` on the right device.
  47         scaler (:obj:`torch.cuda.amp.grad_scaler.GradScaler`, `optional`):
  48             The scaler to use in the step function if training with mixed precision.
  49     """
  50 
  51     def __init__(self, optimizer, device_placement=True, scaler=None):
  52         self.optimizer = optimizer
  53         self.scaler = scaler
  54         self.state = AcceleratorState()
  55 
  56         # Handle device placement
  57         if device_placement:
  58             state_dict = self.optimizer.state_dict()
  59             if self.state.distributed_type == DistributedType.TPU:
  60                 xm.send_cpu_data_to_device(state_dict, self.state.device)
  61             else:
  62                 state_dict = move_to_device(state_dict, self.state.device)
  63             self.optimizer.load_state_dict(state_dict)
  64 
  65     @property
  66     def param_groups(self):
  67         return self.optimizer.param_groups
  68 
  69     @param_groups.setter
  70     def param_groups(self, param_groups):
  71         self.optimizer.param_groups = param_groups
  72 
  73     @property
  74     def defaults(self):
  75         return self.optimizer.defaults
  76 
  77     @defaults.setter
  78     def defaults(self, defaults):
  79         self.optimizer.defaults = defaults
  80 
  81     def add_param_group(self, param_group):
  82         self.optimizer.add_param_group(param_group)
  83 
  84     def load_state_dict(self, state_dict):
  85         if self.state.distributed_type == DistributedType.TPU and self.device_placement:
  86             xm.send_cpu_data_to_device(state_dict, self.state.device)
  87         self.optimizer.load_state_dict(state_dict)
  88 
  89     def state_dict(self):
  90         return self.optimizer.state_dict()
  91 
  92     def zero_grad(self, set_to_none=None):
  93         if version.parse(torch.__version__) < version.parse("1.7.0"):
  94             if set_to_none is not None:
  95                 raise ValueError(
  96                     "`set_to_none` for Optimizer.zero_grad` was introduced in PyTorch 1.7.0 and can't be used for "
  97                     f"earlier versions (found version {torch.__version__})."
  98                 )
  99             self.optimizer.zero_grad()
  100         else:
  101             if set_to_none is not None:
  102                 set_to_none = False
  103             self.optimizer.zero_grad(set_to_none=set_to_none)
  104 
  105     def step(self, closure=None):
  106         if self.state.distributed_type == DistributedType.TPU:
  107             optimizer_args = {"closure": closure} if closure is not None else {}
  108             xm.optimizer_step(self.optimizer, optimizer_args=optimizer_args)
  109         elif self.scaler is not None:
  110             self.scaler.step(self.optimizer, closure)
  111             self.scaler.update()
  112         else:
  113             self.optimizer.step(closure)
  114 
  115     def _switch_parameters(self, parameters_map):
  116         for param_group in self.optimizer.param_groups:
  117             param_group["params"] = [parameters_map.get(p, p) for p in param_group["params"]]
  118 
  119     @property
  120     def is_overflow(self):
  121         """This needs to be implemented at the end"""
  122         return False  # TODO: implement it
  123
  [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
  @@ -52,6 +52,7 @@
           self.optimizer = optimizer
           self.scaler = scaler
           self.state = AcceleratorState()
  +        self.device_placement = device_placement
   
           # Handle device placement
           if device_placement:
  
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@@ -52,6 +52,7 @@\n         self.optimizer = optimizer\n         self.scaler = scaler\n         self.state = AcceleratorState()\n+        self.device_placement = device_placement\n \n         # Handle device placement\n         if device_placement:\n", "issue": "Error when loading optimizer state\nThanks for this awesome product!!\r\n\r\nWhen I try to load optimizer state on TPUs, I get an error, since self.device_placement is never initialized in AcceleratedOptimizer..!\r\nhttps://github.com/huggingface/accelerate/blob/e0a420f7cb32124cadeeae690b56e463f8fc598f/src/accelerate/optimizer.py#L83-L86\nError when loading optimizer state\nThanks for this awesome product!!\r\n\r\nWhen I try to load optimizer state on TPUs, I get an error, since self.device_placement is never initialized in AcceleratedOptimizer..!\r\nhttps://github.com/huggingface/accelerate/blob/e0a420f7cb32124cadeeae690b56e463f8fc598f/src/accelerate/optimizer.py#L83-L86\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 packaging import version\n\nfrom .state import AcceleratorState, DistributedType, is_tpu_available\nfrom .utils import honor_type\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 honor_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, 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, closure=None):\n        if self.state.distributed_type == DistributedType.TPU:\n            optimizer_args = {\"closure\": closure} if closure is not None else {}\n            xm.optimizer_step(self.optimizer, optimizer_args=optimizer_args)\n        elif self.scaler is not None:\n            self.scaler.step(self.optimizer, closure)\n            self.scaler.update()\n        else:\n            self.optimizer.step(closure)\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\n    @property\n    def is_overflow(self):\n        \"\"\"This needs to be implemented at the end\"\"\"\n        return False  # TODO: implement it\n", "path": "src/accelerate/optimizer.py"}]}