code: def generate_and_write_preds_for_qa(
      runner,
      supertask: str,
      output_dir: str,
      phase: str,
      skip_if_done: bool = False,
  ):
      """Generate predictions (test) for QA tasks and write them in XTREME submission format"""
      preds_pickle_path = os.path.join(
          output_dir, f"{supertask}_test_preds.p"
      )
      if skip_if_done and os.path.exists(preds_pickle_path):
          print(f"Skipping cause {preds_pickle_path} exists")
          return
  
      if phase == "val":
          task_name_list = (
              runner.jiant_task_container.task_run_config.val_task_list
          )
      elif phase == "test":
          task_name_list = (
              runner.jiant_task_container.task_run_config.test_task_list
          )
      else:
          raise KeyError(phase)
      task_name_list = [
          task_name
          for task_name in task_name_list
          if task_name.startswith(supertask)
      ]
      if phase == "val":
          test_results_dict = runner.run_val(
              task_name_list=task_name_list
          )
      elif phase == "test":
          test_results_dict = {}
          test_dataloader_dict = runner.get_test_dataloader_dict()
          for task_name in task_name_list:
              test_results_dict[task_name] = jiant_runner.run_test(
                  test_dataloader=test_dataloader_dict[task_name],
                  jiant_model=runner.jiant_model,
                  task=runner.jiant_task_container.task_dict[task_name],
                  device=runner.device,
                  local_rank=runner.rparams.local_rank,
                  return_preds=False,
                  verbose=True,
              )
      else:
          raise KeyError(phase)
  
      # Generate QA preds
      tokenizer = runner.model.tokenizer
      for task_name in task_name_list:
          task_results = test_results_dict[task_name]
          task = runner.jiant_task_container.task_dict[task_name]
          logits = task_results["accumulator"].get_accumulated()
          lang = get_qa_language(supertask=supertask, task=task)
          if phase == "val":
              cached = runner.get_val_dataloader_dict([task_name])[
                  task_name
              ].dataset.chunked_file_data_cache.get_all()
          elif phase == "test":
              cached = runner.get_test_dataloader_dict()[
                  task_name
              ].dataset.chunked_file_data_cache.get_all()
          else:
              raise KeyError(phase)
          data_rows = [row["data_row"] for row in cached]
          (
              results,
              predictions,
          ) = squad_lib.compute_predictions_logits_v3(
              data_rows=data_rows,
              logits=logits,
              n_best_size=task.n_best_size,
              max_answer_length=task.max_answer_length,
              do_lower_case=model_resolution.resolve_is_lower_case(
                  tokenizer
              ),
              version_2_with_negative=task.version_2_with_negative,
              null_score_diff_threshold=task.null_score_diff_threshold,
              skip_get_final_text=(lang == "zh"),
              tokenizer=tokenizer,
          )
          test_results_dict[task_name]["preds"] = predictions
  
      jiant_evaluate.write_preds(
          eval_results_dict=test_results_dict,
          path=preds_pickle_path,
      )
      preds_output_dir = os.path.join(output_dir, "preds", supertask)
      os.makedirs(preds_output_dir, exist_ok=True)
      for task_name, task_results in test_results_dict.items():
          task = runner.jiant_task_container.task_dict[task_name]
          lang = get_qa_language(supertask=supertask, task=task)
          py_io.write_json(
              task_results["preds"],
              os.path.join(preds_output_dir, f"test-{lang}.json"),
          )
      print(
          f"Wrote {supertask} preds for {len(test_results_dict)} languages"
      )
  
imports: import os
  import numpy
  import torch
  import zconf
  import jiant.utils.python.io
  from jiant.utils.display import maybe_tqdm
  import jiant.shared.initialization
  import jiant.shared.model_resolution
  import jiant.proj.main.components.container_setup
  import jiant.proj.main.runner
  from jiant.proj.main.runscript import setup_runner
  import jiant.tasks.lib.templates.squad_style.core
  import jiant.tasks.lib.bucc2018
  import jiant.tasks
  import jiant.proj.main.components.evaluate
  from jiant.proj.main.modeling.primary import wrap_jiant_forward
label_desc: Use image node_id 5 for calling a global function with example usage: generate_and_write_preds_for_qa(runner, supertask, output_dir, phase, skip_if_done) without return types