class_docstr: Standard solver for AudioCraft.
  
  The standard solver implements a base training loop with the following stages:
  train, valid, evaluate and generate that are expected to be all defined for
  solvers in AudioCraft. It also provides a nice default management of Dora history replay,
  checkpoint management across epoch, and logging configuration.
  
  AudioCraft solvers must inherit from the StandardSolver and define the methods
  associated to each stage as well as the show, build_model and build_dataloaders methods.
class_docstr_tok: ["Standard","solver","for","AudioCraft",".","The","standard","solver","implements","a","base","training","loop","with","the","following","stages",":","train",",","valid",",","evaluate","and","generate","that","are","expected","to","be","all","defined","for","solvers","in","AudioCraft",".","It","also","provides","a","nice","default","management","of","Dora","history","replay",",","checkpoint","management","across","epoch",",","and","logging","configuration",".","AudioCraft","solvers","must","inherit","from","the","StandardSolver","and","define","the","methods","associated","to","each","stage","as","well","as","the","show",",","build_model","and","build_dataloaders","methods","."]
docstr: Common logic for train and valid stages.
docstr_tok: ["Common","logic","for","train","and","valid","stages","."]
code: def common_train_valid(self, dataset_split: str, **kwargs: tp.Any):
      """Common logic for train and valid stages."""
      self.model.train(self.is_training)
  
      loader = self.dataloaders[dataset_split]
      # get a different order for distributed training, otherwise this will get ignored
      if flashy.distrib.world_size() > 1 and isinstance(
          loader.sampler,
          torch.utils.data.distributed.DistributedSampler,
      ):
          loader.sampler.set_epoch(self.epoch)
      updates_per_epoch = (
          self.train_updates_per_epoch
          if self.is_training
          else len(loader)
      )
      if self.cfg.benchmark_no_load:
          self.logger.warning(
              "Fake loading for benchmarking: re-using first batch"
          )
          batch = next(iter(loader))
          loader = [batch] * updates_per_epoch  # type: ignore
      lp = self.log_progress(
          self.current_stage,
          loader,
          total=updates_per_epoch,
          updates=self.log_updates,
      )
      average = flashy.averager()  # epoch wise average
      instant_average = flashy.averager()  # average between two logging
      metrics: dict = {}
  
      with self.profiler, self.deadlock_detect:  # profiler will only run for the first 20 updates.
          for idx, batch in enumerate(lp):
              self.deadlock_detect.update("batch")
              if idx >= updates_per_epoch:
                  break
              metrics = {}
              metrics = self.run_step(idx, batch, metrics)
              self.deadlock_detect.update("step")
              # run EMA step
              if (
                  self.ema is not None
                  and self.is_training
                  and (idx + 1) % self.cfg.optim.ema.updates == 0
              ):
                  self.logger.debug("EMA model step")
                  self.ema.step()
              self.deadlock_detect.update("ema")
              self.profiler.step()
              instant_metrics = instant_average(metrics)
              if lp.update(**instant_metrics):
                  instant_average = (
                      flashy.averager()
                  )  # reset averager between two logging
              metrics = average(metrics)  # epoch wise average
              self.deadlock_detect.update("end_batch")
  
      metrics = flashy.distrib.average_metrics(
          metrics, updates_per_epoch
      )
      return metrics
  
imports: from abc import ABC, abstractmethod
  from contextlib import contextmanager
  from pathlib import Path
  import typing
  import flashy
  import omegaconf
  import torch
  from torch import nn
  from ..None import optim
  from ..optim import fsdp
  from ..utils import checkpoint
  from ..utils.autocast import TorchAutocast
  from ..utils.best_state import BestStateDictManager
  from ..utils.deadlock import DeadlockDetect
  from ..utils.profiler import Profiler
  from ..utils.utils import copy_state, dict_from_config, model_hash, with_rank_rng
label_desc: Use image node_id 32 for calling the StandardSolver obj's underlying member method code with example usage: obj.common_train_valid(dataset_split) and returns: metrics