code: def run_doctests(test_module, test_directory, options):
      """
      Assumes the incoming test module is called doctest, and simply executes the
      xdoctest runner on the torch library itself.
      """
      import pathlib
  
      import xdoctest
  
      pkgpath = pathlib.Path(torch.__file__).parent
  
      exclude_module_list = ["torch._vendor.*"]
      enabled = {
          # TODO: expose these options to the user
          # For now disable all feature-conditional tests
          # 'lapack': 'auto',
          # 'cuda': 'auto',
          # 'cuda1': 'auto',
          # 'qengine': 'auto',
          "lapack": 0,
          "cuda": 0,
          "cuda1": 0,
          "qengine": 0,
          "autograd_profiler": 0,
          "cpp_ext": 0,
          "monitor": 0,
          "onnx": "auto",
      }
  
      # Resolve "auto" based on a test to determine if the feature is available.
      if enabled["cuda"] == "auto" and torch.cuda.is_available():
          enabled["cuda"] = True
  
      if (
          enabled["cuda1"] == "auto"
          and torch.cuda.is_available()
          and torch.cuda.device_count() > 1
      ):
          enabled["cuda1"] = True
  
      if enabled["lapack"] == "auto" and torch._C.has_lapack:
          enabled["lapack"] = True
  
      if enabled["qengine"] == "auto":
          try:
              # Is there a better check if quantization is enabled?
              import torch.ao.nn.quantized as nnq  # NOQA: F401
  
              torch.backends.quantized.engine = "qnnpack"
              torch.backends.quantized.engine = "fbgemm"
          except (ImportError, RuntimeError):
              ...
          else:
              enabled["qengine"] = True
  
      if enabled["onnx"] == "auto":
          try:
              import onnx  # NOQA: F401
              import onnxruntime  # NOQA: F401
              import onnxscript  # NOQA: F401
          except ImportError:
              exclude_module_list.append("torch.onnx.*")
              enabled["onnx"] = False
          else:
              enabled["onnx"] = True
  
      # Set doctest environment variables
      if enabled["cuda"]:
          os.environ["TORCH_DOCTEST_CUDA"] = "1"
  
      if enabled["cuda1"]:
          os.environ["TORCH_DOCTEST_CUDA1"] = "1"
  
      if enabled["lapack"]:
          os.environ["TORCH_DOCTEST_LAPACK"] = "1"
  
      if enabled["qengine"]:
          os.environ["TORCH_DOCTEST_QENGINE"] = "1"
  
      if enabled["autograd_profiler"]:
          os.environ["TORCH_DOCTEST_AUTOGRAD_PROFILER"] = "1"
  
      if enabled["cpp_ext"]:
          os.environ["TORCH_DOCTEST_CPP_EXT"] = "1"
  
      if enabled["monitor"]:
          os.environ["TORCH_DOCTEST_MONITOR"] = "1"
  
      if enabled["onnx"]:
          os.environ["TORCH_DOCTEST_ONNX"] = "1"
  
      if 0:
          # TODO: could try to enable some of these
          os.environ["TORCH_DOCTEST_QUANTIZED_DYNAMIC"] = "1"
          os.environ["TORCH_DOCTEST_ANOMALY"] = "1"
          os.environ["TORCH_DOCTEST_AUTOGRAD"] = "1"
          os.environ["TORCH_DOCTEST_HUB"] = "1"
          os.environ["TORCH_DOCTEST_DATALOADER"] = "1"
          os.environ["TORCH_DOCTEST_FUTURES"] = "1"
  
      pkgpath = os.path.dirname(torch.__file__)
  
      xdoctest_config = {
          "global_exec": r"\n".join(
              [
                  "from torch import nn",
                  "import torch.nn.functional as F",
                  "import torch",
              ]
          ),
          "analysis": "static",  # set to "auto" to test doctests in compiled modules
          "style": "google",
          "options": "+IGNORE_WHITESPACE",
      }
      xdoctest_verbose = max(1, options.verbose)
      run_summary = xdoctest.runner.doctest_module(
          os.fspath(pkgpath),
          config=xdoctest_config,
          verbose=xdoctest_verbose,
          command=options.xdoctest_command,
          argv=[],
          exclude=exclude_module_list,
      )
      result = 1 if run_summary.get("n_failed", 0) else 0
      return result
  
imports: import argparse
  import copy
  import glob
  import json
  import os
  import pathlib
  import re
  import shutil
  import signal
  import subprocess
  import sys
  import tempfile
  import time
  from collections import defaultdict
  from contextlib import ExitStack
  from datetime import datetime
  from typing import Any, cast, Dict, List, NamedTuple, Optional, Sequence, Tuple, Union
  import pkg_resources
  import torch
  import torch.distributed
  from torch.multiprocessing import current_process, get_context
  from torch.testing._internal.common_utils import FILE_SCHEMA, get_report_path, IS_CI, parser, retry_shell, set_cwd, shell, TEST_WITH_ASAN, TEST_WITH_ROCM, TEST_WITH_SLOW_GRADCHECK
  from tools.stats.import_test_stats import ADDITIONAL_CI_FILES_FOLDER, TEST_CLASS_TIMES_FILE, TEST_TIMES_FILE
  from tools.stats.upload_metrics import add_global_metric, emit_metric
  from tools.testing.target_determination.determinator import AggregatedHeuristics, get_prediction_confidences, get_test_prioritizations
  from tools.testing.test_run import TestRun
  from tools.testing.test_selections import calculate_shards, get_test_case_configs, NUM_PROCS, ShardedTest, THRESHOLD
label_desc: Use image node_id 14 for calling a global function with example usage: run_doctests(test_module, test_directory, options) and returns: result