uni2ts: Unified Training of Universal Time Series Forecasting Transformers
repository·main·Indexed 23 days ago
https://github.com/salesforceairesearch/uni2tsA PyTorch-based library for large-scale pre-training, fine-tuning, inference, and evaluation of Universal Time Series Transformers, including the Moirai models. It supports benchmarking for Chronos, TimesFM, and VisionTS, and provides tools for reproducing Moirai-1.0-R pre-training and evaluation across Monash TSF, Probabilistic Forecasting (PF), and Long Sequence Forecasting (LSF) datasets. The repository also includes the Moirai Agent, a framework for context-aware forecasting and expert selection using LLMs.
What's inside uni2ts
- Moirai-MoE is a Sparse Mixture-of-Experts (MoE) time series foundation model. Unlike the original Moirai which uses multi-heuristic-defined projection layers for different frequencies, Moirai-MoE uses a single input/output projection layer and delegates pattern capture to sparse MoE Transformers. This allows for data-driven specialization at the token level.
Overview of Moirai Agent
mainMoirai Agent is an intelligent forecasting framework designed to blend historical time series data with external contextual signals. Unlike traditional numerical-only models, it uses Large Language Models (LLMs) as orchestrators to integrate heterogeneous information, select forecasting experts, and adjust predictions based on real-world context.
It provides two core capabilities:
- Context-aware forecasting: Combines numerical history with external contextual inputs to refine lookback windows, detect anomalies, and anticipate future effects.
- Expert selection: Uses an LLM-based selector to choose the most suitable forecasting model for a specific task from a pool of state-of-the-art forecasters.
Reproduce Moirai-1.0-R Pre-training
mainTo reproduce the pre-training runs, execute the
cli.trainmodule from the baseuni2tsfolder. You may need to adjust gradient accumulation settings depending on your hardware.Note that the
dataparameter should be set tolotsa_v1_weightedto use the Large-scale Open Time Series Archive (LOTSA) dataset.# Example: Pre-training the Small model python -m cli.train \ -cp conf/pretrain \ run_name=moirai_small \ model=moirai_small \ data=lotsa_v1_weighted \ trainer.max_epochs=1_000 \ train_dataloader.batch_size=256Set up the GIFT-CTX replication environment
mainTo replicate the Moirai Agent results on the GIFT-CTX benchmark, install the required dependencies and configure your OpenAI API key.
- Install dependencies:
pip install -r requirement.txt - Export your OpenAI API key:
export OPENAI_API_KEY="..."
pip install -r requirement.txt export OPENAI_API_KEY="..."- Install dependencies:
Evaluate fine-tuned Moirai models
mainThe evaluation process for fine-tuned models is identical to the original zero-shot evaluation setup.
To evaluate a specific fine-tuned model, you must update the corresponding evaluation shell scripts by adding the relative checkpoint paths. Checkpoint paths should start with
.outputs/....Reproduce the VN1 Forecasting Challenge results
mainTo reproduce the first-place results achieved by the fine-tuned Moirai-base model in the VN1 Forecasting challenge, follow these steps:
- Environment Setup: Follow the standard
uni2tslibrary instructions to create a virtual environment and install all necessary dependencies. - Download Raw Data: Use the provided
Makefileto download the required raw dataset:make download_data - Preprocess Data: Run
prepare_data.pyafter updating the script with the correct directory path of your downloaded raw dataset. - Configure Environment: Set the
CUSTOM_DATA_PATHenvironment variable in your.envfile to point to your processed dataset directory:echo "CUSTOM_DATA_PATH=PATH_TO_SAVE" >> .env - Fine-tune Moirai-base:
- Open the configuration file and replace
pretrained_model_name_or_pathwith your local path to the pretrained model. - Execute the training command:
python -m cli.train -cp ../project/vn1_competition/fine_tune run_name=run1 - Open the configuration file and replace
- Run Inference: Open
src/main.py, update the weight file path to point to your fine-tuned model weights, and execute the script.
make download_data echo "CUSTOM_DATA_PATH=PATH_TO_SAVE" >> .env python -m cli.train -cp ../project/vn1_competition/fine_tune run_name=run1- Environment Setup: Follow the standard
Create LSF datasets for fine-tuning
mainTo fine-tune Moirai-1.0-R, you must create offline datasets using sliding windows of fixed-length time series samples.
Key Configuration Details:
- Dataset Type: Set
dataset_typeto"wide_multivariate"for multivariate setups or"wide"for univariate setups. - Sliding Window Distance: The number of training samples is determined by the sliding window distance. The default is
distance=1. For large datasets, increasing this value reduces computational cost per epoch. - Normalization: The LSF setup requires normalizing data using training statistics.
- Split Configuration: Dataset splits follow the logic in
src/uni2ts/eval_util/_lsf_dataset.py. Configuration files for these splits are located undercli/conf/finetune/dataandcli/conf/finetune/val_data.
Implementation Files:
- Dataset creation logic is found in
src/uni2ts/data/builder/simple.py. - Creation scripts can be found in
project/moirai-1/finetune_lsf/build_lsf_ft_datasets.sh.
- Dataset Type: Set
Install Gift-Eval replication dependencies
mainInstall the required packages using pip. This process requires Python >= 3.12.
pip install -r requirements.txtEvaluate using popular LSF datasets
mainYou can evaluate models on standard Long Sequence Forecasting (LSF) benchmarks by using predefined Hydra configurations.
- Download the required datasets via the TSLib repository.
- Set the
LSF_PATHenvironment variable in your.envfile to point to the dataset directory. - Run
cli.evalusing thelsf_testdata configuration and specify thedataset_nameandprediction_length.
# 1. Set LSF path echo "LSF_PATH=PATH_TO_TSLIB/dataset" >> .env # 2. Run evaluation python -m cli.eval \ run_name=example_eval_2 \ model=moirai_1.0_R_small \ model.patch_size=32 \ model.context_length=1000 \ data=lsf_test \ data.dataset_name=ETTh1 \ data.prediction_length=96Explore Moirai Agent workflows
mainThe Moirai Agent repository is organized into two primary functional areas for research and replication:
ctx_forecast: Contains detailed scripts for implementing and running context-aware forecasting workflows.gift_eval: Contains scripts focused on replicatinggift_evalresults using standard numeric forecasting through model selection.
For benchmarking information, standard forecasting results are evaluated on the GIFT-Eval leaderboard, while contextual forecasting results are evaluated on the GIFT-CTX Dataset.
Configure the Gift-Eval dataset path
mainBefore running the evaluation, you must download the GiftEval dataset from Hugging Face to a local directory.
Set the
GIFT_EVALenvironment variable in a.envfile pointing to the absolute path of your local dataset directory.GIFT_EVAL=/absolute/path/to/gift_evalDownload Moirai-1.0-R Pre-trained Models
mainPre-trained weights for the Moirai-1.0-R series are available on the Hugging Face Hub in three sizes. You can download them based on your parameter requirements:
- Moirai-1.0-R-Small: 14m parameters
- Moirai-1.0-R-Base: 91m parameters
- Moirai-1.0-R-Large: 311m parameters