WebGPU 新功能's (Chrome 約 120)

François Beaufort
François Beaufort

支援 WGSL 中的 16 位元浮點值

在 WGSL 中,f16 類型是 IEEE-754 binary16 (半精度) 格式的 16 位元浮點值。換句話說,它使用 16 位元來表示浮點數,而不是傳統單精度浮點 (f32) 使用 32 位元。這種小規模可以大幅改善效能,特別是在處理大量資料時。

相較之下,在 Apple M1 Pro 裝置上,在 WebLLM 即時通訊示範中採用 f16Llama2 7B 模型,幾乎比 f32 實作項目快上許多,預填速度加快 28%,解碼速度則提升 41%,如以下螢幕截圖所示。

採用 f32 和 f16 Llama2 7B 模型的 WebLLM 即時通訊示範螢幕截圖。
使用 f32 (左側) 和 f16 (右側) Llama2 7B 模型的網路大型語言模型即時通訊示範。

並非所有 GPU 都支援 16 位元浮點值。當 GPUAdapter 提供 "shader-f16" 功能時,您可以使用這項功能要求 GPUDevice,並建立利用半精度浮點類型 f16 的 WGSL 著色器模組。只有在啟用具有 enable f16;f16 WGSL 擴充功能時,這個類型才能用於 WGSL 著色器模組。否則 createShaderModule() 會產生驗證錯誤。請參閱以下的基本範例和問題 dawn:1510

const adapter = await navigator.gpu.requestAdapter();
if (!adapter.features.has("shader-f16")) {
  throw new Error("16-bit floating-point value support is not available");
}
// Explicitly request 16-bit floating-point value support.
const device = await adapter.requestDevice({
  requiredFeatures: ["shader-f16"],
});

const code = `
  enable f16;

  @compute @workgroup_size(1)
  fn main() {
    const c : vec3h = vec3<f16>(1.0h, 2.0h, 3.0h);
  }
`;

const shaderModule = device.createShaderModule({ code });
// Create a compute pipeline with this shader module
// and run the shader on the GPU...

您可以根據 "shader-f16" 功能支援,使用 alias 在 WGSL 著色器模組程式碼中同時支援 f16f32 類型,如以下程式碼片段所示。

const adapter = await navigator.gpu.requestAdapter();
const hasShaderF16 = adapter.features.has("shader-f16");

const device = await adapter.requestDevice({
  requiredFeatures: hasShaderF16 ? ["shader-f16"] : [],
});

const header = hasShaderF16
  ? `enable f16;
     alias min16float = f16;`
  : `alias min16float = f32;`;

const code = `
  ${header}

  @compute @workgroup_size(1)
  fn main() {
    const c = vec3<min16float>(1.0, 2.0, 3.0);
  }
`;

挑戰極限

根據預設,針對所有色彩附件,保存單一樣本 (像素或子像素) 的轉譯管道輸出資料所需的位元組數上限為 32 個位元組。現在,您可以使用 maxColorAttachmentBytesPerSample 限制,最多要求 64 個。請參考以下範例和問題 dawn:2036

const adapter = await navigator.gpu.requestAdapter();

if (adapter.limits.maxColorAttachmentBytesPerSample < 64) {
  // When the desired limit isn't supported, take action to either fall back to
  // a code path that does not require the higher limit or notify the user that
  // their device does not meet minimum requirements.
}

// Request highest limit of max color attachments bytes per sample.
const device = await adapter.requestDevice({
  requiredLimits: { maxColorAttachmentBytesPerSample: 64 },
});

所有平台上用於跨階段通訊的 maxInterStageShaderVariablesmaxInterStageShaderComponents 限制都已提高。詳情請見問題 dawn:1448

在每個著色器階段中,管道版面配置的繫結群組版面配置項目數量上限為 8 個。現在,您可以使用 maxStorageBuffersPerShaderStage 限制,最多要求 10 個。請參閱問題 dawn:2159

