torch.is_webgpu_available
function is_webgpu_available(): boolean | nullChecks if WebGPU acceleration is available in the current environment.
Returns the result of WebGPU detection, indicating whether GPU acceleration is usable.
You must call torch.init() before checking this, as detection happens during initialization.
Return Values:
true: WebGPU is available and initialized successfullyfalse: WebGPU is not available (browser doesn't support it, or init failed)null: Not yet checked (torch.init() hasn't been called)
Typical Usage:
await torch.init(); // Perform detection
if (torch.is_webgpu_available()) {
// Use GPU-accelerated operations
} else {
// Fall back to CPU with smaller batches
}Browser Support:
- Supported: Chrome 113+, Firefox (experimental flag), Safari 18+
- Not supported: Safari <18, older Chrome, Edge <113, Internet Explorer
- Cached result: Returns cached value from initialization
- null until init: Returns null until torch.init() completes
- Auto-fallback: CPU always available as fallback
- Initialize first: Always call torch.init() before checking
- Check after init: Value is null until torch.init() completes
- Not real-time: Doesn't re-detect if browser support changes
Returns
boolean | null– true if WebGPU available, false if not, null if not yet checkedExamples
// Check availability after initialization
await torch.init();
const gpuAvailable = torch.is_webgpu_available();
if (gpuAvailable === null) {
console.warn('torch.init() has not been called yet');
} else if (gpuAvailable === true) {
console.log('✓ WebGPU is available - using GPU acceleration');
torch.set_default_device('webgpu');
} else {
console.log('✗ WebGPU not available - using CPU fallback');
torch.set_default_device('cpu');
}
// Configure batch size based on device
const batchSize = torch.is_webgpu_available() ? 128 : 16;
for (let i = 0; i < data.length; i += batchSize) {
const batch = data.slice(i, i + batchSize);
processInference(batch);
}
// Fallback logic
const device = torch.is_webgpu_available() ? 'webgpu' : 'cpu';
const tensor = torch.randn([1000, 1000], { device });
// Before initialization
console.log(torch.is_webgpu_available()); // null
await torch.init();
console.log(torch.is_webgpu_available()); // true or falseSee Also
- [PyTorch N/A (torch.js specific)](https://pytorch.org/docs/stable/generated/N/A .html)
- torch.init - Initialize WebGPU and detect availability
- get_default_device - Get current default device
- is_cpu_only_mode - Check if in CPU-only fallback