WebGPU 新功能's (Chrome 約 120)

François Beaufort
François Beaufort

支援 WGSL 中的 16 位元浮點值

在 WGSL 中,f16 類型是 IEEE-754 binary16 (半精度) 格式的 16 位元浮點值。這表示其使用 16 位元表示浮點數,而不是使用 32 位元來表示傳統單一精度浮點 (f32)。這種較小尺寸可能會導致效能大幅提升,尤其是在處理大量資料時。

相較之下,在 Apple M1 Pro 裝置上,在 WebLLM 即時通訊示範中採用 f16 Llama2 7B 模型f16 實作速度,遠比實作 f32 更快,且預填速度提升 28%,解碼速度也提升 41%,如以下螢幕截圖所示。

使用 f32 和 f16 Llama2 7B 模型的 WebLLM 即時通訊示範螢幕截圖。
透過 f32 (左側) 和 f16 (右側) Llama2 7B 模型進行的 WebLLM 即時通訊示範。

並非所有 GPU 都支援 16 位元浮點值。當 GPUAdapter 提供 "shader-f16" 功能時,您現在可以透過此功能要求 GPUDevice,並建立利用半精度浮點類型 f16 的 WGSL 著色器模組。這個類型只有在您搭配 enable f16; 啟用 f16 WGSL 擴充功能時才能使用,才能在 WGSL 著色器模組中使用。否則 createShaderModule() 會產生驗證錯誤。請參閱下列基本範例和 Issue 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...

您可以使用 alias 支援 WGSL 著色器模組程式碼中的 f16f32 類型,視 "shader-f16" 功能支援而定,如以下程式碼片段所示。

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 個節點。請參閱以下範例和 Issue 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 限制已提高。詳情請參閱問題發生時間:1448

在每個著色器階段中,管道版面配置中的繫結群組版面配置項目數量上限為 8 個,儲存空間緩衝區預設為 8。現在您可以使用 maxStorageBuffersPerShaderStage 限制,最多可以要求 10 個。請參閱問題發生時間:2159

已新增 maxBindGroupsPlusVertexBuffers 上限。這包含同時使用的繫結群組和頂點緩衝區運算單元數量上限,並計算低於最高索引值的所有空白運算單元。預設值為 24。請參閱問題發生時間:1849

深度模板狀態變更

為了提升開發人員體驗,我們不再需要使用深度模板狀態 depthWriteEnableddepthCompare 屬性:只有深度格式才需要使用 depthWriteEnabled;如果完全未使用深度格式,則不需要 depthCompare。請參閱「問題日出:2132」。

轉接程式資訊更新

當使用者在 chrome://flags/#enable-webgpu-developer-features 啟用「WebGPU 開發人員功能」旗標時,現在可以使用非標準 typebackend 轉接程式資訊屬性呼叫 requestAdapterInfo()type 可以是「離散的 GPU」、「整合式 GPU」、「CPU」或「不明」。backend 可以是「WebGPU」、「D3D11」、「D3D12」、「metal」、「vulkan」、「openGL」、「openGLES」或「null」。請參閱「問題發生時間:2112 年」和「日出時間:2107」。

https://webgpureport.org 的螢幕截圖,其中顯示後端和輸入轉接器資訊。
位於 https://webgpureport.org 的轉接器資訊後端和類型。

移除 requestAdapterInfo() 中的選用 unmaskHints 清單參數。請參閱問題發生時間:1427

時間戳記查詢量化

時間戳記查詢可讓應用程式以奈秒精確度,測量 GPU 指令的執行時間。然而,由於攻擊攻擊問題,WebGPU 規格使時間戳記查詢為選用項目。Chrome 團隊認為,將解析度降低至 100 毫秒,就能夠將時間戳記查詢量化,在精確度和安全性之間達到良好平衡。請參閱問題發生時間:1800

在 Chrome 中,使用者可以前往 chrome://flags/#enable-webgpu-developer-features 啟用「WebGPU 開發人員功能」標記,藉此停用時間戳記量化功能。請注意,單獨使用這個標記並不會啟用 "timestamp-query" 功能。它的實作仍屬於實驗性質,因此必須使用 chrome://flags/#enable-unsafe-webgpu 的「Unsafe WebGPU Support」標記。

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

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-inside-passes」實驗性功能已重新命名為「chromium-experimental-timestamp-query-inside-passes」,讓開發人員清楚瞭解這項功能仍在實驗階段,且目前只能在以 Chromium 為基礎的瀏覽器中使用。請參閱問題發生時間:1193

先前只導入部分階段的實驗性「管道統計資料-查詢」功能,目前已經還在開發中,因此已經移除。請參閱問題 chromium:1177506

這僅涵蓋部分重點功能。查看完整的修訂版本清單

WebGPU 的新功能

WebGPU 新功能」系列中已說明的所有功能清單。

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