class_docstr: :class:`~opacus.optimizers.optimizer.DPOptimizer` that implements
  adaptive clipping strategy
  https://arxiv.org/pdf/1905.03871.pdf
class_docstr_tok: [":","class",":","`","~opacus.optimizers.optimizer.DPOptimizer","`","that","implements","adaptive","clipping","strategy","https",":","\/\/arxiv.org\/pdf\/1905.03871.pdf"]
code: def clip_and_accumulate(self):
      per_param_norms = [
          g.view(len(g), -1).norm(2, dim=-1) for g in self.grad_samples
      ]
      per_sample_norms = torch.stack(per_param_norms, dim=1).norm(
          2, dim=1
      )
      per_sample_clip_factor = (
          self.max_grad_norm / (per_sample_norms + 1e-6)
      ).clamp(max=1.0)
  
      # the two lines below are the only changes
      # relative to the parent DPOptimizer class.
      self.sample_size += len(per_sample_clip_factor)
      self.unclipped_num += (
          len(per_sample_clip_factor)
          - (per_sample_clip_factor < 1).sum()
      )
  
      for p in self.params:
          _check_processed_flag(p.grad_sample)
          grad_sample = self._get_flat_grad_sample(p)
          grad = contract("i,i...", per_sample_clip_factor, grad_sample)
  
          if p.summed_grad is not None:
              p.summed_grad += grad
          else:
              p.summed_grad = grad
  
          _mark_as_processed(p.grad_sample)
  
file_path: opacus/opacus/optimizers/adaclipoptimizer.py
imports: from __future__ import annotations
  import logging
  from typing import Callable, Optional
  import torch
  from opt_einsum import contract
  from torch.optim import Optimizer
  from .optimizer import DPOptimizer, _check_processed_flag, _generate_noise, _mark_as_processed
label_desc: Use image node_id 3 for calling the AdaClipDPOptimizer obj's underlying member method code with example usage: obj.clip_and_accumulate() without return types