class_docstr: HiFi-GAN scale discriminator module.
docstr: Fix the compatibility of weight / spectral normalization issue.
  
  Some pretrained models are trained with configs that use weight / spectral
  normalization, but actually, the norm is not applied. This causes the mismatch
  of the parameters with configs. To solve this issue, when parameter mismatch
  happens in loading pretrained model, we remove the norm from the current model.
  
  See also:
      - https://github.com/espnet/espnet/pull/5240
      - https://github.com/espnet/espnet/pull/5249
      - https://github.com/kan-bayashi/ParallelWaveGAN/pull/409
docstr_tok: ["Fix","the","compatibility","of","weight","\/","spectral","normalization","issue",".","Some","pretrained","models","are","trained","with","configs","that","use","weight","\/","spectral","normalization",",","but","actually",",","the","norm","is","not","applied",".","This","causes","the","mismatch","of","the","parameters","with","configs",".","To","solve","this","issue",",","when","parameter","mismatch","happens","in","loading","pretrained","model",",","we","remove","the","norm","from","the","current","model",".","See","also",":","-","https",":","\/\/github.com\/espnet\/espnet\/pull\/5240","-","https",":","\/\/github.com\/espnet\/espnet\/pull\/5249","-","https",":","\/\/github.com\/kan-bayashi\/ParallelWaveGAN\/pull\/409"]
code: def _load_state_dict_pre_hook(
      self,
      state_dict,
      prefix,
      local_metadata,
      strict,
      missing_keys,
      unexpected_keys,
      error_msgs,
  ):
      """Fix the compatibility of weight / spectral normalization issue.
  
      Some pretrained models are trained with configs that use weight / spectral
      normalization, but actually, the norm is not applied. This causes the mismatch
      of the parameters with configs. To solve this issue, when parameter mismatch
      happens in loading pretrained model, we remove the norm from the current model.
  
      See also:
          - https://github.com/espnet/espnet/pull/5240
          - https://github.com/espnet/espnet/pull/5249
          - https://github.com/kan-bayashi/ParallelWaveGAN/pull/409
  
      """
      current_module_keys = [
          x for x in state_dict.keys() if x.startswith(prefix)
      ]
      if self.use_weight_norm and any(
          [k.endswith("weight") for k in current_module_keys]
      ):
          logging.warning(
              "It seems weight norm is not applied in the pretrained model but the"
              " current model uses it. To keep the compatibility, we remove the norm"
              " from the current model. This may cause unexpected behavior due to the"
              " parameter mismatch in finetuning. To avoid this issue, please change"
              " the following parameters in config to false:\n"
              " - discriminator_params.follow_official_norm\n"
              " - discriminator_params.scale_discriminator_params.use_weight_norm\n"
              " - discriminator_params.scale_discriminator_params.use_spectral_norm\n"
              "\n"
              "See also:\n"
              " - https://github.com/espnet/espnet/pull/5240\n"
              " - https://github.com/espnet/espnet/pull/5249"
          )
          self.remove_weight_norm()
          self.use_weight_norm = False
          for k in current_module_keys:
              if k.endswith("weight_g") or k.endswith("weight_v"):
                  del state_dict[k]
  
      if self.use_spectral_norm and any(
          [k.endswith("weight") for k in current_module_keys]
      ):
          logging.warning(
              "It seems spectral norm is not applied in the pretrained model but the"
              " current model uses it. To keep the compatibility, we remove the norm"
              " from the current model. This may cause unexpected behavior due to the"
              " parameter mismatch in finetuning. To avoid this issue, please change"
              " the following parameters in config to false:\n"
              " - discriminator_params.follow_official_norm\n"
              " - discriminator_params.scale_discriminator_params.use_weight_norm\n"
              " - discriminator_params.scale_discriminator_params.use_spectral_norm\n"
              "\n"
              "See also:\n"
              " - https://github.com/espnet/espnet/pull/5240\n"
              " - https://github.com/espnet/espnet/pull/5249"
          )
          self.remove_spectral_norm()
          self.use_spectral_norm = False
          for k in current_module_keys:
              if (
                  k.endswith("weight_u")
                  or k.endswith("weight_v")
                  or k.endswith("weight_orig")
              ):
                  del state_dict[k]
  
imports: import copy
  import logging
  from typing import Any, Dict, List, Optional
  import numpy
  import torch
  import torch.nn.functional
  from espnet2.gan_tts.hifigan.residual_block import ResidualBlock
label_desc: Use image node_id 7 for calling the HiFiGANScaleDiscriminator obj's underlying member method code with example usage: obj._load_state_dict_pre_hook(state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs) without return types