已新增 maxBindGroupsPlusVertexBuffers 的使用限制。此資料包含同時使用的繫結群組和頂點緩衝區運算單元的數量上限,系統會計算低於最高索引的任何空版位。預設值為 24。請參閱問題 dawn:1849

深度模板狀態變更

為改善開發人員體驗,您不再需要使用深度模板狀態 depthWriteEnableddepthCompare 屬性:只有深度格式才需要 depthWriteEnabled,而且如果完全不使用深度格式,則不需要 depthCompare。請參閱問題 dawn:2132

轉接器資訊更新

現在,當使用者啟用「WebGPU 開發人員功能」時,在呼叫 requestAdapterInfo() 時,才會顯示非標準的 typebackend 轉接器資訊屬性flag (chrome://flags/#enable-webgpu-developer-features)。type 可以是「獨立 GPU」、「整合 GPU」、「CPU」或「不明」。backend 為「WebGPU」、「D3D11」、「D3D12」、「metal」、「vulkan」、「openGL」、「openGLES」或「null」。請參閱問題 dawn:2112問題 dawn:2107

https://webgpureport.org 的螢幕截圖,顯示後端和輸入轉接程式資訊。
https://webgpureport.org 上顯示的轉接程式資訊後端和類型。

已移除 requestAdapterInfo() 中的選用 unmaskHints 清單參數。請參閱問題 dawn:1427

時間戳記查詢量化

時間戳記查詢可讓應用程式以奈秒的精確度,測量 GPU 指令的執行時間。然而,WebGPU 規格基於「時間攻擊」的考量,使得時間戳記查詢變為選用。Chrome 團隊認為,將時間戳記查詢量化至 100 微秒,能在精確度和安全性之間取得良好平衡。請參閱問題 dawn:1800

在 Chrome 中,使用者可以啟用「WebGPU 開發人員功能」來停用時間戳記量化功能flag (位於 chrome://flags/#enable-webgpu-developer-features)。請注意,單獨使用此旗標並不會啟用 "timestamp-query" 功能。實作功能仍為實驗階段,因此需要「不安全的 WebGPU 支援」標記在 chrome://flags/#enable-unsafe-webgpu 中。

在 Dawn 中,新增名為「timestamp_quantization」的新裝置切換鈕已加入,且預設為啟用。下列程式碼片段說明如何允許「timestamp-query」實驗性功能,在要求使用裝置時,沒有時間戳記量化。

wgpu::DawnTogglesDescriptor deviceTogglesDesc = {};

const char* allowUnsafeApisToggle = "allow_unsafe_apis";
deviceTogglesDesc.enabledToggles = &allowUnsafeApisToggle;
deviceTogglesDesc.enabledToggleCount = 1;

const char* timestampQuantizationToggle = "timestamp_quantization";
deviceTogglesDesc.disabledToggles = &timestampQuantizationToggle;
deviceTogglesDesc.disabledToggleCount = 1;

wgpu::DeviceDescriptor desc = {.nextInChain = &deviceTogglesDesc};

// Request a device with no timestamp quantization.
myAdapter.RequestDevice(&desc, myCallback, myUserData);

春季清潔功能

實驗性的「timestamp-query-in-passes」功能已重新命名為「chromium-experimental-timestamp-query-inside-passes」向開發人員清楚說明,這項功能目前仍在實驗階段,且僅適用於以 Chromium 為基礎的瀏覽器。請參閱問題 dawn:1193

實驗性的「pipeline-statistics-query」功能 (僅部分實作) 目前仍處於開發階段,因此現已移除。請參閱問題 chromium:1177506

這只涵蓋部分重點功能。請參閱完整的修訂版本清單

WebGPU 新功能

WebGPU 最新消息系列中所有包含的清單。

Chrome 127

Chrome 126

Chrome 125

Chrome 124

Chrome 123

Chrome 122

Chrome 121

Chrome 120

Chrome 119

Chrome 118

Chrome 117

Chrome 116

Chrome 115

Chrome 114

Chrome 113