All files / timesfm-core/src model.ts

92.91% Statements 249/268
95% Branches 57/60
81.25% Functions 13/16
92.91% Lines 249/268

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475                                                        1x                       1x 1x 1x 1x 1x 1x 1x 1x 1x                       1x 1x 1x                       1x 2x 2x 2x 2x           1x 10x 10x 10x 10x 10x   10x 10x 10x 10x                                   10x 11x 1x 1x         1x 1x         10x 10x 10x 10x   10x     10x 10x 10x 10x 10x 10x   10x 10x 9x 9x 9x 9x     11x 9x 9x   10x 11x                             10x 46x 46x 46x 46x 46x 46x   10x 3x 3x   10x 25x 25x                                       10x 93x 93x 93x 93x 93x 1x 1x         93x 3x 89x 93x 93x     93x   93x 1x 1x             90x 90x 90x 90x 93x 21x 21x 21x 21x 21x     90x 90x   90x   93x 87x 155x 155x   155x 155x 155x   155x 155x 21045x 21045x 155x 87x 93x   3x 3x 3x 3x 3x   90x 90x 90x   90x 90x   93x   114x   114x 114x   114x 1x 1x 1x 1x 1x 1x   114x   114x 1x 1x 1x 1x 1x 1x       114x 114x   114x         114x 158x 158x 158x 114x 114x   114x   114x 1x 1x 1x 1x 1x 1x     114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x 114x                             114x 1x 1x 1x 1x 1x 1x   114x 111x 3x 114x   114x 114x 114x 114x 114x 114x 114x 114x 114x   114x 114x 114x 4x 4x 114x   114x 114x 114x 93x 93x 93x                       10x   2x                   2x 2x 2x 2x 1x 1x 1x 1x 1x 1x   1x 1x 2x 2x 2x           2x 2x 2x   10x 10x 10x 10x 10x   10x     10x 4x 4x 10x     10x  
/**
 * TimesFM Model — the main public API.
 *
 * Usage:
 *
 * ```typescript
 * import { TimesFMModel } from '@agentix-e/timesfm-core';
 * import { createForecastConfig } from '@agentix-e/timesfm-core';
 *
 * // Always requires a valid ONNX model path
 * const model = await TimesFMModel.fromPretrained({
 *   modelPath: './models/timesfm-2.5.onnx',
 * });
 *
 * model.compile(createForecastConfig({
 *   maxContext: 1024,
 *   maxHorizon: 256,
 * }));
 *
 * const { pointForecast, quantileForecast } = await model.forecast(
 *   24,
 *   [new Float32Array([1, 2, 3, ...])],
 * );
 *
 * await model.dispose();
 * ```
 */
 
import {
  TIMESFM_25_CONFIG,
  type IInferenceEngine,
  type ForecastConfig,
  type ModelConfig,
  type ModelLoadOptions,
  type ForecastOutput,
  type ForecastCallOptions,
  type ITimesFMModel,
  type CovariateForecastParams,
  type CovariateForecastOutput,
} from './types';
import { ModelNotFoundError, ModelNotCompiledError, HorizonExceededError } from './errors';
import { validateAndNormalizeConfig } from './config';
import { TimesFMInferenceEngine } from './inference/onnx-engine';
import { preprocess } from './preprocessor';
import { decode } from './inference/decode-loop';
import { postProcess } from './postprocessor';
import { resolveModelConfig } from './model-descriptor';
import { computeStats } from './utils/stats';
import { allNonNegative } from './utils/tensor-utils';
 
// Cached dynamic import for optional @agentix-e/timesfm-xreg peer dependency.
//
// Uses a Promise-based singleton pattern to handle concurrent calls safely:
//   • First call initializes the promise (only one import() in flight)
//   • Subsequent calls wait on the same promise
//   • On failure, the promise is reset so the next call retries
//   • After success, _xregModule is set so subsequent calls resolve instantly
//
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
type XRegModule = typeof import('@agentix-e/timesfm-xreg');
let _xregModule: XRegModule | null = null;
let _xregModulePromise: Promise<XRegModule> | null = null;
let _xregImportFn: (spec: string) => Promise<unknown> = (spec: string) => import(spec);
 
/**
 * Override the import function used to load @agentix-e/timesfm-xreg.
 *
 * **Test-only seam** — NOT part of the public API. Exported only so tests
 * can exercise the dynamic-import failure path without vitest mocking.
 *
 * Reset with `null` to restore default behaviour.
 *
 * @internal
 */
