Skip to content

Unconsistent behaviour of testing when returning dict from test_step() #6614

@ashleve

Description

@ashleve

🐛 Bug

When returning dict of values from test_step() in LightningModule:

  def test_step(self, batch, batch_idx):
      output = self.layer(batch)
      loss = self.loss(batch, output)
      self.log('fake_test_acc', loss)
      return {"y": loss, "param": None}

The logged values and returned values are both displayed in terminal when testing:
image

If we declare test_epoch_end() method in LightningModule, then the extra values are ignored:

def test_epoch_end(self, outputs) -> None:
    pass

image

Is this intentional behaviour??
This gets especially confusing when returning a non scalar tensor from test_step():

def test_step(self, batch, batch_idx):
    output = self.layer(batch)
    loss = self.loss(batch, output)
    self.log('fake_test_acc', loss)
    return {"output": output}

Without declaring test_epoch_end method I get the following error:

ValueError: only one element tensors can be converted to Python scalars

But again, this exception error doesn't happen if empy test_epoch_end() method is declared in my LightningModule...

To Reproduce

Repo with boring model:
https://github.com/hobogalaxy/lit-bug2

Expected behavior

I believe the returned values should be just ignored in terminal output and shouln't have the potential to crash the run right?

Environment

  • CUDA:
    • GPU:
      • GeForce RTX 2060
    • available: True
    • version: 11.1
  • Packages:
    • numpy: 1.19.2
    • pyTorch_debug: False
    • pyTorch_version: 1.8.0
    • pytorch-lightning: 1.2.4
    • tqdm: 4.59.0
  • System:

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedOpen to be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions