ruvnet/ruflo · error · Error

Contrastive loss not initialized

Error message

Contrastive loss not initialized

What it means

Thrown by computeContrastiveLoss() when contrastiveLoss is null. The InfoNCE loss (temperature 0.07) is constructed by initializeTraining() whenever the optional @ruvector/attention package imports — it has no config flag — so a null here means init never ran, the package was unavailable (init logged a warning and skipped ALL attention features), or cleanup() nulled it.

Source

Thrown at v3/@claude-flow/cli/src/services/ruvector-training.ts:636

  values: Float32Array[]
): Float32Array {
  if (!hyperbolicAttention) {
    throw new Error('Hyperbolic attention not initialized');
  }

  return hyperbolicAttention.computeRaw(query, keys, values);
}

/**
 * Compute contrastive loss for training
 */
export function computeContrastiveLoss(
  anchor: Float32Array,
  positives: Float32Array[],
  negatives: Float32Array[]
): { loss: number; gradient: Float32Array } {
  if (!contrastiveLoss) {
    throw new Error('Contrastive loss not initialized');
  }

  const loss = contrastiveLoss.compute(anchor, positives, negatives);
  const gradient = contrastiveLoss.backward(anchor, positives, negatives);

  return { loss, gradient };
}

/**
 * Optimizer step
 */
export function optimizerStep(
  params: Float32Array,
  gradients: Float32Array
): Float32Array {
  if (!optimizer) {
    throw new Error('Optimizer not initialized');
  }

View on GitHub (pinned to fa13ee4ad6)

Solutions

  1. Await initializeTraining() and assert features includes 'InfoNCE Loss' before the training loop starts.
  2. If the feature is missing, install/restore @ruvector/attention (npm install without --omit=optional) — the LoRA backend initializing successfully does NOT imply attention features are present.
  3. Check init's console output for '[ruvector] @ruvector/attention unavailable' to distinguish missing-package from missing-init.
  4. Re-initialize after cleanup().

Example fix

// before
await initializeTraining(); // may skip attention features silently
const { loss, gradient } = computeContrastiveLoss(anchor, pos, neg); // throws

// after
const init = await initializeTraining();
if (!init.features.includes('InfoNCE Loss')) {
  throw new Error('Contrastive loss requires @ruvector/attention — reinstall deps');
}
const { loss, gradient } = computeContrastiveLoss(anchor, pos, neg);
Defensive patterns

Strategy: validation

Validate before calling

const init = await initializeTraining();
if (!init.features.includes('InfoNCE Loss')) {
  throw new Error('Contrastive loss requires @ruvector/attention — reinstall optional deps');
}
const { loss, gradient } = computeContrastiveLoss(anchor, positives, negatives);

Type guard

async function contrastiveReady(): Promise<boolean> {
  const init = await initializeTraining();
  return init.features.includes('InfoNCE Loss');
}

Try / catch

try {
  return computeContrastiveLoss(anchor, pos, neg);
} catch (e) {
  if (e instanceof Error && e.message === 'Contrastive loss not initialized') {
    const init = await initializeTraining();
    if (!init.features.includes('InfoNCE Loss')) throw new Error('Missing @ruvector/attention');
    return computeContrastiveLoss(anchor, pos, neg);
  }
  throw e;
}

Prevention

When it happens

Trigger: Computing contrastive loss before awaiting initializeTraining(); running in an install where @ruvector/attention failed to resolve (pruned optional dependency, broken hoisting) while the WASM/JS LoRA core initialized fine, masking the gap; calling after cleanup().

Common situations: Training pipelines that only use LoRA (which works without @ruvector/attention) and later add a contrastive-loss step that silently requires it; CI caching node_modules from a --production install; upgrading package managers and losing optional deps.

Related errors


AI-assisted analysis of ruvnet/ruflo@fa13ee4ad6 (2026-08-18). Data as JSON: /api/errors/67c6ec8ddbd3bf59. Report an issue: GitHub.