o
    ¡¿¯ia  ã                   @   sT   d Z ddlmZmZ ddlmZmZ ddlZddlm	Z	 G dd„ de	ej
jeƒZdS )z2ESPnetModel abstract class for GAN-based training.é    )ÚABCÚabstractmethod)ÚDictÚUnionN)ÚAbsESPnetModelc                   @   sp   e Zd ZdZe	ddedejdee	e
ejee	ejf ef f fdd„ƒZedejdee	ejf fdd	„ƒZd
S )ÚAbsGANESPnetModela  The common abstract class among each GAN-based task.

    "ESPnetModel" is referred to a class which inherits torch.nn.Module,
    and makes the dnn-models "forward" as its member field, a.k.a delegate
    pattern. And "forward" must accept the argument "forward_generator" and
    Return the dict of "loss", "stats", "weight", and "optim_idx".
    "optim_idx" for generator must be 0 and that for discriminator must be 1.

    Example:
        >>> from espnet2.tasks.abs_task import AbsTask
        >>> class YourESPnetModel(AbsGANESPnetModel):
        ...     def forward(self, input, input_lengths, forward_generator=True):
        ...         ...
        ...         if forward_generator:
        ...             # return loss for the generator
        ...             # optim idx 0 indicates generator optimizer
        ...             return dict(loss=loss, stats=stats, weight=weight, optim_idx=0)
        ...         else:
        ...             # return loss for the discriminator
        ...             # optim idx 1 indicates discriminator optimizer
        ...             return dict(loss=loss, stats=stats, weight=weight, optim_idx=1)
        >>> class YourTask(AbsTask):
        ...     @classmethod
        ...     def build_model(cls, args: argparse.Namespace) -> YourESPnetModel:
    TÚforward_generatorÚbatchÚreturnc                 K   ó   t ‚)aa  Return the generator loss or the discrimiantor loss.

        This method must have an argument "forward_generator" to switch the generator
        loss calculation and the discrimiantor loss calculation. If forward_generator
        is true, return the generator loss with optim_idx 0. If forward_generator is
        false, return the discrimiantor loss with optim_idx 1.

        Args:
            forward_generator (bool): Whether to return the generator loss or the
                discrimiantor loss. This must have the default value.

        Returns:
            Dict[str, Any]:
                * loss (Tensor): Loss scalar tensor.
                * stats (Dict[str, float]): Statistics to be monitored.
                * weight (Tensor): Weight tensor to summarize losses.
                * optim_idx (int): Optimizer index (0 for G and 1 for D).

        ©ÚNotImplementedError)Úselfr   r	   © r   úV/home/ubuntu/.local/lib/python3.10/site-packages/espnet2/train/abs_gan_espnet_model.pyÚforward)   s   zAbsGANESPnetModel.forwardc                 K   r   )Nr   )r   r	   r   r   r   Úcollect_featsD   s   zAbsGANESPnetModel.collect_featsN)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚboolÚtorchÚTensorr   Ústrr   Úintr   r   r   r   r   r   r      s    þþý ü$r   )r   Úabcr   r   Útypingr   r   r   Úespnet2.train.abs_espnet_modelr   ÚnnÚModuler   r   r   r   r   Ú<module>   s   