Skip to content

Missing cleanup after trainer.fit() and trainer.test() #4385

@ananthsub

Description

@ananthsub

🐛 Bug

The Lightning trainer holds references to the LightningModule/DataModule after fit/test complete. This can leads to different behavior in calls likeL

Please reproduce using the BoringModel and post here

def test_x(tmpdir):
    # validation checks do not regularly since we don'tre-instantiate the trainer inside each loop
    for i in range(2):
      trainer = pl.Trainer(max_epochs=4, check_val_every_n_epoch=2, logger=False, checkpoint_callback=False)
      test_module = BoringModel()
      trainer.fit(test_module)


reuse_trainer = pl.Trainer(max_epochs=4, check_val_every_n_epoch=2, logger=False, checkpoint_callback=False)
def test_reuse(tmpdir):
    # validation checks do not run on the second loop since we don't re-instantiate the trainer inside each loop
    for i in range(2):
      test_module = BoringModel()
      reuse_trainer.fit(test_module)

To Reproduce

Expected behavior

The latter test_reuse should re-bind the model + hooks when calling fit again inside each loop

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingduplicateThis issue or pull request already exists

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions