docstr: Build a new model instance.
docstr_tok: ["Build","a","new","model","instance","."]
code: def build_model(cls, args, task):
      """Build a new model instance."""
      # make sure that all args are properly defaulted (in case there are any new ones)
      base_architecture(args)
  
      if args.encoder_layers != args.decoder_layers:
          raise ValueError(
              "--encoder-layers must match --decoder-layers"
          )
  
      max_source_positions = getattr(
          args, "max_source_positions", DEFAULT_MAX_SOURCE_POSITIONS
      )
      max_target_positions = getattr(
          args, "max_target_positions", DEFAULT_MAX_TARGET_POSITIONS
      )
  
      def load_pretrained_embedding_from_file(
          embed_path, dictionary, embed_dim
      ):
          num_embeddings = len(dictionary)
          padding_idx = dictionary.pad()
          embed_tokens = Embedding(
              num_embeddings, embed_dim, padding_idx
          )
          embed_dict = utils.parse_embedding(embed_path)
          utils.print_embed_overlap(embed_dict, dictionary)
          return utils.load_embedding(
              embed_dict, dictionary, embed_tokens
          )
  
      if args.encoder_embed_path:
          pretrained_encoder_embed = (
              load_pretrained_embedding_from_file(
                  args.encoder_embed_path,
                  task.source_dictionary,
                  args.encoder_embed_dim,
              )
          )
      else:
          num_embeddings = len(task.source_dictionary)
          pretrained_encoder_embed = Embedding(
              num_embeddings,
              args.encoder_embed_dim,
              task.source_dictionary.pad(),
          )
  
      if args.share_all_embeddings:
          # double check all parameters combinations are valid
          if task.source_dictionary != task.target_dictionary:
              raise ValueError(
                  "--share-all-embeddings requires a joint dictionary"
              )
          if args.decoder_embed_path and (
              args.decoder_embed_path != args.encoder_embed_path
          ):
              raise ValueError(
                  "--share-all-embed not compatible with --decoder-embed-path"
              )
          if args.encoder_embed_dim != args.decoder_embed_dim:
              raise ValueError(
                  "--share-all-embeddings requires --encoder-embed-dim to "
                  "match --decoder-embed-dim"
              )
          pretrained_decoder_embed = pretrained_encoder_embed
          args.share_decoder_input_output_embed = True
      else:
          # separate decoder input embeddings
          pretrained_decoder_embed = None
          if args.decoder_embed_path:
              pretrained_decoder_embed = (
                  load_pretrained_embedding_from_file(
                      args.decoder_embed_path,
                      task.target_dictionary,
                      args.decoder_embed_dim,
                  )
              )
      # one last double check of parameter combinations
      if args.share_decoder_input_output_embed and (
          args.decoder_embed_dim != args.decoder_out_embed_dim
      ):
          raise ValueError(
              "--share-decoder-input-output-embeddings requires "
              "--decoder-embed-dim to match --decoder-out-embed-dim"
          )
  
      if args.encoder_freeze_embed:
          pretrained_encoder_embed.weight.requires_grad = False
      if args.decoder_freeze_embed:
          pretrained_decoder_embed.weight.requires_grad = False
  
      encoder = LSTMEncoder(
          dictionary=task.source_dictionary,
          embed_dim=args.encoder_embed_dim,
          hidden_size=args.encoder_hidden_size,
          num_layers=args.encoder_layers,
          dropout_in=args.encoder_dropout_in,
          dropout_out=args.encoder_dropout_out,
          bidirectional=args.encoder_bidirectional,
          pretrained_embed=pretrained_encoder_embed,
          max_source_positions=max_source_positions,
      )
      decoder = LSTMDecoder(
          dictionary=task.target_dictionary,
          embed_dim=args.decoder_embed_dim,
          hidden_size=args.decoder_hidden_size,
          out_embed_dim=args.decoder_out_embed_dim,
          num_layers=args.decoder_layers,
          dropout_in=args.decoder_dropout_in,
          dropout_out=args.decoder_dropout_out,
          attention=utils.eval_bool(args.decoder_attention),
          encoder_output_units=encoder.output_units,
          pretrained_embed=pretrained_decoder_embed,
          share_input_output_embed=args.share_decoder_input_output_embed,
          adaptive_softmax_cutoff=(
              utils.eval_str_list(
                  args.adaptive_softmax_cutoff, type=int
              )
              if args.criterion == "adaptive_loss"
              else None
          ),
          max_target_positions=max_target_positions,
          residuals=False,
      )
      return cls(encoder, decoder)
  
imports: from typing import Dict, List, Optional, Tuple
  import torch
  import torch.nn
  import torch.nn.functional
  from fairseq import utils
  from fairseq.models import FairseqEncoder, FairseqEncoderDecoderModel, FairseqIncrementalDecoder, register_model, register_model_architecture
  from fairseq.modules import AdaptiveSoftmax, FairseqDropout
  from torch import Tensor
label_desc: Use image node_id 3 for calling the LSTMModel obj's underlying member method code with example usage: obj.build_model(cls, args, task) and returns: cls, utils