code: def infer_tests_to_run(
      output_file,
      diff_with_last_commit=False,
      filters=None,
      json_output_file=None,
  ):
      modified_files = get_modified_python_files(
          diff_with_last_commit=diff_with_last_commit
      )
      print(f"\n### MODIFIED FILES ###\n{_print_list(modified_files)}")
  
      # Create the map that will give us all impacted modules.
      impacted_modules_map = create_reverse_dependency_map()
      impacted_files = modified_files.copy()
      for f in modified_files:
          if f in impacted_modules_map:
              impacted_files.extend(impacted_modules_map[f])
  
      # Remove duplicates
      impacted_files = sorted(list(set(impacted_files)))
      print(f"\n### IMPACTED FILES ###\n{_print_list(impacted_files)}")
  
      # Grab the corresponding test files:
      if "setup.py" in impacted_files:
          test_files_to_run = ["tests"]
          repo_utils_launch = True
      else:
          # Grab the corresponding test files:
          test_files_to_run = []
          for f in impacted_files:
              # Modified test files are always added
              if f.startswith("tests/"):
                  test_files_to_run.append(f)
              # Example files are tested separately
              elif f.startswith("examples/pytorch"):
                  test_files_to_run.append(
                      "examples/pytorch/test_pytorch_examples.py"
                  )
                  test_files_to_run.append(
                      "examples/pytorch/test_accelerate_examples.py"
                  )
              elif f.startswith("examples/flax"):
                  test_files_to_run.append(
                      "examples/flax/test_flax_examples.py"
                  )
              else:
                  new_tests = module_to_test_file(f)
                  if new_tests is not None:
                      if isinstance(new_tests, str):
                          test_files_to_run.append(new_tests)
                      else:
                          test_files_to_run.extend(new_tests)
  
          # Remove duplicates
          test_files_to_run = sorted(list(set(test_files_to_run)))
          # Make sure we did not end up with a test file that was removed
          test_files_to_run = [
              f
              for f in test_files_to_run
              if os.path.isfile(f) or os.path.isdir(f)
          ]
          if filters is not None:
              filtered_files = []
              for filter in filters:
                  filtered_files.extend(
                      [
                          f
                          for f in test_files_to_run
                          if f.startswith(filter)
                      ]
                  )
              test_files_to_run = filtered_files
          repo_utils_launch = any(
              f.split(os.path.sep)[1] == "repo_utils"
              for f in test_files_to_run
          )
  
      if repo_utils_launch:
          repo_util_file = (
              Path(output_file).parent / "test_repo_utils.txt"
          )
          with open(repo_util_file, "w", encoding="utf-8") as f:
              f.write("tests/repo_utils")
  
      print(f"\n### TEST TO RUN ###\n{_print_list(test_files_to_run)}")
      if len(test_files_to_run) > 0:
          with open(output_file, "w", encoding="utf-8") as f:
              f.write(" ".join(test_files_to_run))
  
          # Create a map that maps test categories to test files, i.e. `models/bert` -> [...test_modeling_bert.py, ...]
  
          # Get all test directories (and some common test files) under `tests` and `tests/models` if `test_files_to_run`
          # contains `tests` (i.e. when `setup.py` is changed).
          if "tests" in test_files_to_run:
              test_files_to_run = get_all_tests()
  
          if json_output_file is not None:
              test_map = {}
              for test_file in test_files_to_run:
                  # `test_file` is a path to a test folder/file, starting with `tests/`. For example,
                  #   - `tests/models/bert/test_modeling_bert.py` or `tests/models/bert`
                  #   - `tests/trainer/test_trainer.py` or `tests/trainer`
                  #   - `tests/test_modeling_common.py`
                  names = test_file.split(os.path.sep)
                  if names[1] == "models":
                      # take the part like `models/bert` for modeling tests
                      key = "/".join(names[1:3])
                  elif len(names) > 2 or not test_file.endswith(".py"):
                      # test folders under `tests` or python files under them
                      # take the part like tokenization, `pipeline`, etc. for other test categories
                      key = "/".join(names[1:2])
                  else:
                      # common test files directly under `tests/`
                      key = "common"
  
                  if key not in test_map:
                      test_map[key] = []
                  test_map[key].append(test_file)
  
              # sort the keys & values
              keys = sorted(test_map.keys())
              test_map = {
                  k: " ".join(sorted(test_map[k])) for k in keys
              }
              with open(json_output_file, "w", encoding="UTF-8") as fp:
                  json.dump(test_map, fp, ensure_ascii=False)
  
imports: import argparse
  import collections
  import json
  import os
  import re
  from contextlib import contextmanager
  from pathlib import Path
  from git import Repo
label_desc: Use image node_id 16 for calling a global function with example usage: infer_tests_to_run(output_file, diff_with_last_commit, filters, json_output_file) without return types