code: def main(cfg: DictConfig):
      if isinstance(cfg, Namespace):
          cfg = convert_namespace_to_omegaconf(cfg)
  
      assert (
          cfg.common_eval.path is not None
      ), "--path required for generation!"
      assert (
          not cfg.generation.sampling
          or cfg.generation.nbest == cfg.generation.beam
      ), "--sampling requires --nbest to be equal to --beam"
      assert (
          cfg.generation.replace_unk is None
          or cfg.dataset.dataset_impl == "raw"
      ), "--replace-unk requires a raw text dataset (--dataset-impl=raw)"
  
      if cfg.common_eval.results_path is not None:
          os.makedirs(cfg.common_eval.results_path, exist_ok=True)
          output_path = os.path.join(
              cfg.common_eval.results_path,
              "generate-{}.txt".format(cfg.dataset.gen_subset),
          )
          with open(
              output_path, "w", buffering=1, encoding="utf-8"
          ) as h:
              return _main(cfg, h)
      else:
          return _main(cfg, sys.stdout)
  
file_path: fairseq/fairseq_cli/generate.py
imports: import ast
  import logging
  import math
  import os
  import sys
  from argparse import Namespace
  from itertools import chain
  import numpy
  import torch
  from omegaconf import DictConfig
  from fairseq import checkpoint_utils, options, scoring, tasks, utils
  from fairseq.dataclass.utils import convert_namespace_to_omegaconf
  from fairseq.logging import progress_bar
  from fairseq.logging.meters import StopwatchMeter, TimeMeter
label_desc: Use image node_id 1 for calling a global function with example usage: main(cfg) and returns: _main, _main