ruvnet/ruflo · error · Error

Optimizer not initialized

Error message

Optimizer not initialized

What it means

Thrown by optimizerStep() when the module-level optimizer is null. Like the InfoNCE loss, the AdamW optimizer (lr, 0.9, 0.999, 1e-8, 0.01) is created unconditionally whenever @ruvector/attention imports during initializeTraining() — there is no enabling flag. A null optimizer therefore means: init not awaited, the attention package unavailable (init warned and disabled attention features), or cleanup() ran.

Source

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

  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');
  }

  return optimizer.step(params, gradients);
}

/**
 * Get curriculum difficulty for current step
 */
export function getCurriculumDifficulty(step: number): number {
  if (!curriculum) {
    return 1.0; // Full difficulty if no curriculum
  }

  return curriculum.getDifficulty(step);
}

/**
 * Mine hard negatives for better training

View on GitHub (pinned to fa13ee4ad6)

Solutions

  1. Await initializeTraining() up front and assert features includes 'AdamW Optimizer' before entering the training loop.
  2. Restore the optional @ruvector/attention dependency in the failing environment (check init warnings).
  3. Pair optimizerStep with a computeContrastiveLoss/computeFlashAttention feature check — they all share the same root cause.
  4. Re-initialize after cleanup() if the process continues.

Example fix

// before
for (const batch of batches) {
  params = optimizerStep(params, grads(batch)); // throws 'Optimizer not initialized'
}

// after
const init = await initializeTraining();
if (!init.features.includes('AdamW Optimizer')) {
  throw new Error('Optimizer requires @ruvector/attention');
}
for (const batch of batches) {
  params = optimizerStep(params, grads(batch));
}
Defensive patterns

Strategy: validation

Validate before calling

const init = await initializeTraining();
if (!init.features.includes('AdamW Optimizer')) {
  throw new Error('Optimizer requires @ruvector/attention — check optional deps');
}
params = optimizerStep(params, gradients);

Type guard

async function optimizerReady(): Promise<boolean> {
  const init = await initializeTraining();
  return init.features.includes('AdamW Optimizer');
}

Try / catch

try {
  return optimizerStep(params, grads);
} catch (e) {
  if (e instanceof Error && e.message === 'Optimizer not initialized') {
    const init = await initializeTraining();
    if (!init.features.includes('AdamW Optimizer')) throw new Error('Missing @ruvector/attention');
    return optimizerStep(params, grads);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling optimizerStep(params, grads) in a training loop whose bootstrap never awaited initializeTraining(); running in a stripped install where @ruvector/attention is absent so the LoRA core came up on the JS fallback without attention/optimizer; post-cleanup usage in tests.

Common situations: Custom training loops written against the full feature set but deployed with an install that pruned optionalDependencies; long test files where cleanup() in one describe block poisons the next; monorepos where the package resolves in dev but not in the built artifact.

Related errors


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