### Loading Configuration via CLI Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Example of how to load a model configuration from a JSON file using the `run.py` script. This allows for easy management and overriding of model hyperparameters. ```bash python run.py -e "my_experiment" -d datasets -r results \ --args_from_dict_path configs/lag_llama.json \ --single_dataset "electricity_hourly" --lr 1e-4 ``` -------------------------------- ### Create Train and Validation Datasets with Dates Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Loads a named dataset and splits it into training and validation ListDataset objects. The split can be controlled by the number of validation windows or an explicit validation start date. Ensure the dataset path and name are correctly specified. ```python from data.data_utils import create_train_and_val_datasets_with_dates # Prepare training and validation splits for the electricity_hourly dataset train_data, val_data, total_train_pts, total_val_pts, total_val_windows, max_end_date, total_pts = \ create_train_and_val_datasets_with_dates( name="electricity_hourly", dataset_path="datasets", data_id=0, history_length=32 + 700, # context_length + max(lags_seq) prediction_length=24, num_val_windows=14, # Use last 14 windows as validation; omit to use val_start_date instead last_k_percentage=None, # Use 100% of training history; set e.g. 20 to replicate paper experiments ) print(f"Train series: {len(train_data)}") print(f"Total train points: {total_train_pts}") print(f"Total val points: {total_val_pts}") print(f"Max train end date: {max_end_date}") # Train series: 370 # Total train points: 1,997,360 # Total val points: ... # Max train end date: 2014-12-27 00:00:00 ``` -------------------------------- ### Pretraining with run.py CLI Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Command-line interface for pretraining Lag-Llama on multiple datasets. Specify experiment name, data directory, results directory, seed, batch size, model dimensions, configuration path, datasets for training and testing, sampling strategy, learning rate, early stopping patience, number of parallel samples, Weights & Biases entity and project, and GPU. ```bash python run.py \ -e "pretraining_lag_llama" \ -d "datasets" \ -r "experiments/results" \ --seed 42 \ --batch_size 512 \ -m 1000 \ -n 128 \ --args_from_dict_path configs/lag_llama.json \ --all_datasets "electricity_hourly" "traffic" "weather" "ett_h1" "ett_h2" "ett_m1" "ett_m2" \ --test_datasets "weather" "exchange_rate" \ --stratified_sampling "series" \ --lr 0.0001 \ --early_stopping_patience 50 \ --num_parallel_samples 100 \ --wandb_entity "my-entity" \ --wandb_project "lag-llama" \ --gpu 0 ``` -------------------------------- ### Fine-tuning with run.py CLI Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Command-line interface for fine-tuning a pretrained Lag-Llama model on a single dataset. Key parameters include experiment name, data directory, results directory, seed, batch size, model dimensions, configuration path, specific dataset, checkpoint path, last k percentage for history, validation windows, learning rate, early stopping patience, Weights & Biases entity and project, and GPU. ```bash python run.py \ -e "pretraining_lag_llama_finetune_on_weather" \ -d "datasets" \ -r "experiments/results" \ --seed 42 \ --batch_size 512 \ -m 1000 \ -n 128 \ --args_from_dict_path configs/lag_llama.json \ --single_dataset "weather" \ --use_dataset_prediction_length \ --get_ckpt_path_from_experiment_name "pretraining_lag_llama" \ --single_dataset_last_k_percentage 100 \ --num_validation_windows 1 \ --lr 0.00001 \ --early_stopping_patience 50 \ --wandb_entity "my-entity" \ --wandb_project "lag-llama" \ --gpu 0 ``` -------------------------------- ### LagLlamaEstimator: Zero-Shot Forecasting with Pretrained Weights Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Demonstrates how to use `LagLlamaEstimator` for zero-shot forecasting by loading a pretrained checkpoint. This approach requires no training and is suitable for immediate predictions on new time series data. Ensure the 'electricity_hourly' dataset is available or adjust the dataset loading path. ```python import torch from huggingface_hub import hf_hub_download from lag_llama.gluon.estimator import LagLlamaEstimator from gluonts.evaluation import make_evaluation_predictions, Evaluator from gluonts.dataset.repository.datasets import get_dataset from pathlib import Path # Download pretrained checkpoint ckpt_path = hf_hub_download( repo_id="time-series-foundation-models/Lag-Llama", filename="lag-llama.ckpt" ) # Build estimator from pretrained checkpoint (zero-shot: no training needed) estimator = LagLlamaEstimator( ckpt_path=ckpt_path, prediction_length=24, # Forecast horizon context_length=32, # History timesteps fed to model (start from 32; tune upward) input_size=1, n_layer=8, n_head=9, n_embd_per_head=16, batch_size=64, num_parallel_samples=100, # Samples drawn per forecast (for probabilistic output) rope_scaling=None, # Use {"type": "linear", "factor": 2.0} for longer contexts scaling="robust", # "mean" | "std" | "robust" | None lags_seq=["Q", "M", "W", "D", "H", "T", "S"], # Lag frequencies time_feat=True, device=torch.device("cuda" if torch.cuda.is_available() else "cpu"), ) # Load test dataset (GluonTS built-in) dataset = get_dataset("electricity_hourly", path=Path("datasets")) # Create predictor (no training; uses loaded checkpoint) predictor = estimator.create_predictor( transformation=estimator.create_transformation(), module=estimator.create_lightning_module(use_kv_cache=True), ) # Run zero-shot probabilistic forecasting forecast_it, ts_it = make_evaluation_predictions( dataset=dataset.test, predictor=predictor, num_samples=100, ) forecasts = list(forecast_it) tss = list(ts_it) # Evaluate with CRPS (mean_wQuantileLoss) and MSE evaluator = Evaluator(num_workers=0) agg_metrics, _ = evaluator(iter(tss), iter(forecasts), num_series=len(dataset.test)) print(f"CRPS: {agg_metrics['mean_wQuantileLoss']:.4f}") print(f"MSE: {agg_metrics['MSE']:.4f}") # Expected output (electricity_hourly, zero-shot): # CRPS: 0.0521 # MSE: ... ``` -------------------------------- ### LagLlamaEstimator.create_lightning_module Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Instantiates a `LagLlamaLightningModule`, either from scratch or from a saved checkpoint. This method can be used to obtain the raw lightning module for inspection or custom training loops, and it supports enabling KV-caching for efficient autoregressive decoding. ```APIDOC ## `LagLlamaEstimator.create_lightning_module` — Build / Load Lightning Module `create_lightning_module` instantiates a `LagLlamaLightningModule` either from scratch or from a saved checkpoint (when `ckpt_path` is set on the estimator). It accepts `use_kv_cache` to enable caching at inference time. This method is called internally by `create_predictor` and `train_model`, but can be called directly when you need the raw lightning module for inspection or custom training loops. ```python from lag_llama.gluon.estimator import LagLlamaEstimator import torch # Load from a pretrained checkpoint (e.g., downloaded from HuggingFace) estimator = LagLlamaEstimator( prediction_length=12, context_length=64, n_layer=8, n_head=9, n_embd_per_head=16, ckpt_path="lag-llama.ckpt", scaling="robust", time_feat=True, device=torch.device("cpu"), ) # Load weights; use_kv_cache=True enables fast autoregressive decoding module = estimator.create_lightning_module(use_kv_cache=True) print(type(module)) # # Count parameters n_params = sum(p.numel() for p in module.parameters()) print(f"Model parameters: {n_params:,}") # Expected for default config (8 layers, 9 heads, 16 embd/head): ~9,600,000 # Inspect hyperparameters saved in the checkpoint print(module.hparams.keys()) # odict_keys(['model_kwargs', 'context_length', 'prediction_length', 'loss', 'lr', ...]) ``` ``` -------------------------------- ### LagLlamaEstimator.create_lightning_module for Model Instantiation Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Instantiates a LagLlamaLightningModule from scratch or a checkpoint. Set `use_kv_cache=True` to enable caching for fast autoregressive decoding. This method is useful for custom training loops or inspection. ```python from lag_llama.gluon.estimator import LagLlamaEstimator import torch # Load from a pretrained checkpoint (e.g., downloaded from HuggingFace) estimator = LagLlamaEstimator( prediction_length=12, context_length=64, n_layer=8, n_head=9, n_embd_per_head=16, ckpt_path="lag-llama.ckpt", scaling="robust", time_feat=True, device=torch.device("cpu"), ) # Load weights; use_kv_cache=True enables fast autoregressive decoding module = estimator.create_lightning_module(use_kv_cache=True) print(type(module)) # # Count parameters n_params = sum(p.numel() for p in module.parameters()) print(f"Model parameters: {n_params:,}") # Expected for default config (8 layers, 9 heads, 16 embd/head): ~9,600,000 # Inspect hyperparameters saved in the checkpoint print(module.hparams.keys()) # odict_keys(['model_kwargs', 'context_length', 'prediction_length', 'loss', 'lr', ...]) ``` -------------------------------- ### Prepare Custom Dataset for Evaluation Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Creates a minimal custom dataset with daily frequency and a single series for evaluation purposes. It then generates forecast predictions and prints their shapes and median values. ```python custom_data = ListDataset( [{"start": pd.Period("2020-01-01", freq="D"), "target": np.sin(np.linspace(0, 4 * np.pi, 200)).astype(np.float32)}], freq="D", ) forecast_it, ts_it = make_evaluation_predictions( dataset=custom_data, predictor=predictor, num_samples=100 ) forecasts = list(forecast_it) # forecasts[0].samples has shape (num_samples, prediction_length) = (100, 7) print("Forecast samples shape:", forecasts[0].samples.shape) # (100, 7) # Get the median (50th percentile) forecast print("Median forecast:", forecasts[0].quantile(0.5)) ``` -------------------------------- ### Data Augmentation with Lag-Llama Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Demonstrates the application of frequency-domain and time-domain data augmentations within the Lag-Llama framework. Imports necessary augmentation classes and functions, applies them to sample data, and prints the resulting tensor shapes. ```python import torch from data.augmentations.augmentations import ApplyAugmentations, Jitter, MagnitudeWarp, TimeWarp from data.augmentations.freq_mask import freq_mask from data.augmentations.freq_mix import freq_mix # Batch of 8 time series: past (200 steps) and future (24 steps) past = torch.randn(8, 200) future = torch.randn(8, 24) # 1. Frequency-domain augmentations (applied directly to past+future tensors) past_fm, future_fm = freq_mask(past, future, rate=0.5) # Mask 50% of frequency bins past_fx, future_fx = freq_mix(past, future, rate=0.25) # Mix 25% of frequency content # 2. Time-domain augmentations (composed and applied jointly) augmentor = ApplyAugmentations([ Jitter(p=0.5, sigma=0.03), MagnitudeWarp(p=0.3, sigma=0.2, knot=4), TimeWarp(p=0.3, sigma=0.2, knot=4), ]) past_aug, future_aug = augmentor(past, future) print("Past shape unchanged: ", past_aug.shape) # torch.Size([8, 200]) print("Future shape unchanged:", future_aug.shape) # torch.Size([8, 24]) ``` -------------------------------- ### LagLlamaModel: Core Forward Pass for Custom Inference Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Shows how to directly instantiate `LagLlamaModel` for custom inference loops or fine-tuning. This bypasses the `LagLlamaEstimator` wrapper and provides direct access to the PyTorch `nn.Module`. Note that `lags_seq` should contain resolved integer lag indices when directly instantiating the model. ```python import torch from lag_llama.model.module import LagLlamaModel from gluonts.torch.distributions import StudentTOutput # Directly instantiate the model (e.g., for custom inference loops) model = LagLlamaModel( context_length=64, max_context_length=2048, scaling="robust", input_size=1, n_layer=8, n_embd_per_head=16, n_head=9, lags_seq=[0, 1, 2, 3, 4, 11, 23], # Already resolved integer lag indices distr_output=StudentTOutput(), rope_scaling=None, num_parallel_samples=100, time_feat=False, dropout=0.0, ) model.eval() batch_size = 4 context_len = 64 max_lag = max([0, 1, 2, 3, 4, 11, 23]) # = 23 ``` -------------------------------- ### LagLlamaModel.forward Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt The core PyTorch `nn.Module` backbone for Lag-Llama. Its `forward` method processes past target values, observed masks, and optional time features to produce probabilistic forecasts. ```APIDOC ## `LagLlamaModel.forward` — Core Model Forward Pass `LagLlamaModel` is the PyTorch `nn.Module` backbone. Its `forward` method accepts past target values, a mask of observed values, optional time features, and optional future targets (for teacher-forced training). It scales the input using the configured scaler, constructs lagged feature sequences, runs them through the transformer stack, and returns distribution parameters along with location and scale tensors for denormalization. ### Usage Example ```python import torch from lag_llama.model.module import LagLlamaModel from gluonts.torch.distributions import StudentTOutput # Directly instantiate the model (e.g., for custom inference loops) model = LagLlamaModel( context_length=64, max_context_length=2048, scaling="robust", input_size=1, n_layer=8, n_embd_per_head=16, n_head=9, lags_seq=[0, 1, 2, 3, 4, 11, 23], # Already resolved integer lag indices distr_output=StudentTOutput(), rope_scaling=None, num_parallel_samples=100, time_feat=False, dropout=0.0, ) model.eval() bsz = 4 context_len = 64 max_lag = max([0, 1, 2, 3, 4, 11, 23]) # = 23 # Example input tensors (replace with actual data) # past_target = torch.randn(bsz, context_len + max_lag) # observed_mask = torch.ones(bsz, context_len + max_lag, dtype=torch.bool) # time_features = None # or torch.randn(bsz, context_len + max_lag, num_time_features) # with torch.no_grad(): # # The actual forward pass would require specific input shapes and types # # This is a placeholder to show the model instantiation and eval mode # pass ``` ``` -------------------------------- ### LagLlamaEstimator Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt The main entry point for constructing, training, and evaluating Lag-Llama models. It handles data transformations, instance splitting, and predictor creation, supporting zero-shot forecasting by loading pretrained weights. ```APIDOC ## `LagLlamaEstimator` — Main Estimator Class `LagLlamaEstimator` is the top-level entry point for constructing, training, and evaluating Lag-Llama. It wraps the model in a PyTorch Lightning workflow and handles data transformation, instance splitting, and predictor creation. Key parameters include `prediction_length`, `context_length`, architecture sizes (`n_layer`, `n_head`, `n_embd_per_head`), distribution output type, augmentation probabilities, and an optional checkpoint path for loading pretrained weights. ### Usage Example ```python import torch from huggingface_hub import hf_hub_download from lag_llama.gluon.estimator import LagLlamaEstimator from gluonts.evaluation import make_evaluation_predictions, Evaluator from gluonts.dataset.repository.datasets import get_dataset from pathlib import Path # Download pretrained checkpoint ckpt_path = hf_hub_download( repo_id="time-series-foundation-models/Lag-Llama", filename="lag-llama.ckpt" ) # Build estimator from pretrained checkpoint (zero-shot: no training needed) estimator = LagLlamaEstimator( ckpt_path=ckpt_path, prediction_length=24, # Forecast horizon context_length=32, # History timesteps fed to model (start from 32; tune upward) input_size=1, n_layer=8, n_head=9, n_embd_per_head=16, batch_size=64, num_parallel_samples=100, # Samples drawn per forecast (for probabilistic output) rope_scaling=None, # Use {"type": "linear", "factor": 2.0} for longer contexts scaling="robust", # "mean" | "std" | "robust" | None lags_seq=["Q", "M", "W", "D", "H", "T", "S"], # Lag frequencies time_feat=True, device=torch.device("cuda" if torch.cuda.is_available() else "cpu"), ) # Load test dataset (GluonTS built-in) dataset = get_dataset("electricity_hourly", path=Path("datasets")) # Create predictor (no training; uses loaded checkpoint) predictor = estimator.create_predictor( transformation=estimator.create_transformation(), module=estimator.create_lightning_module(use_kv_cache=True), ) # Run zero-shot probabilistic forecasting forecast_it, ts_it = make_evaluation_predictions( dataset=dataset.test, predictor=predictor, num_samples=100, ) forecasts = list(forecast_it) tss = list(ts_it) # Evaluate with CRPS (mean_wQuantileLoss) and MSE evaluator = Evaluator(num_workers=0) agg_metrics, _ = evaluator(iter(tss), iter(forecasts), num_series=len(dataset.test)) print(f"CRPS: {agg_metrics['mean_wQuantileLoss']:.4f}") print(f"MSE: {agg_metrics['MSE']:.4f}") ``` ``` -------------------------------- ### Create Test Dataset Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Loads the test split of a named dataset, truncating each series to match the evaluation protocol. Returns a ListDataset, the canonical prediction length, and the total number of test points. The history_length parameter is used to trim each series. ```python from data.data_utils import create_test_dataset test_data, prediction_length, total_points = create_test_dataset( name="electricity_hourly", dataset_path="datasets", history_length=32 + 700, # context_length + max(lags_seq); used to trim each series ) print(f"Prediction length: {prediction_length}") # 24 print(f"Number of test series: {len(test_data)}") # 370 print(f"Total test points: {total_points}") # Prediction length: 24 # Number of test series: 370 # Total test points: ~12,580 ``` -------------------------------- ### Lag-Llama Model Configuration JSON Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt JSON configuration file for defining the Lag-Llama architecture and training parameters. Overrides CLI arguments and sets parameters like number of layers, attention heads, embedding dimensions, context length, and data augmentation rates. ```json { "use_single_instance_sampler": true, "stratified_sampling": "series", "data_normalization": "robust", "n_layer": 8, "n_head": 9, "n_embd_per_head": 16, "time_feat": true, "context_length": 32, "aug_prob": 0.5, "freq_mask_rate": 0.5, "freq_mixing_rate": 0.25, "weight_decay": 0.0, "dropout": 0.0 } ``` -------------------------------- ### CombinedDataset for Weighted Multi-Dataset Sampling Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Wraps a list of GluonTS datasets and samples from them according to optional weights, enabling stratified sampling strategies. It can be used for multi-dataset pretraining. Uniform sampling is the default if weights are not provided. ```python from data.data_utils import CombinedDataset from gluonts.dataset.common import ListDataset import pandas as pd, numpy as np # Two datasets with different sizes ds1 = ListDataset( [{"start": pd.Period("2020-01-01", freq="H"), "target": np.random.randn(500).astype(np.float32)} for _ in range(100)], freq="H", ) ds2 = ListDataset( [{"start": pd.Period("2020-01-01", freq="D"), "target": np.random.randn(200).astype(np.float32)} for _ in range(20)], freq="D", ) # Uniform sampling (equal weight per dataset, not per series) combined_uniform = CombinedDataset([ds1, ds2], weights=None) # Stratified by number of series (ds1 gets 100/120 weight, ds2 gets 20/120) combined_stratified = CombinedDataset([ds1, ds2], weights=[100, 20]) print(f"Total series in combined: {len(combined_uniform)}") # 120 # Iterate to draw random samples (dataset is an infinite stream when used with Cyclic) from gluonts.itertools import Cyclic for i, sample in enumerate(Cyclic(combined_stratified)): if i >= 5: break print(f"Sample {i}: target length={len(sample['target'])}") ``` -------------------------------- ### Set Seed for Reproducibility Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Sets all random seeds (Python random, NumPy, PyTorch CPU and CUDA) and configures cuDNN for deterministic behavior. This should be called before model construction or data loader initialization to ensure reproducible runs. ```python from utils.utils import set_seed import torch, numpy as np, random set_seed(42, deterministic=True) # All subsequent random operations are reproducible x = torch.randn(3, 3) print(x) # tensor([[ 0.3367, 0.1288, 0.2345], # [ 0.2303, -1.1229, -0.1863], # [ 2.2082, -0.6380, 0.4617]]) ``` -------------------------------- ### Reproducible Output Confirmation Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Ensures that random number generation is reproducible by comparing tensor outputs after setting a seed. ```python set_seed(42) y = torch.randn(3, 3) assert torch.allclose(x, y), "Seeds do not match!" print("Reproducible output confirmed.") ``` -------------------------------- ### LagLlamaEstimator.create_predictor Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Composes the data transformation pipeline with the lightning module to create a GluonTS-compatible `PyTorchPredictor`. This predictor handles batching and manages the model's autoregressive forward pass, and is used for making predictions. ```APIDOC ## `LagLlamaEstimator.create_predictor` — GluonTS Predictor `create_predictor` composes the data transformation pipeline with the lightning module to produce a GluonTS-compatible `PyTorchPredictor`. The predictor handles batching and runs the model's autoregressive forward pass. It is the object passed to `make_evaluation_predictions`. ```python from lag_llama.gluon.estimator import LagLlamaEstimator from gluonts.evaluation import make_evaluation_predictions from gluonts.dataset.common import ListDataset import pandas as pd, numpy as np, torch estimator = LagLlamaEstimator( prediction_length=7, context_length=32, n_layer=4, n_head=4, n_embd_per_head=32, batch_size=32, num_parallel_samples=100, scaling="mean", ckpt_path="lag-llama.ckpt", device=torch.device("cuda" if torch.cuda.is_available() else "cpu"), ) # Build predictor from transformation + lightning module transformation = estimator.create_transformation() module = estimator.create_lightning_module(use_kv_cache=True) predictor = estimator.create_predictor(transformation, module) ``` ``` -------------------------------- ### Lag-Llama Model Inference Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Perform inference using the LagLlamaModel with past target and observed values. Ensure `use_kv_cache` is set to `False` for non-autoregressive inference. ```python past_target = torch.randn(bsz, context_len + max_lag + 1) past_observed = torch.ones(bsz, context_len + max_lag + 1) # 1=observed, 0=missing with torch.no_grad(): # Returns: (distr_params_tuple, loc, scale) params, loc, scale = model( past_target=past_target, past_observed_values=past_observed, past_time_feat=None, future_time_feat=None, future_target=None, use_kv_cache=False, ) # params is a tuple of StudentT distribution parameters: (df, loc_delta, scale_delta) # Each tensor has shape (bsz, context_len) print("Param shapes:", [p.shape for p in params]) # Expected: [torch.Size([4, 64]), torch.Size([4, 64]), torch.Size([4, 64])] print("Scaler loc shape:", loc.shape) # (4, 1) print("Scaler scale shape:", scale.shape) # (4, 1) ``` -------------------------------- ### LagLlamaEstimator.create_predictor for GluonTS Predictor Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Composes data transformations with the lightning module to create a GluonTS-compatible PyTorchPredictor. This predictor handles batching and the autoregressive forward pass, and is used with `make_evaluation_predictions`. ```python from lag_llama.gluon.estimator import LagLlamaEstimator from gluonts.evaluation import make_evaluation_predictions from gluonts.dataset.common import ListDataset import pandas as pd, numpy as np, torch estimator = LagLlamaEstimator( prediction_length=7, context_length=32, n_layer=4, n_head=4, n_embd_per_head=32, batch_size=32, num_parallel_samples=100, scaling="mean", ckpt_path="lag-llama.ckpt", device=torch.device("cuda" if torch.cuda.is_available() else "cpu"), ) # Build predictor from transformation + lightning module transformation = estimator.create_transformation() module = estimator.create_lightning_module(use_kv_cache=True) predictor = estimator.create_predictor(transformation, module) ``` -------------------------------- ### LagLlamaModel.reset_cache for KV-Cache Management Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Resets the KV-cache and y_cache flag after each full prediction sequence to prevent stale cache entries during autoregressive inference. This is crucial for maintaining accuracy in subsequent forecasts. ```python from lag_llama.model.module import LagLlamaModel from gluonts.torch.distributions import StudentTOutput import torch model = LagLlamaModel( context_length=32, max_context_length=2048, scaling="mean", input_size=1, n_layer=4, n_embd_per_head=16, n_head=4, lags_seq=[0, 1, 2, 3], distr_output=StudentTOutput(), num_parallel_samples=50, time_feat=False, ) model.eval() past_target = torch.randn(2, 36) # bsz=2, context(32) + max_lag(3) + 1 past_obs = torch.ones(2, 36) # First prediction step — initializes kv_cache with torch.no_grad(): params, loc, scale = model( past_target=past_target, past_observed_values=past_obs, use_kv_cache=True, ) print("Cache populated:", model.transformer.h[0].attn.kv_cache is not None) # True # After finishing the forecast for this batch, always reset model.reset_cache() print("Cache cleared:", model.transformer.h[0].attn.kv_cache is None) # True print("y_cache flag:", model.y_cache) # False ``` -------------------------------- ### LagLlamaModel.reset_cache Source: https://context7.com/time-series-foundation-models/lag-llama/llms.txt Resets the KV-cache used during autoregressive inference. This method should be called after each full prediction sequence to ensure that stale cache entries do not affect subsequent forecasts. It clears the `kv_cache` in all attention blocks and resets the `y_cache` flag. ```APIDOC ## `LagLlamaModel.reset_cache` — KV-Cache Reset During autoregressive inference, the model caches key and value tensors in each attention block for efficiency. `reset_cache` must be called after each full prediction sequence to prevent stale cache entries from polluting subsequent forecasts. It resets the `y_cache` flag on the model and sets `kv_cache = None` in all attention blocks. ```python from lag_llama.model.module import LagLlamaModel from gluonts.torch.distributions import StudentTOutput import torch model = LagLlamaModel( context_length=32, max_context_length=2048, scaling="mean", input_size=1, n_layer=4, n_embd_per_head=16, n_head=4, lags_seq=[0, 1, 2, 3], distr_output=StudentTOutput(), num_parallel_samples=50, time_feat=False, ) model.eval() past_target = torch.randn(2, 36) # bsz=2, context(32) + max_lag(3) + 1 past_obs = torch.ones(2, 36) # First prediction step — initializes kv_cache with torch.no_grad(): params, loc, scale = model( past_target=past_target, past_observed_values=past_obs, use_kv_cache=True, ) print("Cache populated:", model.transformer.h[0].attn.kv_cache is not None) # True # After finishing the forecast for this batch, always reset model.reset_cache() print("Cache cleared:", model.transformer.h[0].attn.kv_cache is None) # True print("y_cache flag:", model.y_cache) # False ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.