@jax-js/jax 0.1.10 → 0.1.11
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.
- package/README.md +4 -1
- package/dist/{backend-Ctqs8la1.js → backend-DZvR7mZV.js} +730 -21
- package/dist/{backend-DMauYnfl.cjs → backend-DlYlOYqN.cjs} +735 -20
- package/dist/index.cjs +140 -3
- package/dist/index.d.cts +66 -3
- package/dist/index.d.ts +66 -3
- package/dist/index.js +140 -4
- package/dist/{webgl-CvQ1QBX1.js → webgl-D8-14NzA.js} +7 -1
- package/dist/{webgl-kvVt7-T7.cjs → webgl-Ovaaa-Qx.cjs} +7 -1
- package/dist/{webgpu-v_W_-oKw.js → webgpu-Dg8FpYrH.js} +6 -1
- package/dist/{webgpu-DMSx7a6M.cjs → webgpu-uU9nnttc.cjs} +6 -1
- package/package.json +1 -1
|
@@ -314,6 +314,16 @@ function runWithCache(cache, key, thunk) {
|
|
|
314
314
|
return value;
|
|
315
315
|
}
|
|
316
316
|
}
|
|
317
|
+
/** Async version of `runWithCache`. */
|
|
318
|
+
async function runWithCacheAsync(cache, key, thunk) {
|
|
319
|
+
const keyStr = JSON.stringify(key);
|
|
320
|
+
if (cache.has(keyStr)) return cache.get(keyStr);
|
|
321
|
+
else {
|
|
322
|
+
const value = await thunk();
|
|
323
|
+
cache.set(keyStr, value);
|
|
324
|
+
return value;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
317
327
|
|
|
318
328
|
//#endregion
|
|
319
329
|
//#region src/alu.ts
|
|
@@ -416,8 +426,23 @@ var AluExp = class AluExp {
|
|
|
416
426
|
this.src = src;
|
|
417
427
|
this.arg = arg;
|
|
418
428
|
if (AluGroup.RequiredFloat.has(op) && !isFloatDtype(dtype)) throw new TypeError(`Unsupported dtype for ${op}: ${dtype}`);
|
|
419
|
-
|
|
420
|
-
|
|
429
|
+
switch (op) {
|
|
430
|
+
case AluOp.Bitcast:
|
|
431
|
+
if (dtype === DType.Bool || src[0].dtype === DType.Bool || byteWidth(dtype) !== byteWidth(src[0].dtype)) throw new TypeError(`Bitcast from ${src[0].dtype} -> ${dtype}`);
|
|
432
|
+
break;
|
|
433
|
+
case AluOp.Threefry2x32:
|
|
434
|
+
if (dtype !== DType.Uint32 || src.some((x) => x.dtype !== DType.Uint32)) throw new TypeError("Threefry2x32 requires uint32 types");
|
|
435
|
+
break;
|
|
436
|
+
case AluOp.BitCombine:
|
|
437
|
+
if (src[0].dtype !== src[1].dtype || isFloatDtype(src[0].dtype)) throw new TypeError(`BitCombine[${arg}] requires matching integral dtype, got ${src[0].dtype} and ${src[1].dtype}`);
|
|
438
|
+
break;
|
|
439
|
+
case AluOp.BitShift:
|
|
440
|
+
if (src[0].dtype === DType.Bool || src[1].dtype === DType.Bool || isFloatDtype(src[0].dtype) || isFloatDtype(src[1].dtype)) throw new TypeError(`BitShift[${arg}] requires two integral, non-bool dtypes, got ${src[0].dtype} and ${src[1].dtype}`);
|
|
441
|
+
break;
|
|
442
|
+
case AluOp.BitInvert:
|
|
443
|
+
if (isFloatDtype(src[0].dtype)) throw new TypeError(`BitInvert requires an integral dtype, got ${src[0].dtype}`);
|
|
444
|
+
break;
|
|
445
|
+
}
|
|
421
446
|
}
|
|
422
447
|
static add(a, b) {
|
|
423
448
|
return new AluExp(AluOp.Add, a.dtype, [a, b]);
|
|
@@ -494,6 +519,12 @@ var AluExp = class AluExp {
|
|
|
494
519
|
c1
|
|
495
520
|
], mode);
|
|
496
521
|
}
|
|
522
|
+
static bitCombine(a, b, mode) {
|
|
523
|
+
return new AluExp(AluOp.BitCombine, a.dtype, [a, b], mode);
|
|
524
|
+
}
|
|
525
|
+
static bitShift(a, b, mode) {
|
|
526
|
+
return new AluExp(AluOp.BitShift, a.dtype, [a, b], mode);
|
|
527
|
+
}
|
|
497
528
|
static cmplt(a, b) {
|
|
498
529
|
return new AluExp(AluOp.Cmplt, DType.Bool, [a, b]);
|
|
499
530
|
}
|
|
@@ -966,6 +997,16 @@ var AluExp = class AluExp {
|
|
|
966
997
|
case AluOp.Mod: return x % y;
|
|
967
998
|
case AluOp.Min: return Math.min(x, y);
|
|
968
999
|
case AluOp.Max: return Math.max(x, y);
|
|
1000
|
+
case AluOp.BitCombine: {
|
|
1001
|
+
let r;
|
|
1002
|
+
if (this.arg === "and") r = x & y;
|
|
1003
|
+
else if (this.arg === "or") r = x | y;
|
|
1004
|
+
else r = x ^ y;
|
|
1005
|
+
return this.dtype === DType.Int32 ? r | 0 : r >>> 0;
|
|
1006
|
+
}
|
|
1007
|
+
case AluOp.BitShift:
|
|
1008
|
+
if (this.arg === "shl") return this.dtype === DType.Int32 ? x << y | 0 : x << y >>> 0;
|
|
1009
|
+
return x >>> y;
|
|
969
1010
|
case AluOp.Cmplt: return Number(x < y);
|
|
970
1011
|
case AluOp.Cmpne: return Number(x != y);
|
|
971
1012
|
default: throw new Error(`Missing implemementation for ${this.op}`);
|
|
@@ -1087,6 +1128,18 @@ var AluExp = class AluExp {
|
|
|
1087
1128
|
}
|
|
1088
1129
|
if (BIN_SYM[node.op]) return `(${parts[0]} ${BIN_SYM[node.op]} ${parts[1]})`;
|
|
1089
1130
|
if (CMP_SYM[node.op]) return `(${parts[0]} ${CMP_SYM[node.op]} ${parts[1]})`;
|
|
1131
|
+
if (node.op === AluOp.BitCombine) {
|
|
1132
|
+
const sym = {
|
|
1133
|
+
and: "&",
|
|
1134
|
+
or: "|",
|
|
1135
|
+
xor: "^"
|
|
1136
|
+
}[node.arg];
|
|
1137
|
+
return `(${parts[0]} ${sym} ${parts[1]})`;
|
|
1138
|
+
}
|
|
1139
|
+
if (node.op === AluOp.BitShift) {
|
|
1140
|
+
const sym = node.arg === "shl" ? "<<" : ">>";
|
|
1141
|
+
return `(${parts[0]} ${sym} ${parts[1]})`;
|
|
1142
|
+
}
|
|
1090
1143
|
if (UNARY_SYM[node.op]) return `${UNARY_SYM[node.op]}${parts[0]}`;
|
|
1091
1144
|
if (node.op === AluOp.Cast) return `Cast<${node.dtype}>(${strip1(parts[0])})`;
|
|
1092
1145
|
if (node.op === AluOp.Bitcast) return `Bitcast<${node.dtype}>(${strip1(parts[0])})`;
|
|
@@ -1179,6 +1232,9 @@ let AluOp = /* @__PURE__ */ function(AluOp$1) {
|
|
|
1179
1232
|
AluOp$1["Reciprocal"] = "Reciprocal";
|
|
1180
1233
|
AluOp$1["Cast"] = "Cast";
|
|
1181
1234
|
AluOp$1["Bitcast"] = "Bitcast";
|
|
1235
|
+
AluOp$1["BitCombine"] = "BitCombine";
|
|
1236
|
+
AluOp$1["BitInvert"] = "BitInvert";
|
|
1237
|
+
AluOp$1["BitShift"] = "BitShift";
|
|
1182
1238
|
AluOp$1["Cmplt"] = "Cmplt";
|
|
1183
1239
|
AluOp$1["Cmpne"] = "Cmpne";
|
|
1184
1240
|
AluOp$1["Where"] = "Where";
|
|
@@ -1198,7 +1254,9 @@ const AluGroup = {
|
|
|
1198
1254
|
AluOp.Idiv,
|
|
1199
1255
|
AluOp.Mod,
|
|
1200
1256
|
AluOp.Min,
|
|
1201
|
-
AluOp.Max
|
|
1257
|
+
AluOp.Max,
|
|
1258
|
+
AluOp.BitCombine,
|
|
1259
|
+
AluOp.BitShift
|
|
1202
1260
|
]),
|
|
1203
1261
|
Unary: new Set([
|
|
1204
1262
|
AluOp.Sin,
|
|
@@ -3159,6 +3217,147 @@ function wasm_threefry2x32(cg) {
|
|
|
3159
3217
|
});
|
|
3160
3218
|
}
|
|
3161
3219
|
|
|
3220
|
+
//#endregion
|
|
3221
|
+
//#region src/backend/wasm/parallel.ts
|
|
3222
|
+
/** Check if SharedArrayBuffer is available. */
|
|
3223
|
+
function hasSharedArrayBuffer() {
|
|
3224
|
+
return typeof SharedArrayBuffer !== "undefined" && typeof Worker !== "undefined";
|
|
3225
|
+
}
|
|
3226
|
+
const MIN_ELEMS_PER_THREAD = 256;
|
|
3227
|
+
const WORKER_SOURCE = `
|
|
3228
|
+
let memory = null;
|
|
3229
|
+
let cachedModule = null;
|
|
3230
|
+
let cachedFunc = null;
|
|
3231
|
+
|
|
3232
|
+
self.onmessage = (e) => {
|
|
3233
|
+
const msg = e.data;
|
|
3234
|
+
if (msg.type === "init") {
|
|
3235
|
+
memory = msg.memory;
|
|
3236
|
+
postMessage({ type: "ready" });
|
|
3237
|
+
return;
|
|
3238
|
+
}
|
|
3239
|
+
try {
|
|
3240
|
+
const { module, ptrs, begin, end } = msg;
|
|
3241
|
+
if (module !== cachedModule) {
|
|
3242
|
+
cachedModule = module;
|
|
3243
|
+
const instance = new WebAssembly.Instance(module, { env: { memory } });
|
|
3244
|
+
cachedFunc = instance.exports.kernel;
|
|
3245
|
+
}
|
|
3246
|
+
cachedFunc(...ptrs, begin, end);
|
|
3247
|
+
postMessage({ type: "done", ok: true });
|
|
3248
|
+
} catch (err) {
|
|
3249
|
+
postMessage({ type: "done", ok: false, error: String(err) });
|
|
3250
|
+
}
|
|
3251
|
+
};
|
|
3252
|
+
`;
|
|
3253
|
+
/** Pool of Web Workers for parallel WASM kernel dispatch. */
|
|
3254
|
+
var WasmWorkerPool = class {
|
|
3255
|
+
#memory;
|
|
3256
|
+
#numWorkers;
|
|
3257
|
+
#workers = [];
|
|
3258
|
+
#ready = Promise.resolve();
|
|
3259
|
+
/** Serializes dispatches so concurrent read() calls don't clobber onmessage. */
|
|
3260
|
+
#queue = Promise.resolve();
|
|
3261
|
+
#epoch = 0n;
|
|
3262
|
+
#epochEnd = 0n;
|
|
3263
|
+
#hooks = /* @__PURE__ */ new Map();
|
|
3264
|
+
constructor(memory, numWorkers) {
|
|
3265
|
+
if (numWorkers <= 0) throw new Error("numWorkers must be positive");
|
|
3266
|
+
this.#memory = memory;
|
|
3267
|
+
this.#numWorkers = numWorkers;
|
|
3268
|
+
}
|
|
3269
|
+
get epoch() {
|
|
3270
|
+
return this.#epoch;
|
|
3271
|
+
}
|
|
3272
|
+
waitForEpoch(target) {
|
|
3273
|
+
if (target <= this.#epoch) return Promise.resolve();
|
|
3274
|
+
return new Promise((resolve) => {
|
|
3275
|
+
if (target <= this.#epoch) return resolve();
|
|
3276
|
+
const hooks = this.#hooks.get(target);
|
|
3277
|
+
if (hooks) hooks.push(resolve);
|
|
3278
|
+
else this.#hooks.set(target, [resolve]);
|
|
3279
|
+
});
|
|
3280
|
+
}
|
|
3281
|
+
#ensureInit() {
|
|
3282
|
+
if (this.#workers.length > 0) return;
|
|
3283
|
+
const blob = new Blob([WORKER_SOURCE], { type: "application/javascript" });
|
|
3284
|
+
const url = URL.createObjectURL(blob);
|
|
3285
|
+
this.#workers = [];
|
|
3286
|
+
const readyPromises = [];
|
|
3287
|
+
for (let i = 0; i < this.#numWorkers; i++) {
|
|
3288
|
+
const worker = new Worker(url, { type: "module" });
|
|
3289
|
+
this.#workers.push(worker);
|
|
3290
|
+
readyPromises.push(new Promise((resolve, reject) => {
|
|
3291
|
+
worker.onmessage = () => resolve();
|
|
3292
|
+
worker.onerror = (e) => reject(new Error(e.message || "Worker failed to load"));
|
|
3293
|
+
}));
|
|
3294
|
+
worker.postMessage({
|
|
3295
|
+
type: "init",
|
|
3296
|
+
memory: this.#memory
|
|
3297
|
+
});
|
|
3298
|
+
}
|
|
3299
|
+
this.#ready = Promise.all(readyPromises).then(() => {
|
|
3300
|
+
URL.revokeObjectURL(url);
|
|
3301
|
+
});
|
|
3302
|
+
this.#queue = this.#ready;
|
|
3303
|
+
}
|
|
3304
|
+
/**
|
|
3305
|
+
* Dispatch a kernel across multiple workers.
|
|
3306
|
+
*
|
|
3307
|
+
* Returns an epoch that can be used to wait for the ongoing work to complete,
|
|
3308
|
+
* which is guaranteed to be monotonically increasing.
|
|
3309
|
+
*/
|
|
3310
|
+
dispatch(module$1, ptrs, size) {
|
|
3311
|
+
this.#ensureInit();
|
|
3312
|
+
this.#epochEnd++;
|
|
3313
|
+
const result = this.#queue.then(() => this.#dispatchNow(module$1, ptrs, size));
|
|
3314
|
+
this.#queue = result.then(() => {}, () => {}).then(() => {
|
|
3315
|
+
this.#epoch++;
|
|
3316
|
+
const hooks = this.#hooks.get(this.#epoch);
|
|
3317
|
+
if (hooks) {
|
|
3318
|
+
for (const hook of hooks) hook();
|
|
3319
|
+
this.#hooks.delete(this.#epoch);
|
|
3320
|
+
}
|
|
3321
|
+
});
|
|
3322
|
+
return this.#epochEnd;
|
|
3323
|
+
}
|
|
3324
|
+
async #dispatchNow(module$1, ptrs, size) {
|
|
3325
|
+
if (size === 0) return;
|
|
3326
|
+
const n = Math.min(this.#workers.length, Math.ceil(size / MIN_ELEMS_PER_THREAD));
|
|
3327
|
+
const chunkSize = Math.ceil(size / n / 16) * 16;
|
|
3328
|
+
const promises = [];
|
|
3329
|
+
for (let i = 0; i < n; i++) {
|
|
3330
|
+
const begin = i * chunkSize;
|
|
3331
|
+
const end = Math.min(begin + chunkSize, size);
|
|
3332
|
+
if (begin >= size) break;
|
|
3333
|
+
const worker = this.#workers[i];
|
|
3334
|
+
promises.push(new Promise((resolve, reject) => {
|
|
3335
|
+
worker.onmessage = (e) => {
|
|
3336
|
+
if (e.data.ok) resolve();
|
|
3337
|
+
else reject(/* @__PURE__ */ new Error(`Worker error: ${e.data.error}`));
|
|
3338
|
+
};
|
|
3339
|
+
worker.postMessage({
|
|
3340
|
+
module: module$1,
|
|
3341
|
+
ptrs,
|
|
3342
|
+
begin,
|
|
3343
|
+
end
|
|
3344
|
+
});
|
|
3345
|
+
}));
|
|
3346
|
+
}
|
|
3347
|
+
await Promise.all(promises);
|
|
3348
|
+
}
|
|
3349
|
+
};
|
|
3350
|
+
/** Try to create a worker pool. Returns null if workers are unavailable. */
|
|
3351
|
+
function createWorkerPool(memory) {
|
|
3352
|
+
if (!hasSharedArrayBuffer()) return null;
|
|
3353
|
+
try {
|
|
3354
|
+
const numWorkers = Math.max(1, typeof navigator !== "undefined" && navigator.hardwareConcurrency || 4);
|
|
3355
|
+
return new WasmWorkerPool(memory, numWorkers);
|
|
3356
|
+
} catch {
|
|
3357
|
+
return null;
|
|
3358
|
+
}
|
|
3359
|
+
}
|
|
3360
|
+
|
|
3162
3361
|
//#endregion
|
|
3163
3362
|
//#region src/backend/wasm/wasmblr.ts
|
|
3164
3363
|
/**
|
|
@@ -3496,7 +3695,7 @@ var CodeGenerator = class {
|
|
|
3496
3695
|
concat(importSectionBytes, encodeString(this.memory.aString));
|
|
3497
3696
|
concat(importSectionBytes, encodeString(this.memory.bString));
|
|
3498
3697
|
importSectionBytes.push(2);
|
|
3499
|
-
if (this.memory.
|
|
3698
|
+
if (this.memory.max) {
|
|
3500
3699
|
if (this.memory.isShared) importSectionBytes.push(3);
|
|
3501
3700
|
else importSectionBytes.push(1);
|
|
3502
3701
|
concat(importSectionBytes, encodeUnsigned(this.memory.min));
|
|
@@ -3903,6 +4102,8 @@ var I32x4 = class extends V128 {
|
|
|
3903
4102
|
min_u = VECTOR_OP("min_u", 183, ["v128", "v128"], "v128");
|
|
3904
4103
|
max_s = VECTOR_OP("max_s", 184, ["v128", "v128"], "v128");
|
|
3905
4104
|
max_u = VECTOR_OP("max_u", 185, ["v128", "v128"], "v128");
|
|
4105
|
+
trunc_sat_f32x4_s = VECTOR_OP("trunc_sat_f32x4_s", 248, ["v128"], "v128");
|
|
4106
|
+
trunc_sat_f32x4_u = VECTOR_OP("trunc_sat_f32x4_u", 249, ["v128"], "v128");
|
|
3906
4107
|
};
|
|
3907
4108
|
var F32x4 = class extends V128 {
|
|
3908
4109
|
splat = VECTOR_OP("splat", 19, ["f32"], "v128");
|
|
@@ -3929,10 +4130,333 @@ var F32x4 = class extends V128 {
|
|
|
3929
4130
|
max = VECTOR_OP("max", 233, ["v128", "v128"], "v128");
|
|
3930
4131
|
pmin = VECTOR_OP("pmin", 234, ["v128", "v128"], "v128");
|
|
3931
4132
|
pmax = VECTOR_OP("pmax", 235, ["v128", "v128"], "v128");
|
|
4133
|
+
convert_i32x4_s = VECTOR_OP("convert_i32x4_s", 250, ["v128"], "v128");
|
|
4134
|
+
convert_i32x4_u = VECTOR_OP("convert_i32x4_u", 251, ["v128"], "v128");
|
|
3932
4135
|
};
|
|
3933
4136
|
|
|
3934
4137
|
//#endregion
|
|
3935
4138
|
//#region src/backend/wasm.ts
|
|
4139
|
+
/**
|
|
4140
|
+
* SIMD version of translateExp: emits v128 (f32x4 or i32x4) instructions instead of scalar.
|
|
4141
|
+
* gidx always steps by 4. strideMap classifies each GlobalIndex as broadcast/contiguous/gather.
|
|
4142
|
+
*/
|
|
4143
|
+
function translateExpSimd(cg, funcs, exp, ctx, strideMap) {
|
|
4144
|
+
const references = /* @__PURE__ */ new Map();
|
|
4145
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4146
|
+
const countReferences = (exp$1) => {
|
|
4147
|
+
references.set(exp$1, (references.get(exp$1) ?? 0) + 1);
|
|
4148
|
+
if (!seen.has(exp$1)) {
|
|
4149
|
+
seen.add(exp$1);
|
|
4150
|
+
for (const src of exp$1.src) countReferences(src);
|
|
4151
|
+
}
|
|
4152
|
+
};
|
|
4153
|
+
const expContext = /* @__PURE__ */ new Map();
|
|
4154
|
+
const gen = (exp$1) => {
|
|
4155
|
+
if (expContext.has(exp$1)) return cg.local.get(expContext.get(exp$1));
|
|
4156
|
+
const { op, src, arg, dtype } = exp$1;
|
|
4157
|
+
const isInt = dtype === DType.Int32 || dtype === DType.Uint32 || dtype === DType.Bool;
|
|
4158
|
+
const isSigned = dtype === DType.Int32;
|
|
4159
|
+
if (op === AluOp.Add) {
|
|
4160
|
+
gen(src[0]);
|
|
4161
|
+
gen(src[1]);
|
|
4162
|
+
if (isInt) cg.i32x4.add();
|
|
4163
|
+
else cg.f32x4.add();
|
|
4164
|
+
} else if (op === AluOp.Sub) {
|
|
4165
|
+
gen(src[0]);
|
|
4166
|
+
gen(src[1]);
|
|
4167
|
+
if (isInt) cg.i32x4.sub();
|
|
4168
|
+
else cg.f32x4.sub();
|
|
4169
|
+
} else if (op === AluOp.Mul) {
|
|
4170
|
+
gen(src[0]);
|
|
4171
|
+
gen(src[1]);
|
|
4172
|
+
if (isInt) cg.i32x4.mul();
|
|
4173
|
+
else cg.f32x4.mul();
|
|
4174
|
+
} else if (op === AluOp.Min) {
|
|
4175
|
+
gen(src[0]);
|
|
4176
|
+
gen(src[1]);
|
|
4177
|
+
if (isInt) if (isSigned) cg.i32x4.min_s();
|
|
4178
|
+
else cg.i32x4.min_u();
|
|
4179
|
+
else cg.f32x4.min();
|
|
4180
|
+
} else if (op === AluOp.Max) {
|
|
4181
|
+
gen(src[0]);
|
|
4182
|
+
gen(src[1]);
|
|
4183
|
+
if (isInt) if (isSigned) cg.i32x4.max_s();
|
|
4184
|
+
else cg.i32x4.max_u();
|
|
4185
|
+
else cg.f32x4.max();
|
|
4186
|
+
} else if (op === AluOp.Sqrt) {
|
|
4187
|
+
gen(src[0]);
|
|
4188
|
+
cg.f32x4.sqrt();
|
|
4189
|
+
} else if (op === AluOp.Floor) {
|
|
4190
|
+
gen(src[0]);
|
|
4191
|
+
cg.f32x4.floor();
|
|
4192
|
+
} else if (op === AluOp.Ceil) {
|
|
4193
|
+
gen(src[0]);
|
|
4194
|
+
cg.f32x4.ceil();
|
|
4195
|
+
} else if (op === AluOp.Const) if (isInt) {
|
|
4196
|
+
cg.i32.const(arg);
|
|
4197
|
+
cg.i32x4.splat();
|
|
4198
|
+
} else {
|
|
4199
|
+
cg.f32.const(arg);
|
|
4200
|
+
cg.f32x4.splat();
|
|
4201
|
+
}
|
|
4202
|
+
else if (op === AluOp.Cast) {
|
|
4203
|
+
gen(src[0]);
|
|
4204
|
+
const dtype0 = src[0].dtype;
|
|
4205
|
+
const src0IsInt = dtype0 === DType.Int32 || dtype0 === DType.Uint32 || dtype0 === DType.Bool;
|
|
4206
|
+
if (isInt && !src0IsInt) if (isSigned) cg.i32x4.trunc_sat_f32x4_s();
|
|
4207
|
+
else cg.i32x4.trunc_sat_f32x4_u();
|
|
4208
|
+
else if (!isInt && src0IsInt) if (dtype0 === DType.Int32 || dtype0 === DType.Bool) cg.f32x4.convert_i32x4_s();
|
|
4209
|
+
else cg.f32x4.convert_i32x4_u();
|
|
4210
|
+
} else if (op === AluOp.Cmplt) {
|
|
4211
|
+
gen(src[0]);
|
|
4212
|
+
gen(src[1]);
|
|
4213
|
+
const srcDtype = src[0].dtype;
|
|
4214
|
+
if (srcDtype === DType.Float32) cg.f32x4.lt();
|
|
4215
|
+
else if (srcDtype === DType.Int32) cg.i32x4.lt_s();
|
|
4216
|
+
else if (srcDtype === DType.Uint32) cg.i32x4.lt_u();
|
|
4217
|
+
else throw new UnsupportedOpError(op, dtype, "wasm");
|
|
4218
|
+
cg.i32.const(1);
|
|
4219
|
+
cg.i32x4.splat();
|
|
4220
|
+
cg.v128.and();
|
|
4221
|
+
} else if (op === AluOp.Cmpne) {
|
|
4222
|
+
gen(src[0]);
|
|
4223
|
+
gen(src[1]);
|
|
4224
|
+
const srcDtype = src[0].dtype;
|
|
4225
|
+
if (srcDtype === DType.Float32) cg.f32x4.ne();
|
|
4226
|
+
else cg.i32x4.ne();
|
|
4227
|
+
cg.i32.const(1);
|
|
4228
|
+
cg.i32x4.splat();
|
|
4229
|
+
cg.v128.and();
|
|
4230
|
+
} else if (op === AluOp.Where) {
|
|
4231
|
+
gen(src[1]);
|
|
4232
|
+
gen(src[2]);
|
|
4233
|
+
gen(src[0]);
|
|
4234
|
+
cg.i32.const(0);
|
|
4235
|
+
cg.i32x4.splat();
|
|
4236
|
+
cg.i32x4.ne();
|
|
4237
|
+
cg.v128.bitselect();
|
|
4238
|
+
} else if (op === AluOp.Variable || op === AluOp.Special) throw new Error(`translateExpSimd: unexpected ${op}(${arg})`);
|
|
4239
|
+
else if (op === AluOp.GlobalIndex) {
|
|
4240
|
+
const [gid, len] = arg;
|
|
4241
|
+
const indexSubtree = src[0];
|
|
4242
|
+
const stride = strideMap.get(exp$1) ?? GATHER;
|
|
4243
|
+
if (stride.kind === "contiguous") {
|
|
4244
|
+
translateExp(cg, funcs, indexSubtree, ctx);
|
|
4245
|
+
{
|
|
4246
|
+
const maxIdx = Math.max(len - SIMD_LANES, 0);
|
|
4247
|
+
const wideIdx = cg.local.declare(cg.i32);
|
|
4248
|
+
cg.local.set(wideIdx);
|
|
4249
|
+
cg.local.get(wideIdx);
|
|
4250
|
+
cg.i32.const(maxIdx);
|
|
4251
|
+
cg.local.get(wideIdx);
|
|
4252
|
+
cg.i32.const(maxIdx);
|
|
4253
|
+
cg.i32.lt_u();
|
|
4254
|
+
cg.select();
|
|
4255
|
+
}
|
|
4256
|
+
cg.i32.const(byteWidth(dtype));
|
|
4257
|
+
cg.i32.mul();
|
|
4258
|
+
cg.local.get(gid);
|
|
4259
|
+
cg.i32.add();
|
|
4260
|
+
if (isInt) cg.i32x4.load(4);
|
|
4261
|
+
else cg.f32x4.load(4);
|
|
4262
|
+
} else if (stride.kind === "broadcast") {
|
|
4263
|
+
translateExp(cg, funcs, indexSubtree, ctx);
|
|
4264
|
+
const local = cg.local.declare(cg.i32);
|
|
4265
|
+
cg.local.tee(local);
|
|
4266
|
+
cg.i32.const(0);
|
|
4267
|
+
cg.local.get(local), cg.i32.const(len), cg.i32.lt_u();
|
|
4268
|
+
cg.select();
|
|
4269
|
+
cg.i32.const(byteWidth(dtype));
|
|
4270
|
+
cg.i32.mul();
|
|
4271
|
+
cg.local.get(gid);
|
|
4272
|
+
cg.i32.add();
|
|
4273
|
+
if (isInt) {
|
|
4274
|
+
cg.i32.load(2);
|
|
4275
|
+
cg.i32x4.splat();
|
|
4276
|
+
} else {
|
|
4277
|
+
cg.f32.load(2);
|
|
4278
|
+
cg.f32x4.splat();
|
|
4279
|
+
}
|
|
4280
|
+
} else {
|
|
4281
|
+
const steppingLocal = ctx["gidx"];
|
|
4282
|
+
const origValue = cg.local.declare(cg.i32);
|
|
4283
|
+
cg.local.get(steppingLocal);
|
|
4284
|
+
cg.local.set(origValue);
|
|
4285
|
+
if (isInt) {
|
|
4286
|
+
cg.i32.const(0);
|
|
4287
|
+
cg.i32x4.splat();
|
|
4288
|
+
} else {
|
|
4289
|
+
cg.f32.const(0);
|
|
4290
|
+
cg.f32x4.splat();
|
|
4291
|
+
}
|
|
4292
|
+
const vec = cg.local.declare(isInt ? cg.i32x4 : cg.f32x4);
|
|
4293
|
+
cg.local.set(vec);
|
|
4294
|
+
const idx = cg.local.declare(cg.i32);
|
|
4295
|
+
const scalarVal = cg.local.declare(isInt ? cg.i32 : cg.f32);
|
|
4296
|
+
for (let lane = 0; lane < SIMD_LANES; lane++) {
|
|
4297
|
+
cg.local.get(origValue);
|
|
4298
|
+
if (lane > 0) {
|
|
4299
|
+
cg.i32.const(lane);
|
|
4300
|
+
cg.i32.add();
|
|
4301
|
+
}
|
|
4302
|
+
cg.local.set(steppingLocal);
|
|
4303
|
+
translateExp(cg, funcs, indexSubtree, ctx);
|
|
4304
|
+
cg.local.tee(idx);
|
|
4305
|
+
cg.i32.const(0);
|
|
4306
|
+
cg.local.get(idx), cg.i32.const(len), cg.i32.lt_u();
|
|
4307
|
+
cg.select();
|
|
4308
|
+
cg.i32.const(byteWidth(dtype));
|
|
4309
|
+
cg.i32.mul();
|
|
4310
|
+
cg.local.get(gid);
|
|
4311
|
+
cg.i32.add();
|
|
4312
|
+
if (isInt) cg.i32.load(2);
|
|
4313
|
+
else cg.f32.load(2);
|
|
4314
|
+
cg.local.set(scalarVal);
|
|
4315
|
+
cg.local.get(vec);
|
|
4316
|
+
cg.local.get(scalarVal);
|
|
4317
|
+
if (isInt) cg.i32x4.replace_lane(lane);
|
|
4318
|
+
else cg.f32x4.replace_lane(lane);
|
|
4319
|
+
cg.local.set(vec);
|
|
4320
|
+
}
|
|
4321
|
+
cg.local.get(origValue);
|
|
4322
|
+
cg.local.set(steppingLocal);
|
|
4323
|
+
cg.local.get(vec);
|
|
4324
|
+
}
|
|
4325
|
+
} else throw new Error(`translateExpSimd: unsupported op ${op}`);
|
|
4326
|
+
if ((references.get(exp$1) ?? 0) > 1) {
|
|
4327
|
+
const local = cg.local.declare(isInt ? cg.i32x4 : cg.f32x4);
|
|
4328
|
+
cg.local.tee(local);
|
|
4329
|
+
expContext.set(exp$1, local);
|
|
4330
|
+
}
|
|
4331
|
+
};
|
|
4332
|
+
countReferences(exp);
|
|
4333
|
+
gen(exp);
|
|
4334
|
+
}
|
|
4335
|
+
/** Number of SIMD lanes (f32x4 / i32x4 = 4 lanes). */
|
|
4336
|
+
const SIMD_LANES = 4;
|
|
4337
|
+
function referencesGidx(exp) {
|
|
4338
|
+
if (exp.op === AluOp.Special && exp.arg[0] === "gidx") return true;
|
|
4339
|
+
return exp.src.some(referencesGidx);
|
|
4340
|
+
}
|
|
4341
|
+
/** When tileSize > N but doesn't divide evenly, the last group before the
|
|
4342
|
+
* inner reset is shorter than N — a SIMD group could straddle it. */
|
|
4343
|
+
function hasFragmentRisk(tileSize, N) {
|
|
4344
|
+
return isFinite(tileSize) && tileSize > N && tileSize % N !== 0;
|
|
4345
|
+
}
|
|
4346
|
+
const GATHER = { kind: "gather" };
|
|
4347
|
+
/**
|
|
4348
|
+
* Classify how a GlobalIndex's index expression behaves as gidx increments.
|
|
4349
|
+
*/
|
|
4350
|
+
function analyzeStride(exp) {
|
|
4351
|
+
if (!referencesGidx(exp)) return {
|
|
4352
|
+
kind: "broadcast",
|
|
4353
|
+
tileSize: Infinity
|
|
4354
|
+
};
|
|
4355
|
+
if (exp.op === AluOp.Special && exp.arg[0] === "gidx") return {
|
|
4356
|
+
kind: "contiguous",
|
|
4357
|
+
tileSize: Infinity
|
|
4358
|
+
};
|
|
4359
|
+
if (exp.op === AluOp.Idiv && exp.src[1].op === AluOp.Const) {
|
|
4360
|
+
const N = exp.src[1].arg;
|
|
4361
|
+
const inner = analyzeStride(exp.src[0]);
|
|
4362
|
+
if (inner.kind === "broadcast") return inner;
|
|
4363
|
+
if (inner.kind !== "contiguous") return GATHER;
|
|
4364
|
+
if (hasFragmentRisk(inner.tileSize, N)) return GATHER;
|
|
4365
|
+
return {
|
|
4366
|
+
kind: "broadcast",
|
|
4367
|
+
tileSize: Math.min(inner.tileSize, N)
|
|
4368
|
+
};
|
|
4369
|
+
}
|
|
4370
|
+
if (exp.op === AluOp.Mod && exp.src[1].op === AluOp.Const) {
|
|
4371
|
+
const N = exp.src[1].arg;
|
|
4372
|
+
const inner = analyzeStride(exp.src[0]);
|
|
4373
|
+
if (inner.kind === "broadcast") return inner;
|
|
4374
|
+
if (inner.kind !== "contiguous") return GATHER;
|
|
4375
|
+
if (hasFragmentRisk(inner.tileSize, N)) return GATHER;
|
|
4376
|
+
return {
|
|
4377
|
+
kind: "contiguous",
|
|
4378
|
+
tileSize: Math.min(inner.tileSize, N)
|
|
4379
|
+
};
|
|
4380
|
+
}
|
|
4381
|
+
if (exp.op === AluOp.Mul) {
|
|
4382
|
+
for (let i = 0; i < 2; i++) if (exp.src[i].op === AluOp.Const) {
|
|
4383
|
+
const inner = analyzeStride(exp.src[1 - i]);
|
|
4384
|
+
if (inner.kind === "broadcast") return inner;
|
|
4385
|
+
return GATHER;
|
|
4386
|
+
}
|
|
4387
|
+
}
|
|
4388
|
+
if (exp.op === AluOp.Add) {
|
|
4389
|
+
const lhsHasGidx = referencesGidx(exp.src[0]);
|
|
4390
|
+
const rhsHasGidx = referencesGidx(exp.src[1]);
|
|
4391
|
+
if (lhsHasGidx && !rhsHasGidx) return analyzeStride(exp.src[0]);
|
|
4392
|
+
if (!lhsHasGidx && rhsHasGidx) return analyzeStride(exp.src[1]);
|
|
4393
|
+
}
|
|
4394
|
+
return GATHER;
|
|
4395
|
+
}
|
|
4396
|
+
/** Ops that have direct SIMD (f32x4) instruction variants. */
|
|
4397
|
+
const simdF32Ops = new Set([
|
|
4398
|
+
AluOp.Add,
|
|
4399
|
+
AluOp.Sub,
|
|
4400
|
+
AluOp.Mul,
|
|
4401
|
+
AluOp.Floor,
|
|
4402
|
+
AluOp.Ceil,
|
|
4403
|
+
AluOp.Min,
|
|
4404
|
+
AluOp.Max,
|
|
4405
|
+
AluOp.Sqrt,
|
|
4406
|
+
AluOp.Cast,
|
|
4407
|
+
AluOp.Where,
|
|
4408
|
+
AluOp.Const,
|
|
4409
|
+
AluOp.GlobalIndex
|
|
4410
|
+
]);
|
|
4411
|
+
/** Ops that have direct SIMD (i32x4) instruction variants. */
|
|
4412
|
+
const simdI32Ops = new Set([
|
|
4413
|
+
AluOp.Add,
|
|
4414
|
+
AluOp.Sub,
|
|
4415
|
+
AluOp.Mul,
|
|
4416
|
+
AluOp.Min,
|
|
4417
|
+
AluOp.Max,
|
|
4418
|
+
AluOp.Cast,
|
|
4419
|
+
AluOp.Where,
|
|
4420
|
+
AluOp.Const,
|
|
4421
|
+
AluOp.GlobalIndex
|
|
4422
|
+
]);
|
|
4423
|
+
/** Ops that produce Bool (i32x4 bitmask) in SIMD. */
|
|
4424
|
+
const simdBoolOps = new Set([
|
|
4425
|
+
AluOp.Cmplt,
|
|
4426
|
+
AluOp.Cmpne,
|
|
4427
|
+
AluOp.Const,
|
|
4428
|
+
AluOp.GlobalIndex
|
|
4429
|
+
]);
|
|
4430
|
+
/**
|
|
4431
|
+
* Check if a kernel is eligible for SIMD codegen.
|
|
4432
|
+
*
|
|
4433
|
+
* A kernel qualifies when:
|
|
4434
|
+
* - size >= 4 (need at least 4 elements for a SIMD group)
|
|
4435
|
+
* - For reductions: the reduction op has a SIMD variant for its dtype
|
|
4436
|
+
* - All nodes have a supported dtype (f32, i32, u32, bool) with SIMD variants
|
|
4437
|
+
*/
|
|
4438
|
+
function isSimdEligible(tunedExp, kernel) {
|
|
4439
|
+
if (kernel.size < SIMD_LANES) return false;
|
|
4440
|
+
if (kernel.reduction) {
|
|
4441
|
+
if (!simdSupportedOpsForDtype(kernel.reduction.dtype)?.has(kernel.reduction.op)) return false;
|
|
4442
|
+
}
|
|
4443
|
+
const check = (exp, visited) => {
|
|
4444
|
+
if (visited.has(exp)) return true;
|
|
4445
|
+
visited.add(exp);
|
|
4446
|
+
const supportedOps = simdSupportedOpsForDtype(exp.dtype);
|
|
4447
|
+
if (!supportedOps || !supportedOps.has(exp.op)) return false;
|
|
4448
|
+
if (exp.op === AluOp.GlobalIndex) return true;
|
|
4449
|
+
for (const child of exp.src) if (!check(child, visited)) return false;
|
|
4450
|
+
return true;
|
|
4451
|
+
};
|
|
4452
|
+
return check(tunedExp, /* @__PURE__ */ new Set());
|
|
4453
|
+
}
|
|
4454
|
+
function simdSupportedOpsForDtype(dtype) {
|
|
4455
|
+
if (dtype === DType.Float32) return simdF32Ops;
|
|
4456
|
+
if (dtype === DType.Int32 || dtype === DType.Uint32) return simdI32Ops;
|
|
4457
|
+
if (dtype === DType.Bool) return simdBoolOps;
|
|
4458
|
+
return null;
|
|
4459
|
+
}
|
|
3936
4460
|
const moduleCache = /* @__PURE__ */ new Map();
|
|
3937
4461
|
/** Backend that compiles into WebAssembly bytecode for immediate execution. */
|
|
3938
4462
|
var WasmBackend = class {
|
|
@@ -3942,11 +4466,18 @@ var WasmBackend = class {
|
|
|
3942
4466
|
#nextSlot;
|
|
3943
4467
|
#allocator;
|
|
3944
4468
|
#buffers;
|
|
4469
|
+
#workerPool;
|
|
4470
|
+
#pendingWork = /* @__PURE__ */ new Map();
|
|
3945
4471
|
constructor() {
|
|
3946
|
-
this.#memory = new WebAssembly.Memory({
|
|
4472
|
+
this.#memory = hasSharedArrayBuffer() ? new WebAssembly.Memory({
|
|
4473
|
+
initial: 0,
|
|
4474
|
+
maximum: 65536,
|
|
4475
|
+
shared: true
|
|
4476
|
+
}) : new WebAssembly.Memory({ initial: 0 });
|
|
3947
4477
|
this.#allocator = new WasmAllocator(this.#memory);
|
|
3948
4478
|
this.#nextSlot = 1;
|
|
3949
4479
|
this.#buffers = /* @__PURE__ */ new Map();
|
|
4480
|
+
this.#workerPool = createWorkerPool(this.#memory);
|
|
3950
4481
|
}
|
|
3951
4482
|
malloc(size, initialData) {
|
|
3952
4483
|
const ptr = this.#allocator.malloc(size);
|
|
@@ -3977,40 +4508,65 @@ var WasmBackend = class {
|
|
|
3977
4508
|
}
|
|
3978
4509
|
}
|
|
3979
4510
|
async read(slot, start, count) {
|
|
3980
|
-
|
|
4511
|
+
const epoch = this.#pendingWork.get(slot);
|
|
4512
|
+
if (epoch) await this.#workerPool.waitForEpoch(epoch);
|
|
4513
|
+
return this.#readData(slot, start, count);
|
|
3981
4514
|
}
|
|
3982
4515
|
readSync(slot, start, count) {
|
|
4516
|
+
const epoch = this.#pendingWork.get(slot);
|
|
4517
|
+
if (epoch && this.#workerPool.epoch < epoch) throw new Error("cannot read synchronously from a slot with async work");
|
|
4518
|
+
return this.#readData(slot, start, count);
|
|
4519
|
+
}
|
|
4520
|
+
#readData(slot, start, count) {
|
|
3983
4521
|
const buffer = this.#getBuffer(slot);
|
|
3984
4522
|
if (start === void 0) start = 0;
|
|
3985
4523
|
if (count === void 0) count = buffer.byteLength - start;
|
|
3986
|
-
return buffer.slice(start, start + count);
|
|
4524
|
+
if (buffer.buffer instanceof SharedArrayBuffer) return new Uint8Array(buffer.slice(start, start + count));
|
|
4525
|
+
else return buffer.slice(start, start + count);
|
|
3987
4526
|
}
|
|
3988
4527
|
async prepareKernel(kernel) {
|
|
3989
|
-
|
|
4528
|
+
const kernelHash = FpHash.hash(kernel);
|
|
4529
|
+
const module$1 = await runWithCacheAsync(moduleCache, kernelHash.toString(), () => WebAssembly.compile(codegenWasm(kernel)));
|
|
4530
|
+
return new Executable(kernel, {
|
|
4531
|
+
module: module$1,
|
|
4532
|
+
parallel: this.#workerPool !== null
|
|
4533
|
+
});
|
|
3990
4534
|
}
|
|
3991
4535
|
prepareKernelSync(kernel) {
|
|
3992
4536
|
const kernelHash = FpHash.hash(kernel);
|
|
3993
|
-
const module$1 = runWithCache(moduleCache, kernelHash.toString(), () =>
|
|
3994
|
-
|
|
3995
|
-
|
|
4537
|
+
const module$1 = runWithCache(moduleCache, kernelHash.toString(), () => new WebAssembly.Module(codegenWasm(kernel)));
|
|
4538
|
+
return new Executable(kernel, {
|
|
4539
|
+
module: module$1,
|
|
4540
|
+
parallel: false
|
|
3996
4541
|
});
|
|
3997
|
-
return new Executable(kernel, { module: module$1 });
|
|
3998
4542
|
}
|
|
3999
4543
|
async prepareRoutine(routine) {
|
|
4000
4544
|
return this.prepareRoutineSync(routine);
|
|
4001
4545
|
}
|
|
4002
4546
|
prepareRoutineSync(routine) {
|
|
4003
|
-
return new Executable(routine,
|
|
4547
|
+
return new Executable(routine, {
|
|
4548
|
+
module: void 0,
|
|
4549
|
+
parallel: false
|
|
4550
|
+
});
|
|
4004
4551
|
}
|
|
4005
4552
|
dispatch(exe, inputs, outputs) {
|
|
4006
4553
|
const tracing = isTracing();
|
|
4007
4554
|
const start = tracing ? performance.now() : 0;
|
|
4008
4555
|
if (exe.source instanceof Routine) runCpuRoutine(exe.source, inputs.map((slot) => this.#getBuffer(slot)), outputs.map((slot) => this.#getBuffer(slot)));
|
|
4009
4556
|
else {
|
|
4010
|
-
const instance = new WebAssembly.Instance(exe.data.module, { env: { memory: this.#memory } });
|
|
4011
|
-
const func = instance.exports.kernel;
|
|
4012
4557
|
const ptrs = [...inputs, ...outputs].map((slot) => this.#buffers.get(slot).ptr);
|
|
4013
|
-
|
|
4558
|
+
if (exe.data.parallel && this.#workerPool) {
|
|
4559
|
+
const epoch = this.#workerPool.dispatch(exe.data.module, ptrs, exe.source.size);
|
|
4560
|
+
for (const slot of outputs) this.#pendingWork.set(slot, epoch);
|
|
4561
|
+
} else {
|
|
4562
|
+
if (inputs.some((slot) => {
|
|
4563
|
+
const epoch = this.#pendingWork.get(slot);
|
|
4564
|
+
return epoch && this.#workerPool.epoch < epoch;
|
|
4565
|
+
})) throw new Error("cannot dispatch synchronously with pending async work");
|
|
4566
|
+
const instance = new WebAssembly.Instance(exe.data.module, { env: { memory: this.#memory } });
|
|
4567
|
+
const func = instance.exports.kernel;
|
|
4568
|
+
func(...ptrs, 0, exe.source.size);
|
|
4569
|
+
}
|
|
4014
4570
|
}
|
|
4015
4571
|
if (tracing) {
|
|
4016
4572
|
const info = traceSourceInfo(exe.source);
|
|
@@ -4023,12 +4579,36 @@ var WasmBackend = class {
|
|
|
4023
4579
|
return new Uint8Array(this.#memory.buffer, buffer.ptr, buffer.size);
|
|
4024
4580
|
}
|
|
4025
4581
|
};
|
|
4582
|
+
/** Emit a runtime guard: enter the if-block only when [begin, end) is SIMD-aligned. */
|
|
4583
|
+
function emitAlignmentGuard(cg, paramBegin, paramEnd) {
|
|
4584
|
+
const mask = SIMD_LANES - 1;
|
|
4585
|
+
cg.local.get(paramEnd);
|
|
4586
|
+
cg.local.get(paramBegin);
|
|
4587
|
+
cg.i32.sub();
|
|
4588
|
+
cg.i32.const(mask);
|
|
4589
|
+
cg.i32.and();
|
|
4590
|
+
cg.i32.eqz();
|
|
4591
|
+
cg.local.get(paramBegin);
|
|
4592
|
+
cg.i32.const(mask);
|
|
4593
|
+
cg.i32.and();
|
|
4594
|
+
cg.i32.eqz();
|
|
4595
|
+
cg.i32.and();
|
|
4596
|
+
cg.if(cg.void);
|
|
4597
|
+
}
|
|
4026
4598
|
function codegenWasm(kernel) {
|
|
4027
4599
|
const tune = tuneNullopt(kernel);
|
|
4028
4600
|
const re = kernel.reduction;
|
|
4029
4601
|
if (DEBUG >= 3) console.info(`kernel.exp: ${kernel.exp}\ntune.exp: ${tune.exp}`);
|
|
4602
|
+
const useSimd = isSimdEligible(tune.exp, kernel);
|
|
4603
|
+
const bufferStrides = /* @__PURE__ */ new Map();
|
|
4604
|
+
if (useSimd) tune.exp.collect((e) => e.op === AluOp.GlobalIndex).forEach((gi) => {
|
|
4605
|
+
const result = analyzeStride(gi.src[0]);
|
|
4606
|
+
if (result.kind !== "gather" && (result.tileSize < SIMD_LANES || isFinite(result.tileSize) && result.tileSize % SIMD_LANES !== 0)) bufferStrides.set(gi, GATHER);
|
|
4607
|
+
else bufferStrides.set(gi, result);
|
|
4608
|
+
});
|
|
4030
4609
|
const cg = new CodeGenerator();
|
|
4031
4610
|
cg.memory.import("env", "memory");
|
|
4611
|
+
if (hasSharedArrayBuffer()) cg.memory.pages(0, 65536).shared(true);
|
|
4032
4612
|
const distinctOps = mapSetUnion(tune.exp.distinctOps(), tune.epilogue?.distinctOps());
|
|
4033
4613
|
const funcs = {};
|
|
4034
4614
|
if (distinctOps.has(AluOp.Sin)) funcs.sin = wasm_sin(cg);
|
|
@@ -4040,12 +4620,127 @@ function codegenWasm(kernel) {
|
|
|
4040
4620
|
if (distinctOps.has(AluOp.Erf)) funcs.erf = wasm_erf(cg, funcs.exp);
|
|
4041
4621
|
if (distinctOps.has(AluOp.Erfc)) funcs.erfc = wasm_erfc(cg, funcs.exp);
|
|
4042
4622
|
if (distinctOps.has(AluOp.Threefry2x32)) funcs.threefry2x32 = wasm_threefry2x32(cg);
|
|
4043
|
-
const
|
|
4623
|
+
const paramBegin = kernel.nargs + 1;
|
|
4624
|
+
const paramEnd = kernel.nargs + 2;
|
|
4625
|
+
const kernelFunc = cg.function(rep(kernel.nargs + 3, cg.i32), [], () => {
|
|
4044
4626
|
const gidx = cg.local.declare(cg.i32);
|
|
4627
|
+
cg.local.get(paramBegin);
|
|
4628
|
+
cg.local.set(gidx);
|
|
4629
|
+
if (useSimd) {
|
|
4630
|
+
emitAlignmentGuard(cg, paramBegin, paramEnd);
|
|
4631
|
+
cg.loop(cg.void);
|
|
4632
|
+
if (!re) {
|
|
4633
|
+
cg.block(cg.void);
|
|
4634
|
+
cg.local.get(gidx);
|
|
4635
|
+
cg.local.get(paramEnd);
|
|
4636
|
+
cg.i32.ge_u();
|
|
4637
|
+
cg.br_if(0);
|
|
4638
|
+
cg.local.get(kernel.nargs);
|
|
4639
|
+
cg.local.get(gidx);
|
|
4640
|
+
cg.i32.const(byteWidth(kernel.dtype));
|
|
4641
|
+
cg.i32.mul();
|
|
4642
|
+
cg.i32.add();
|
|
4643
|
+
translateExpSimd(cg, funcs, tune.exp, { gidx }, bufferStrides);
|
|
4644
|
+
cg.v128.store(4);
|
|
4645
|
+
cg.local.get(gidx);
|
|
4646
|
+
cg.i32.const(SIMD_LANES);
|
|
4647
|
+
cg.i32.add();
|
|
4648
|
+
cg.local.set(gidx);
|
|
4649
|
+
cg.br(1);
|
|
4650
|
+
cg.end();
|
|
4651
|
+
} else {
|
|
4652
|
+
const reIsInt = kernel.exp.dtype === DType.Int32 || kernel.exp.dtype === DType.Uint32;
|
|
4653
|
+
cg.block(cg.void);
|
|
4654
|
+
cg.local.get(gidx);
|
|
4655
|
+
cg.local.get(paramEnd);
|
|
4656
|
+
cg.i32.ge_u();
|
|
4657
|
+
cg.br_if(0);
|
|
4658
|
+
const vecAcc = cg.local.declare(reIsInt ? cg.i32x4 : cg.f32x4);
|
|
4659
|
+
if (reIsInt) {
|
|
4660
|
+
cg.i32.const(re.identity);
|
|
4661
|
+
cg.i32x4.splat();
|
|
4662
|
+
} else {
|
|
4663
|
+
cg.f32.const(re.identity);
|
|
4664
|
+
cg.f32x4.splat();
|
|
4665
|
+
}
|
|
4666
|
+
cg.local.set(vecAcc);
|
|
4667
|
+
const ridx = cg.local.declare(cg.i32);
|
|
4668
|
+
cg.i32.const(0);
|
|
4669
|
+
cg.local.set(ridx);
|
|
4670
|
+
cg.loop(cg.void);
|
|
4671
|
+
cg.block(cg.void);
|
|
4672
|
+
cg.local.get(ridx);
|
|
4673
|
+
cg.i32.const(re.size);
|
|
4674
|
+
cg.i32.ge_u();
|
|
4675
|
+
cg.br_if(0);
|
|
4676
|
+
translateExpSimd(cg, funcs, tune.exp, {
|
|
4677
|
+
gidx,
|
|
4678
|
+
ridx
|
|
4679
|
+
}, bufferStrides);
|
|
4680
|
+
cg.local.get(vecAcc);
|
|
4681
|
+
if (reIsInt) if (re.op === AluOp.Add) cg.i32x4.add();
|
|
4682
|
+
else if (re.op === AluOp.Mul) cg.i32x4.mul();
|
|
4683
|
+
else if (re.op === AluOp.Min) if (re.dtype === DType.Int32) cg.i32x4.min_s();
|
|
4684
|
+
else cg.i32x4.min_u();
|
|
4685
|
+
else if (re.op === AluOp.Max) if (re.dtype === DType.Int32) cg.i32x4.max_s();
|
|
4686
|
+
else cg.i32x4.max_u();
|
|
4687
|
+
else throw new Error(`invalid SIMD reduction op: ${re.op}`);
|
|
4688
|
+
else if (re.op === AluOp.Add) cg.f32x4.add();
|
|
4689
|
+
else if (re.op === AluOp.Mul) cg.f32x4.mul();
|
|
4690
|
+
else if (re.op === AluOp.Min) cg.f32x4.min();
|
|
4691
|
+
else if (re.op === AluOp.Max) cg.f32x4.max();
|
|
4692
|
+
else throw new Error(`invalid SIMD reduction op: ${re.op}`);
|
|
4693
|
+
cg.local.set(vecAcc);
|
|
4694
|
+
cg.local.get(ridx);
|
|
4695
|
+
cg.i32.const(1);
|
|
4696
|
+
cg.i32.add();
|
|
4697
|
+
cg.local.set(ridx);
|
|
4698
|
+
cg.br(1);
|
|
4699
|
+
cg.end();
|
|
4700
|
+
cg.end();
|
|
4701
|
+
for (let lane = 0; lane < SIMD_LANES; lane++) {
|
|
4702
|
+
cg.local.get(kernel.nargs);
|
|
4703
|
+
cg.local.get(gidx);
|
|
4704
|
+
if (lane > 0) {
|
|
4705
|
+
cg.i32.const(lane);
|
|
4706
|
+
cg.i32.add();
|
|
4707
|
+
}
|
|
4708
|
+
cg.i32.const(byteWidth(kernel.dtype));
|
|
4709
|
+
cg.i32.mul();
|
|
4710
|
+
cg.i32.add();
|
|
4711
|
+
const acc = cg.local.declare(reIsInt ? cg.i32 : cg.f32);
|
|
4712
|
+
cg.local.get(vecAcc);
|
|
4713
|
+
if (reIsInt) cg.i32x4.extract_lane(lane);
|
|
4714
|
+
else cg.f32x4.extract_lane(lane);
|
|
4715
|
+
cg.local.set(acc);
|
|
4716
|
+
const laneGidx = cg.local.declare(cg.i32);
|
|
4717
|
+
cg.local.get(gidx);
|
|
4718
|
+
if (lane > 0) {
|
|
4719
|
+
cg.i32.const(lane);
|
|
4720
|
+
cg.i32.add();
|
|
4721
|
+
}
|
|
4722
|
+
cg.local.set(laneGidx);
|
|
4723
|
+
translateExp(cg, funcs, tune.epilogue, {
|
|
4724
|
+
acc,
|
|
4725
|
+
gidx: laneGidx
|
|
4726
|
+
});
|
|
4727
|
+
dty(cg, null, kernel.dtype).store(Math.log2(byteWidth(kernel.dtype)));
|
|
4728
|
+
}
|
|
4729
|
+
cg.local.get(gidx);
|
|
4730
|
+
cg.i32.const(SIMD_LANES);
|
|
4731
|
+
cg.i32.add();
|
|
4732
|
+
cg.local.set(gidx);
|
|
4733
|
+
cg.br(1);
|
|
4734
|
+
cg.end();
|
|
4735
|
+
}
|
|
4736
|
+
cg.end();
|
|
4737
|
+
cg.return();
|
|
4738
|
+
cg.end();
|
|
4739
|
+
}
|
|
4045
4740
|
cg.loop(cg.void);
|
|
4046
4741
|
cg.block(cg.void);
|
|
4047
4742
|
cg.local.get(gidx);
|
|
4048
|
-
cg.
|
|
4743
|
+
cg.local.get(paramEnd);
|
|
4049
4744
|
cg.i32.ge_u();
|
|
4050
4745
|
cg.br_if(0);
|
|
4051
4746
|
cg.local.get(kernel.nargs);
|
|
@@ -4184,6 +4879,11 @@ function translateExp(cg, funcs, exp, ctx) {
|
|
|
4184
4879
|
else cg.i32.gt_u();
|
|
4185
4880
|
cg.select();
|
|
4186
4881
|
} else throw new UnsupportedOpError(op, dtype, "wasm");
|
|
4882
|
+
else if (op === AluOp.BitCombine) if (arg === "and") cg.i32.and();
|
|
4883
|
+
else if (arg === "or") cg.i32.or();
|
|
4884
|
+
else cg.i32.xor();
|
|
4885
|
+
else if (op === AluOp.BitShift) if (arg === "shl") cg.i32.shl();
|
|
4886
|
+
else cg.i32.shr_u();
|
|
4187
4887
|
else if (op === AluOp.Cmplt) {
|
|
4188
4888
|
const srcDtype = src[0].dtype;
|
|
4189
4889
|
if (isFloatDtype(srcDtype)) dtyF(cg, op, srcDtype).lt();
|
|
@@ -4360,7 +5060,7 @@ async function createBackend(device) {
|
|
|
4360
5060
|
if (!navigator.gpu) return null;
|
|
4361
5061
|
const adapter = await navigator.gpu.requestAdapter({ powerPreference: "high-performance" });
|
|
4362
5062
|
if (!adapter) return null;
|
|
4363
|
-
const { WebGPUBackend } = await Promise.resolve().then(() => require("./webgpu-
|
|
5063
|
+
const { WebGPUBackend } = await Promise.resolve().then(() => require("./webgpu-uU9nnttc.cjs"));
|
|
4364
5064
|
const importantLimits = [
|
|
4365
5065
|
"maxBufferSize",
|
|
4366
5066
|
"maxComputeInvocationsPerWorkgroup",
|
|
@@ -4398,7 +5098,7 @@ async function createBackend(device) {
|
|
|
4398
5098
|
});
|
|
4399
5099
|
if (!gl) return null;
|
|
4400
5100
|
if (!gl.getExtension("EXT_color_buffer_float")) return null;
|
|
4401
|
-
const { WebGLBackend } = await Promise.resolve().then(() => require("./webgl-
|
|
5101
|
+
const { WebGLBackend } = await Promise.resolve().then(() => require("./webgl-Ovaaa-Qx.cjs"));
|
|
4402
5102
|
return new WebGLBackend(gl);
|
|
4403
5103
|
} else throw new Error(`Backend not found: ${device}`);
|
|
4404
5104
|
}
|
|
@@ -4432,6 +5132,15 @@ var UnsupportedRoutineError = class extends Error {
|
|
|
4432
5132
|
super(`routine '${name}' is not supported in ${device} backend`);
|
|
4433
5133
|
}
|
|
4434
5134
|
};
|
|
5135
|
+
/**
|
|
5136
|
+
* If the WebGPU backend has been initialized, return the `GPUDevice` that this
|
|
5137
|
+
* backend runs on. This is useful for sharing buffers.
|
|
5138
|
+
*/
|
|
5139
|
+
function getWebGPUDevice() {
|
|
5140
|
+
const backend = initializedBackends.get("webgpu");
|
|
5141
|
+
if (!backend) throw new Error("WebGPU backend not initialized, call init('webgpu') first");
|
|
5142
|
+
return backend.device;
|
|
5143
|
+
}
|
|
4435
5144
|
|
|
4436
5145
|
//#endregion
|
|
4437
5146
|
Object.defineProperty(exports, 'AluExp', {
|
|
@@ -4626,6 +5335,12 @@ Object.defineProperty(exports, 'getBackend', {
|
|
|
4626
5335
|
return getBackend;
|
|
4627
5336
|
}
|
|
4628
5337
|
});
|
|
5338
|
+
Object.defineProperty(exports, 'getWebGPUDevice', {
|
|
5339
|
+
enumerable: true,
|
|
5340
|
+
get: function () {
|
|
5341
|
+
return getWebGPUDevice;
|
|
5342
|
+
}
|
|
5343
|
+
});
|
|
4629
5344
|
Object.defineProperty(exports, 'init', {
|
|
4630
5345
|
enumerable: true,
|
|
4631
5346
|
get: function () {
|