# Buggy code
def forward(
    self, xs, ilens, ys, labels, olens, spembs=None, spcs=None, *args, **kwargs
):
    """Calculate forward propagation.

    Args:
        xs (Tensor): Batch of padded character ids (B, Tmax).
        ilens (LongTensor): Batch of lengths of each input batch (B,).
        ys (Tensor): Batch of padded target features (B, Lmax, odim).
        olens (LongTensor): Batch of the lengths of each target (B,).
        spembs (Tensor, optional): Batch of speaker embedding vectors (B, spk_embed_dim).
        spcs (Tensor, optional): Batch of groundtruth spectrograms (B, Lmax, spc_dim).

    Returns:
        Tensor: Loss value.

    """
    # remove unnecessary padded part (for multi-gpus)
    max_in = max(ilens)
    max_out = max(olens)
    if max_in != xs.shape[1]:
        xs = xs[:, :max_in]
    if max_out != ys.shape[1]:
        ys = ys[:, :max_out]
        labels = labels[:, :max_out]

    # calculate tacotron2 outputs
    hs, hlens = self.enc(xs, ilens)
    if self.spk_embed_dim is not None:
        spembs = F.normalize(spembs).unsqueeze(1).expand(-1, hs.size(1), -1)
        hs = torch.cat([hs, spembs], dim=-1)
    after_outs, before_outs, logits, att_ws = self.dec(hs, hlens, ys)

    # modifiy mod part of groundtruth
    if self.reduction_factor > 1:
        olens = olens.new([olen - olen % self.reduction_factor for olen in olens])
        max_out = max(olens)
        ys = ys[:, :max_out]
        labels = labels[:, :max_out]
        labels[:, -1] = 1.0  # make sure at least one frame has 1

    # caluculate taco2 loss
    l1_loss, mse_loss, bce_loss = self.taco2_loss(
        after_outs, before_outs, logits, ys, labels, olens
    )
    loss = l1_loss + mse_loss + bce_loss
    report_keys = [
        {"l1_loss": l1_loss.item()},
        {"mse_loss": mse_loss.item()},
        {"bce_loss": bce_loss.item()},
    ]

    # caluculate attention loss
    if self.use_guided_attn_loss:
        attn_loss = self.attn_loss(att_ws, ilens, olens)
        loss = loss + attn_loss
        report_keys += [
            {"attn_loss": attn_loss.item()},
        ]

    # caluculate cbhg loss
    if self.use_cbhg:
        # remove unnecessary padded part (for multi-gpus)
        if max_out != spcs.shape[1]:
            spcs = spcs[:, :max_out]

        # caluculate cbhg outputs & loss and report them
        cbhg_outs, _ = self.cbhg(after_outs, olens)
        cbhg_l1_loss, cbhg_mse_loss = self.cbhg_loss(cbhg_outs, spcs, olens)
        loss = loss + cbhg_l1_loss + cbhg_mse_loss
        report_keys += [
            {"cbhg_l1_loss": cbhg_l1_loss.item()},
            {"cbhg_mse_loss": cbhg_mse_loss.item()},
        ]

    report_keys += [{"loss": loss.item()}]
    self.reporter.report(report_keys)

    return loss


# Traceback (RuntimeError)
�[JException in main training loop: The size of tensor a (864) must match the size of tensor b (432) at non-singleton dimension 1
Traceback (most recent call last):
File "/home/abelab/k_inoue/work/tool/espnet/tools/venv/lib/python3.7/site-packages/chainer/training/trainer.py", line 315, in run
update()
File "/home/abelab/k_inoue/work/tool/espnet/espnet/tts/pytorch_backend/tts.py", line 153, in update
self.update_core()
File "/home/abelab/k_inoue/work/tool/espnet/espnet/tts/pytorch_backend/tts.py", line 134, in update_core
loss = self.model(**x).mean() / self.accum_grad
File "/home/abelab/k_inoue/work/tool/espnet/tools/venv/lib/python3.7/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(*input, **kwargs)
File "/home/abelab/k_inoue/work/tool/espnet/espnet/nets/pytorch_backend/e2e_tts_tacotron2.py", line 529, in forward
attn_loss = self.attn_loss(att_ws, ilens, olens)
File "/home/abelab/k_inoue/work/tool/espnet/tools/venv/lib/python3.7/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(*input, **kwargs)
File "/home/abelab/k_inoue/work/tool/espnet/espnet/nets/pytorch_backend/e2e_tts_tacotron2.py", line 68, in forward
losses = self.guided_attn_masks * att_ws
Will finalize trainer extensions and updater before reraising the exception.
Traceback (most recent call last):
File "/home/abelab/k_inoue/work/tool/espnet/egs/blizzard17/tts1/../../../espnet/bin/tts_train.py", line 179, in <module>
main(sys.argv[1:])
File "/home/abelab/k_inoue/work/tool/espnet/egs/blizzard17/tts1/../../../espnet/bin/tts_train.py", line 173, in main
train(args)
File "/home/abelab/k_inoue/work/tool/espnet/espnet/tts/pytorch_backend/tts.py", line 424, in train
trainer.run()
File "/home/abelab/k_inoue/work/tool/espnet/tools/venv/lib/python3.7/site-packages/chainer/training/trainer.py", line 329, in run
six.reraise(*sys.exc_info())
File "/home/abelab/k_inoue/work/tool/espnet/tools/venv/lib/python3.7/site-packages/six.py", line 693, in reraise
raise value
File "/home/abelab/k_inoue/work/tool/espnet/tools/venv/lib/python3.7/site-packages/chainer/training/trainer.py", line 315, in run
update()
File "/home/abelab/k_inoue/work/tool/espnet/espnet/tts/pytorch_backend/tts.py", line 153, in update
self.update_core()
File "/home/abelab/k_inoue/work/tool/espnet/espnet/tts/pytorch_backend/tts.py", line 134, in update_core
loss = self.model(**x).mean() / self.accum_grad
File "/home/abelab/k_inoue/work/tool/espnet/tools/venv/lib/python3.7/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(*input, **kwargs)
File "/home/abelab/k_inoue/work/tool/espnet/espnet/nets/pytorch_backend/e2e_tts_tacotron2.py", line 529, in forward
attn_loss = self.attn_loss(att_ws, ilens, olens)
File "/home/abelab/k_inoue/work/tool/espnet/tools/venv/lib/python3.7/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(*input, **kwargs)
File "/home/abelab/k_inoue/work/tool/espnet/espnet/nets/pytorch_backend/e2e_tts_tacotron2.py", line 68, in forward
losses = self.guided_attn_masks * att_ws
RuntimeError: The size of tensor a (864) must match the size of tensor b (432) at non-singleton dimension 1

# Fixed code
def forward(
    self, xs, ilens, ys, labels, olens, spembs=None, spcs=None, *args, **kwargs
):
    """Calculate forward propagation.

    Args:
        xs (Tensor): Batch of padded character ids (B, Tmax).
        ilens (LongTensor): Batch of lengths of each input batch (B,).
        ys (Tensor): Batch of padded target features (B, Lmax, odim).
        olens (LongTensor): Batch of the lengths of each target (B,).
        spembs (Tensor, optional): Batch of speaker embedding vectors (B, spk_embed_dim).
        spcs (Tensor, optional): Batch of groundtruth spectrograms (B, Lmax, spc_dim).

    Returns:
        Tensor: Loss value.

    """
    # remove unnecessary padded part (for multi-gpus)
    max_in = max(ilens)
    max_out = max(olens)
    if max_in != xs.shape[1]:
        xs = xs[:, :max_in]
    if max_out != ys.shape[1]:
        ys = ys[:, :max_out]
        labels = labels[:, :max_out]

    # calculate tacotron2 outputs
    hs, hlens = self.enc(xs, ilens)
    if self.spk_embed_dim is not None:
        spembs = F.normalize(spembs).unsqueeze(1).expand(-1, hs.size(1), -1)
        hs = torch.cat([hs, spembs], dim=-1)
    after_outs, before_outs, logits, att_ws = self.dec(hs, hlens, ys)

    # modifiy mod part of groundtruth
    if self.reduction_factor > 1:
        olens = olens.new([olen - olen % self.reduction_factor for olen in olens])
        max_out = max(olens)
        ys = ys[:, :max_out]
        labels = labels[:, :max_out]
        labels[:, -1] = 1.0  # make sure at least one frame has 1

    # caluculate taco2 loss
    l1_loss, mse_loss, bce_loss = self.taco2_loss(
        after_outs, before_outs, logits, ys, labels, olens
    )
    loss = l1_loss + mse_loss + bce_loss
    report_keys = [
        {"l1_loss": l1_loss.item()},
        {"mse_loss": mse_loss.item()},
        {"bce_loss": bce_loss.item()},
    ]

    # caluculate attention loss
    if self.use_guided_attn_loss:
        # NOTE(kan-bayashi): length of output for auto-regressive input will be changed when r > 1
        if self.reduction_factor > 1:
            olens_in = olens.new([olen // self.reduction_factor for olen in olens])
        else:
            olens_in = olens
        attn_loss = self.attn_loss(att_ws, ilens, olens_in)
        loss = loss + attn_loss
        report_keys += [
            {"attn_loss": attn_loss.item()},
        ]

    # caluculate cbhg loss
    if self.use_cbhg:
        # remove unnecessary padded part (for multi-gpus)
        if max_out != spcs.shape[1]:
            spcs = spcs[:, :max_out]

        # caluculate cbhg outputs & loss and report them
        cbhg_outs, _ = self.cbhg(after_outs, olens)
        cbhg_l1_loss, cbhg_mse_loss = self.cbhg_loss(cbhg_outs, spcs, olens)
        loss = loss + cbhg_l1_loss + cbhg_mse_loss
        report_keys += [
            {"cbhg_l1_loss": cbhg_l1_loss.item()},
            {"cbhg_mse_loss": cbhg_mse_loss.item()},
        ]

    report_keys += [{"loss": loss.item()}]
    self.reporter.report(report_keys)

    return loss