# yuqinie98/patchtst architecture

> Architecture diagram of the GitHub repository yuqinie98/patchtst, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/yuqinie98/patchtst
- Repository: https://github.com/yuqinie98/patchtst

## Overview

This repository is a research codebase for long-term time-series forecasting centered on PatchTST, with two largely separate execution paths: a supervised forecasting stack and a self-supervised pretrain/fine-tune stack. Both use the same core modeling idea—patching the input series into tokens and applying Transformer-style sequence modeling—but they organize training and data handling differently.

1. **Top-level split: supervised vs self-supervised**
   - `PatchTST_supervised/` contains the main benchmark training pipeline for forecasting.
   - `PatchTST_self_supervised/` contains masked pretraining plus downstream fine-tuning/linear probing.
   - The README explicitly treats them as separate workflows with separate entry scripts and script folders.

2. **Supervised training flow**
   - Main entry point is `PatchTST_supervised/run_longExp.py`.
   - Training logic is organized around `exp/exp_basic.py` and `exp/exp_main.py`, which define the experiment lifecycle: dataset loading, model construction, training, validation, testing, and result logging.
   - Shell scripts under `PatchTST_supervised/scripts/` parameterize runs for specific datasets and model variants.

3. **Supervised model zoo**
   - `PatchTST_supervised/models/` includes PatchTST plus baseline forecasting models: `Linear`, `NLinear`, `DLinear`, `Transformer`, `Informer`, `Autoformer`, and some statistical baselines.
   - The model implementations rely on shared building blocks in `layers/`, especially PatchTST-specific patch/backbone modules and generic transformer embedding/attention components.

4. **Supervised data ingestion**
   - `PatchTST_supervised/data_provider/` is the dataset boundary.
   - `data_loader.py` and `data_factory.py` turn CSV-based benchmark datasets into train/validation/test loaders.
   - `utils/timefeatures.py` and `utils/masking.py` support calendar feature extraction and sequence masking needed by the forecasting pipelines.

5. **PatchTST architectural core**
   - The PatchTST-specific implementation is split across `layers/PatchTST_backbone.py`, `layers/PatchTST_layers.py`, and `models/PatchTST.py`.
   - The key architectural idea is to convert each channel’s history into patches, embed those patches as tokens, and process them with shared Transformer weights across channels.
   - `layers/RevIN.py` adds reversible normalization, which is an important preprocessing/postprocessing boundary for time-series stability.

6. **Self-supervised training flow**
   - Entry points are `PatchTST_self_supervised/patchtst_pretrain.py`, `patchtst_finetune.py`, and `patchtst_supervised.py`.
   - The self-supervised path is organized more like a modern training stack, with `src/learner.py` coordinating the optimization loop and `src/callback/` providing modular training behaviors.
   - Pretraining saves a model artifact under `saved_model/` for reuse in downstream tasks.

7. **Self-supervised data and task abstraction**
   - `src/data/datamodule.py` and `src/data/pred_dataset.py` define the data module and prediction datasets.
   - `datautils.py` acts as a dataset preparation utility layer.
   - This path is built around masked patch reconstruction, so `src/callback/patch_mask.py` is a central training-time transformation.

8. **Self-supervised model stack**
   - `src/models/patchTST.py` contains the self-supervised PatchTST model definition.
   - Shared layers live under `src/models/layers/`, including attention, positional encoding, heads, and RevIN.
   - The model consumes patchified sequences and produces either reconstruction or forecasting outputs depending on the training phase.

9. **Training infrastructure and callbacks**
   - `src/callback/` contains reusable training infrastructure: scheduling, tracking/logging, distributed support, patch masking, and transforms.
   - This suggests the self-supervised code is more framework-like than the supervised code, with training behavior separated from the model itself.
   - `src/basics.py`, `src/utils.py`, and `src/metrics.py` provide common helpers and evaluation support.

10. **Baseline model families under Formers**
   - `PatchTST_supervised/Formers/` and its nested `FEDformer/` and `Pyraformer/` directories are preserved baseline implementations from related forecasting papers.
   - These subtrees have their own `run.py`, `exp/`, `data_provider/`, `layers/`, and shell scripts, meaning they are effectively self-contained benchmarking mini-projects inside the repo.
   - Their presence matters architecturally because the repo is not just PatchTST; it is also a benchmarking harness against prior Transformer-based forecasters.

