All files / timesfm-core/src/utils revin.ts

100% Statements 120/120
95% Branches 38/40
100% Functions 5/5
100% Lines 120/120

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                                1x                           1x 11x 11x 11x 11x 11x 11x 11x   11x 11x   11x   3x 11x 11x 11x   8x 27x 27x 27x 8x   11x 11x                                   1x 313x 313x 313x 313x 313x 313x 313x 313x 313x         313x   313x 408x 408x   408x 2757x 2757x 2757x 2757x   2757x 87860x 87860x 280x 87860x 87580x 87580x 87580x 87860x 2757x   408x 408x   313x 313x                                       1x 310x 310x 310x 310x 310x 310x 310x 310x 310x 310x 310x   310x   403x 403x   403x 5685x 726546x 7265390x 7265390x 7265390x 7265390x   7265390x 7265332x 7265390x 58x 58x 58x 7265390x 726546x 5685x   403x 403x   310x 310x                             22x 22x 18x 18x   4x 22x 2x 2x     2x 2x 2x 2x 4x 4x 8x 8x 4x 4x 2x 4x 4x         18x 18x 16x 16x 16x 16x 2x 2x  
/**
 * Reversible Instance Normalization (RevIN).
 *
 * Mirrors the Python `revin()` function in torch/util.py and flax/util.py.
 *
 * RevIN normalizes time-series patches before feeding them into the
 * transformer, then reverses the normalization on the output so forecasts
 * are in the original data scale.
 *
 *   Forward:  y = (x - μ) / σ
 *   Reverse:  y = x * σ + μ
 *
 * When σ < ε (1e-6), the forward pass substitutes 1.0 to avoid division
 * by zero.  This happens for constant-valued series.
 */
 
const TOLERANCE = 1e-6;
 
// ---------------------------------------------------------------------------
// Scalar RevIN (one batch element, one patch)
// ---------------------------------------------------------------------------
 
/**
 * Apply RevIN to a single patch of values.
 *
 * @param values   The (possibly multi-dimensional) value array.
 * @param mu       Per-dimension mean(s).
 * @param sigma    Per-dimension std deviation(s).
 * @param reverse  If true, perform inverse normalisation (denormalize).
 */
export function revin(
  values: Float32Array,
  mu: Float32Array | Float32Array[],
  sigma: Float32Array | Float32Array[],
  reverse: boolean,
): Float32Array {
  const len = values.length;
  const result = new Float32Array(len);
 
  const muFlat = flattenParam(mu, len);
  const sigmaFlat = flattenParam(sigma, len);
 
  if (reverse) {
    // x * σ + μ
    for (let i = 0; i < len; i++) {
      result[i] = values[i] * sigmaFlat[i] + muFlat[i];
    }
  } else {
    // (x - μ) / max(σ, ε)
    for (let i = 0; i < len; i++) {
      const safeSigma = sigmaFlat[i] < TOLERANCE ? 1.0 : sigmaFlat[i];
      result[i] = (values[i] - muFlat[i]) / safeSigma;
    }
  }
 
  return result;
}
 
// ---------------------------------------------------------------------------
// Batch RevIN — for (batch, patches, patchLen) and (batch, patches, patchLen, q)
// ---------------------------------------------------------------------------
 
/**
 * Apply RevIN across a batch of patched time series.
 *
 * Shape: `values[b][p * patchLen + i]` where b = batch, p = patch, i = offset.
 *
 * @param values      Flat array per batch element (all patches concatenated).
 * @param mu          Mean per batch element, or per (batch, patch) pair.
 * @param sigma       Std per batch element, or per (batch, patch) pair.
 * @param reverse     If true, denormalize.
 * @param numPatches  Number of patches per batch element.
 * @param patchLen    Length of each patch (output: per-element).
 */
