torch.DeviceInput
export type DeviceInput = DeviceType | Device | string;Values
DeviceTypeDevicestring
Device input type - can be string, Device object, or device type.
Flexible device specification that accepts multiple formats for convenience. Internally normalizes to the canonical device type before use.
Accepted Formats:
- String: 'cpu' or 'webgpu' (simple, most common)
- Device object: new Device('webgpu', 0) (explicit control)
- DeviceType: Type union of 'cpu' or 'webgpu'
Examples of Valid Inputs:
- 'cpu'
- 'webgpu'
- 'webgpu:0'
- new Device('cpu')
- new Device('webgpu', 0)
Use Cases:
- Tensor creation:
torch.randn([3, 4], { device: 'cpu' }) - Device movement:
tensor.to('webgpu') - Device checking:
tensor.device === 'cpu' - Configuration: Store device spec as string for serialization
Examples
// String format (most common)
const x = torch.zeros(3, 4, { device: 'cpu' });
// Device object
const device = new Device('webgpu', 0);
const y = torch.ones(3, 4, { device });
// Coercion from string
const tensors = ['cpu', 'webgpu'].map(d =>
torch.randn([2, 3], { device: d as DeviceInput })
);
// Configuration storage
const config = { device: 'webgpu' as DeviceInput };
const tensor = torch.randn([10, 20], config);