Skip to content

Commit

Permalink
feat: handling wrong model type
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelLarkin committed Oct 2, 2024
1 parent 409e757 commit 66d4d43
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions hfgl/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
dynamic_range_compression_torch,
get_spectral_transform,
)
from loguru import logger
from pydantic import ValidationError
from torch.nn import AvgPool1d, Conv1d, Conv2d, ConvTranspose1d
from torch.nn.utils import spectral_norm
from torch.nn.utils.parametrizations import weight_norm
Expand Down Expand Up @@ -454,8 +456,16 @@ class HiFiGANGenerator(pl.LightningModule):

def __init__(self, config: dict | VocoderConfig):
super().__init__()

if not isinstance(config, VocoderConfig):
config = VocoderConfig(**config)
try:
config = VocoderConfig(**config)
except ValidationError as e:
logger.error(f"{e}")
raise TypeError(
"Unable to load config. Possible causes: is it really a VocoderConfig? or the correct version?"
)

self.config = config
self.generator = Generator(self.config)

Expand All @@ -464,7 +474,14 @@ def on_load_checkpoint(self, checkpoint):
Note, this shouldn't fail on different versions of pydantic anymore,
but it will fail on breaking changes to the config. We should catch those exceptions
and handle them appropriately."""
self.config = VocoderConfig(**checkpoint["hyper_parameters"]["config"])
try:
config = VocoderConfig(**checkpoint["hyper_parameters"]["config"])
except ValidationError as e:
logger.error(f"{e}")
raise TypeError(
"Unable to load config. Possible causes: is it really a VocoderConfig? or the correct version?"
)
self.config = config

def on_save_checkpoint(self, checkpoint):
"""Serialize the checkpoint hyperparameters"""
Expand All @@ -479,7 +496,13 @@ def __init__(self, config: dict | VocoderConfig):
# Because we serialize the configurations when saving checkpoints,
# sometimes what is passed is actually just a dict.
if not isinstance(config, VocoderConfig):
config = VocoderConfig(**config)
try:
config = VocoderConfig(**config)
except ValidationError as e:
logger.error(f"{e}")
raise TypeError(
"Unable to load config. Possible causes: is it really a VocoderConfig? or the correct version?"
)
self.config = config
self.mpd = MultiPeriodDiscriminator(config)
self.msd = MultiScaleDiscriminator(config)
Expand Down

0 comments on commit 66d4d43

Please sign in to comment.