-
Notifications
You must be signed in to change notification settings - Fork 357
Add packed integer dot product sample #572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2e6bba3
Add packed integer dot product sample
lyonsno 4b7624b
Compile the packed dot product reference for Node 20 tests
lyonsno bd3a0f1
Simplify packed integer dot product sample for learners
lyonsno 0ca514e
remove gui, tweak input structure
kainino0x 0495650
tweaks
kainino0x 57577d6
add a gui with the actual lhs/rhs values to make it interactive
kainino0x File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| <title>webgpu-samples: packedIntegerDotProduct</title> | ||
| <style> | ||
| :root { | ||
| color-scheme: light dark; | ||
| } | ||
| body { | ||
| width: calc(100vw - 280px); | ||
| height: 400px; | ||
| overflow-y: hidden; | ||
| } | ||
| pre { | ||
| white-space: pre-wrap; | ||
| height: 370px; | ||
| } | ||
| </style> | ||
| <script defer src="main.js" type="module"></script> | ||
| <script defer type="module" src="../../js/iframe-helper.js"></script> | ||
| </head> | ||
| <body> | ||
| <pre id="output">Computing packed integer dot products…</pre> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { GUI } from 'dat.gui'; | ||
| import packedWGSL from './packed.wgsl'; | ||
| import { quitIfWebGPUNotAvailableOrMissingFeatures } from '../util'; | ||
|
|
||
| type vec4i = readonly [number, number, number, number]; | ||
| // Pack four signed 8-bit components into a u32, low byte first. | ||
| function pack4xI8([x, y, z, w]: vec4i): number { | ||
| // `&` operator applies sign extension to i32 before operating. | ||
| // `>>> 0` converts the final i32 to u32. | ||
| /*prettier-ignore*/ | ||
| return ((x & 0xff) | | ||
| ((y & 0xff) << 8) | | ||
| ((z & 0xff) << 16) | | ||
| ((w & 0xff) << 24)) >>> 0; | ||
| } | ||
|
|
||
| const outputElement = document.querySelector('#output') as HTMLElement; | ||
| if ( | ||
| !navigator.gpu?.wgslLanguageFeatures.has('packed_4x8_integer_dot_product') | ||
| ) { | ||
| result.textContent = | ||
| "This sample requires the WGSL language feature 'packed_4x8_integer_dot_product'."; | ||
| } else { | ||
| const adapter = await navigator.gpu.requestAdapter({ | ||
| featureLevel: 'compatibility', | ||
| }); | ||
| const device = await adapter?.requestDevice(); | ||
| quitIfWebGPUNotAvailableOrMissingFeatures(adapter, device); | ||
|
|
||
| const kInputSize = 2 * Uint32Array.BYTES_PER_ELEMENT; | ||
| const inputBuffer = device.createBuffer({ | ||
| size: kInputSize, | ||
| usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.STORAGE, | ||
| }); | ||
|
|
||
| const kOutputSize = Int32Array.BYTES_PER_ELEMENT; | ||
| const outputBuffer = device.createBuffer({ | ||
| size: kOutputSize, | ||
| usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC, | ||
| }); | ||
| const readbackBuffer = device.createBuffer({ | ||
| size: kOutputSize, | ||
| usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ, | ||
| }); | ||
|
|
||
| const pipeline = await device.createComputePipelineAsync({ | ||
| layout: 'auto', | ||
| compute: { module: device.createShaderModule({ code: packedWGSL }) }, | ||
| }); | ||
| const bindGroup = device.createBindGroup({ | ||
| layout: pipeline.getBindGroupLayout(0), | ||
| entries: [ | ||
| { binding: 0, resource: { buffer: inputBuffer } }, | ||
| { binding: 1, resource: { buffer: outputBuffer } }, | ||
| ], | ||
| }); | ||
|
|
||
| async function updateResult() { | ||
| // If an update is still in progress just wait until it's done. | ||
| if (readbackBuffer.mapState !== 'unmapped') { | ||
| setTimeout(updateResult, 0); | ||
| return; | ||
| } | ||
|
|
||
| const lhs = [settings.lhs0, settings.lhs1, settings.lhs2, settings.lhs3]; | ||
| const rhs = [settings.rhs0, settings.rhs1, settings.rhs2, settings.rhs3]; | ||
|
|
||
| device.queue.writeBuffer( | ||
| inputBuffer, | ||
| 0, | ||
| new Uint32Array([lhs, rhs].map(pack4xI8)) | ||
| ); | ||
| const encoder = device.createCommandEncoder(); | ||
| const pass = encoder.beginComputePass(); | ||
| pass.setPipeline(pipeline); | ||
| pass.setBindGroup(0, bindGroup); | ||
| pass.dispatchWorkgroups(1); | ||
| pass.end(); | ||
| encoder.copyBufferToBuffer(outputBuffer, 0, readbackBuffer, 0, kOutputSize); | ||
| device.queue.submit([encoder.finish()]); | ||
|
|
||
| await readbackBuffer.mapAsync(GPUMapMode.READ); | ||
| const result = new Int32Array(readbackBuffer.getMappedRange())[0]; | ||
|
|
||
| // Result should be the same in JS, show that for comparison. | ||
| const expected = | ||
| lhs[0] * rhs[0] + lhs[1] * rhs[1] + lhs[2] * rhs[2] + lhs[3] * rhs[3]; | ||
|
|
||
| const lhsStr = `[${lhs | ||
| .map((x) => x.toString().padStart(4)) | ||
| .join(', ')}] (0x${pack4xI8(lhs).toString(16).padStart(8, '0')})`; | ||
| const rhsStr = `[${rhs | ||
| .map((x) => x.toString().padStart(4)) | ||
| .join(', ')}] (0x${pack4xI8(rhs).toString(16).padStart(8, '0')})`; | ||
| const outStr = result.toString().padStart(6); | ||
| const expStr = expected.toString().padStart(6); | ||
| outputElement.textContent = ` | ||
|
|
||
| WGSL dot4I8Packed of ${lhsStr} | ||
| by ${rhsStr} gave ${outStr} (JS gave ${expStr})`; | ||
|
|
||
| readbackBuffer.unmap(); | ||
| } | ||
|
|
||
| const settings = { | ||
| lhs0: 1, | ||
| lhs1: -2, | ||
| lhs2: 3, | ||
| lhs3: -4, | ||
| rhs0: -5, | ||
| rhs1: 6, | ||
| rhs2: -7, | ||
| rhs3: 8, | ||
| }; | ||
| const gui = new GUI(); | ||
| gui.add(settings, 'lhs0', -127, 128, 1).onChange(updateResult); | ||
| gui.add(settings, 'lhs1', -127, 128, 1).onChange(updateResult); | ||
| gui.add(settings, 'lhs2', -127, 128, 1).onChange(updateResult); | ||
| gui.add(settings, 'lhs3', -127, 128, 1).onChange(updateResult); | ||
| gui.add(settings, 'rhs0', -127, 128, 1).onChange(updateResult); | ||
| gui.add(settings, 'rhs1', -127, 128, 1).onChange(updateResult); | ||
| gui.add(settings, 'rhs2', -127, 128, 1).onChange(updateResult); | ||
| gui.add(settings, 'rhs3', -127, 128, 1).onChange(updateResult); | ||
| updateResult(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export default { | ||
| name: 'Packed Integer Dot Product', | ||
| description: | ||
| 'Packs four signed 8-bit integers into each u32, computes their dot product with dot4I8Packed, and reads back the results.', | ||
| filename: __DIRNAME__, | ||
| sources: [{ path: 'main.ts' }, { path: 'packed.wgsl' }], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| requires packed_4x8_integer_dot_product; | ||
|
|
||
| struct Input { lhs: u32, rhs: u32 } | ||
|
|
||
| @group(0) @binding(0) var<storage, read> input: Input; | ||
| @group(0) @binding(1) var<storage, read_write> output: i32; | ||
|
|
||
| @compute @workgroup_size(1) | ||
| fn main() { | ||
| output = dot4I8Packed(input.lhs, input.rhs); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.