Skip to content

Commit

Permalink
feat: minor changes, types, etc....
Browse files Browse the repository at this point in the history
Co-authored-by: loading <[email protected]>
  • Loading branch information
eliassjogreen and load1n9 committed Dec 26, 2021
1 parent 817cd4e commit d31945f
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 14 deletions.
17 changes: 14 additions & 3 deletions backend/webgpu/backend.ts
Original file line number Diff line number Diff line change
@@ -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<T extends DataType = DataType>
extends BackendRequest<T> {
pipeline: string;
data: WebGPUData<T>[];
workgroups: Workgroups;
}

export class WebGPUBackend implements Backend {
type = "webgpu" as const;
Expand All @@ -11,7 +20,7 @@ export class WebGPUBackend implements Backend {
device!: GPUDevice;
pipelines: Map<string, [GPUComputePipeline, GPUBindGroupLayout]> = new Map();

async initialize(options?: GPURequestAdapterOptions) {
async initialize(options?: GPURequestAdapterOptions): Promise<void> {
if (this.initalized) {
return;
}
Expand All @@ -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<string> {
Expand Down
2 changes: 1 addition & 1 deletion backend/webgpu/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class WebGPUData<T extends DataType = DataType> implements Data<T> {
) as DataArray<T>;
}

dispose() {
dispose(): void {
this.buffer.destroy();
}
}
3 changes: 3 additions & 0 deletions backend/webgpu/shaders/binary.ts
Original file line number Diff line number Diff line change
@@ -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}>;
Expand Down
3 changes: 3 additions & 0 deletions backend/webgpu/shaders/matmul.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
11 changes: 11 additions & 0 deletions backend/webgpu/shaders/misc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const constants = `
var<private> e: f32 = 2.718281828459045;
var<private> pi: f32 = 3.141592653589793;
var<private> tau: f32 = 6.283185307179586;
var<private> phi: f32 = 1.618033988749895;
var<private> feigd: f32 = 4.66920160910299;
var<private> feiga: f32 = -2.5029078750958926;
var<private> gauss: f32 = 0.8346268416740732;
`;

export const prelude = constants;
3 changes: 3 additions & 0 deletions backend/webgpu/shaders/pad.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 3 additions & 0 deletions backend/webgpu/shaders/transpose.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 3 additions & 0 deletions backend/webgpu/shaders/unary.ts
Original file line number Diff line number Diff line change
@@ -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}>;
Expand Down
10 changes: 0 additions & 10 deletions backend/webgpu/types.ts
Original file line number Diff line number Diff line change
@@ -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<T extends DataType = DataType>
extends BackendRequest<T> {
pipeline: string;
data: WebGPUData<T>[];
workgroups: Workgroups;
}
Empty file added benchmarks/deps.ts
Empty file.
1 change: 1 addition & 0 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "https://deno.land/x/byte_type/mod.ts";
export { enableValidationErrors } from "https://crux.land/[email protected]";
Empty file added tests/deps.ts
Empty file.

0 comments on commit d31945f

Please sign in to comment.