code: def build_model(cfg: FairseqDataclass, task, from_checkpoint=False):
      model = None
      model_type = getattr(cfg, "_name", None) or getattr(
          cfg, "arch", None
      )
  
      if not model_type and len(cfg) == 1:
          # this is hit if config object is nested in directory that is named after model type
  
          model_type = next(iter(cfg))
          if model_type in MODEL_DATACLASS_REGISTRY:
              cfg = cfg[model_type]
          else:
              raise Exception(
                  "Could not infer model type from directory. Please add _name field to indicate model type. "
                  "Available models: "
                  + str(MODEL_DATACLASS_REGISTRY.keys())
                  + " Requested model type: "
                  + model_type
              )
  
      if model_type in ARCH_MODEL_REGISTRY:
          # case 1: legacy models
          model = ARCH_MODEL_REGISTRY[model_type]
      elif model_type in MODEL_DATACLASS_REGISTRY:
          # case 2: config-driven models
          model = MODEL_REGISTRY[model_type]
  
      if model_type in MODEL_DATACLASS_REGISTRY:
          # set defaults from dataclass. note that arch name and model name can be the same
          dc = MODEL_DATACLASS_REGISTRY[model_type]
  
          if isinstance(cfg, argparse.Namespace):
              cfg = dc.from_namespace(cfg)
          else:
              cfg = merge_with_parent(dc(), cfg, from_checkpoint)
      else:
          if model_type in ARCH_CONFIG_REGISTRY:
              with open_dict(cfg) if OmegaConf.is_config(
                  cfg
              ) else ExitStack():
                  # this calls the different "arch" functions (like base_architecture()) that you indicate
                  # if you specify --arch on the command line. this is only applicable to the old argparse based models
                  # hydra models should expose different architectures via different config files
                  # it will modify the cfg object and default parameters according to the arch
                  ARCH_CONFIG_REGISTRY[model_type](cfg)
  
      assert model is not None, (
          f"Could not infer model type from {cfg}. "
          "Available models: {}".format(MODEL_DATACLASS_REGISTRY.keys())
          + f" Requested model type: {model_type}"
      )
  
      return model.build_model(cfg, task)
  
file_path: fairseq/fairseq/models/__init__.py
imports: import argparse
  import importlib
  import os
  from contextlib import ExitStack
  from fairseq.dataclass import FairseqDataclass
  from fairseq.dataclass.utils import merge_with_parent
  from hydra.core.config_store import ConfigStore
  from omegaconf import open_dict, OmegaConf
  from .composite_encoder import CompositeEncoder
  from .distributed_fairseq_model import DistributedFairseqModel
  from .fairseq_decoder import FairseqDecoder
  from .fairseq_encoder import FairseqEncoder
  from .fairseq_incremental_decoder import FairseqIncrementalDecoder
  from .fairseq_model import BaseFairseqModel, FairseqEncoderDecoderModel, FairseqEncoderModel, FairseqLanguageModel, FairseqModel, FairseqMultiModel
label_desc: Use image node_id 1 for calling a global function with example usage: build_model(cfg, task, from_checkpoint) and returns: model