1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
// Licensed under the MIT License.
4
import { TRACE_FUNC_BEGIN, TRACE_FUNC_END } from 'onnxruntime-common';
6
import { WebGpuBackend } from '../backend-webgpu';
7
import { LOG_DEBUG } from '../log';
9
import { createShaderHelper } from './ops/common';
10
import { Artifact, GpuData, ProgramInfo } from './types';
13
* ProgramManager is the main class behind running computations
14
* It builds ProgramInfo's into Artifacts
15
* It compiles given ProgramInfo's into WebGL Prorams (cached as Artifacts)
16
* Uses the artifact to run the computation by calling Draw on
17
* the WebGL drawing buffer
18
* ProgramManager automatically maps (binds) input variables to their
19
* corresponding Location's in the binary program
21
export class ProgramManager {
22
repo: Map<unknown, Artifact>; // this should be per-session object
23
attributesBound: boolean;
25
constructor(private backend: WebGpuBackend) {
26
this.repo = new Map();
27
this.attributesBound = false;
29
getArtifact(key: unknown): Artifact | undefined {
30
return this.repo.get(key);
32
setArtifact(key: unknown, artifact: Artifact): void {
33
this.repo.set(key, artifact);
36
buildArtifact: Artifact,
39
dispatchGroup: [number, number, number],
40
uniformBufferBinding: GPUBindingResource | undefined,
42
TRACE_FUNC_BEGIN(buildArtifact.programInfo.name);
43
const device = this.backend.device;
44
const computePassEncoder = this.backend.getComputePassEncoder();
45
this.backend.writeTimestamp(this.backend.pendingDispatchNumber * 2);
47
for (const input of inputs) {
48
entries.push({ binding: entries.length, resource: { buffer: input.buffer } });
50
for (const output of outputs) {
51
entries.push({ binding: entries.length, resource: { buffer: output.buffer } });
53
if (uniformBufferBinding) {
54
entries.push({ binding: entries.length, resource: uniformBufferBinding });
56
const bindGroup = device.createBindGroup({
57
layout: buildArtifact.computePipeline.getBindGroupLayout(0),
59
label: buildArtifact.programInfo.name,
62
if (this.backend.sessionStatus === 'capturing') {
64
kernelId: this.backend.currentKernelId!,
65
computePipeline: buildArtifact.computePipeline,
69
const sessionCommandList = this.backend.capturedCommandList.get(this.backend.currentSessionId!);
70
sessionCommandList!.push(commandInfo);
73
computePassEncoder.setPipeline(buildArtifact.computePipeline);
74
computePassEncoder.setBindGroup(0, bindGroup);
75
computePassEncoder.dispatchWorkgroups(...dispatchGroup);
76
this.backend.writeTimestamp(this.backend.pendingDispatchNumber * 2 + 1);
77
this.backend.pendingDispatchNumber++;
80
this.backend.pendingDispatchNumber >= this.backend.maxDispatchNumber ||
81
this.backend.queryType === 'at-passes'
83
this.backend.endComputePass();
85
if (this.backend.pendingDispatchNumber >= this.backend.maxDispatchNumber) {
88
TRACE_FUNC_END(buildArtifact.programInfo.name);
91
// this.repo.forEach(a => this.glContext.deleteProgram(a.program));
93
build(programInfo: ProgramInfo, normalizedDispatchGroupSize: [number, number, number]): Artifact {
94
TRACE_FUNC_BEGIN(programInfo.name);
95
const device = this.backend.device;
96
const extensions: string[] = [];
97
if (device.features.has('shader-f16')) {
98
extensions.push('enable f16;');
100
const shaderHelper = createShaderHelper(normalizedDispatchGroupSize, this.backend.device.limits);
101
const userCode = programInfo.getShaderSource(shaderHelper);
102
const code = `${extensions.join('\n')}\n${shaderHelper.additionalImplementations}\n${userCode}`;
103
const shaderModule = device.createShaderModule({ code, label: programInfo.name });
104
LOG_DEBUG('verbose', () => `[WebGPU] ${programInfo.name} shader code: ${code}`);
106
const computePipeline = device.createComputePipeline({
107
compute: { module: shaderModule, entryPoint: 'main' },
109
label: programInfo.name,
112
TRACE_FUNC_END(programInfo.name);
113
return { programInfo, computePipeline, uniformVariablesInfo: shaderHelper.variablesInfo };
116
normalizeDispatchGroupSize(
117
dispatchGroup: ReturnType<ProgramInfo['getRunData']>['dispatchGroup'],
118
): [number, number, number] {
119
const x = typeof dispatchGroup === 'number' ? dispatchGroup : dispatchGroup.x;
120
const y = typeof dispatchGroup === 'number' ? 1 : dispatchGroup.y || 1;
121
const z = typeof dispatchGroup === 'number' ? 1 : dispatchGroup.z || 1;
122
const limitPerDimension = this.backend.device.limits.maxComputeWorkgroupsPerDimension;
123
if (x <= limitPerDimension && y <= limitPerDimension && z <= limitPerDimension) {
126
const size = x * y * z;
127
let dispatchAverage = Math.ceil(Math.sqrt(size));
128
if (dispatchAverage > limitPerDimension) {
129
dispatchAverage = Math.ceil(Math.cbrt(size));
130
if (dispatchAverage > limitPerDimension) {
131
throw new Error('Total dispatch size exceeds WebGPU maximum.');
133
return [dispatchAverage, dispatchAverage, dispatchAverage];
135
return [dispatchAverage, dispatchAverage, 1];