google-research/timesfm · error · ValueError

Context + horizon must be less than the context limit. {fc.m

Error message

Context + horizon must be less than the context limit. {fc.max_context} + {fc.max_horizon} > {self.model.config.context_limit}.

What it means

During compile(), TimesFM validates the forecast_config: max_context + max_horizon must not exceed the model's context_limit (16384 for TimesFM 2.5 200M). Context and horizon share one positional budget, so requesting too much total window would exceed the compiled sequence length. Note max_context/max_horizon are first rounded up to patch-size multiples (patch 32, output patch 128), which can push an otherwise-valid config over the limit.

Source

Thrown at src/timesfm/timesfm_2p5/timesfm_2p5_torch.py:410

    if fc.max_context % self.model.p != 0:
      logging.info(
        "When compiling, max context needs to be multiple of the patch size"
        " %d. Using max context = %d instead.",
        self.model.p,
        new_context := math.ceil(fc.max_context / self.model.p) * self.model.p,
      )
      fc = dataclasses.replace(fc, max_context=new_context)
    if fc.max_horizon % self.model.o != 0:
      logging.info(
        "When compiling, max horizon needs to be multiple of the output patch"
        " size %d. Using max horizon = %d instead.",
        self.model.o,
        new_horizon := math.ceil(fc.max_horizon / self.model.o) * self.model.o,
      )
      fc = dataclasses.replace(fc, max_horizon=new_horizon)
    if fc.max_context + fc.max_horizon > self.model.config.context_limit:
      raise ValueError(
        "Context + horizon must be less than the context limit."
        f" {fc.max_context} + {fc.max_horizon} >"
        f" {self.model.config.context_limit}."
      )
    if fc.use_continuous_quantile_head and (fc.max_horizon > self.model.os):
      raise ValueError(
        f"Continuous quantile head is not supported for horizons > {self.model.os}."
      )
    self.forecast_config = fc

    def _compiled_decode(horizon, inputs, masks):
      if horizon > fc.max_horizon:
        raise ValueError(
          f"Horizon must be less than the max horizon. {horizon} > {fc.max_horizon}."
        )

      inputs = (
        torch.from_numpy(np.array(inputs)).to(self.model.device).to(torch.float32)

View on GitHub (pinned to 331c6d33cb)

Solutions

  1. Reduce max_context and/or max_horizon so their sum is <= 16384.
  2. Compute rounded values before compiling: context must be a multiple of 32, horizon a multiple of 128; verify rounded_context + rounded_horizon <= 16384.
  3. Keep a safety margin (e.g. sum <= 16000) to absorb rounding.
  4. Split very long series into windows and forecast iteratively instead of compiling above the limit.

Example fix

// before
fc = ForecastConfig(max_context=16384, max_horizon=256)
model.compile(fc)  # ValueError: 16384 + 256 > 16384
// after
fc = ForecastConfig(max_context=16128, max_horizon=256)  # 16128+256 = 16384 <= limit
model.compile(fc)
Defensive patterns

Strategy: validation

Validate before calling

CONTEXT_LIMIT = 16384
ctx = math.ceil(fc.max_context / 32) * 32
hor = math.ceil(fc.max_horizon / 128) * 128
assert ctx + hor <= CONTEXT_LIMIT, f"{ctx}+{hor} exceeds context limit"

Try / catch

try:
    model.compile(fc)
except ValueError as e:
    if "context limit" in str(e):
        fc = dataclasses.replace(fc, max_context=fc.max_context - 256)
        model.compile(fc)
    else:
        raise

Prevention

When it happens

Trigger: Calling model.compile(forecast_config) where, after rounding, fc.max_context + fc.max_horizon > 16384 — e.g. max_context=16384 with any nonzero horizon, or max_context=16320 + max_horizon=128 (sum 16448 > 16384).

Common situations: Forecasting near-limit-length series while also requesting a long horizon; forgetting horizons round up to multiples of 128; copying configs from a model with a larger context limit.

Related errors


AI-assisted analysis of google-research/timesfm@331c6d33cb (2026-08-29). Data as JSON: /api/errors/630efddca1d6b056. Report an issue: GitHub.