torch.DeviceConfig
export interface DeviceConfig<Name extends string = string> {
/**
* The device name.
* Should be unique and descriptive: 'webgl', 'wasm', 'metal', etc.
*/
readonly name: Name;
/**
* Initialize the device.
* Called when the device is first used.
*
* @returns Promise resolving to device context
* @throws If device initialization fails
*/
initialize(): Promise<DeviceContext>;
/**
* Device capabilities.
* Used for operation selection and validation.
*/
readonly capabilities: DeviceCapabilities;
/**
* Create a buffer on this device.
*
* @param size - Buffer size in bytes
* @param usage - Buffer usage flags
* @returns New device buffer
*/
createBuffer(size: number, usage: BufferUsage | readonly BufferUsage[]): DeviceBuffer;
/**
* Dispatch a compute operation.
*
* @param config - Dispatch configuration
*/
dispatch(config: DispatchConfig): void;
/**
* Synchronize device operations.
* Ensures all pending operations complete.
*
* @returns Promise that resolves when sync is complete
*/
sync(): Promise<void>;
/**
* Copy data from CPU to device buffer.
*
* @param buffer - Target buffer
* @param data - Source data
* @param offset - Offset in buffer (default: 0)
*/
writeBuffer?(buffer: DeviceBuffer, data: ArrayBufferView, offset?: number): void;
/**
* Copy data from device buffer to CPU.
*
* @param buffer - Source buffer
* @param size - Number of bytes to read
* @param offset - Offset in buffer (default: 0)
* @returns Promise resolving to data
*/
readBuffer?(buffer: DeviceBuffer, size: number, offset?: number): Promise<ArrayBuffer>;
}Nameextends string- readonly
name(Name) - – The device name. Should be unique and descriptive: 'webgl', 'wasm', 'metal', etc.
initialize(() => Promise<DeviceContext>)- – Initialize the device. Called when the device is first used.
- readonly
capabilities(DeviceCapabilities) - – Device capabilities. Used for operation selection and validation.
createBuffer((size: number, usage: BufferUsage | readonly BufferUsage[]) => DeviceBuffer)- – Create a buffer on this device.
dispatch((config: DispatchConfig) => void)- – Dispatch a compute operation.
sync(() => Promise<void>)- – Synchronize device operations. Ensures all pending operations complete.
writeBuffer((buffer: DeviceBuffer, data: ArrayBufferView, offset?: number) => void)optional- – Copy data from CPU to device buffer.
readBuffer((buffer: DeviceBuffer, size: number, offset?: number) => Promise<ArrayBuffer>)optional- – Copy data from device buffer to CPU.
Configuration for registering a custom device.
Examples
const webglConfig: DeviceConfig<'webgl'> = {
name: 'webgl',
initialize: async () => {
const canvas = document.createElement('canvas');
const gl = canvas.getContext('webgl2');
return { name: 'webgl', ready: true, context: gl, destroy: () => {} };
},
capabilities: {
compute: false, // WebGL doesn't have compute shaders
storage: true,
maxBufferSize: 256 * 1024 * 1024,
maxWorkgroupSize: [0, 0, 0],
maxWorkgroupsPerDimension: 0,
supportedDTypes: ['float32'],
},
createBuffer: (size, usage) => { ... },
dispatch: (config) => { ... },
sync: async () => { ... },
};