code: def make_dot(
      var,
      params=None,
      show_attrs=False,
      show_saved=False,
      max_attr_chars=50,
  ):
      """Produces Graphviz representation of PyTorch autograd graph.
  
      If a node represents a backward function, it is gray. Otherwise, the node
      represents a tensor and is either blue, orange, or green:
       - Blue: reachable leaf tensors that requires grad (tensors whose `.grad`
           fields will be populated during `.backward()`)
       - Orange: saved tensors of custom autograd functions as well as those
           saved by built-in backward nodes
       - Green: tensor passed in as outputs
       - Dark green: if any output is a view, we represent its base tensor with
           a dark green node.
  
      Args:
          var: output tensor
          params: dict of (name, tensor) to add names to node that requires grad
          show_attrs: whether to display non-tensor attributes of backward nodes
              (Requires PyTorch version >= 1.9)
          show_saved: whether to display saved tensor nodes that are not by custom
              autograd functions. Saved tensor nodes for custom functions, if
              present, are always displayed. (Requires PyTorch version >= 1.9)
          max_attr_chars: if show_attrs is `True`, sets max number of characters
              to display for any given attribute.
      """
      if LooseVersion(torch.__version__) < LooseVersion("1.9") and (
          show_attrs or show_saved
      ):
          warnings.warn(
              "make_dot: showing grad_fn attributes and saved variables"
              " requires PyTorch version >= 1.9. (This does NOT apply to"
              " saved tensors saved by custom autograd functions.)"
          )
  
      if params is not None:
          assert all(isinstance(p, Variable) for p in params.values())
          param_map = {id(v): k for k, v in params.items()}
      else:
          param_map = {}
  
      node_attr = dict(
          style="filled",
          shape="box",
          align="left",
          fontsize="10",
          ranksep="0.1",
          height="0.2",
          fontname="monospace",
      )
      dot = Digraph(node_attr=node_attr, graph_attr=dict(size="12,12"))
      seen = set()
  
      def size_to_str(size):
          return "(" + (", ").join(["%d" % v for v in size]) + ")"
  
      def get_var_name(var, name=None):
          if not name:
              name = param_map[id(var)] if id(var) in param_map else ""
          return "%s\n %s" % (name, size_to_str(var.size()))
  
      def add_nodes(fn):
          assert not torch.is_tensor(fn)
          if fn in seen:
              return
          seen.add(fn)
  
          if show_saved:
              for attr in dir(fn):
                  if not attr.startswith(SAVED_PREFIX):
                      continue
                  val = getattr(fn, attr)
                  seen.add(val)
                  attr = attr[len(SAVED_PREFIX) :]
                  if torch.is_tensor(val):
                      dot.edge(str(id(fn)), str(id(val)), dir="none")
                      dot.node(
                          str(id(val)),
                          get_var_name(val, attr),
                          fillcolor="orange",
                      )
                  if isinstance(val, tuple):
                      for i, t in enumerate(val):
                          if torch.is_tensor(t):
                              name = attr + "[%s]" % str(i)
                              dot.edge(
                                  str(id(fn)), str(id(t)), dir="none"
                              )
                              dot.node(
                                  str(id(t)),
                                  get_var_name(t, name),
                                  fillcolor="orange",
                              )
  
          if hasattr(fn, "variable"):
              # if grad_accumulator, add the node for `.variable`
              var = fn.variable
              seen.add(var)
              dot.node(
                  str(id(var)), get_var_name(var), fillcolor="lightblue"
              )
              dot.edge(str(id(var)), str(id(fn)))
  
          # add the node for this grad_fn
          dot.node(
              str(id(fn)), get_fn_name(fn, show_attrs, max_attr_chars)
          )
  
          # recurse
          if hasattr(fn, "next_functions"):
              for u in fn.next_functions:
                  if u[0] is not None:
                      dot.edge(str(id(u[0])), str(id(fn)))
                      add_nodes(u[0])
  
          # note: this used to show .saved_tensors in pytorch0.2, but stopped
          # working* as it was moved to ATen and Variable-Tensor merged
          # also note that this still works for custom autograd functions
          if hasattr(fn, "saved_tensors"):
              for t in fn.saved_tensors:
                  seen.add(t)
                  dot.edge(str(id(t)), str(id(fn)), dir="none")
                  dot.node(
                      str(id(t)), get_var_name(t), fillcolor="orange"
                  )
  
      def add_base_tensor(var, color="darkolivegreen1"):
          if var in seen:
              return
          seen.add(var)
          dot.node(str(id(var)), get_var_name(var), fillcolor=color)
          if var.grad_fn:
              add_nodes(var.grad_fn)
              dot.edge(str(id(var.grad_fn)), str(id(var)))
          if var._is_view():
              add_base_tensor(var._base, color="darkolivegreen3")
              dot.edge(str(id(var._base)), str(id(var)), style="dotted")
  
      # handle multiple outputs
      if isinstance(var, tuple):
          for v in var:
              add_base_tensor(v)
      else:
          add_base_tensor(var)
  
      resize_graph(dot)
  
      return dot
  
file_path: pytorchviz/torchviz/dot.py
imports: from collections import namedtuple
  from distutils.version import LooseVersion
  from graphviz import Digraph
  import torch
  from torch.autograd import Variable
  import warnings
label_desc: Use image node_id 2 for calling a global function with example usage: make_dot(var, params, show_attrs, show_saved, max_attr_chars) and returns: dot, unknown, unknown