code: def gen_allowed_objs_and_ids(
      record=False, c_binding_only=True
  ) -> AllowedObjects:
      """
      Walk torch.* and get the ids of all the stuff in it
      """
  
      warnings.filterwarnings(
          "ignore", category=UserWarning, module="torch.distributed"
      )
      torch_object_ids = dict()
      ctx_mamager_classes = set()
      c_binding_in_graph_functions = set()
      non_c_binding_in_graph_functions = set()
      torch_name_rule_map = dict()
  
      # Add obj to ctx_mamager_classes set if it's a torch context manager class.
      # This is used to generate the ctx manager class list based on heuristic.
      def heuristic_record_if_ctx_manager(obj, module, name):
          if (
              issubclass(type(obj), type)
              and hasattr(obj, "__enter__")
              and hasattr(obj, "__exit__")
          ):
              torch_name_rule_map[
                  f"{module.__name__}.{name}"
              ] = TorchCtxManagerClassVariable
              ctx_mamager_classes.add(obj)
  
      # In some platforms, these functions were loaded as classes instead of functions.
      # To mitigate these weired cases, we need this special check.
      def is_special_functions(obj):
          return hashable(obj) and obj in {
              torch._C._cuda_isCurrentStreamCapturing,
              torch._C._graph_pool_handle,
          }
  
      # Add obj to c_binding_in_graph_functions set or non_c_binding_in_graph_functions set
      # if it's a torch function or method.
      # This is used to generate the in graph function list based on heuristic.
      def heuristic_record_if_in_graph_function(obj, module, name):
          try:
              if hasattr(obj, "__wrapped__"):
                  obj = obj.__wrapped__
          except Exception:
              pass
          if isinstance(
              obj,
              (
                  types.FunctionType,
                  types.BuiltinFunctionType,
                  types.MethodDescriptorType,
                  types.WrapperDescriptorType,
              ),
          ) or is_special_functions(obj):
              torch_name_rule_map[
                  f"{module.__name__}.{name}"
              ] = TorchInGraphFunctionVariable
              if c_binding_only:
                  if not hasattr(obj, "__code__"):
                      c_binding_in_graph_functions.add(obj)
              else:
                  if hasattr(obj, "__code__"):
                      non_c_binding_in_graph_functions.add(obj)
                  else:
                      c_binding_in_graph_functions.add(obj)
  
      def _is_allowed_module_prefix(obj):
          allowed_modules = ("torch", "math")
          # torch.nn.modules.rnn is disallowed because these modules internally
          # flatten their parameters.  This flattening process will call
          # Tensor.set_ with a Storage, and Storages cannot be traced with
          # AOTAutograd; so we need to graph-break. To ensure this, we inline
          # these functions, rather than keep them opaque-ly in the graph.
          disallowed_modules = [
              "torch.optim.",
              "torch.nn.modules.rnn.",
              "torch._dynamo.",
              "torch._C._dynamo.",
              "torch._inductor.",
              "torch._C.inductor.",
              "torch.fx.",
              "torch._C._autograd",
              "torch._C._cudart",
              "torch._C._distributed_autograd",
              "torch._C._distributed_c10d",
              "torch._C._distributed_rpc",
              "torch._C._functorch",
              "torch._C._monitor",
              "torch._C._nvtx",
              "torch._C._lazy",
              "torch._C._profiler",
              "torch.__config__",
              "torch._custom_op",
              "torch._decomp",
              "torch._dispatch",
              "torch._export",
              "torch._functorch.make_functional",
              "torch._functorch.compile_utils",
              "torch._functorch.partitioners",
              "torch._functorch.aot_autograd",
              "torch._functorch.compilers",
              "torch._functorch.fx_minifier",
              "torch.autograd.profiler_util",
              "torch.autograd.profiler",
              "torch._jit_internal",
              "torch._library",
              "torch._lobpcg",
              "torch._logging",
              "torch._meta_registrations",
              "torch._namedtensor_internals",
              "torch._numpy",
              "torch._sources",
              "torch._subclasses",
              "torch._tensor",
              "torch._tensor_str",
              "torch._utils",
              "torch._utils_internal",
              "torch._vmap_internals",
              "torch.compiler",
              "torch.distributed",
              "torch.export",
              "torch.hub",
              "torch.jit",
              "torch.library",
              "torch.masked.maskedtensor",
              "torch.nn.init",
              "torch.nn.modules.module",
              "torch.nn.parallel",
              "torch.nn.utils",
              "torch.multiprocessing",
              "torch.onnx",
              "torch.overrides",
              "torch.package",
              "torch.profiler",
              "torch.serialization",
              "torch.storage",
              "torch.utils",
          ]
          if config.trace_distributed:
              disallowed_modules.append("torch.distributed.")
  
          allowed_modules_dot = tuple(
              [x + "." for x in allowed_modules]
          )
          module = inspect.getmodule(obj)
          if module is None:
              return False
  
          mod_name = module.__name__
  
          if any(mod_name.startswith(m) for m in disallowed_modules):
              return False
  
          return mod_name in allowed_modules or mod_name.startswith(
              allowed_modules_dot
          )
  
      def _find_torch_objects(module):
          if any(
              module.__name__.startswith(mod_name)
              for mod_name in config.allowed_functions_module_string_ignorelist
          ):
              return
          torch_object_ids[id(module)] = module.__name__
          for name, obj in list(module.__dict__.items()):
              if id(obj) not in torch_object_ids:
                  # Dynamo allows all builtins into the graph and does not attempt
                  # to introspect into them. We don't want to allow instances of
                  # HigherOrderOperator into the graph all the time (Dynamo needs
                  # to introspect the body functions of these HigherOrderOperator
                  # first, decide they are safe, and then allow them into the graph).
                  # So we exclude HigherOrderOperator from being a builtin.
                  import torch._ops
  
                  if isinstance(obj, torch._ops.HigherOrderOperator):
                      continue
  
                  # We want to trace through `grad` and `vmap`
                  if obj in (
                      torch.func.grad,
                      deprecated_func.grad,
                      torch.func.vmap,
                      deprecated_func.vmap,
                      torch.nn.functional.triplet_margin_with_distance_loss,
                      torch.cond,
                  ):
                      continue
  
                  if isinstance(obj, types.ModuleType):
                      if obj.__name__.startswith(
                          "torch."
                      ) and _is_allowed_module_prefix(obj):
                          torch_object_ids[
                              id(obj)
                          ] = f"{module.__name__}.{name}"
                          _find_torch_objects(obj)
                  elif _is_allowed_module_prefix(obj):
                      if record:
                          heuristic_record_if_ctx_manager(
                              obj, module, name
                          )
                          heuristic_record_if_in_graph_function(
                              obj, module, name
                          )
                      torch_object_ids[
                          id(obj)
                      ] = f"{module.__name__}.{name}"
                  elif inspect.getmodule(
                      obj
                  ) is None and not is_safe_constant(obj):
                      if record:
                          heuristic_record_if_ctx_manager(
                              obj, module, name
                          )
                          heuristic_record_if_in_graph_function(
                              obj, module, name
                          )
                      torch_object_ids[
                          id(obj)
                      ] = f"{module.__name__}.{name}"
  
      _find_torch_objects(torch)
      _find_torch_objects(math)
  
      return AllowedObjects(
          torch_object_ids,
          ctx_mamager_classes,
          c_binding_in_graph_functions,
          non_c_binding_in_graph_functions,
          torch_name_rule_map,
      )
  
imports: import dataclasses
  import importlib
  import inspect
  import math
  import types
  import unittest
  import warnings
  from typing import Any, Dict, Set
  import torch
  import torch._dynamo.config
  import torch._dynamo.test_case
  import torch._functorch.deprecated
  from torch._dynamo.skipfiles import FUNC_INLINELIST, LEGACY_MOD_INLINELIST, MOD_INLINELIST
  from torch._dynamo.trace_rules import load_object, torch_c_binding_in_graph_functions, torch_ctx_manager_classes, torch_non_c_binding_in_graph_functions
  from torch._dynamo.utils import hashable, is_safe_constant, istype
  from torch._dynamo.variables import TorchCtxManagerClassVariable, TorchInGraphFunctionVariable
label_desc: Use image node_id 2 for calling a global function with example usage: gen_allowed_objs_and_ids(record, c_binding_only) and returns: AllowedObjects, unknown, unknown, False, False