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. createController

spark.worker.createController

function createController(): SparkController

Create a controller for pause/resume/stop and checkpointing.

The controller enables UI-driven flow control during training or long-running tasks:

  • Pause: Suspend execution until resume is clicked
  • Resume: Continue from pause point
  • Stop: Terminate execution immediately
  • Checkpoint: Cooperative yield point that respects pause/stop/reload

The controller should be exposed to the UI and called from async functions that perform long-running work. Checkpoints are lightweight and allow UI interactions to take effect without explicit polling.

Key features:

  • Non-blocking checkpoints (only pause/stop when requested)
  • Hot code reload support (new code loaded while keeping state)
  • Stateful - pause/resume/stop state persists across checkpoints
  • Query status anytime (useful for progress bars, status displays)
Each call to createController() returns the same singleton controller. Multiple calls will not create independent controllers.

Returns

SparkController– SparkController instance with status, pause, resume, stop, and checkpoint methods

Examples

function torch() {
  const ctrl = spark.createController();
  const model = spark.persist('model', () => nn.Sequential(...));

  async function train(epochs = 100) {
    for (let e = 0; e < epochs; e++) {
      for (const batch of data) {
        // Checkpoint allows pause/stop from UI
        await ctrl.checkpoint();

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

  // Expose to UI for control
  spark.expose({ train, ctrl });
}

See Also

  • checkpoint - Add pause/stop/reload checkpoints in async code
Previous
clearPersisted
Next
expose