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."""
      src_dict, dst_dict = (
          task.source_dictionary,
          task.target_dictionary,
      )
      base_architecture(args)
  
      assert hasattr(args, "char_source_dict_size"), (
          "args.char_source_dict_size required. "
          "should be set by load_binarized_dataset()"
      )
  
      if args.share_all_embeddings:
          if src_dict != dst_dict:
              raise RuntimeError(
                  "--share-all-embeddings requires a joined dictionary"
              )
          if args.encoder_embed_dim != args.decoder_embed_dim:
              raise RuntimeError(
                  "--share-all-embeddings requires --encoder-embed-dim "
                  "to match --decoder-embed-dim"
              )
          if args.decoder_pretrained_embed and (
              args.decoder_pretrained_embed
              != args.encoder_pretrained_embed
          ):
              raise RuntimeError(
                  "--share-all-embeddings not compatible with "
                  "--decoder-pretrained-embed"
              )
          encoder_embed_tokens = transformer.build_embedding(
              src_dict,
              args.encoder_embed_dim,
              args.encoder_pretrained_embed,
              args.encoder_freeze_embed,
          )
          decoder_embed_tokens = encoder_embed_tokens
          args.share_decoder_input_output_embed = True
      else:
          encoder_embed_tokens = transformer.build_embedding(
              src_dict,
              args.encoder_embed_dim,
              args.encoder_pretrained_embed,
              args.encoder_freeze_embed,
          )
          decoder_embed_tokens = transformer.build_embedding(
              dst_dict,
              args.decoder_embed_dim,
              args.decoder_pretrained_embed,
              args.decoder_freeze_embed,
          )
  
      args.embed_bytes = getattr(args, "embed_bytes", False)
  
      # If we embed bytes then the number of indices is fixed and does not
      # depend on the dictionary
      if args.embed_bytes:
          num_chars = (
              vocab_constants.NUM_BYTE_INDICES + TAGS.__len__() + 1
          )
      else:
          num_chars = args.char_source_dict_size
  
      # In case use_pretrained_weights is true, verify the model params
      # are correctly set
      if args.embed_bytes and getattr(
          args, "use_pretrained_weights", False
      ):
          verify_pretrain_params(args)
  
      encoder = CharCNNEncoder(
          args,
          src_dict,
          encoder_embed_tokens,
          num_chars=num_chars,
          embed_dim=args.char_embed_dim,
          char_cnn_params=args.char_cnn_params,
          char_cnn_nonlinear_fn=args.char_cnn_nonlinear_fn,
          char_cnn_num_highway_layers=args.char_cnn_num_highway_layers,
          char_cnn_output_dim=getattr(args, "char_cnn_output_dim", -1),
          use_pretrained_weights=getattr(
              args, "use_pretrained_weights", False
          ),
          finetune_pretrained_weights=getattr(
              args, "finetune_pretrained_weights", False
          ),
          weights_file=getattr(args, "pretrained_weights_file", ""),
      )
      decoder = transformer.TransformerDecoder(
          args=args,
          src_dict=src_dict,
          dst_dict=dst_dict,
          embed_tokens=decoder_embed_tokens,
      )
      return cls(task, encoder, decoder)
  
imports: import logging
  import math
  from ast import literal_eval
  import torch
  import torch.nn
  import torch.nn.functional
  from fairseq.models import FairseqEncoder, register_model, register_model_architecture, transformer
  from fairseq.modules import SinusoidalPositionalEmbedding
  from pytorch_translate import char_encoder, transformer, utils, vocab_constants
  from pytorch_translate.char_source_model import verify_pretrain_params
  from pytorch_translate.common_layers import TransformerEncoderGivenEmbeddings, VariableTracker
  from pytorch_translate.data.dictionary import TAGS
label_desc: Use image node_id 3 for calling the CharSourceTransformerModel obj's underlying member method code with example usage: obj.build_model(cls, args, task) and returns: cls