class_docstr: Replace get_attr with placeholder.
  
  The parameters and buffers accessed by the original get_attr are returned;
  they are useful when creating random inputs for the modified graph_module.
class_docstr_tok: ["Replace","get_attr","with","placeholder",".","The","parameters","and","buffers","accessed","by","the","original","get_attr","are","returned",";","they","are","useful","when","creating","random","inputs","for","the","modified","graph_module","."]
code: def _run(self, *args, **kwargs) -> torch.fx.GraphModule:
      graph_module = self.module
      graph = graph_module.graph
      replaced_attrs: List[torch.Tensor] = []
      for node in graph.nodes:
          if node.op == "get_attr":
              replaced_attr: Optional[torch.Tensor] = None
              # get_attr could retrieve either parameter or buffer, so
              # we need to try both.
              try:
                  replaced_attr = graph_module.get_parameter(
                      node.target
                  )
              except AttributeError:
                  # It's possible that model author use buffer instead of
                  # parameter to store trainable weights. In this case,
                  # 1. get_parameter will throw something like
                  #    AttributeError: `bias` is not an nn.Parameter.
                  # 2. get_buffer should work.
                  replaced_attr = graph_module.get_buffer(node.target)
  
              # Reassign op type so that get_attr node becomes placeholder node.
              node.op = "placeholder"
              # The target name in placeholder must be a valid Python identifier.
              # Thus, we replace, e.g., "module.submodule.weight" with
              # "module_submodule_weight".
              node.target = node.target.replace(".", "_")
              # Default value is None. This is needed as long as the "graph_module"
              # has optional inputs. Assume the original forward signature is
              #  def forward(self, x, y=None)
              # and the replaced get_attr node has target "z". Then, the modified
              # signature should be
              #  def forward(self, x, y=None, z=None)
              # Without the following line, the signature will be
              #  def forward(self, x, y=None, z)
              # , which is not valid Python code.
              node.args = (None,)
  
              replaced_attrs.append(replaced_attr)
  
      self._replaced_attrs = tuple(replaced_attrs)
  
      return graph_module
  
file_path: pytorch/torch/onnx/_internal/fx/passes/virtualization.py
imports: from __future__ import annotations
  from typing import List, Optional, Tuple
  import torch
  import torch.fx
  from torch.onnx._internal import _beartype
  from torch.onnx._internal.fx import _pass
label_desc: Use image node_id 2 for calling the ReplaceGetAttrWithPlaceholder obj's underlying member method code with example usage: obj._run() and returns: graph_module