|
18 | 18 | import './flags_webgpu';
|
19 | 19 | import './register_all_kernels';
|
20 | 20 |
|
21 |
| -import {env, registerBackend} from '@tensorflow/tfjs-core'; |
| 21 | +import {device_util, env, registerBackend} from '@tensorflow/tfjs-core'; |
22 | 22 | import glslangInit from '@webgpu/glslang/dist/web-devel/glslang.onefile';
|
23 | 23 |
|
24 | 24 | import {WebGPUBackend} from './backend_webgpu';
|
25 | 25 | import * as webgpu from './webgpu';
|
26 |
| - |
27 |
| -registerBackend('webgpu', async () => { |
28 |
| - // Remove it once we figure out how to correctly read the tensor data before |
29 |
| - // the tensor is disposed in profiling mode. |
30 |
| - env().set('CHECK_COMPUTATION_FOR_ERRORS', false); |
31 |
| - |
32 |
| - const glslang = await glslangInit(); |
33 |
| - const gpuDescriptor: GPURequestAdapterOptions = { |
34 |
| - powerPreference: env().get('WEBGPU_USE_LOW_POWER_GPU') ? 'low-power' : |
35 |
| - 'high-performance' |
36 |
| - }; |
37 |
| - |
38 |
| - const adapter = await navigator.gpu.requestAdapter(gpuDescriptor); |
39 |
| - let deviceDescriptor: GPUDeviceDescriptor = {}; |
40 |
| - const supportTimeQuery = adapter.features.has('timestamp-query'); |
41 |
| - |
42 |
| - if (supportTimeQuery) { |
43 |
| - deviceDescriptor = { |
44 |
| - nonGuaranteedFeatures: ['timestamp-query' as const] |
| 26 | +import {isWebGPUSupported} from './webgpu_util'; |
| 27 | + |
| 28 | +if (device_util.isBrowser() && isWebGPUSupported()) { |
| 29 | + registerBackend('webgpu', async () => { |
| 30 | + // Remove it once we figure out how to correctly read the tensor data |
| 31 | + // before the tensor is disposed in profiling mode. |
| 32 | + env().set('CHECK_COMPUTATION_FOR_ERRORS', false); |
| 33 | + |
| 34 | + const glslang = await glslangInit(); |
| 35 | + const gpuDescriptor: GPURequestAdapterOptions = { |
| 36 | + powerPreference: env().get('WEBGPU_USE_LOW_POWER_GPU') ? |
| 37 | + 'low-power' : |
| 38 | + 'high-performance' |
45 | 39 | };
|
46 |
| - } else { |
47 |
| - console.warn( |
48 |
| - `This device doesn't support timestamp-query extension. ` + |
49 |
| - `Zero will shown for the kernel time when profiling mode is enabled. ` + |
50 |
| - `Using performance.now is not workable for webgpu since it doesn't ` + |
51 |
| - `support synchronously to read data from GPU.`); |
52 |
| - } |
53 |
| - const device: GPUDevice = await adapter.requestDevice(deviceDescriptor); |
54 |
| - return new WebGPUBackend(device, glslang, supportTimeQuery); |
55 |
| -}, 3 /*priority*/); |
| 40 | + |
| 41 | + const adapter = await navigator.gpu.requestAdapter(gpuDescriptor); |
| 42 | + let deviceDescriptor: GPUDeviceDescriptor = {}; |
| 43 | + const supportTimeQuery = adapter.features.has('timestamp-query'); |
| 44 | + |
| 45 | + if (supportTimeQuery) { |
| 46 | + deviceDescriptor = {nonGuaranteedFeatures: ['timestamp-query' as const ]}; |
| 47 | + } else { |
| 48 | + console.warn( |
| 49 | + `This device doesn't support timestamp-query extension. ` + |
| 50 | + `Zero will shown for the kernel time when profiling mode is` + |
| 51 | + `enabled. Using performance.now is not workable for webgpu since` + |
| 52 | + `it doesn't support synchronously to read data from GPU.`); |
| 53 | + } |
| 54 | + const device: GPUDevice = await adapter.requestDevice(deviceDescriptor); |
| 55 | + return new WebGPUBackend(device, glslang, supportTimeQuery); |
| 56 | + }, 3 /*priority*/); |
| 57 | +} |
56 | 58 |
|
57 | 59 | export {webgpu};
|
0 commit comments