class_docstr: This is DP-friendly implementation of nn.MultiheadAttention.
  For full reference see original module refer to
  :class:`torch.nn.MultiheadAttention`.
  
  Current implementation leverages pytorch modules as building blocks
  to allow DP engine to calculate per-sample gradients.
  This is in contrast with original implementation based on nn.functional.
class_docstr_tok: ["This","is","DP-friendly","implementation","of","nn.MultiheadAttention",".","For","full","reference","see","original","module","refer","to",":","class",":","`","torch.nn.MultiheadAttention","`",".","Current","implementation","leverages","pytorch","modules","as","building","blocks","to","allow","DP","engine","to","calculate","per-sample","gradients",".","This","is","in","contrast","with","original","implementation","based","on","nn.functional","."]
code: def state_dict(self, destination=None, prefix="", keep_vars=False):
      if destination is None:
          destination = OrderedDict()
          destination._metadata = OrderedDict()
  
      local_metadata = dict(version=self._version)
      if hasattr(destination, "_metadata"):
          destination._metadata[prefix[:-1]] = local_metadata
  
      destination_alter = OrderedDict()
      if len(prefix.split(".")) > 2:
          alter_key = ".".join(prefix.split(".")[:-2]) + ".emb.weight"
      else:
          alter_key = "emb.weight"
      if alter_key in destination:
          destination_alter[alter_key] = destination[alter_key]
  
      self._save_to_state_dict(destination, prefix, keep_vars)
      for name, module in self._modules.items():
          if module is not None:
              module.state_dict(
                  destination=destination,
                  prefix=prefix + name + ".",
                  keep_vars=keep_vars,
              )
  
      if (self.kdim == self.embed_dim) and (
          self.vdim == self.embed_dim
      ):
          destination_alter[prefix + "in_proj_weight"] = torch.cat(
              (
                  destination[prefix + "qlinear.weight"],
                  destination[prefix + "klinear.weight"],
                  destination[prefix + "vlinear.weight"],
              ),
              0,
          )
      else:
          destination_alter[prefix + "q_proj_weight"] = destination[
              prefix + "qlinear.weight"
          ]
          destination_alter[prefix + "k_proj_weight"] = destination[
              prefix + "klinear.weight"
          ]
          destination_alter[prefix + "v_proj_weight"] = destination[
              prefix + "vlinear.weight"
          ]
  
      if (
          (prefix + "qlinear.bias") in destination
          and (prefix + "klinear.bias") in destination
          and (prefix + "vlinear.bias") in destination
      ):
          destination_alter[prefix + "in_proj_bias"] = torch.cat(
              (
                  destination[prefix + "qlinear.bias"],
                  destination[prefix + "klinear.bias"],
                  destination[prefix + "vlinear.bias"],
              ),
              0,
          )
  
      if self.add_bias_kv:
          destination_alter[prefix + "bias_k"] = self.unsqueeze_0_2(
              destination[prefix + "seq_bias_k.bias"]
          )
          destination_alter[prefix + "bias_v"] = self.unsqueeze_0_2(
              destination[prefix + "seq_bias_v.bias"]
          )
  
      destination_alter[prefix + "out_proj.weight"] = destination[
          prefix + "out_proj.weight"
      ]
      if (prefix + "out_proj.bias") in destination:
          destination_alter[prefix + "out_proj.bias"] = destination[
              prefix + "out_proj.bias"
          ]
  
      for hook in self._state_dict_hooks.values():
          hook_result = hook(
              self, destination_alter, prefix, local_metadata
          )
          if hook_result is not None:
              destination_alter = hook_result
  
      return destination_alter
  
file_path: opacus/opacus/layers/dp_multihead_attention.py
imports: import warnings
  from collections import OrderedDict
  import torch
  import torch.nn
  import torch.nn.functional
  from torch.nn.parameter import Parameter
label_desc: Use image node_id 5 for calling the DPMultiheadAttention obj's underlying member method code with example usage: obj.state_dict(destination, prefix, keep_vars) and returns: destination_alter