Skip to content

feat: implement PatchTST v2 architecture#2099

Open
amruth6002 wants to merge 3 commits intosktime:mainfrom
amruth6002:feat/patchtst-v2
Open

feat: implement PatchTST v2 architecture#2099
amruth6002 wants to merge 3 commits intosktime:mainfrom
amruth6002:feat/patchtst-v2

Conversation

@amruth6002
Copy link
Contributor

Reference Issues/PRs

Closes #2067

What does this implement/fix? Explain your changes.

This PR introduces the PatchTST architecture to the pytorch-forecasting v2 interface.

The implementation adapts the core "Channel Independence" patching mechanism and self-attention encoder from the original thuml implementation to align with the TslibBaseModel standards.

Key Additions:

  1. Layers: Added PatchEmbedding, SelfAttnEncoder, and SelfAttnEncoderLayer to support the architecture without cross-attention components.
  2. Core Model: Implemented PatchTST inside models/patchtst/_patchtst_v2.py. It correctly handles instance normalization, permuting/patching the sequences, and supports Quantile Loss resizing configurations.
  3. Package Wrapper: Created PatchTST_pkg_v2 for sktime compatibility and automated testing scaffolding.
  4. Tests: Added test_patchtst_v2.py verifying point forecasting, univariate flows, and quantile loss architectures via standard parameterized pytest conventions.

All pre-commit hooks and unit tests pass successfully.

@codecov
Copy link

codecov bot commented Feb 27, 2026

Codecov Report

❌ Patch coverage is 87.37374% with 25 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@602ee15). Learn more about missing BASE report.

Files with missing lines Patch % Lines
...ch_forecasting/models/patchtst/_patchtst_pkg_v2.py 51.11% 22 Missing ⚠️
...ytorch_forecasting/models/patchtst/_patchtst_v2.py 97.46% 2 Missing ⚠️
...ch_forecasting/layers/_normalization/_transpose.py 90.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2099   +/-   ##
=======================================
  Coverage        ?   86.64%           
=======================================
  Files           ?      172           
  Lines           ?     9927           
  Branches        ?        0           
=======================================
  Hits            ?     8601           
  Misses          ?     1326           
  Partials        ?        0           
Flag Coverage Δ
cpu 86.64% <87.37%> (?)
pytest 86.64% <87.37%> (?)

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.

from pytorch_forecasting.models.base._tslib_base_model_v2 import TslibBaseModel


class _Transpose(nn.Module):
Copy link
Contributor

Choose a reason for hiding this comment

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

Please move this to the _layers folder under the correct subdirectory, if it already exists, reuse.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done , please recheck.

return x.transpose(*self.dims)


class _FlattenHead(nn.Module):
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done , please recheck

@amruth6002 amruth6002 requested a review from PranavBhatP March 4, 2026 23:09
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.

[ENH] Add v2 interface support for PatchTST

2 participants