`self.log` raised error when number of dataloader is not consistent

Hi all,

Recently I’ve been modifying our codes to adopt the latest lightning logging approach that is self.log. We have two validation dataloaders, A and B. For A we use it every epoch. But for B, we use it every 5 epoch. We implement this mechanism in val_dataloader() and reload dataloader every epoch. See below for implementation.

def val_dataloader():
    if self.should_run_B():
        return [loader_A, loader_B]
        return [loader_A]

However, when we use self.log in validation_step(), an error shows up

You called self.log({name}, ...) twice in {fx} with different arguments. This is not allowed

(see here)

After debug in the source code, I notice that when we are at the epoch that only use one val dataloader, dataloader_idx is always None. On the other hand, when we have two val dataloaders, dataloader_idx would be sequentially presented as 0 or 1 in validation_step. If I understand correctly, this is the main reason causing the error.

The questions are how can I avoid this error. Is it a lightning implementation issue? Is there any better way to implement it? I’m open for any suggestions. Thanks in advance!

This might be a bug with how the dataloader index is determined (and added to the log call). Would you mind reporting this in our GitHub issues? It would be useful to also include your logging call, or even better, the LightningModule in a reproducible form. Thanks!

posted `self.log` raised error when number of dataloader is not consistent · Issue #16431 · Lightning-AI/lightning · GitHub

1 Like