diff --git a/backend/webgpu/backend.ts b/backend/webgpu/backend.ts index 30d15f8..a0495a8 100644 --- a/backend/webgpu/backend.ts +++ b/backend/webgpu/backend.ts @@ -1,6 +1,15 @@ +import { enableValidationErrors } from "../../deps.ts"; import { webgpu } from "../../util.ts"; -import { Backend } from "../types.ts"; -import { WebGPUBackendRequest } from "./types.ts"; +import { Backend, BackendRequest, DataType } from "../types.ts"; +import { WebGPUData } from "./data.ts"; +import { Workgroups } from "./types.ts"; + +export interface WebGPUBackendRequest + extends BackendRequest { + pipeline: string; + data: WebGPUData[]; + workgroups: Workgroups; +} export class WebGPUBackend implements Backend { type = "webgpu" as const; @@ -11,7 +20,7 @@ export class WebGPUBackend implements Backend { device!: GPUDevice; pipelines: Map = new Map(); - async initialize(options?: GPURequestAdapterOptions) { + async initialize(options?: GPURequestAdapterOptions): Promise { if (this.initalized) { return; } @@ -31,6 +40,8 @@ export class WebGPUBackend implements Backend { this.initalized = true; this.adapter = adapter; this.device = await adapter.requestDevice(); + + enableValidationErrors(this.device, true); } async register(code: string): Promise { diff --git a/backend/webgpu/data.ts b/backend/webgpu/data.ts index 3acb671..4b613b9 100644 --- a/backend/webgpu/data.ts +++ b/backend/webgpu/data.ts @@ -70,7 +70,7 @@ export class WebGPUData implements Data { ) as DataArray; } - dispose() { + dispose(): void { this.buffer.destroy(); } } diff --git a/backend/webgpu/shaders/binary.ts b/backend/webgpu/shaders/binary.ts index d5e0585..2284e9c 100644 --- a/backend/webgpu/shaders/binary.ts +++ b/backend/webgpu/shaders/binary.ts @@ -1,6 +1,9 @@ import { DataType } from "../../types.ts"; +import { prelude } from "./misc.ts"; export const binary = (type: DataType, expr: string) => ` +${prelude} + [[block]] struct Data { values: array<${type}>; diff --git a/backend/webgpu/shaders/matmul.ts b/backend/webgpu/shaders/matmul.ts index b48a337..e88cdc5 100644 --- a/backend/webgpu/shaders/matmul.ts +++ b/backend/webgpu/shaders/matmul.ts @@ -1,6 +1,9 @@ import { DataType } from "../../types.ts"; +import { prelude } from "./misc.ts"; export const matmul = (type: DataType) => ` +${prelude} + [[block]] struct Uniform { m: u32; diff --git a/backend/webgpu/shaders/misc.ts b/backend/webgpu/shaders/misc.ts new file mode 100644 index 0000000..58176be --- /dev/null +++ b/backend/webgpu/shaders/misc.ts @@ -0,0 +1,11 @@ +export const constants = ` +var e: f32 = 2.718281828459045; +var pi: f32 = 3.141592653589793; +var tau: f32 = 6.283185307179586; +var phi: f32 = 1.618033988749895; +var feigd: f32 = 4.66920160910299; +var feiga: f32 = -2.5029078750958926; +var gauss: f32 = 0.8346268416740732; +`; + +export const prelude = constants; diff --git a/backend/webgpu/shaders/pad.ts b/backend/webgpu/shaders/pad.ts index b129a46..f61d88f 100644 --- a/backend/webgpu/shaders/pad.ts +++ b/backend/webgpu/shaders/pad.ts @@ -1,6 +1,9 @@ import { DataType } from "../../types.ts"; +import { prelude } from "./misc.ts"; export const pad = (type: DataType) => ` +${prelude} + [[block]] struct Uniform { w: u32; diff --git a/backend/webgpu/shaders/transpose.ts b/backend/webgpu/shaders/transpose.ts index 0f12d4c..fd080e4 100644 --- a/backend/webgpu/shaders/transpose.ts +++ b/backend/webgpu/shaders/transpose.ts @@ -1,6 +1,9 @@ import { DataType } from "../../types.ts"; +import { prelude } from "./misc.ts"; export const transpose = (type: DataType) => ` +${prelude} + [[block]] struct Uniform { w: u32; diff --git a/backend/webgpu/shaders/unary.ts b/backend/webgpu/shaders/unary.ts index b17758d..5f05bce 100644 --- a/backend/webgpu/shaders/unary.ts +++ b/backend/webgpu/shaders/unary.ts @@ -1,6 +1,9 @@ import { DataType } from "../../types.ts"; +import { prelude } from "./misc.ts"; export const unary = (type: DataType, expr: string) => ` +${prelude} + [[block]] struct Data { values: array<${type}>; diff --git a/backend/webgpu/types.ts b/backend/webgpu/types.ts index 0cb979c..b58865e 100644 --- a/backend/webgpu/types.ts +++ b/backend/webgpu/types.ts @@ -1,14 +1,4 @@ -import { BackendRequest, DataType } from "../types.ts"; -import { WebGPUData } from "./data.ts"; - export type Workgroups = | [number, number, number] | [number, number] | [number]; - -export interface WebGPUBackendRequest - extends BackendRequest { - pipeline: string; - data: WebGPUData[]; - workgroups: Workgroups; -} diff --git a/benchmarks/deps.ts b/benchmarks/deps.ts new file mode 100644 index 0000000..e69de29 diff --git a/deps.ts b/deps.ts index 1f6fcc1..f5a653d 100644 --- a/deps.ts +++ b/deps.ts @@ -1 +1,2 @@ export * from "https://deno.land/x/byte_type/mod.ts"; +export { enableValidationErrors } from "https://crux.land/gpu_err@1.0.0"; diff --git a/tests/deps.ts b/tests/deps.ts new file mode 100644 index 0000000..e69de29