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

The TimesFM model has a fixed `context_limit` (max tokens/points the transformer can attend to). At compile time, `max_context + max_horizon` from the ForecastConfig must not exceed it, because compiled decoding processes context and future patches together. Exceeding it would require out-of-bounds positions, so compile() raises ValueError immediately with the offending numbers.

Source

Thrown at src/timesfm/timesfm_2p5/timesfm_2p5_flax.py:533

    fc = forecast_config
    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
    self.model.compile(
      context=self.forecast_config.max_context,
      horizon=self.forecast_config.max_horizon,
      per_core_batch_size=fc.per_core_batch_size,
    )
    self.per_core_batch_size = self.forecast_config.per_core_batch_size
    self.num_devices = self.model.num_devices
    self.global_batch_size = (

View on GitHub (pinned to 331c6d33cb)

Solutions

  1. Lower `max_context` (extra context beyond the model's patch capacity is trimmed anyway)
  2. Lower `max_horizon` to the maximum you actually need
  3. Check `model.config.context_limit` and pick max_context + max_horizon <= context_limit before compiling

Example fix

// before
model.compile(forecast_config=ForecastConfig(max_context=1024, max_horizon=512))
// after
model.compile(forecast_config=ForecastConfig(max_context=768, max_horizon=256))  # within context_limit
Defensive patterns

Strategy: validation

Validate before calling

if fc.max_context + fc.max_horizon > model.config.context_limit:
    fc = dataclasses.replace(fc, max_context=model.config.context_limit - fc.max_horizon)
model.compile(forecast_config=fc)

Try / catch

try:
    model.compile(forecast_config=fc)
except ValueError as e:
    if 'context limit' in str(e):
        fc = dataclasses.replace(fc, max_context=model.config.context_limit - fc.max_horizon)
        model.compile(forecast_config=fc)
    else:
        raise

Prevention

When it happens

Trigger: Calling `model.compile(ForecastConfig(max_context=X, max_horizon=Y))` where X + Y > model.config.context_limit; raising max_horizon for covariates while keeping a large max_context; compiling for a smaller-capacity model variant with the same config used for a larger one.

Common situations: Copying a config from a blog/notebook for a different model size; increasing max_horizon to satisfy the covariate horizon check (error 16) without lowering max_context; choosing an overly large max_context 'to be safe'.

Related errors


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