export function __test_setXregImport(fn: ((spec: string) => Promise<unknown>) | null): void {
  _xregImportFn = fn ?? ((spec: string) => import(spec));
  _xregModule = null; // Invalidate cache so next call uses the new fn
  _xregModulePromise = null; // Invalidate pending promise
}
 
// ---------------------------------------------------------------------------
// TimesFMModel
// ---------------------------------------------------------------------------
 
export class TimesFMModel implements ITimesFMModel {
  private _engine: IInferenceEngine;
  private _config: ModelConfig;
  private _forecastConfig: ForecastConfig | null = null;
  private _globalBatchSize: number = 0;
  private _compiled: boolean = false;
 
  private constructor(engine: IInferenceEngine, config: ModelConfig) {
    this._engine = engine;
    this._config = config;
  }
 
  // -----------------------------------------------------------------------
  // Factory
  // -----------------------------------------------------------------------
 
  /**
   * Create a TimesFM model from a pretrained ONNX checkpoint.
   *
   * The model architecture is resolved from the `model-descriptor.json`
   * co-located with the ONNX file.  When no descriptor is found, the
   * engine falls back to the canonical TimesFM 2.5 200M config.
   *
   * @param options.modelPath  Path to the TimesFM ONNX model file. Required.
   * @param options.executionProvider  'cpu' (default), 'cuda', or 'dml'.
   *
   * @throws {Error} if modelPath is not provided or the file doesn't exist.
   */
  static async fromPretrained(options: ModelLoadOptions): Promise<TimesFMModel> {
    if (!options.modelPath) {
      throw new ModelNotFoundError(
        'modelPath is required. Provide the path to a TimesFM ONNX model file.\n' +
          'To obtain a model:\n' +
          '  1. Run: python scripts/export-onnx.py (if you have the TimesFM PyTorch model)\n' +
          '  2. Or download a pre-converted ONNX model\n' +
          'See docs/GETTING-STARTED.md for details.',
      );
    }
 
    // Resolve architecture from model-descriptor.json (single source of truth).
    // Falls back to TIMESFM_25_CONFIG when no descriptor is present, maintaining
    // backward compatibility with bare .onnx files.
    const { config: mc, descriptor } = await resolveModelConfig(
      options.modelPath,
      TIMESFM_25_CONFIG,
    );
 
    if (descriptor) {
      // Log model identity for traceability
      // eslint-disable-next-line no-console
      console.log(
        `[TimesFM] Loaded descriptor: v${descriptor.model.version}-${descriptor.model.variant}` +
          ` (schema ${descriptor.schema}, hf_rev ${descriptor.model.hf_revision}), ` +
          `${descriptor.onnx.size_bytes > 0 ? (descriptor.onnx.size_bytes / 1024 ** 2).toFixed(0) + ' MB' : ''}`,
      );
    }
 
    const engine =
      options.engine ??
      new TimesFMInferenceEngine(mc, {
        executionProvider: options.executionProvider,
        intraOpNumThreads: options.intraOpNumThreads,
      });
 
    // Only load if the engine is not already loaded (external engine injection)
    if (!engine.isLoaded()) {
      await engine.load(options.modelPath, { skipWarmup: options.skipWarmup });
    }
 
    return new TimesFMModel(engine, mc);
  }
 
  // -----------------------------------------------------------------------
  // Compile
  // -----------------------------------------------------------------------
 
  /**
   * Compile the model with the given forecast configuration.
   *
   * This must be called before `forecast()`.  It validates and normalises
   * the config and sets up batch-size calculations.
   *
   * @returns `this` for method chaining.
   * @throws {RangeError} if context + horizon exceeds the model limit.
   */
  compile(fc: ForecastConfig): this {
    const normalized = validateAndNormalizeConfig(fc, this._config);
    this._forecastConfig = normalized;
    this._globalBatchSize = normalized.perCoreBatchSize;
    this._compiled = true;
    return this;
  }
 
  get isCompiled(): boolean {
    return this._compiled;
  }
 
  get forecastConfig(): ForecastConfig | null {
    return this._forecastConfig;
  }
 
  // -----------------------------------------------------------------------
  // Forecast
  // -----------------------------------------------------------------------
 
