class_docstr: Solver for MusicGen training task.
  
  Used in: https://arxiv.org/abs/2306.05284
docstr: Instantiate models and optimizer.
docstr_tok: ["Instantiate","models","and","optimizer","."]
code: def build_model(self) -> None:
      """Instantiate models and optimizer."""
      # we can potentially not use all quantizers with which the EnCodec model was trained
      # (e.g. we trained the model with quantizers dropout)
      self.compression_model = (
          CompressionSolver.wrapped_model_from_checkpoint(
              self.cfg,
              self.cfg.compression_model_checkpoint,
              device=self.device,
          )
      )
      assert (
          self.compression_model.sample_rate == self.cfg.sample_rate
      ), (
          f"Compression model sample rate is {self.compression_model.sample_rate} but "
          f"Solver sample rate is {self.cfg.sample_rate}."
      )
      # ensure we have matching configuration between LM and compression model
      assert (
          self.cfg.transformer_lm.card
          == self.compression_model.cardinality
      ), (
          "Cardinalities of the LM and compression model don't match: ",
          f"LM cardinality is {self.cfg.transformer_lm.card} vs ",
          f"compression model cardinality is {self.compression_model.cardinality}",
      )
      assert (
          self.cfg.transformer_lm.n_q
          == self.compression_model.num_codebooks
      ), (
          "Numbers of codebooks of the LM and compression models don't match: ",
          f"LM number of codebooks is {self.cfg.transformer_lm.n_q} vs ",
          f"compression model numer of codebooks is {self.compression_model.num_codebooks}",
      )
      self.logger.info(
          "Compression model has %d codebooks with %d cardinality, and a framerate of %d",
          self.compression_model.num_codebooks,
          self.compression_model.cardinality,
          self.compression_model.frame_rate,
      )
      # instantiate LM model
      self.model: models.LMModel = models.builders.get_lm_model(
          self.cfg
      ).to(self.device)
      if self.cfg.fsdp.use:
          assert not self.cfg.autocast, "Cannot use autocast with fsdp"
          self.model = self.wrap_with_fsdp(self.model)
      self.register_ema("model")
      # initialize optimization
      self.optimizer = builders.get_optimizer(
          builders.get_optim_parameter_groups(self.model),
          self.cfg.optim,
      )
      self.lr_scheduler = builders.get_lr_scheduler(
          self.optimizer, self.cfg.schedule, self.total_updates
      )
      self.register_stateful("model", "optimizer", "lr_scheduler")
      self.register_best_state("model")
      self.autocast_dtype = {
          "float16": torch.float16,
          "bfloat16": torch.bfloat16,
      }[self.cfg.autocast_dtype]
      self.scaler: tp.Optional[torch.cuda.amp.GradScaler] = None
      if self.cfg.fsdp.use:
          need_scaler = self.cfg.fsdp.param_dtype == "float16"
      else:
          need_scaler = (
              self.cfg.autocast and self.autocast_dtype is torch.float16
          )
      if need_scaler:
          if self.cfg.fsdp.use:
              from torch.distributed.fsdp.sharded_grad_scaler import (
                  ShardedGradScaler,
              )
  
              self.scaler = ShardedGradScaler()  # type: ignore
          else:
              self.scaler = torch.cuda.amp.GradScaler()
          self.register_stateful("scaler")
  
imports: from pathlib import Path
  import time
  import typing
  import warnings
  import flashy
  import math
  import omegaconf
  import torch
  from torch.nn import functional
  from .None import base, builders
  from .compression import CompressionSolver
  from ..None import metrics
  from ..None import models
  from ..data.audio_dataset import AudioDataset
  from ..data.music_dataset import MusicDataset, MusicInfo, AudioInfo
  from ..data.audio_utils import normalize_audio
  from ..modules.conditioners import JointEmbedCondition, SegmentWithAttributes, WavCondition
  from ..utils.cache import CachedBatchWriter, CachedBatchLoader
  from ..utils.samples.manager import SampleManager
  from ..utils.utils import get_dataset_from_loader, is_jsonable, warn_once, model_hash
label_desc: Use image node_id 5 for calling the MusicGenSolver obj's underlying member method code with example usage: obj.build_model() without return types