Skip to main content
torch.js has not been released yet.
torch.js logotorch.js logotorch.js
PlaygroundContact
Login
Documentation
IntroductionType SafetyTensor ExpressionsTensor IndexingEinsumEinopsAutogradTraining a ModelProfiling & MemoryPyTorch MigrationBest PracticesRuntimesPerformancePyTorch CompatibilityBenchmarksDType Coverage
torch.js· 2026
LegalTerms of UsePrivacy Policy
/
/
  1. docs
  2. Spark
  3. spark
  4. worker
  5. checkpoint

spark.worker.checkpoint

function checkpoint(): Promise<void>

Cooperative checkpoint that enables pause/stop/reload from the UI.

Call this frequently in your training loop to allow the UI to pause, stop, or request hot reload without losing state. Checkpoints are very lightweight when nothing is requested (minimal overhead).

Checkpoints provide:

  • Pause support: Execution pauses until UI clicks resume
  • Stop support: Throws SparkStopError if UI clicked stop
  • Reload support: Throws SparkReloadError if code reload requested
  • UI yield: Small timeout allowing UI to process events

Call this in inner training loops (per batch) for responsive UI.

Checkpoints should be called frequently (every batch or every N batches) for responsive UI. They have minimal overhead when pause/stop not active.

Returns

Promise<void>– Promise that resolves when checkpoint completes, or throws if stopped/reload

Examples

// In your training loop
async function train() {
  for (let epoch = 0; epoch < 100; epoch++) {
    for (const batch of data) {
      // Checkpoint allows UI control
      try {
        await spark.checkpoint();
      } catch (err) {
        if (err instanceof SparkStopError) {
          console.log('Training stopped by user');
          return; // Exit cleanly
        }
        throw err; // Reload errors are handled by spark
      }

      // Training step
      const output = model(batch.x);
      const loss = criterion(output, batch.y);
      // ...
    }
  }
}

See Also

  • createController - Get a controller to check status or manually pause/resume
Previous
useSpark
Next
clearAllPersisted