11. **Execution and configuration boundary**
   - Most experimentation is driven by shell scripts in `scripts/` and `Formers/*/*.sh`.
   - Datasets are expected externally in a local `dataset/` directory and are not bundled with the repository.
   - Outputs are written to model/result artifacts such as `result.txt` and `saved_model/`, making the repo oriented around offline experiment runs rather than a serving system.

12. **Common design patterns across the repo**
   - Shared concerns recur across both stacks: time-feature extraction, masking, metrics, normalization, and Transformer-style layer blocks.
   - The major architectural boundary is not frontend/backend, but rather “data ingestion → patch/token construction → sequence model → forecast/reconstruction → metrics/logging.”
   - The most important diagram elements are the two training pipelines, the shared PatchTST backbone, the dataset loaders, and the baseline model zoo.

Main entry points: [`PatchTST_supervised/exp/exp_main.py`](https://github.com/yuqinie98/patchtst/blob/main/PatchTST_supervised/exp/exp_main.py) (Experiment loop), [`PatchTST_supervised/models`](https://github.com/yuqinie98/patchtst/tree/main/PatchTST_supervised/models) (Model zoo), [`PatchTST_self_supervised/src/learner.py`](https://github.com/yuqinie98/patchtst/blob/main/PatchTST_self_supervised/src/learner.py) (Learner), [`PatchTST_self_supervised/src/models/patchTST.py`](https://github.com/yuqinie98/patchtst/blob/main/PatchTST_self_supervised/src/models/patchTST.py) (PatchTST model)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_supervised["Supervised stack"]
  node_sup_entry["Run exp<br/>entrypoint<br/>[run_longExp.py]"]
  node_sup_exp["Experiment loop<br/>training orchestration<br/>[exp_main.py]"]
  node_sup_data[("Data provider<br/>dataset boundary<br/>[data_factory.py]")]
  node_sup_models["Model zoo<br/>forecast models"]
  node_sup_patch{{"PatchTST core<br/>patch transformer<br/>[PatchTST.py]"}}
  node_sup_layers["Shared layers<br/>nn building blocks"]
  node_sup_utils["Utils<br/>support code"]
end

subgraph group_selfsup["Self-supervised stack"]
  node_ss_entry["Pretrain / finetune<br/>entrypoints"]
  node_ss_learner["Learner<br/>training loop<br/>[learner.py]"]
  node_ss_callbacks["Callbacks<br/>training framework"]
  node_ss_data[("Data module<br/>dataset module<br/>[datamodule.py]")]
  node_ss_prep["Dataset prep<br/>prep utilities<br/>[datautils.py]"]
  node_ss_model{{"PatchTST model<br/>self-supervised model<br/>[patchTST.py]"}}
  node_ss_layers["Model layers<br/>nn building blocks"]
end

subgraph group_formers["Baseline formers"]
  node_formers_fed["FEDformer<br/>baseline project"]
  node_formers_pyr["Pyraformer<br/>baseline project"]
  node_formers_scripts["Former scripts<br/>run scripts<br/>[Autoformer.sh]"]
end

node_sup_entry -->|"launches"| node_sup_exp
node_sup_exp -->|"loads"| node_sup_data
node_sup_exp -->|"builds"| node_sup_models
node_sup_models -->|"includes"| node_sup_patch
node_sup_patch -->|"uses"| node_sup_layers
node_sup_exp -->|"reports"| node_sup_utils
node_sup_data -->|"formats"| node_sup_utils
node_ss_entry -->|"orchestrates"| node_ss_learner
node_ss_learner -->|"composes"| node_ss_callbacks
node_ss_entry -->|"binds"| node_ss_data
node_ss_data -->|"prepares"| node_ss_prep
node_ss_learner -->|"trains"| node_ss_model
node_ss_model -->|"uses"| node_ss_layers
node_ss_callbacks -.->|"masks"| node_ss_model
node_sup_models -.->|"contains"| node_formers_fed
node_sup_models -.->|"contains"| node_formers_pyr
node_formers_scripts -.->|"runs"| node_formers_fed
node_formers_scripts -.->|"runs"| node_formers_pyr

click node_sup_entry "https://github.com/yuqinie98/patchtst/blob/main/PatchTST_supervised/run_longExp.py"
click node_sup_exp "https://github.com/yuqinie98/patchtst/blob/main/PatchTST_supervised/exp/exp_main.py"
click node_sup_data "https://github.com/yuqinie98/patchtst/blob/main/PatchTST_supervised/data_provider/data_factory.py"
click node_sup_models "https://github.com/yuqinie98/patchtst/tree/main/PatchTST_supervised/models"
click node_sup_patch "https://github.com/yuqinie98/patchtst/blob/main/PatchTST_supervised/models/PatchTST.py"
click node_sup_layers "https://github.com/yuqinie98/patchtst/tree/main/PatchTST_supervised/layers"
click node_sup_utils "https://github.com/yuqinie98/patchtst/tree/main/PatchTST_supervised/utils"
click node_ss_entry "https://github.com/yuqinie98/patchtst/blob/main/PatchTST_self_supervised/patchtst_pretrain.py"
click node_ss_learner "https://github.com/yuqinie98/patchtst/blob/main/PatchTST_self_supervised/src/learner.py"
click node_ss_callbacks "https://github.com/yuqinie98/patchtst/tree/main/PatchTST_self_supervised/src/callback"
click node_ss_data "https://github.com/yuqinie98/patchtst/blob/main/PatchTST_self_supervised/src/data/datamodule.py"
click node_ss_prep "https://github.com/yuqinie98/patchtst/blob/main/PatchTST_self_supervised/datautils.py"
click node_ss_model "https://github.com/yuqinie98/patchtst/blob/main/PatchTST_self_supervised/src/models/patchTST.py"
click node_ss_layers "https://github.com/yuqinie98/patchtst/tree/main/PatchTST_self_supervised/src/models/layers"
click node_formers_fed "https://github.com/yuqinie98/patchtst/tree/main/PatchTST_supervised/Formers/FEDformer"
click node_formers_pyr "https://github.com/yuqinie98/patchtst/tree/main/PatchTST_supervised/Formers/Pyraformer"
click node_formers_scripts "https://github.com/yuqinie98/patchtst/blob/main/PatchTST_supervised/Formers/Autoformer.sh"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_sup_entry,node_sup_exp,node_sup_data,node_sup_models,node_sup_patch,node_sup_layers,node_sup_utils toneBlue
class node_ss_entry,node_ss_learner,node_ss_callbacks,node_ss_data,node_ss_prep,node_ss_model,node_ss_layers toneAmber
class node_formers_fed,node_formers_pyr,node_formers_scripts toneMint
```

## Components

### Supervised stack

Benchmark forecasting pipeline with multiple model families and shared experiment utilities.

- [Run exp](https://github.com/yuqinie98/patchtst/blob/main/PatchTST_supervised/run_longExp.py): entrypoint · Main supervised forecasting entry script. · `PatchTST_supervised/run_longExp.py`
- [Experiment loop](https://github.com/yuqinie98/patchtst/blob/main/PatchTST_supervised/exp/exp_main.py): training orchestration · Loads data, builds models, runs train/val/test, and records results. · `PatchTST_supervised/exp/exp_main.py`
- [Data provider](https://github.com/yuqinie98/patchtst/blob/main/PatchTST_supervised/data_provider/data_factory.py): dataset boundary · CSV benchmark loading and split construction for forecasting experiments. · `PatchTST_supervised/data_provider/data_factory.py`
- [Model zoo](https://github.com/yuqinie98/patchtst/tree/main/PatchTST_supervised/models): forecast models · PatchTST plus linear, transformer, informer, autoformer, and statistical baselines. · `PatchTST_supervised/models`
- [PatchTST core](https://github.com/yuqinie98/patchtst/blob/main/PatchTST_supervised/models/PatchTST.py): patch transformer · Patchify-embed-transformer backbone for supervised forecasting. · `PatchTST_supervised/models/PatchTST.py`
- [Shared layers](https://github.com/yuqinie98/patchtst/tree/main/PatchTST_supervised/layers): nn building blocks · Patch backbone, embeddings, attention, encoder-decoder modules, and RevIN. · `PatchTST_supervised/layers`
- [Utils](https://github.com/yuqinie98/patchtst/tree/main/PatchTST_supervised/utils): support code · Time features, masking, metrics, and experiment helpers used across the supervised stack. · `PatchTST_supervised/utils`

### Self-supervised stack

Masked pretrain and downstream fine-tuning workflow built around a learner/callback training framework.

- [Pretrain / finetune](https://github.com/yuqinie98/patchtst/blob/main/PatchTST_self_supervised/patchtst_pretrain.py): entrypoints · Self-supervised pretraining, finetuning, and supervised downstream scripts. · `PatchTST_self_supervised/patchtst_pretrain.py`
- [Learner](https://github.com/yuqinie98/patchtst/blob/main/PatchTST_self_supervised/src/learner.py): training loop · Coordinates optimization and phase-specific behavior for self-supervised runs. · `PatchTST_self_supervised/src/learner.py`
- [Callbacks](https://github.com/yuqinie98/patchtst/tree/main/PatchTST_self_supervised/src/callback): training framework · Scheduling, tracking, distributed support, masking, and transforms. · `PatchTST_self_supervised/src/callback`
- [Data module](https://github.com/yuqinie98/patchtst/blob/main/PatchTST_self_supervised/src/data/datamodule.py): dataset module · Data module and prediction dataset abstractions for self-supervised workflows. · `PatchTST_self_supervised/src/data/datamodule.py`
- [Dataset prep](https://github.com/yuqinie98/patchtst/blob/main/PatchTST_self_supervised/datautils.py): prep utilities · Dataset preparation helpers and shared time-feature logic. · `PatchTST_self_supervised/datautils.py`
- [PatchTST model](https://github.com/yuqinie98/patchtst/blob/main/PatchTST_self_supervised/src/models/patchTST.py): self-supervised model · Patchified sequence model used for masked reconstruction and forecasting. · `PatchTST_self_supervised/src/models/patchTST.py`
- [Model layers](https://github.com/yuqinie98/patchtst/tree/main/PatchTST_self_supervised/src/models/layers): nn building blocks · Attention, positional encoding, heads, and RevIN for the self-supervised stack. · `PatchTST_self_supervised/src/models/layers`

### Baseline formers

Self-contained benchmark subprojects preserved under the supervised tree.

- [FEDformer](https://github.com/yuqinie98/patchtst/tree/main/PatchTST_supervised/Formers/FEDformer): baseline project · Standalone FEDformer/Informer/Autoformer benchmark subtree with its own data, exp, layers, and scripts. · `PatchTST_supervised/Formers/FEDformer`
- [Pyraformer](https://github.com/yuqinie98/patchtst/tree/main/PatchTST_supervised/Formers/Pyraformer): baseline project · Standalone Pyraformer benchmark subtree with preprocessing, model code, and scripts. · `PatchTST_supervised/Formers/Pyraformer`
- [Former scripts](https://github.com/yuqinie98/patchtst/blob/main/PatchTST_supervised/Formers/Autoformer.sh): run scripts · Shell entry points for baseline benchmarking under Formers. · `PatchTST_supervised/Formers/Autoformer.sh`

## Connections

- Run exp → Experiment loop: launches · Main script creates and runs experiment objects.
- Experiment loop → Data provider: loads · Experiment lifecycle constructs train/val/test data loaders.
- Experiment loop → Model zoo: builds · Experiment selects a forecasting model implementation.
- Model zoo → PatchTST core: includes · PatchTST is the central model family in the supervised zoo.
- PatchTST core → Shared layers: uses · PatchTST depends on shared patching, attention, and normalization layers.
- Experiment loop → Utils: reports · Training and evaluation use shared metrics and helper utilities.
- Data provider → Utils: formats · Dataset handling relies on time features and masking helpers.
- Pretrain / finetune → Learner: orchestrates · Entry scripts instantiate the learner for pretrain and finetune phases.
- Learner → Callbacks: composes · Learner delegates training behavior to modular callbacks.
- Pretrain / finetune → Data module: binds · Training scripts wire in the data module and prediction datasets.
- Data module → Dataset prep: prepares · Data module relies on dataset preparation utilities.
- Learner → PatchTST model: trains · Learner optimizes the PatchTST self-supervised model.
- PatchTST model → Model layers: uses · Model is built from reusable attention, heads, encoding, and RevIN layers.
- Callbacks → PatchTST model: masks · Patch masking is a central training-time transformation for pretraining.
- Model zoo → FEDformer: contains · FEDformer-family baselines are preserved alongside the main supervised stack.
- Model zoo → Pyraformer: contains · Pyraformer is included as a separate baseline subtree.
- Former scripts → FEDformer: runs · Shell scripts launch baseline benchmark experiments.
- Former scripts → Pyraformer: runs · Shell scripts also cover Pyraformer benchmark runs.
