Skip to content

fix: return predictions on multiple predict calls#2129

Open
pankajbaid567 wants to merge 3 commits intosktime:mainfrom
pankajbaid567:fix/issue-2108-predict-twice-empty
Open

fix: return predictions on multiple predict calls#2129
pankajbaid567 wants to merge 3 commits intosktime:mainfrom
pankajbaid567:fix/issue-2108-predict-twice-empty

Conversation

@pankajbaid567
Copy link

Reference Issues/PRs

Fixes #2108.

What does this implement/fix? Explain your changes.

When calling .predict() twice on a TemporalFusionTransformer model, the second call returns an empty prediction. This happens because PyTorch Lightning clones or duplicates callbacks internally within the Trainer instantiated in the .predict() method if a trainer is already present or attached. Consequently, the local predict_callback instance's .result is never populated, while the actual duplicated callback instance processes and holds the results in Trainer.

This PR fixes the bug by retrieving the active PredictCallback directly from trainer.callbacks dynamically (scanning in reverse to get the latest updated instance contextually injected) and falling back to the local predict_callback object if not found. This ensures the output is populated correctly on repeated .predict() calls.

What should a reviewer concentrate their feedback on?

  • The dynamic lookup of PredictCallback inside _base_model.predict, iterating backward over trainer.callbacks.
  • The added regression test test_prediction_with_dataloader_twice to ensure it functionally validates against this behaviour.

Did you add any tests for the change?

Yes, added a regression test: test_prediction_with_dataloader_twice within tests/test_models/test_temporal_fusion_transformer.py. It explicitly calls .predict() twice and ensures the prediction index length is non-empty (> 0) on both runs.

Any other comments?

N/A

PR checklist

  • The PR title starts with either [ENH], [MNT], [DOC], or [BUG]. [BUG] - bugfix, [MNT] - CI, test framework, [ENH] - adding or improving code, [DOC] - writing or improving documentation or docstrings.
  • Added/modified tests
  • Used pre-commit hooks when committing to ensure that code is compliant with hooks. Install hooks with pre-commit install.
    To run hooks independent of commit, execute pre-commit run --all-files

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@codecov
Copy link

codecov bot commented Mar 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@fee1b29). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2129   +/-   ##
=======================================
  Coverage        ?   86.63%           
=======================================
  Files           ?      165           
  Lines           ?     9733           
  Branches        ?        0           
=======================================
  Hits            ?     8432           
  Misses          ?     1301           
  Partials        ?        0           
Flag Coverage Δ
cpu 86.63% <100.00%> (?)
pytest 86.63% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Calling BaseModel.predict a second time after trainer attaches to the model causes pred returned to be empty

1 participant