class_docstr: Helper class to simplify the handling of gradient buckets
class_docstr_tok: ["Helper","class","to","simplify","the","handling","of","gradient","buckets"]
code: def _add_grad_as_view(
      self, param: torch.Tensor, keep_existing_value: bool = True
  ) -> None:
      assert (
          self.buffer.numel() > 0
      ), "Cannot add a gradient to a collapsed bucket, please rebuild"
      assert param.dtype == self.buffer.dtype
      assert param.device == self.buffer.device
  
      fill_next = self._fill + param.numel()
      assert fill_next <= self.buffer.numel()
  
      # Copy the current grad value, if any
      if param.grad is not None:
          # keep param.grad in place
          if keep_existing_value:
              self.buffer[self._fill : fill_next].copy_(
                  param.grad.data.flatten()
              )
          param.grad.data = self.buffer[self._fill : fill_next].view_as(
              param.data
          )
      else:
          param.grad = self.buffer[self._fill : fill_next].view_as(
              param.data
          )
      self._fill = fill_next
  
imports: from typing import Any, Callable, List, Optional, Union
  import torch
label_desc: Use image node_id 12 for calling the GradBucket obj's underlying member method code with example usage: obj._add_grad_as_view(param, keep_existing_value) without return types