export function revinBatch(
  values: Float32Array[],
  mu: Float32Array[],
  sigma: Float32Array[],
  reverse: boolean,
  numPatches: number,
  patchLen: number,
): Float32Array[] {
  const batchSize = values.length;
  const result: Float32Array[] = [];
 
  // Determine broadcast pattern:
  // - mu/sigma length === batchSize     → per-batch (apply to all patches)
  // - mu/sigma length === batchSize * numPatches → per-patch
  const perPatch = mu.length === batchSize * numPatches;
 
  for (let b = 0; b < batchSize; b++) {
    const totalLen = values[b].length;
    const out = new Float32Array(totalLen);
 
    for (let p = 0; p < numPatches; p++) {
      const patchOffset = p * patchLen;
      const muIndex = perPatch ? b * numPatches + p : b;
      const m = mu[muIndex][0]; // scalar per patch/batch
      const s = sigma[muIndex][0]; // scalar per patch/batch
 
      for (let i = 0; i < patchLen && patchOffset + i < totalLen; i++) {
        const idx = patchOffset + i;
        if (reverse) {
          out[idx] = values[b][idx] * s + m;
        } else {
          const safeS = s < TOLERANCE ? 1.0 : s;
          out[idx] = (values[b][idx] - m) / safeS;
        }
      }
    }
 
    result.push(out);
  }
 
  return result;
}
 
// ---------------------------------------------------------------------------
// 4-D RevIN (batch, patches, patchLen, numQuantiles)
// ---------------------------------------------------------------------------
 
/**
 * Apply RevIN to quantile-shaped outputs.
 *
 * Shape: (batch, patches, patchLen, numQuantiles).
 *
 * mu/sigma are broadcast from (batch,) or (batch, patches) with
 * two trailing singleton dims added internally.
 *
 * **Contract**: The returned array always has exportedPatches-worth of
 * elements per batch entry.  When the caller only populated m < exportedPatches
 * sub-patches, the trailing (exportedPatches - m) sub-patches are zero-filled.
 * Callers MUST index by their known m, not by array.length.  Changing this
 * memory strategy requires updating all callers (notably decode-loop.ts).
 */
export function revinBatch4D(
  values: Float32Array[],
  mu: Float32Array[],
  sigma: Float32Array[],
  reverse: boolean,
  numPatches: number,
  patchLen: number,
  numQuantiles: number,
): Float32Array[] {
  const batchSize = values.length;
  const result: Float32Array[] = [];
  const perPatch = mu.length === batchSize * numPatches;
 
  for (let b = 0; b < batchSize; b++) {
    // Total elements per batch: numPatches * patchLen * numQuantiles
    const totalLen = values[b].length;
    const out = new Float32Array(totalLen);
 
    for (let p = 0; p < numPatches; p++) {
      for (let i = 0; i < patchLen; i++) {
        for (let q = 0; q < numQuantiles; q++) {
          const idx = (p * patchLen + i) * numQuantiles + q;
          const muIndex = perPatch ? b * numPatches + p : b;
          const m = mu[muIndex][0];
          const s = sigma[muIndex][0];
 
          if (reverse) {
            out[idx] = values[b][idx] * s + m;
          } else {
            const safeS = s < TOLERANCE ? 1.0 : s;
            out[idx] = (values[b][idx] - m) / safeS;
          }
        }
      }
    }
 
    result.push(out);
  }
 
  return result;
}
 
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
 
/**
 * Flatten a RevIN parameter (mu or sigma) into a per-element array
 * matching the value length.
 *
 * Handles broadcasting:
 *   - length 1          → repeat to all elements
 *   - length === len    → direct (already broadcast)
 *   - length === len / patchLen → per-patch (repeats within each patch)
 */
function flattenParam(param: Float32Array | Float32Array[], len: number): Float32Array {
  if (param instanceof Float32Array) {
    return broadcast1D(param, len);
  }
  // Array of scalars — each element is a Float32Array([scalar])
  const flat = new Float32Array(len);
  if (param.length === len) {
    for (let i = 0; i < len; i++) flat[i] = param[i][0];
  } else {
    // Assume it's per-patch sized — repeat values evenly.
    // Handle the remainder to avoid dropping trailing elements.
    const ratio = Math.floor(len / param.length);
    const remainder = len % param.length;
    let cursor = 0;
    for (let i = 0; i < param.length; i++) {
      const repeatCount = ratio + (i < remainder ? 1 : 0);
      for (let j = 0; j < repeatCount; j++) {
        flat[cursor + j] = param[i][0];
      }
      cursor += repeatCount;
    }
  }
  return flat;
}
 
/**
 * Broadcast a scalar or 1-D array to length `len`.
 */
function broadcast1D(arr: Float32Array, len: number): Float32Array {
  if (arr.length === 1) {
    const result = new Float32Array(len);
    result.fill(arr[0]);
    return result;
  }
  return arr; // assume already correct length
}