Skip to content

feat: Add native implementation of PatchTST for long-term forecasting - #2352

Open
nareshmethuku wants to merge 9 commits into
sktime:mainfrom
nareshmethuku:feat/patchtst
Open

feat: Add native implementation of PatchTST for long-term forecasting#2352
nareshmethuku wants to merge 9 commits into
sktime:mainfrom
nareshmethuku:feat/patchtst

Conversation

@nareshmethuku

Copy link
Copy Markdown

This PR introduces the PatchTST (A Time Series is Worth 64 Words) architecture to pytorch-forecasting. PatchTST leverages Channel Independence and patching of time series to achieve state-of-the-art performance on long-term forecasting.

Changes

  • Implements PatchTST and core submodules (PatchEmbedding, FlattenHead).
  • Inherits from BaseModelWithCovariates with full support for static, time-varying continuous, and time-varying categorical covariates. Following the principles of Channel Independence, all covariates are mapped into independent channels.
  • Full support for probabilistic loss functions like QuantileLoss via multi-quantile forecasting in the flattening head.
  • Includes comprehensive integration and unit tests for the architecture checking diverse dataloader and multi-target setups.
  • Updated README.md to list PatchTST as a supported model.

Tests

Tested via pytest tests/test_models/test_patch_tst.py under various configurations:

  • test_integration (standard MAE training loop)
  • test_quantile_loss
  • test_multiple_targets
  • test_no_exogenous_variables

All tests pass locally with full coverage. Code is formatted via pre-commit.

- Implements PatchTST architecture with Channel Independence and patching
- Inherits from BaseModelWithCovariates with full support for covariates
- Integrates multi-quantile support in FlattenHead for probabilistic loss
- Adds full integration and unit tests for the architecture
- Updates README to include PatchTST

@phoeenniixx phoeenniixx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the PR and welcome to pytorch-forecasting!
Can you please also implement pkg class for PatchTST? (See DeepAR_pkg, or any other model pkg class for example) This will be used to test the model over a fixed set of test scenarios by the unified test framework.

See the pkg class doc here: https://pytorch-forecasting.readthedocs.io/en/stable/pkg.html

Also can you please move the sub_modules to layers module?

@phoeenniixx

phoeenniixx commented Jul 25, 2026

Copy link
Copy Markdown
Member

A suggestion:
We are working on a v2 rework of the library (see the docs for v2 API here)
v2 is expected to make the package more user friendly and reduce the coupling in classes
Would you be interested in implementing this model in v2 as well?

Also, it would be greatly appreciated if you could give us some feedback on v2 - try out the tutorials here.
You can also suggest some feats that you would like to have in v2 (as we are still in development phase of the API, it is quite flexible :) )

@codecov

codecov Bot commented Jul 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 69.31034% with 89 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@e7c7965). Learn more about missing BASE report.

Files with missing lines Patch % Lines
...orch_forecasting/models/patch_tst/_patch_tst_v2.py 22.72% 51 Missing ⚠️
pytorch_forecasting/models/patch_tst/patch_tst.py 83.89% 19 Missing ⚠️
..._forecasting/models/patch_tst/_patch_tst_pkg_v2.py 37.93% 18 Missing ⚠️
...rch_forecasting/models/patch_tst/_patch_tst_pkg.py 96.55% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2352   +/-   ##
=======================================
  Coverage        ?   86.95%           
=======================================
  Files           ?      182           
  Lines           ?    10450           
  Branches        ?        0           
=======================================
  Hits            ?     9087           
  Misses          ?     1363           
  Partials        ?        0           
Flag Coverage Δ
cpu 86.95% <69.31%> (?)
pytest 86.95% <69.31%> (?)

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

☔ View full report in Codecov by Harness.
📢 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.

@phoeenniixx phoeenniixx added enhancement New feature or request module:models ptf-v1 Related to `pytorch-forecasting` v1 labels Jul 26, 2026
… (V1 & V2)

This PR introduces the PatchTST (A Time Series is Worth 64 Words) architecture to pytorch-forecasting.
Changes
- Implements PatchTST inheriting from BaseModelWithCovariates with full support for covariates.
- Implements PatchTST_pkg integrating the model deeply into the unified test suite.
- Extracted and added PatchEmbedding and PatchTSTFlattenHead to the core pytorch_forecasting.layers module as requested.
- Provided a full implementation for the upcoming V2 architecture in _patch_tst_v2.py (inheriting from TslibBaseModel) and registered it with _patch_tst_pkg_v2.py.
@nareshmethuku

Copy link
Copy Markdown
Author

Hi @phoeenniixx, I've addressed all the requested changes, updated the branch, and all checks are passing now. Could you please take another look when you have a chance? Also looping in @fkiraly @benHeid @jdb78 for review if available. Thank you!

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

Labels

enhancement New feature or request module:models ptf-v1 Related to `pytorch-forecasting` v1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants