torch.autograd.set_multithreading_enabled
function set_multithreading_enabled(enabled: boolean): voidEnable or disable multithreading for autograd operations (no-op in JavaScript).
This function is provided for API compatibility with PyTorch but has no effect in torch.js since JavaScript's single-threaded execution model doesn't support traditional OS-level threading. Parallelism in torch.js is achieved through WebGPU compute shaders and Web Workers.
PyTorch context: In PyTorch (C++), autograd operations can use multiple CPU threads to parallelize gradient computation. This function controls that behavior.
torch.js implementation: torch.js achieves parallelism through:
- WebGPU compute shaders: GPU-based parallelism for tensor operations
- Web Workers: Background threads for non-blocking operations
- Async/await: Cooperative multitasking via event loop
The autograd engine cannot use traditional multithreading, so this setting is ignored.
When torch.js uses parallelism:
- GPU operations run in parallel via WebGPU compute shaders
- I/O operations use Web Workers to avoid blocking
- Async operations interleave via JavaScript's event loop
- No-op implementation: Call has no effect on torch.js behavior
- GPU parallelism still works: WebGPU compute shaders provide GPU parallelism
- Web Workers available: Use Web Workers for CPU-side parallelism if needed
- Async operations available: Use async/await for I/O parallelism
- Single-threaded JS: JavaScript's single-threaded nature is fundamental
- Cross-platform code: Safe to call for code that needs to work with PyTorch
- No CPU multithreading: torch.js cannot use multiple CPU threads for autograd
- Don't rely on this setting: Behavior is identical regardless of enabled value
- GPU ≠ CPU threads: GPU parallelism is not the same as CPU multithreading
- Compatibility only: Included only for API compatibility with PyTorch
Parameters
enabledboolean- True to enable (ignored in torch.js), false to disable (ignored)
Returns
void
Examples
// Safe to call for PyTorch compatibility, but has no effect
torch.autograd.set_multithreading_enabled(true);
// Autograd operations still run on single JS thread
// GPU parallelism comes from WebGPU compute shaders instead// Useful for cross-platform code
function setupAutograd() {
// Works in both PyTorch and torch.js
torch.autograd.set_multithreading_enabled(true);
// PyTorch: Uses multiple CPU threads
// torch.js: No effect, uses WebGPU instead
}// Don't rely on this for parallelism in torch.js
torch.autograd.set_multithreading_enabled(false);
// GPU computation still parallelizes via WebGPU
const x = torch.randn(1000, 1000);
const y = x.matmul(x); // GPU parallel, not CPU threadsSee Also
- PyTorch torch.autograd.set_multithreading_enabled()
- torch.autograd - Autograd module
- torch.webgpu - WebGPU module for GPU control