wgpu-shift 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gpu_shift/__init__.py +19 -0
- gpu_shift/__main__.py +8 -0
- gpu_shift/_capturer.py +191 -0
- gpu_shift/_scanner.py +71 -0
- gpu_shift/bench.py +321 -0
- gpu_shift/capture.py +669 -0
- gpu_shift/check.py +224 -0
- gpu_shift/cli.py +609 -0
- gpu_shift/demo.py +272 -0
- gpu_shift/device.py +170 -0
- gpu_shift/diff.py +510 -0
- gpu_shift/manifest.py +171 -0
- gpu_shift/mcp_server.py +352 -0
- gpu_shift/report.py +218 -0
- gpu_shift/run.py +206 -0
- gpu_shift/runtime.py +592 -0
- gpu_shift/scan.py +380 -0
- gpu_shift/setup.py +245 -0
- gpu_shift/skill_data/SKILL.md +56 -0
- gpu_shift/stage.py +248 -0
- gpu_shift/switch.py +268 -0
- wgpu_shift-0.1.0.dist-info/METADATA +125 -0
- wgpu_shift-0.1.0.dist-info/RECORD +26 -0
- wgpu_shift-0.1.0.dist-info/WHEEL +4 -0
- wgpu_shift-0.1.0.dist-info/entry_points.txt +3 -0
- wgpu_shift-0.1.0.dist-info/licenses/LICENSE +21 -0
gpu_shift/demo.py
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
"""Zero-setup 15-second live GPU demonstration (`gpu-shift demo`)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import statistics
|
|
6
|
+
import time
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
import numpy as np
|
|
11
|
+
|
|
12
|
+
from gpu_shift.bench import BenchPoint, find_crossover
|
|
13
|
+
from gpu_shift.device import AdapterInfo, get_device
|
|
14
|
+
from gpu_shift.diff import compare_outputs
|
|
15
|
+
from gpu_shift.runtime import ComputeKernel, ceil_div, pack_uniforms, snapshot_vram
|
|
16
|
+
|
|
17
|
+
_DEMO_WGSL = """
|
|
18
|
+
struct Params {
|
|
19
|
+
width: u32,
|
|
20
|
+
height: u32,
|
|
21
|
+
radius: i32,
|
|
22
|
+
inv_2_spatial: f32,
|
|
23
|
+
inv_2_range: f32,
|
|
24
|
+
_pad0: u32,
|
|
25
|
+
_pad1: u32,
|
|
26
|
+
_pad2: u32,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
@group(0) @binding(0)
|
|
30
|
+
var<storage, read> src: array<f32>;
|
|
31
|
+
|
|
32
|
+
@group(0) @binding(1)
|
|
33
|
+
var<storage, read_write> dst: array<f32>;
|
|
34
|
+
|
|
35
|
+
@group(0) @binding(2)
|
|
36
|
+
var<uniform> params: Params;
|
|
37
|
+
|
|
38
|
+
var<workgroup> smem: array<f32, 1024>;
|
|
39
|
+
|
|
40
|
+
@compute @workgroup_size(16, 16, 1)
|
|
41
|
+
fn main(
|
|
42
|
+
@builtin(workgroup_id) wid: vec3<u32>,
|
|
43
|
+
@builtin(local_invocation_id) lid: vec3<u32>,
|
|
44
|
+
@builtin(global_invocation_id) gid: vec3<u32>,
|
|
45
|
+
) {
|
|
46
|
+
let width_i = i32(params.width);
|
|
47
|
+
let height_i = i32(params.height);
|
|
48
|
+
let r = params.radius;
|
|
49
|
+
let lx = lid.x;
|
|
50
|
+
let ly = lid.y;
|
|
51
|
+
let tile_dim = u32(16 + 2 * r);
|
|
52
|
+
let origin_x = i32(wid.x * 16u) - r;
|
|
53
|
+
let origin_y = i32(wid.y * 16u) - r;
|
|
54
|
+
|
|
55
|
+
for (var dy: u32 = 0u; dy < 32u; dy = dy + 16u) {
|
|
56
|
+
let sy = ly + dy;
|
|
57
|
+
if (sy < tile_dim) {
|
|
58
|
+
let gy = clamp(origin_y + i32(sy), 0, height_i - 1);
|
|
59
|
+
let g_row = u32(gy) * params.width;
|
|
60
|
+
let s_row = sy * 32u;
|
|
61
|
+
for (var dx: u32 = 0u; dx < 32u; dx = dx + 16u) {
|
|
62
|
+
let sx = lx + dx;
|
|
63
|
+
if (sx < tile_dim) {
|
|
64
|
+
let gx = clamp(origin_x + i32(sx), 0, width_i - 1);
|
|
65
|
+
smem[s_row + sx] = src[g_row + u32(gx)];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
workgroupBarrier();
|
|
72
|
+
|
|
73
|
+
let x = gid.x;
|
|
74
|
+
let y = gid.y;
|
|
75
|
+
if (x >= params.width || y >= params.height) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
let cy = i32(ly) + r;
|
|
79
|
+
let cx = i32(lx) + r;
|
|
80
|
+
let center = smem[u32(cy) * 32u + u32(cx)];
|
|
81
|
+
|
|
82
|
+
var acc: f32 = 0.0;
|
|
83
|
+
var w_sum: f32 = 0.0;
|
|
84
|
+
for (var dy: i32 = -r; dy <= r; dy = dy + 1) {
|
|
85
|
+
let s_row = u32(cy + dy) * 32u;
|
|
86
|
+
let dy2 = f32(dy * dy);
|
|
87
|
+
for (var dx: i32 = -r; dx <= r; dx = dx + 1) {
|
|
88
|
+
let neighbor = smem[s_row + u32(cx + dx)];
|
|
89
|
+
let diff = neighbor - center;
|
|
90
|
+
let weight = exp(-((f32(dx * dx) + dy2) * params.inv_2_spatial + diff * diff * params.inv_2_range));
|
|
91
|
+
acc = fma(weight, neighbor, acc);
|
|
92
|
+
w_sum = w_sum + weight;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
dst[y * params.width + x] = acc / w_sum;
|
|
96
|
+
}
|
|
97
|
+
"""
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@dataclass(frozen=True)
|
|
101
|
+
class DemoResult:
|
|
102
|
+
"""Result summary for `gpu-shift demo`."""
|
|
103
|
+
|
|
104
|
+
adapter: AdapterInfo
|
|
105
|
+
diff_passed: int
|
|
106
|
+
diff_total: int
|
|
107
|
+
max_abs_err: float
|
|
108
|
+
vram_clean: bool
|
|
109
|
+
vram_delta_bytes: int
|
|
110
|
+
sweep_points: tuple[BenchPoint, ...]
|
|
111
|
+
crossover_elements: int | None
|
|
112
|
+
peak_speedup: float
|
|
113
|
+
verdict: str
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _cpu_bilateral(
|
|
117
|
+
image: np.ndarray,
|
|
118
|
+
radius: int = 3,
|
|
119
|
+
sigma_spatial: float = 3.0,
|
|
120
|
+
sigma_range: float = 0.15,
|
|
121
|
+
) -> np.ndarray:
|
|
122
|
+
img = np.ascontiguousarray(image, dtype=np.float32)
|
|
123
|
+
h, w = img.shape
|
|
124
|
+
padded = np.pad(img, radius, mode="edge")
|
|
125
|
+
inv_2_spatial = np.float32(1.0 / (2.0 * sigma_spatial * sigma_spatial))
|
|
126
|
+
inv_2_range = np.float32(1.0 / (2.0 * sigma_range * sigma_range))
|
|
127
|
+
acc = np.zeros_like(img, dtype=np.float32)
|
|
128
|
+
w_sum = np.zeros_like(img, dtype=np.float32)
|
|
129
|
+
for dy in range(-radius, radius + 1):
|
|
130
|
+
y0 = radius + dy
|
|
131
|
+
for dx in range(-radius, radius + 1):
|
|
132
|
+
x0 = radius + dx
|
|
133
|
+
sw = np.float32(np.exp(-float(dx * dx + dy * dy) * float(inv_2_spatial)))
|
|
134
|
+
neighbor = padded[y0 : y0 + h, x0 : x0 + w]
|
|
135
|
+
diff = neighbor - img
|
|
136
|
+
weight = sw * np.exp(-(diff * diff) * inv_2_range)
|
|
137
|
+
acc += weight * neighbor
|
|
138
|
+
w_sum += weight
|
|
139
|
+
return (acc / w_sum).astype(np.float32, copy=False)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _gpu_bilateral(
|
|
143
|
+
kernel: ComputeKernel,
|
|
144
|
+
image: np.ndarray,
|
|
145
|
+
radius: int = 3,
|
|
146
|
+
sigma_spatial: float = 3.0,
|
|
147
|
+
sigma_range: float = 0.15,
|
|
148
|
+
) -> np.ndarray:
|
|
149
|
+
img = np.ascontiguousarray(image, dtype=np.float32)
|
|
150
|
+
h, w = img.shape
|
|
151
|
+
uniforms = pack_uniforms(
|
|
152
|
+
"IIiff",
|
|
153
|
+
int(w),
|
|
154
|
+
int(h),
|
|
155
|
+
int(radius),
|
|
156
|
+
float(1.0 / (2.0 * sigma_spatial * sigma_spatial)),
|
|
157
|
+
float(1.0 / (2.0 * sigma_range * sigma_range)),
|
|
158
|
+
)
|
|
159
|
+
out = kernel.dispatch(
|
|
160
|
+
inputs={0: img},
|
|
161
|
+
outputs={1: (img.shape, np.float32)},
|
|
162
|
+
uniforms={2: uniforms},
|
|
163
|
+
workgroups=(ceil_div(w, 16), ceil_div(h, 16), 1),
|
|
164
|
+
)
|
|
165
|
+
return out[1]
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def run_demo(
|
|
169
|
+
*,
|
|
170
|
+
power_preference: str = "high-performance",
|
|
171
|
+
sides: tuple[int, ...] = (16, 32, 64, 128, 256, 512),
|
|
172
|
+
) -> DemoResult:
|
|
173
|
+
"""Run an interactive-speed bilateral filter verification + crossover demo on local GPU."""
|
|
174
|
+
adapter, _ = get_device(power_preference=power_preference)
|
|
175
|
+
adapter_info = AdapterInfo.from_wgpu(adapter)
|
|
176
|
+
kernel = ComputeKernel(_DEMO_WGSL, power_preference=power_preference)
|
|
177
|
+
|
|
178
|
+
rng = np.random.default_rng(7)
|
|
179
|
+
diff_cases = [
|
|
180
|
+
rng.uniform(0.0, 1.0, size=(1, 1)).astype(np.float32),
|
|
181
|
+
rng.uniform(0.0, 1.0, size=(17, 13)).astype(np.float32),
|
|
182
|
+
rng.uniform(0.0, 1.0, size=(64, 64)).astype(np.float32),
|
|
183
|
+
rng.uniform(0.0, 1.0, size=(19, 15)).astype(np.float32)[::-1],
|
|
184
|
+
]
|
|
185
|
+
|
|
186
|
+
passed_count = 0
|
|
187
|
+
worst_abs = 0.0
|
|
188
|
+
for arr in diff_cases:
|
|
189
|
+
cpu_out = _cpu_bilateral(arr)
|
|
190
|
+
gpu_out = _gpu_bilateral(kernel, arr)
|
|
191
|
+
ok, max_abs, _, _, _, _, _ = compare_outputs(
|
|
192
|
+
gpu_out, cpu_out, rtol=1e-4, atol=1e-5
|
|
193
|
+
)
|
|
194
|
+
if ok:
|
|
195
|
+
passed_count += 1
|
|
196
|
+
if np.isfinite(max_abs):
|
|
197
|
+
worst_abs = max(worst_abs, max_abs)
|
|
198
|
+
|
|
199
|
+
# 15-dispatch VRAM soak test
|
|
200
|
+
soak_img = diff_cases[2]
|
|
201
|
+
_gpu_bilateral(kernel, soak_img)
|
|
202
|
+
before = snapshot_vram(run_gc=True)
|
|
203
|
+
for _ in range(15):
|
|
204
|
+
_gpu_bilateral(kernel, soak_img)
|
|
205
|
+
after = snapshot_vram(run_gc=True)
|
|
206
|
+
vram_delta = after.resource_mem_bytes - before.resource_mem_bytes
|
|
207
|
+
vram_clean = (
|
|
208
|
+
after.kernel_active_buffers == 0
|
|
209
|
+
and after.wgpu_buffers <= before.wgpu_buffers
|
|
210
|
+
and vram_delta <= 0
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
# Transfer-inclusive crossover sweep
|
|
214
|
+
sweep_points: list[BenchPoint] = []
|
|
215
|
+
for side in sides:
|
|
216
|
+
img = rng.uniform(0.0, 1.0, size=(side, side)).astype(np.float32)
|
|
217
|
+
reps = 5 if side <= 256 else 3
|
|
218
|
+
|
|
219
|
+
_cpu_bilateral(img)
|
|
220
|
+
_gpu_bilateral(kernel, img)
|
|
221
|
+
|
|
222
|
+
cpu_times = []
|
|
223
|
+
for _ in range(reps):
|
|
224
|
+
t0 = time.perf_counter()
|
|
225
|
+
_cpu_bilateral(img)
|
|
226
|
+
cpu_times.append((time.perf_counter() - t0) * 1000.0)
|
|
227
|
+
|
|
228
|
+
gpu_times = []
|
|
229
|
+
for _ in range(reps):
|
|
230
|
+
t0 = time.perf_counter()
|
|
231
|
+
_gpu_bilateral(kernel, img)
|
|
232
|
+
gpu_times.append((time.perf_counter() - t0) * 1000.0)
|
|
233
|
+
|
|
234
|
+
cpu_ms = float(statistics.median(cpu_times))
|
|
235
|
+
gpu_ms = float(statistics.median(gpu_times))
|
|
236
|
+
speedup = cpu_ms / gpu_ms if gpu_ms > 0 else 0.0
|
|
237
|
+
sweep_points.append(
|
|
238
|
+
BenchPoint(
|
|
239
|
+
label=f"{side}x{side}",
|
|
240
|
+
shape=(side, side),
|
|
241
|
+
elements=side * side,
|
|
242
|
+
cpu_ms=cpu_ms,
|
|
243
|
+
gpu_ms=gpu_ms,
|
|
244
|
+
speedup=speedup,
|
|
245
|
+
)
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
crossover = find_crossover(sweep_points, min_speedup=1.15)
|
|
249
|
+
peak = max((p.speedup for p in sweep_points), default=0.0)
|
|
250
|
+
if crossover is not None:
|
|
251
|
+
verdict = f"GPU faster from {crossover:,} elements (peak {peak:.2f}x on {adapter_info.device})"
|
|
252
|
+
else:
|
|
253
|
+
verdict = f"CPU faster at tested sizes (peak {peak:.2f}x on {adapter_info.device})"
|
|
254
|
+
|
|
255
|
+
return DemoResult(
|
|
256
|
+
adapter=adapter_info,
|
|
257
|
+
diff_passed=passed_count,
|
|
258
|
+
diff_total=len(diff_cases),
|
|
259
|
+
max_abs_err=worst_abs,
|
|
260
|
+
vram_clean=vram_clean,
|
|
261
|
+
vram_delta_bytes=vram_delta,
|
|
262
|
+
sweep_points=tuple(sweep_points),
|
|
263
|
+
crossover_elements=crossover,
|
|
264
|
+
peak_speedup=peak,
|
|
265
|
+
verdict=verdict,
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def get_bundled_examples_dir() -> Path | None:
|
|
270
|
+
"""Return local examples directory when running from a source checkout."""
|
|
271
|
+
candidate = Path(__file__).resolve().parent.parent.parent / "examples"
|
|
272
|
+
return candidate if candidate.is_dir() else None
|
gpu_shift/device.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"""WebGPU adapter discovery, cached device creation, and smoke test helpers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import threading
|
|
7
|
+
import time
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
import numpy as np
|
|
12
|
+
import wgpu
|
|
13
|
+
|
|
14
|
+
SMOKE_WGSL = """
|
|
15
|
+
@group(0) @binding(0)
|
|
16
|
+
var<storage, read> src: array<f32>;
|
|
17
|
+
|
|
18
|
+
@group(0) @binding(1)
|
|
19
|
+
var<storage, read_write> dst: array<f32>;
|
|
20
|
+
|
|
21
|
+
@compute @workgroup_size(64)
|
|
22
|
+
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
|
|
23
|
+
let i = gid.x;
|
|
24
|
+
if (i < arrayLength(&src)) {
|
|
25
|
+
dst[i] = src[i] * 2.0 + 1.0;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
_DEVICE_LOCK = threading.Lock()
|
|
31
|
+
_DEVICE_CACHE: dict[tuple[int, str], tuple[Any, Any]] = {}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass(frozen=True)
|
|
35
|
+
class AdapterInfo:
|
|
36
|
+
"""Summary metadata for a discovered WebGPU adapter."""
|
|
37
|
+
|
|
38
|
+
device: str
|
|
39
|
+
adapter_type: str
|
|
40
|
+
backend_type: str
|
|
41
|
+
vendor: str
|
|
42
|
+
driver: str
|
|
43
|
+
description: str
|
|
44
|
+
|
|
45
|
+
@property
|
|
46
|
+
def is_hardware(self) -> bool:
|
|
47
|
+
"""Return True for physical GPUs (DiscreteGPU / IntegratedGPU), False for CPU emulators."""
|
|
48
|
+
return self.adapter_type.lower() in {"discretegpu", "integratedgpu"}
|
|
49
|
+
|
|
50
|
+
@classmethod
|
|
51
|
+
def from_wgpu(cls, adapter: Any) -> AdapterInfo:
|
|
52
|
+
info = adapter.info
|
|
53
|
+
return cls(
|
|
54
|
+
device=str(info.get("device", "")),
|
|
55
|
+
adapter_type=str(info.get("adapter_type", "")),
|
|
56
|
+
backend_type=str(info.get("backend_type", "")),
|
|
57
|
+
vendor=str(info.get("vendor", "")),
|
|
58
|
+
driver=str(info.get("driver", "")),
|
|
59
|
+
description=str(info.get("description", "")),
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def list_adapters() -> list[AdapterInfo]:
|
|
64
|
+
"""Return metadata for all available WebGPU adapters."""
|
|
65
|
+
adapters = wgpu.gpu.enumerate_adapters_sync()
|
|
66
|
+
return [AdapterInfo.from_wgpu(adapter) for adapter in adapters]
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def request_device(power_preference: str = "high-performance") -> tuple[Any, Any]:
|
|
70
|
+
"""Request a fresh WebGPU adapter and device synchronously."""
|
|
71
|
+
adapter = wgpu.gpu.request_adapter_sync(power_preference=power_preference)
|
|
72
|
+
if adapter is None:
|
|
73
|
+
raise RuntimeError("No compatible WebGPU adapter found on this machine.")
|
|
74
|
+
device = adapter.request_device_sync()
|
|
75
|
+
return adapter, device
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def get_device(power_preference: str = "high-performance") -> tuple[Any, Any]:
|
|
79
|
+
"""Return a PID-scoped cached (adapter, device) pair for the given power preference."""
|
|
80
|
+
key = (os.getpid(), power_preference)
|
|
81
|
+
with _DEVICE_LOCK:
|
|
82
|
+
cached = _DEVICE_CACHE.get(key)
|
|
83
|
+
if cached is not None:
|
|
84
|
+
return cached
|
|
85
|
+
pair = request_device(power_preference=power_preference)
|
|
86
|
+
_DEVICE_CACHE[key] = pair
|
|
87
|
+
return pair
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def has_hardware_gpu(power_preference: str = "high-performance") -> bool:
|
|
91
|
+
"""Return True if a physical (non-emulated) WebGPU GPU adapter is available."""
|
|
92
|
+
try:
|
|
93
|
+
adapter, _ = get_device(power_preference=power_preference)
|
|
94
|
+
return AdapterInfo.from_wgpu(adapter).is_hardware
|
|
95
|
+
except Exception: # noqa: BLE001
|
|
96
|
+
return False
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def clear_device_cache() -> None:
|
|
100
|
+
"""Clear cached WebGPU devices (also invoked after Unix fork or device loss)."""
|
|
101
|
+
with _DEVICE_LOCK:
|
|
102
|
+
_DEVICE_CACHE.clear()
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
if hasattr(os, "register_at_fork"):
|
|
106
|
+
os.register_at_fork(after_in_child=clear_device_cache)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def run_smoke_test(
|
|
110
|
+
size: int = 1024,
|
|
111
|
+
power_preference: str = "high-performance",
|
|
112
|
+
) -> dict[str, Any]:
|
|
113
|
+
"""Run a minimal WGSL compute shader and compare output against NumPy."""
|
|
114
|
+
adapter, device = get_device(power_preference=power_preference)
|
|
115
|
+
adapter_info = AdapterInfo.from_wgpu(adapter)
|
|
116
|
+
|
|
117
|
+
src_data = np.linspace(-10.0, 10.0, size, dtype=np.float32)
|
|
118
|
+
expected = src_data * np.float32(2.0) + np.float32(1.0)
|
|
119
|
+
|
|
120
|
+
t0 = time.perf_counter()
|
|
121
|
+
shader = device.create_shader_module(code=SMOKE_WGSL)
|
|
122
|
+
pipeline = device.create_compute_pipeline(
|
|
123
|
+
layout="auto",
|
|
124
|
+
compute={"module": shader, "entry_point": "main"},
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
src_buf = device.create_buffer_with_data(
|
|
128
|
+
data=src_data,
|
|
129
|
+
usage=wgpu.BufferUsage.STORAGE,
|
|
130
|
+
)
|
|
131
|
+
dst_buf = device.create_buffer(
|
|
132
|
+
size=src_data.nbytes,
|
|
133
|
+
usage=wgpu.BufferUsage.STORAGE | wgpu.BufferUsage.COPY_SRC,
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
try:
|
|
137
|
+
bind_group = device.create_bind_group(
|
|
138
|
+
layout=pipeline.get_bind_group_layout(0),
|
|
139
|
+
entries=[
|
|
140
|
+
{"binding": 0, "resource": {"buffer": src_buf}},
|
|
141
|
+
{"binding": 1, "resource": {"buffer": dst_buf}},
|
|
142
|
+
],
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
encoder = device.create_command_encoder()
|
|
146
|
+
pass_enc = encoder.begin_compute_pass()
|
|
147
|
+
pass_enc.set_pipeline(pipeline)
|
|
148
|
+
pass_enc.set_bind_group(0, bind_group)
|
|
149
|
+
workgroups = (size + 63) // 64
|
|
150
|
+
pass_enc.dispatch_workgroups(workgroups, 1, 1)
|
|
151
|
+
pass_enc.end()
|
|
152
|
+
|
|
153
|
+
device.queue.submit([encoder.finish()])
|
|
154
|
+
raw = device.queue.read_buffer(dst_buf)
|
|
155
|
+
elapsed_ms = (time.perf_counter() - t0) * 1000.0
|
|
156
|
+
finally:
|
|
157
|
+
src_buf.destroy()
|
|
158
|
+
dst_buf.destroy()
|
|
159
|
+
|
|
160
|
+
actual = np.frombuffer(raw, dtype=np.float32).copy()
|
|
161
|
+
max_abs_err = float(np.max(np.abs(actual - expected)))
|
|
162
|
+
passed = bool(np.allclose(actual, expected, rtol=1e-5, atol=1e-6))
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
"adapter": adapter_info,
|
|
166
|
+
"size": size,
|
|
167
|
+
"passed": passed,
|
|
168
|
+
"max_abs_err": max_abs_err,
|
|
169
|
+
"elapsed_ms": elapsed_ms,
|
|
170
|
+
}
|