spark.worker.persist
function persist<T>(key: string, initializer: () => T): TPersist state across hot code reloads in the worker.
When the torch function is hot reloaded (code updated without restarting),
normally all variables are recreated. Use persist() to keep expensive
objects like models and optimizers alive across reloads.
The initializer function is only called on first use. On subsequent reloads, the persisted value is returned directly, allowing model weights and optimizer state to survive code changes.
Perfect for:
- Large models that take time to initialize
- Training state and optimizer variables
- Cached computation results
- Long-lived resources
The persisted value is stored in the worker's memory and survives hot reloads, but will be reset if the page is fully reloaded or the worker is terminated.
Parameters
keystring- Unique identifier for this persisted state
initializer() => T- Function that creates the initial value (only runs once)
Returns
T– The persisted value (or newly created value on first call)Examples
// In torch function:
function torch() {
// These survive hot reloads!
const model = spark.persist('model', () => {
console.log('Creating model...'); // Only logs once
return nn.Sequential([
nn.Linear(784, 128),
nn.ReLU(),
nn.Linear(128, 10)
]);
});
const optimizer = spark.persist('optimizer', () => {
console.log('Creating optimizer...'); // Only logs once
return optim.Adam(model.parameters());
});
async function train() {
// model and optimizer persist across code reloads
// Change training code freely without losing progress
}
spark.expose({ train });
}See Also
- hasPersisted - Check if a value is already persisted
- clearPersisted - Clear a persisted value
- clearAllPersisted - Clear all persisted state