  /**
   * Forecast a batch of univariate time series.
   *
   * @param horizon  Number of future time points to forecast.
   * @param inputs   List of 1-D time series.  Each can have different length.
   *                 May contain NaN values (leading NaNs are stripped,
   *                 internal NaNs are linearly interpolated).
   * @param options  Optional AbortSignal and progress callback.
   *
   * @returns Point forecasts + quantile forecasts.
   *
   * @throws {Error} if the model has not been compiled.
   * @throws {DOMException} AbortError if the operation was cancelled.
   */
  async forecast(
    horizon: number,
    inputs: Float32Array[],
    options?: ForecastCallOptions,
  ): Promise<ForecastOutput> {
    if (!this._compiled || !this._forecastConfig) {
      throw new ModelNotCompiledError('Model not compiled. Call compile() before forecast().');
    }
 
    // Per-call config overrides (used internally by XReg covariate workflows
    // for returnBackcast without mutating global state).  The stored config
    // on the model instance is never modified.
    const fc: ForecastConfig = options?.configOverrides
      ? { ...this._forecastConfig, ...options.configOverrides }
      : this._forecastConfig;
    const signal = options?.signal;
    const onProgress = options?.onProgress;
 
    // Check for early abort
    signal?.throwIfAborted();
 
    if (horizon > fc.maxHorizon) {
      throw new HorizonExceededError(`Horizon ${horizon} exceeds maxHorizon ${fc.maxHorizon}.`);
    }
 
    // Pad batch to globalBatchSize — synthetic zero-padding series
    // are appended to align the input count to perCoreBatchSize.
    // These are tracked separately so we can skip them in the decode
    // pipeline and trim the output to numInputs, avoiding wasted ONNX
    // inference on dummy data (~12% compute saving at typical batch sizes).
    const paddedInputs = [...inputs];
    const numInputs = inputs.length;
    const remainder = numInputs % this._globalBatchSize;
    let padCount = 0;
    if (remainder !== 0) {
      padCount = this._globalBatchSize - remainder;
      for (let i = 0; i < padCount; i++) {
        paddedInputs.push(new Float32Array([0, 0, 0]));
      }
    }
 
    // Input z-score normalization
    let normalizedInputs = paddedInputs;
    const inputStats: { mu: number; sigma: number }[] = [];
    /** Per-series flag: true if raw (pre-normalized) input is all ≥ 0. */
    const isPositiveFlags: boolean[] = [];
 
    if (fc.normalizeInputs) {
      normalizedInputs = paddedInputs.map((arr) => {
        const { mean: mu, std: sigma } = computeStats(arr);
        const safeSigma = sigma < 1e-6 ? 1 : sigma;
 
        const isNonNeg = allNonNegative(arr);
        inputStats.push({ mu, sigma: safeSigma });
        isPositiveFlags.push(isNonNeg);
 
        const result = new Float32Array(arr.length);
        for (let i = 0; i < arr.length; i++) {
          result[i] = Number.isFinite(arr[i]) ? (arr[i] - mu) / safeSigma : 0;
        }
        return result;
      });
    } else {
      // When not normalizing, still compute positivity flags
      for (const arr of paddedInputs) {
        inputStats.push({ mu: 0, sigma: 1 });
        isPositiveFlags.push(allNonNegative(arr));
      }
    }
 
    const allPointForecasts: Float32Array[] = [];
    const allQuantileForecasts: Float32Array[][] = [];
    const allBackcasts: Float32Array[] = [];
 
    const batchSize = this._globalBatchSize;
    const numBatches = Math.ceil(normalizedInputs.length / batchSize);
 
    for (let bi = 0; bi < numBatches; bi++) {
      // Abort check at batch boundary
      signal?.throwIfAborted();
 
      const batchStart = bi * batchSize;
      const batchInputs = normalizedInputs.slice(batchStart, batchStart + batchSize);
 
      onProgress?.({
        phase: 'preprocess',
        step: bi + 1,
        total: numBatches,
        batchIndex: bi,
        totalBatches: numBatches,
      });
 
      const preprocessed = preprocess(batchInputs, fc, this._config);
 
      onProgress?.({
        phase: 'prefill',
        step: bi + 1,
        total: numBatches,
        batchIndex: bi,
        totalBatches: numBatches,
      });
 
      // ── Decode (main path, and optionally flip path in parallel) ──────
 
      let decodeResult: Awaited<ReturnType<typeof decode>>;
      let flipResult: Awaited<ReturnType<typeof decode>> | null = null;
 
      if (fc.forceFlipInvariance) {
        // Pre-build the negated flip inputs while the main decode runs.
        // Then launch both decode() calls concurrently via Promise.all
        // so the flip path doesn't add serial latency.  ONNX Runtime
        // sessions are safe for concurrent calls in the Node.js event loop.
        const negInputs = batchInputs.map((arr) => {
          const neg = new Float32Array(arr.length);
          for (let i = 0; i < arr.length; i++) neg[i] = -arr[i];
          return neg;
        });
        const flipPre = preprocess(negInputs, fc, this._config);
 
        signal?.throwIfAborted();
 
        onProgress?.({
          phase: 'flip',
          step: bi + 1,
          total: numBatches,
          batchIndex: bi,
          totalBatches: numBatches,
        });
 
        // Parallel: main + flip decode run concurrently
        [decodeResult, flipResult] = await Promise.all([
          decode(
            this._engine,
            preprocessed.patchedInputs,
            preprocessed.patchedMasks,
            preprocessed.contextMu,
            preprocessed.contextSigma,
            preprocessed.lastStats,
            fc.maxHorizon,
            fc,
            this._config,
            signal,
          ),
          decode(
            this._engine,
            flipPre.patchedInputs,
            flipPre.patchedMasks,
            flipPre.contextMu,
            flipPre.contextSigma,
            flipPre.lastStats,
            fc.maxHorizon,
            fc,
            this._config,
            signal,
          ),
        ]);
      } else {
        decodeResult = await decode(
          this._engine,
          preprocessed.patchedInputs,
          preprocessed.patchedMasks,
          preprocessed.contextMu,
          preprocessed.contextSigma,
          preprocessed.lastStats,
          fc.maxHorizon,
          fc,
          this._config,
          signal,
        );
      }
 
      onProgress?.({
        phase: 'postprocess',
        step: bi + 1,
        total: numBatches,
        batchIndex: bi,
        totalBatches: numBatches,
      });
 
      const batchInputStats = fc.normalizeInputs
        ? inputStats.slice(batchStart, batchStart + batchSize)
        : null;
      const batchIsPositive = isPositiveFlags.slice(batchStart, batchStart + batchSize);
 
      const output = postProcess(
        decodeResult,
        horizon,
        fc,
        this._config,
        batchInputStats,
        flipResult,
        batchIsPositive,
      );
 
      allPointForecasts.push(...output.pointForecast);
      allQuantileForecasts.push(...output.quantileForecast);
      if (output.backcast) {
        allBackcasts.push(...output.backcast);
      }
    }
 
    return {
      pointForecast: allPointForecasts.slice(0, numInputs),
      quantileForecast: allQuantileForecasts.slice(0, numInputs),
      backcast: allBackcasts.length > 0 ? allBackcasts.slice(0, numInputs) : undefined,
    };
  }
 
