[BUG] Foundation model fine-tuning metrics use all pretrained quantiles instead of user-selected predictions
Found the issue while developing TiRex-2 for Darts and it seems to affect all foundation models.
TLDR: Fine-tuning a foundation model with likelihood=None and deterministic metrics will fail, because _compute_loss returns probabilistic parameters but _update_metrics() expects a deterministic forecast.
Here is the Codex report:
Describe the bug
Fine-tuning a foundation model with likelihood=None and torch_metrics=MeanAbsoluteError() fails during the first training metrics update. The training forward pass returns all pretrained quantiles for the quantile loss, but PLForecastingModule._update_metrics() interprets that output as a deterministic forecast.
Reproduced with TiRex and real pretrained weights on CPU. This concerns training metrics, not the fine-tuning loss itself.
To Reproduce
Install Darts with Torch support and tirex-ts>=1.4.0, then run:
from torchmetrics import MeanAbsoluteError
from darts.models import TiRexModel
from darts.utils.timeseries_generation import linear_timeseries
series = linear_timeseries(length=18).astype("float32")
model = TiRexModel(
input_chunk_length=12,
output_chunk_length=6,
accept_license=True,
tirex_kwargs={"backend": "torch"},
enable_finetuning={"unfreeze": ["tirex.output_patch_embedding*"]},
likelihood=None,
torch_metrics=MeanAbsoluteError(),
n_epochs=1,
pl_trainer_kwargs={"accelerator": "cpu"},
)
model.fit(series)Relevant traceback:
PLForecastingModule._train_val_step:
self._update_metrics(output, batch.future_target, metrics)
PLForecastingModule._update_metrics:
metrics.update(prediction, target)
RuntimeError: Predictions and targets are expected to have the same shape, but got torch.Size([54, 1]) and torch.Size([6, 1]).Expected behavior
Fine-tuning should compute metrics using the median forecast for deterministic models, while retaining all pretrained quantiles for the training loss.
System
- Python: 3.14
- Darts: local source checkout reporting 0.47.0
- Torch: 2.14.0
- PyTorch Lightning: 2.5.6
- TorchMetrics: 1.9.0
- tirex-ts: 1.4.2
- Execution: macOS, CPU
Additional context
During training, TiRex returns (batch, horizon, components, 9) predictions. With no likelihood, _update_metrics() calls squeeze(-1), which leaves the nine-quantile dimension intact. Reshaping then produces nine times as many prediction rows as target rows.
Code inspection indicates the same pattern in Chronos2Model, TimesFM2p5Model, TimesFM3Model, and PatchTSTFMModel: they return all pretrained quantiles during training and inherit the same metrics implementation. These additional models were not independently reproduced here.
There is also a related correctness problem when a user supplies a subset of pretrained quantiles: _update_metrics() samples the full training output using the user likelihood's smaller quantile grid, associating forecast values with incorrect quantile levels. Validation selects the user quantiles before updating metrics and does not have this mismatch.
A possible fix is to adapt training predictions to the user-facing quantile selection specifically for metrics, without changing the all-quantile training loss. Omitting torch_metrics avoids the failing metrics path.
Source: unit8co/darts