def METHOD_NAME(cls, cfg, task):
    """Build a new model instance."""
    # --  TODO T96535332
    #  bug caused by interaction between OmegaConf II and argparsing
    cfg.decoder.input_dim = int(cfg.decoder.input_dim)
    cfg.decoder.output_dim = int(cfg.decoder.output_dim)
    # --
    if cfg.encoder.layers_to_keep:
        cfg.encoder.layers = len(cfg.encoder.layers_to_keep.split(","))
    if cfg.decoder.layers_to_keep:
        cfg.decoder.layers = len(cfg.decoder.layers_to_keep.split(","))
    src_dict, tgt_dict = task.source_dictionary, task.target_dictionary
    if cfg.share_all_embeddings:
        if src_dict != tgt_dict:
            raise ValueError("--share-all-embeddings requires a joined dictionary")
        if cfg.encoder.embed_dim != cfg.decoder.embed_dim:
            raise ValueError(
                "--share-all-embeddings requires --encoder-embed-dim to match --decoder-embed-dim"
            )
        if cfg.decoder.embed_path and (
            cfg.decoder.embed_path != cfg.encoder.embed_path
        ):
            raise ValueError(
                "--share-all-embeddings not compatible with --decoder-embed-path"
            )
        encoder_embed_tokens = cls.build_embedding(
            cfg, src_dict, cfg.encoder.embed_dim, cfg.encoder.embed_path
        )
        decoder_embed_tokens = encoder_embed_tokens
        cfg.share_decoder_input_output_embed = True
    else:
        encoder_embed_tokens = cls.build_embedding(
            cfg, src_dict, cfg.encoder.embed_dim, cfg.encoder.embed_path
        )
        decoder_embed_tokens = cls.build_embedding(
            cfg, tgt_dict, cfg.decoder.embed_dim, cfg.decoder.embed_path
        )
    if cfg.offload_activations:
        cfg.checkpoint_activations = True  # offloading implies checkpointing
    encoder = cls.build_encoder(cfg, src_dict, encoder_embed_tokens)
    decoder = cls.build_decoder(cfg, tgt_dict, decoder_embed_tokens)
    if not cfg.share_all_embeddings:
        # fsdp_wrap is a no-op when --ddp-backend != fully_sharded
        encoder = fsdp_wrap(encoder, min_num_params=cfg.min_params_to_wrap)
        decoder = fsdp_wrap(decoder, min_num_params=cfg.min_params_to_wrap)
    return cls(cfg, encoder, decoder)