  /**
   * Forecast with exogenous covariates via {@link @agentix-e/timesfm-xreg}.
   *
   * Internally caches the dynamic import of the optional peer dependency
   * so that repeated calls avoid re-resolution overhead.
   *
   * Concurrent safety: uses a Promise-based singleton pattern so that
   * multiple concurrent first-time calls share a single import() and
   * import failures are retried on subsequent calls.
   */
  async forecastWithCovariates(params: CovariateForecastParams): Promise<CovariateForecastOutput> {
    // Fast path: module is already loaded (after first successful import).
    if (_xregModule) {
      return await _xregModule.forecastWithCovariates(this, params);
    }
 
    // Init path: start (or join) a single inflight import promise.
    //
    // Wrap in a helper that handles both async (rejected promise) and
    // sync (thrown error) failures from the import function — the
    // test seam __test_setXregImport may provide a function that throws
    // synchronously.
    if (!_xregModulePromise) {
      const startImport = async (): Promise<XRegModule> => {
        try {
          const mod = (await _xregImportFn('@agentix-e/timesfm-xreg')) as XRegModule;
          _xregModule = mod;
          return mod;
        } catch {
          _xregModulePromise = null;
          throw new Error(
            'forecastWithCovariates requires @agentix-e/timesfm-xreg.\n' +
              'Install it: npm install @agentix-e/timesfm-xreg',
          );
        }
      };
      _xregModulePromise = startImport();
    }
 
    // _xregModulePromise is guaranteed non-null after the init block above.
    // The Promise<XRegModule> | null type on the module-level var prevents
    // TypeScript from narrowing — we use a type assertion here because the
    // runtime guarantee is structurally enforced by the code above.
    const promise = _xregModulePromise as Promise<XRegModule>;
    return (await promise).forecastWithCovariates(this, params);
  }
 
  async dispose(): Promise<void> {
    await this._engine.dispose();
    this._compiled = false;
    this._forecastConfig = null;
  }
 
  get engine(): IInferenceEngine {
    return this._engine;
  }
  get modelConfig(): ModelConfig {
    return this._config;
  }
  get globalBatchSize(): number {
    return this._globalBatchSize;
  }
}