Skip to content

🎥 Make videos programmatically with React

2.1 KB
Raw Blame
import type * as OnnxRuntimeNode from 'onnxruntime-node';
import {importNodeModule} from './import-node-module';

// Single-element ONNX Mul graphs (Y = X * X), opset 13. The fp16 graph casts
// float32 inputs to float16 for multiplication, then back for the Node binding.
const webGpuProbeModel = new Uint8Array([
	0x08, 0x08, 0x3a, 0x40, 0x0a, 0x0e, 0x0a, 0x01, 0x58, 0x0a, 0x01, 0x58, 0x12,
	0x01, 0x59, 0x22, 0x03, 0x4d, 0x75, 0x6c, 0x12, 0x0c, 0x77, 0x65, 0x62, 0x67,
	0x70, 0x75, 0x2d, 0x70, 0x72, 0x6f, 0x62, 0x65, 0x5a, 0x0f, 0x0a, 0x01, 0x58,
	0x12, 0x0a, 0x0a, 0x08, 0x08, 0x01, 0x12, 0x04, 0x0a, 0x02, 0x08, 0x01, 0x62,
	0x0f, 0x0a, 0x01, 0x59, 0x12, 0x0a, 0x0a, 0x08, 0x08, 0x01, 0x12, 0x04, 0x0a,
	0x02, 0x08, 0x01, 0x42, 0x02, 0x10, 0x0d,
]);

const webGpuFp16ProbeModel = new Uint8Array([
	0x08, 0x08, 0x3a, 0x72, 0x0a, 0x17, 0x0a, 0x01, 0x58, 0x12, 0x01, 0x48, 0x22,
	0x04, 0x43, 0x61, 0x73, 0x74, 0x2a, 0x09, 0x0a, 0x02, 0x74, 0x6f, 0x18, 0x0a,
	0xa0, 0x01, 0x02, 0x0a, 0x0e, 0x0a, 0x01, 0x48, 0x0a, 0x01, 0x48, 0x12, 0x01,
	0x53, 0x22, 0x03, 0x4d, 0x75, 0x6c, 0x0a, 0x17, 0x0a, 0x01, 0x53, 0x12, 0x01,
	0x59, 0x22, 0x04, 0x43, 0x61, 0x73, 0x74, 0x2a, 0x09, 0x0a, 0x02, 0x74, 0x6f,
	0x18, 0x01, 0xa0, 0x01, 0x02, 0x12, 0x0c, 0x77, 0x65, 0x62, 0x67, 0x70, 0x75,
	0x2d, 0x70, 0x72, 0x6f, 0x62, 0x65, 0x5a, 0x0f, 0x0a, 0x01, 0x58, 0x12, 0x0a,
	0x0a, 0x08, 0x08, 0x01, 0x12, 0x04, 0x0a, 0x02, 0x08, 0x01, 0x62, 0x0f, 0x0a,
	0x01, 0x59, 0x12, 0x0a, 0x0a, 0x08, 0x08, 0x01, 0x12, 0x04, 0x0a, 0x02, 0x08,
	0x01, 0x42, 0x02, 0x10, 0x0d,
]);

export const probeNodeWebGpu = async ({
	requiresShaderF16,
}: {
	requiresShaderF16: boolean;
}): Promise<void> => {
	const {InferenceSession, Tensor} =
		await importNodeModule<typeof OnnxRuntimeNode>('onnxruntime-node');
	const session = await InferenceSession.create(
		requiresShaderF16 ? webGpuFp16ProbeModel : webGpuProbeModel,
		{
			executionProviders: ['webgpu'],
			extra: {session: {disable_cpu_ep_fallback: '1'}},
		},
	);
	try {
		await session.run({X: new Tensor('float32', new Float32Array([1]), [1])});
	} finally {
		await session.release();
	}
};