mirror of
https://github.com/storytold/lightcraft.git
synced 2026-11-28 09:19:25 +01:00
Merge pull request #4811 from FredApps/fredapps/sam3-dx12-eight-keys
SAM 3: reduce DX12 attention pipeline startup time
This commit is contained in:
4 files changed
+22
-5
No files matched your search
@@ -59,10 +59,16 @@ const PATCH_K: usize = 3 * PATCH * PATCH;
|
||||
|
||||
/// Longest wait for one forward pass before the device is given up on.
|
||||
const TIMEOUT: Duration = Duration::from_secs(120);
|
||||
/// Workgroup memory the attention kernel needs: its key and value tiles (2 × [`ATTENTION_KEYS`] × 64 floats).
|
||||
/// Conservative workgroup memory for key and value tiles (2 × [`ATTENTION_KEYS`] × 64 floats).
|
||||
const ATTENTION_BYTES: u32 = 2 * ATTENTION_KEYS * HEAD_DIM as u32 * 4;
|
||||
/// Keys per step of the attention kernel.
|
||||
/// The maximum keys per step; also used to bound the required workgroup memory.
|
||||
const ATTENTION_KEYS: u32 = 32;
|
||||
|
||||
/// On DX12, creating the fully unrolled 32-key shader can be very slow on some drivers. Smaller
|
||||
/// tiles shorten initialization; the existing 32-key limits remain conservative for every backend.
|
||||
fn attention_keys(backend: wgpu::Backend) -> u32 {
|
||||
if backend == wgpu::Backend::Dx12 { 8 } else { ATTENTION_KEYS }
|
||||
}
|
||||
/// The matrix product's blocking.
|
||||
const TILE: Tile = Tile { rows: 4, cols: 4, bk: 16 };
|
||||
|
||||
@@ -255,7 +261,7 @@ fn make_device(backends: wgpu::Backends) -> Result<Dev, String> {
|
||||
let k = Kernels {
|
||||
matmul: Matmul { pipeline: pipe("sam3 matmul", &matmul_source(TILE, false), "main"), tile: TILE },
|
||||
matmul16: OnceLock::new(),
|
||||
attention: pipe("sam3 attention", &attention_source(ATTENTION_KEYS), "main"),
|
||||
attention: pipe("sam3 attention", &attention_source(attention_keys(info.backend)), "main"),
|
||||
layernorm: pipe("sam3 ops", ops, "layernorm"),
|
||||
rope: pipe("sam3 ops", ops, "rope"),
|
||||
im2col: pipe("sam3 ops", ops, "im2col"),
|
||||
|
||||
@@ -30,7 +30,7 @@ const MLP: usize = 2048;
|
||||
const LAYERS: usize = 6;
|
||||
/// PyTorch's `nn.LayerNorm` default (`TORCH_LN_EPS`).
|
||||
const EPS: f32 = 1e-5;
|
||||
/// Keys per step of the attention kernel (the prompt's 32 tokens are one step).
|
||||
/// Required key-count alignment, kept at 32 even when the DX12 kernel uses smaller tiles.
|
||||
const KEYS: u32 = 32;
|
||||
/// `p.gelu` of the matrix product: ReLU.
|
||||
const RELU: u32 = 2;
|
||||
@@ -49,7 +49,7 @@ pub(super) fn kernels(dev: &'static Dev) -> Result<&'static Kernels, String> {
|
||||
KERNELS
|
||||
.get_or_init(|| {
|
||||
let validation = dev.device.push_error_scope(wgpu::ErrorFilter::Validation);
|
||||
let source = attention_source(KEYS);
|
||||
let source = attention_source(super::attention_keys(dev.info.backend));
|
||||
let pipe = |entry: &str| pipeline(&dev.device, &dev.pipeline_layout, "sam3 detr", &source, entry);
|
||||
let k = Kernels { attention: pipe("attention"), add: pipe("add") };
|
||||
match pollster::block_on(validation.pop()) {
|
||||
|
||||
@@ -83,6 +83,8 @@ fn the_kernel_source_is_written_out() {
|
||||
assert!(!src.contains("{{"), "a mark is left");
|
||||
assert!(src.contains("let s31 = ") && !src.contains("let s32 = "));
|
||||
assert!(src.contains("let q7 = ") && !src.contains("let q8 = "));
|
||||
let dx12 = attention_source(super::super::attention_keys(wgpu::Backend::Dx12));
|
||||
assert!(dx12.contains("let s7 = ") && !dx12.contains("let s8 = "));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -456,3 +456,12 @@ fn an_adapter_short_of_the_limits_is_refused_with_what_it_lacks() {
|
||||
assert_eq!(short.len(), 1, "{short:?}");
|
||||
assert!(short[0].starts_with("max_compute_workgroup_storage_size 8192 <"), "{short:?}");
|
||||
}
|
||||
|
||||
/// DX12 pipeline creation can take minutes for the fully unrolled attention shader; other backends keep the larger tile.
|
||||
#[test]
|
||||
fn attention_tiles_are_smaller_on_dx12() {
|
||||
assert_eq!(attention_keys(wgpu::Backend::Dx12), 8);
|
||||
assert_eq!(attention_keys(wgpu::Backend::Vulkan), ATTENTION_KEYS);
|
||||
let src = attention_source(attention_keys(wgpu::Backend::Dx12));
|
||||
assert!(src.contains("let s7 = ") && !src.contains("let s8 = "));
|
||||
}
|
||||
Reference in new issue
Block a user