@simulatte/doppler 0.1.6 → 0.1.7
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/CHANGELOG.md +126 -0
- package/README.md +16 -23
- package/package.json +14 -1
- package/src/adapters/adapter-registry.js +12 -1
- package/src/adapters/lora-loader.js +23 -6
- package/src/bridge/extension-client.d.ts +5 -0
- package/src/bridge/extension-client.js +40 -0
- package/src/bridge/index.d.ts +2 -1
- package/src/bridge/index.js +6 -4
- package/src/browser/browser-converter.js +26 -1
- package/src/browser/file-picker.js +6 -0
- package/src/browser/safetensors-parser-browser.js +84 -1
- package/src/browser/shard-io-browser.js +2 -2
- package/src/browser/tensor-source-download.js +8 -2
- package/src/browser/tensor-source-http.d.ts +1 -0
- package/src/browser/tensor-source-http.js +5 -1
- package/src/client/doppler-api.browser.js +20 -4
- package/src/client/doppler-api.js +19 -3
- package/src/client/doppler-provider/generation.js +12 -0
- package/src/client/doppler-provider/model-manager.d.ts +10 -0
- package/src/client/doppler-provider/model-manager.js +91 -19
- package/src/client/doppler-provider/source-runtime.d.ts +2 -1
- package/src/client/doppler-provider/source-runtime.js +132 -13
- package/src/client/doppler-registry.json +8 -7
- package/src/config/backward-registry-loader.js +17 -2
- package/src/config/execution-v0-contract-check.js +113 -15
- package/src/config/kernel-path-contract-check.js +57 -29
- package/src/config/kernel-path-loader.js +5 -36
- package/src/config/kernels/kernel-ref-digests.js +1 -1
- package/src/config/kernels/registry.js +14 -1
- package/src/config/kernels/registry.json +7 -5
- package/src/config/loader.d.ts +1 -1
- package/src/config/loader.js +12 -2
- package/src/config/merge-contract-check.js +59 -4
- package/src/config/merge-helpers.js +128 -7
- package/src/config/merge.d.ts +1 -0
- package/src/config/merge.js +10 -0
- package/src/config/param-validator.js +47 -2
- package/src/config/presets/kernel-paths/{gemma2-q4k-dequant-f32a.json → gemma2-q4k-dequant-f32a-nosubgroups.json} +3 -3
- package/src/config/presets/kernel-paths/gemma3-f16-fused-f32a-online-streamingprefill.json +223 -0
- package/src/config/presets/kernel-paths/{gemma3-q4k-dequant-f32a.json → gemma3-q4k-dequant-f32a-nosubgroups.json} +3 -3
- package/src/config/presets/kernel-paths/registry.json +29 -8
- package/src/config/presets/models/gemma2.json +2 -2
- package/src/config/presets/runtime/experiments/bench/gemma3-bench-q4k.json +1 -1
- package/src/config/presets/runtime/experiments/debug/gemma3-debug-q4k.json +1 -1
- package/src/config/presets/runtime/experiments/verify/gemma3-verify.json +1 -1
- package/src/config/presets/runtime/kernels/dequant-f16-q4k.json +6 -13
- package/src/config/presets/runtime/kernels/dequant-f32-q4k.json +6 -13
- package/src/config/presets/runtime/kernels/embeddinggemma-q4k-dequant-f32a.json +37 -0
- package/src/config/presets/runtime/kernels/fused-q4k.json +6 -13
- package/src/config/presets/runtime/kernels/gemma2-q4k-dequant-f16a.json +33 -0
- package/src/config/presets/runtime/kernels/gemma2-q4k-dequant-f32a-nosubgroups.json +33 -0
- package/src/config/presets/runtime/kernels/gemma2-q4k-fused-f32a.json +33 -0
- package/src/config/presets/runtime/kernels/safe-q4k.json +6 -13
- package/src/config/presets/runtime/platform/metal-apple-q4k.json +1 -1
- package/src/config/runtime.js +6 -1
- package/src/config/schema/debug.schema.d.ts +5 -0
- package/src/config/schema/doppler.schema.js +16 -21
- package/src/config/schema/inference-defaults.schema.js +3 -3
- package/src/config/schema/kernel-path.schema.d.ts +5 -1
- package/src/config/schema/kernel-thresholds.schema.js +12 -4
- package/src/config/schema/manifest.schema.d.ts +2 -1
- package/src/config/schema/manifest.schema.js +16 -3
- package/src/config/training-defaults.js +30 -22
- package/src/converter/conversion-plan.js +94 -9
- package/src/converter/core.d.ts +7 -0
- package/src/converter/core.js +14 -9
- package/src/converter/execution-v0-manifest.js +4 -1
- package/src/converter/index.d.ts +1 -0
- package/src/converter/index.js +1 -0
- package/src/converter/manifest-inference.js +43 -12
- package/src/converter/parsers/diffusion.js +0 -3
- package/src/converter/quantization-info.js +35 -15
- package/src/converter/shard-packer.d.ts +1 -1
- package/src/converter/shard-packer.js +4 -1
- package/src/debug/config.js +123 -11
- package/src/debug/signals.js +7 -1
- package/src/debug/tensor.d.ts +2 -0
- package/src/debug/tensor.js +13 -2
- package/src/distribution/p2p-control-plane.js +52 -12
- package/src/distribution/p2p-observability.js +43 -7
- package/src/distribution/p2p-webrtc-browser.js +20 -0
- package/src/distribution/shard-delivery.js +77 -26
- package/src/formats/gguf/types.js +33 -16
- package/src/formats/rdrr/groups.d.ts +12 -4
- package/src/formats/rdrr/groups.js +3 -6
- package/src/formats/rdrr/parsing.js +39 -2
- package/src/formats/rdrr/types.d.ts +2 -1
- package/src/gpu/command-recorder.js +86 -61
- package/src/gpu/device.d.ts +1 -0
- package/src/gpu/device.js +73 -19
- package/src/gpu/kernel-tuner/benchmarks.js +326 -316
- package/src/gpu/kernel-tuner/cache.js +71 -4
- package/src/gpu/kernel-tuner/tuner.js +22 -4
- package/src/gpu/kernels/attention.js +15 -34
- package/src/gpu/kernels/backward/adam.js +62 -58
- package/src/gpu/kernels/backward/attention_backward.js +257 -169
- package/src/gpu/kernels/backward/conv2d_backward.js +14 -1
- package/src/gpu/kernels/cast.js +191 -149
- package/src/gpu/kernels/check-stop.js +33 -44
- package/src/gpu/kernels/conv2d.js +27 -17
- package/src/gpu/kernels/cross_entropy_loss.js +21 -15
- package/src/gpu/kernels/depthwise_conv2d.js +36 -26
- package/src/gpu/kernels/dequant.js +178 -126
- package/src/gpu/kernels/energy.d.ts +3 -21
- package/src/gpu/kernels/energy.js +111 -88
- package/src/gpu/kernels/feature-check.js +1 -1
- package/src/gpu/kernels/fused_ffn.js +84 -65
- package/src/gpu/kernels/fused_matmul_residual.js +56 -33
- package/src/gpu/kernels/fused_matmul_rmsnorm.js +62 -45
- package/src/gpu/kernels/gather.js +33 -15
- package/src/gpu/kernels/gelu.js +19 -11
- package/src/gpu/kernels/grouped_pointwise_conv2d.js +33 -23
- package/src/gpu/kernels/groupnorm.js +34 -23
- package/src/gpu/kernels/kv-quantize.js +5 -2
- package/src/gpu/kernels/layernorm.js +35 -19
- package/src/gpu/kernels/logit-merge.js +5 -3
- package/src/gpu/kernels/matmul.js +58 -39
- package/src/gpu/kernels/modulate.js +23 -15
- package/src/gpu/kernels/moe.js +221 -175
- package/src/gpu/kernels/pixel_shuffle.js +22 -14
- package/src/gpu/kernels/relu.js +18 -10
- package/src/gpu/kernels/repeat_channels.js +25 -17
- package/src/gpu/kernels/residual.js +37 -27
- package/src/gpu/kernels/rmsnorm.js +57 -41
- package/src/gpu/kernels/rope.js +3 -0
- package/src/gpu/kernels/sample.js +27 -38
- package/src/gpu/kernels/sana_linear_attention.js +18 -10
- package/src/gpu/kernels/scale.js +18 -11
- package/src/gpu/kernels/shader-cache.js +4 -2
- package/src/gpu/kernels/silu.js +120 -72
- package/src/gpu/kernels/softmax.js +44 -25
- package/src/gpu/kernels/split_qkv.js +23 -13
- package/src/gpu/kernels/transpose.js +18 -10
- package/src/gpu/kernels/transpose.wgsl +5 -3
- package/src/gpu/kernels/upsample2d.js +21 -13
- package/src/gpu/kernels/utils.js +20 -13
- package/src/gpu/partitioned-buffer-pool.js +10 -2
- package/src/gpu/perf-guards.js +2 -9
- package/src/gpu/profiler.js +27 -22
- package/src/gpu/readback-utils.d.ts +16 -0
- package/src/gpu/readback-utils.js +41 -0
- package/src/gpu/submit-tracker.js +13 -0
- package/src/gpu/uniform-cache.d.ts +1 -0
- package/src/gpu/uniform-cache.js +30 -9
- package/src/hotswap/intent-bundle.js +6 -0
- package/src/hotswap/manifest.d.ts +10 -1
- package/src/hotswap/manifest.js +12 -2
- package/src/hotswap/runtime.js +30 -8
- package/src/index-browser.d.ts +44 -0
- package/src/index-browser.js +14 -0
- package/src/inference/browser-harness-contract-helpers.d.ts +5 -0
- package/src/inference/browser-harness-contract-helpers.js +28 -0
- package/src/inference/browser-harness-diffusion-energy-suites.d.ts +2 -0
- package/src/inference/browser-harness-diffusion-energy-suites.js +269 -0
- package/src/inference/browser-harness-model-helpers.d.ts +16 -0
- package/src/inference/browser-harness-model-helpers.js +217 -0
- package/src/inference/browser-harness-report-helpers.d.ts +7 -0
- package/src/inference/browser-harness-report-helpers.js +42 -0
- package/src/inference/browser-harness-runtime-helpers.d.ts +61 -0
- package/src/inference/browser-harness-runtime-helpers.js +415 -0
- package/src/inference/browser-harness-suite-helpers.d.ts +28 -0
- package/src/inference/browser-harness-suite-helpers.js +268 -0
- package/src/inference/browser-harness-text-helpers.d.ts +27 -0
- package/src/inference/browser-harness-text-helpers.js +788 -0
- package/src/inference/browser-harness.d.ts +6 -0
- package/src/inference/browser-harness.js +130 -1996
- package/src/inference/kv-cache/base.js +140 -94
- package/src/inference/kv-cache/tiered.js +5 -3
- package/src/inference/moe-router.js +88 -56
- package/src/inference/multi-model-network.js +5 -3
- package/src/inference/network-evolution.d.ts +11 -2
- package/src/inference/network-evolution.js +20 -21
- package/src/inference/pipelines/context.d.ts +3 -0
- package/src/inference/pipelines/context.js +142 -2
- package/src/inference/pipelines/diffusion/helpers.js +7 -2
- package/src/inference/pipelines/diffusion/pipeline.js +2 -1
- package/src/inference/pipelines/diffusion/sd3-transformer.js +10 -10
- package/src/inference/pipelines/diffusion/vae.js +3 -7
- package/src/inference/pipelines/energy/pipeline.js +27 -21
- package/src/inference/pipelines/energy/quintel.d.ts +5 -0
- package/src/inference/pipelines/energy/quintel.js +11 -0
- package/src/inference/pipelines/energy-head/row-head-pipeline.js +17 -13
- package/src/inference/pipelines/structured/json-head-pipeline.js +26 -11
- package/src/inference/pipelines/text/attention/projections.js +151 -101
- package/src/inference/pipelines/text/attention/record.js +62 -8
- package/src/inference/pipelines/text/attention/run.js +62 -8
- package/src/inference/pipelines/text/config.js +3 -4
- package/src/inference/pipelines/text/embed.js +2 -8
- package/src/inference/pipelines/text/execution-plan.js +41 -19
- package/src/inference/pipelines/text/execution-v0-contract-helpers.d.ts +59 -0
- package/src/inference/pipelines/text/execution-v0-contract-helpers.js +937 -0
- package/src/inference/pipelines/text/execution-v0-runtime-builders.d.ts +15 -0
- package/src/inference/pipelines/text/execution-v0-runtime-builders.js +279 -0
- package/src/inference/pipelines/text/execution-v0.js +62 -1013
- package/src/inference/pipelines/text/generator-steps.d.ts +46 -0
- package/src/inference/pipelines/text/generator-steps.js +298 -207
- package/src/inference/pipelines/text/generator.js +6 -23
- package/src/inference/pipelines/text/init.js +78 -20
- package/src/inference/pipelines/text/kernel-path-auto-select.js +2 -0
- package/src/inference/pipelines/text/kernel-trace.d.ts +2 -0
- package/src/inference/pipelines/text/kernel-trace.js +6 -0
- package/src/inference/pipelines/text/layer.js +3 -9
- package/src/inference/pipelines/text/linear-attention.d.ts +10 -0
- package/src/inference/pipelines/text/linear-attention.js +80 -6
- package/src/inference/pipelines/text/logits/gpu.js +10 -5
- package/src/inference/pipelines/text/logits/index.js +10 -11
- package/src/inference/pipelines/text/logits/utils.d.ts +7 -0
- package/src/inference/pipelines/text/logits/utils.js +9 -0
- package/src/inference/pipelines/text/lora-apply.js +50 -32
- package/src/inference/pipelines/text/model-load.js +279 -104
- package/src/inference/pipelines/text/moe-cache.js +5 -4
- package/src/inference/pipelines/text/moe-cpu-gptoss.js +74 -69
- package/src/inference/pipelines/text/moe-cpu.js +42 -38
- package/src/inference/pipelines/text/moe-gpu.js +110 -86
- package/src/inference/pipelines/text/ops.js +90 -90
- package/src/inference/pipelines/text/probes.js +9 -9
- package/src/inference/pipelines/text/weights.js +17 -7
- package/src/inference/pipelines/text.js +13 -1
- package/src/inference/speculative.d.ts +2 -2
- package/src/inference/speculative.js +4 -18
- package/src/inference/test-harness.d.ts +1 -1
- package/src/inference/test-harness.js +15 -5
- package/src/inference/tokenizer.d.ts +0 -5
- package/src/inference/tokenizer.js +4 -23
- package/src/inference/tokenizers/bpe.js +9 -0
- package/src/inference/tokenizers/bundled.js +20 -0
- package/src/inference/tokenizers/sentencepiece.js +12 -0
- package/src/loader/doppler-loader.js +38 -22
- package/src/loader/dtype-utils.js +3 -44
- package/src/loader/embedding-loader.js +7 -3
- package/src/loader/experts/expert-cache.js +13 -6
- package/src/loader/experts/expert-loader.js +10 -6
- package/src/loader/final-weights-loader.js +8 -4
- package/src/loader/layer-loader.js +2 -1
- package/src/loader/loader-state.js +2 -2
- package/src/loader/memory-monitor.js +8 -0
- package/src/loader/multi-model-loader.d.ts +14 -0
- package/src/loader/multi-model-loader.js +70 -24
- package/src/loader/shard-cache.js +81 -12
- package/src/loader/shard-resolver.js +25 -3
- package/src/loader/tensors/tensor-loader.js +209 -144
- package/src/loader/tensors/tensor-reader.js +76 -19
- package/src/loader/weight-downcast.js +1 -1
- package/src/memory/buffer-pool.d.ts +9 -1
- package/src/memory/buffer-pool.js +109 -44
- package/src/memory/unified-detect.js +1 -1
- package/src/rules/inference/kernel-path.rules.json +24 -8
- package/src/rules/rule-registry.js +25 -1
- package/src/storage/backends/opfs-store.js +68 -24
- package/src/storage/downloader.js +364 -83
- package/src/storage/index.d.ts +3 -0
- package/src/storage/index.js +3 -0
- package/src/storage/preflight.d.ts +2 -2
- package/src/storage/preflight.js +24 -2
- package/src/storage/quickstart-downloader.js +11 -5
- package/src/storage/registry.js +10 -4
- package/src/storage/reports.js +1 -1
- package/src/storage/shard-manager.d.ts +15 -1
- package/src/storage/shard-manager.js +51 -3
- package/src/storage/source-artifact-store.d.ts +52 -0
- package/src/storage/source-artifact-store.js +234 -0
- package/src/tooling/command-api-constants.d.ts +9 -0
- package/src/tooling/command-api-constants.js +9 -0
- package/src/tooling/command-api-family-normalizers.d.ts +9 -0
- package/src/tooling/command-api-family-normalizers.js +343 -0
- package/src/tooling/command-api-helpers.d.ts +25 -0
- package/src/tooling/command-api-helpers.js +262 -0
- package/src/tooling/command-api.js +16 -602
- package/src/tooling/command-envelope.js +4 -1
- package/src/tooling/command-runner-shared.js +52 -18
- package/src/tooling/lean-execution-contract.js +150 -3
- package/src/tooling/node-browser-command-runner.js +161 -271
- package/src/tooling/node-command-runner.js +29 -3
- package/src/tooling/node-converter.js +27 -1
- package/src/tooling/node-source-runtime.d.ts +1 -1
- package/src/tooling/node-source-runtime.js +84 -3
- package/src/tooling/node-webgpu.js +24 -21
- package/src/tooling/opfs-cache.js +21 -4
- package/src/tooling/runtime-input-composition.d.ts +38 -0
- package/src/tooling/runtime-input-composition.js +86 -0
- package/src/tooling/source-runtime-bundle.d.ts +40 -5
- package/src/tooling/source-runtime-bundle.js +261 -34
- package/src/tooling/source-runtime-materializer.d.ts +6 -0
- package/src/tooling/source-runtime-materializer.js +93 -0
- package/src/training/attention-backward.js +32 -17
- package/src/training/autograd.js +80 -52
- package/src/training/checkpoint-watch.d.ts +2 -1
- package/src/training/checkpoint-watch.js +39 -6
- package/src/training/checkpoint.js +40 -11
- package/src/training/clip.js +2 -1
- package/src/training/datasets/token-batch.js +20 -8
- package/src/training/distillation/checkpoint-watch.js +1 -0
- package/src/training/distillation/student-fixture.d.ts +22 -0
- package/src/training/distillation/student-fixture.js +846 -0
- package/src/training/distillation/suite-data.d.ts +45 -0
- package/src/training/distillation/suite-data.js +189 -0
- package/src/training/lora-pipeline.js +4 -7
- package/src/training/lora.js +26 -12
- package/src/training/loss.js +5 -6
- package/src/training/objectives/cross_entropy.js +2 -5
- package/src/training/objectives/distill_kd.js +4 -8
- package/src/training/objectives/distill_triplet.js +4 -8
- package/src/training/objectives/ul_stage2_base.js +4 -8
- package/src/training/operator-command.js +2 -0
- package/src/training/optimizer.js +19 -7
- package/src/training/runner.js +2 -1
- package/src/training/suite.js +18 -978
- package/src/training/tensor-factory.d.ts +9 -0
- package/src/training/tensor-factory.js +13 -0
- package/src/training/trainer.js +3 -5
- package/src/training/ul_dataset.js +3 -5
- package/src/training/workloads.js +70 -79
- package/src/version.js +1 -1
- package/tools/convert-safetensors-node.js +22 -16
- package/tools/doppler-cli.js +44 -25
package/src/gpu/kernels/cast.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
3
|
import { getDevice } from '../device.js';
|
|
4
|
-
import { acquireBuffer } from '../../memory/buffer-pool.js';
|
|
4
|
+
import { acquireBuffer, releaseBuffer } from '../../memory/buffer-pool.js';
|
|
5
5
|
import { createTensor } from '../tensor.js';
|
|
6
6
|
import { dispatch, recordDispatch } from './dispatch.js';
|
|
7
7
|
import { createPipeline, createUniformBufferWithView } from './utils.js';
|
|
@@ -44,6 +44,7 @@ export async function castF32ToF16(
|
|
|
44
44
|
) {
|
|
45
45
|
const device = getDevice();
|
|
46
46
|
const { outputBuffer = null } = options;
|
|
47
|
+
const ownsOutput = outputBuffer == null;
|
|
47
48
|
const numElements = input.shape.reduce((a, b) => a * b, 1);
|
|
48
49
|
|
|
49
50
|
const pipeline = await createPipeline('cast', 'f32_to_f16');
|
|
@@ -51,35 +52,41 @@ export async function castF32ToF16(
|
|
|
51
52
|
const outputSize = numElements * DTYPE_SIZES.f16;
|
|
52
53
|
const output = outputBuffer || acquireBuffer(outputSize, undefined, 'cast_f32_to_f16_output');
|
|
53
54
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
label: 'cast_f32_to_f16_bind_group',
|
|
66
|
-
layout: pipeline.getBindGroupLayout(0),
|
|
67
|
-
entries: [
|
|
68
|
-
{ binding: 0, resource: { buffer: uniformBuffer } },
|
|
69
|
-
{ binding: 1, resource: { buffer: input.buffer } },
|
|
70
|
-
{ binding: 2, resource: { buffer: output } },
|
|
71
|
-
],
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
// Use 2D dispatch for large tensors (like embeddings with 300M+ elements)
|
|
75
|
-
const workgroups = Math.ceil(numElements / WORKGROUP_SIZES.DEFAULT);
|
|
76
|
-
const dispatchSize = calculate2DDispatch(workgroups);
|
|
55
|
+
let uniformBuffer = null;
|
|
56
|
+
try {
|
|
57
|
+
uniformBuffer = createUniformBufferWithView(
|
|
58
|
+
'cast_f32_to_f16_uniforms',
|
|
59
|
+
16,
|
|
60
|
+
(view) => {
|
|
61
|
+
view.setUint32(0, numElements, true);
|
|
62
|
+
},
|
|
63
|
+
null,
|
|
64
|
+
device
|
|
65
|
+
);
|
|
77
66
|
|
|
78
|
-
|
|
67
|
+
const bindGroup = device.createBindGroup({
|
|
68
|
+
label: 'cast_f32_to_f16_bind_group',
|
|
69
|
+
layout: pipeline.getBindGroupLayout(0),
|
|
70
|
+
entries: [
|
|
71
|
+
{ binding: 0, resource: { buffer: uniformBuffer } },
|
|
72
|
+
{ binding: 1, resource: { buffer: input.buffer } },
|
|
73
|
+
{ binding: 2, resource: { buffer: output } },
|
|
74
|
+
],
|
|
75
|
+
});
|
|
79
76
|
|
|
80
|
-
|
|
77
|
+
const workgroups = Math.ceil(numElements / WORKGROUP_SIZES.DEFAULT);
|
|
78
|
+
const dispatchSize = calculate2DDispatch(workgroups);
|
|
81
79
|
|
|
82
|
-
|
|
80
|
+
dispatch(device, pipeline, bindGroup, dispatchSize, 'cast_f32_to_f16');
|
|
81
|
+
return createTensor(output, 'f16', [...input.shape], input.label ? `${input.label}_f16` : 'cast_f32_to_f16_output');
|
|
82
|
+
} catch (error) {
|
|
83
|
+
if (ownsOutput) {
|
|
84
|
+
releaseBuffer(output);
|
|
85
|
+
}
|
|
86
|
+
throw error;
|
|
87
|
+
} finally {
|
|
88
|
+
uniformBuffer?.destroy();
|
|
89
|
+
}
|
|
83
90
|
}
|
|
84
91
|
|
|
85
92
|
|
|
@@ -89,6 +96,7 @@ export async function castF16ToF32(
|
|
|
89
96
|
) {
|
|
90
97
|
const device = getDevice();
|
|
91
98
|
const { outputBuffer = null } = options;
|
|
99
|
+
const ownsOutput = outputBuffer == null;
|
|
92
100
|
const numElements = input.shape.reduce((a, b) => a * b, 1);
|
|
93
101
|
|
|
94
102
|
const pipeline = await createPipeline('cast', 'f16_to_f32');
|
|
@@ -96,34 +104,41 @@ export async function castF16ToF32(
|
|
|
96
104
|
const outputSize = numElements * DTYPE_SIZES.f32;
|
|
97
105
|
const output = outputBuffer || acquireBuffer(outputSize, undefined, 'cast_f16_to_f32_output');
|
|
98
106
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
label: 'cast_f16_to_f32_bind_group',
|
|
111
|
-
layout: pipeline.getBindGroupLayout(0),
|
|
112
|
-
entries: [
|
|
113
|
-
{ binding: 0, resource: { buffer: uniformBuffer } },
|
|
114
|
-
{ binding: 1, resource: { buffer: input.buffer } },
|
|
115
|
-
{ binding: 2, resource: { buffer: output } },
|
|
116
|
-
],
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
const workgroups = Math.ceil(numElements / WORKGROUP_SIZES.DEFAULT);
|
|
120
|
-
const dispatchSize = calculate2DDispatch(workgroups);
|
|
107
|
+
let uniformBuffer = null;
|
|
108
|
+
try {
|
|
109
|
+
uniformBuffer = createUniformBufferWithView(
|
|
110
|
+
'cast_f16_to_f32_uniforms',
|
|
111
|
+
16,
|
|
112
|
+
(view) => {
|
|
113
|
+
view.setUint32(0, numElements, true);
|
|
114
|
+
},
|
|
115
|
+
null,
|
|
116
|
+
device
|
|
117
|
+
);
|
|
121
118
|
|
|
122
|
-
|
|
119
|
+
const bindGroup = device.createBindGroup({
|
|
120
|
+
label: 'cast_f16_to_f32_bind_group',
|
|
121
|
+
layout: pipeline.getBindGroupLayout(0),
|
|
122
|
+
entries: [
|
|
123
|
+
{ binding: 0, resource: { buffer: uniformBuffer } },
|
|
124
|
+
{ binding: 1, resource: { buffer: input.buffer } },
|
|
125
|
+
{ binding: 2, resource: { buffer: output } },
|
|
126
|
+
],
|
|
127
|
+
});
|
|
123
128
|
|
|
124
|
-
|
|
129
|
+
const workgroups = Math.ceil(numElements / WORKGROUP_SIZES.DEFAULT);
|
|
130
|
+
const dispatchSize = calculate2DDispatch(workgroups);
|
|
125
131
|
|
|
126
|
-
|
|
132
|
+
dispatch(device, pipeline, bindGroup, dispatchSize, 'cast_f16_to_f32');
|
|
133
|
+
return createTensor(output, 'f32', [...input.shape], input.label ? `${input.label}_f32` : 'cast_f16_to_f32_output');
|
|
134
|
+
} catch (error) {
|
|
135
|
+
if (ownsOutput) {
|
|
136
|
+
releaseBuffer(output);
|
|
137
|
+
}
|
|
138
|
+
throw error;
|
|
139
|
+
} finally {
|
|
140
|
+
uniformBuffer?.destroy();
|
|
141
|
+
}
|
|
127
142
|
}
|
|
128
143
|
|
|
129
144
|
|
|
@@ -134,6 +149,7 @@ export async function recordCastF32ToF16(
|
|
|
134
149
|
) {
|
|
135
150
|
const device = recorder.device;
|
|
136
151
|
const { outputBuffer = null } = options;
|
|
152
|
+
const ownsOutput = outputBuffer == null;
|
|
137
153
|
const numElements = input.shape.reduce((a, b) => a * b, 1);
|
|
138
154
|
|
|
139
155
|
const pipeline = await createPipeline('cast', 'f32_to_f16');
|
|
@@ -141,32 +157,37 @@ export async function recordCastF32ToF16(
|
|
|
141
157
|
const outputSize = numElements * DTYPE_SIZES.f16;
|
|
142
158
|
const output = outputBuffer || acquireBuffer(outputSize, undefined, 'cast_f32_to_f16_output');
|
|
143
159
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
view
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const bindGroup = device.createBindGroup({
|
|
154
|
-
label: 'cast_f32_to_f16_bind_group',
|
|
155
|
-
layout: pipeline.getBindGroupLayout(0),
|
|
156
|
-
entries: [
|
|
157
|
-
{ binding: 0, resource: { buffer: uniformBuffer } },
|
|
158
|
-
{ binding: 1, resource: { buffer: input.buffer } },
|
|
159
|
-
{ binding: 2, resource: { buffer: output } },
|
|
160
|
-
],
|
|
161
|
-
});
|
|
160
|
+
try {
|
|
161
|
+
const uniformBuffer = createUniformBufferWithView(
|
|
162
|
+
'cast_f32_to_f16_uniforms',
|
|
163
|
+
16,
|
|
164
|
+
(view) => {
|
|
165
|
+
view.setUint32(0, numElements, true);
|
|
166
|
+
},
|
|
167
|
+
recorder
|
|
168
|
+
);
|
|
162
169
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
170
|
+
const bindGroup = device.createBindGroup({
|
|
171
|
+
label: 'cast_f32_to_f16_bind_group',
|
|
172
|
+
layout: pipeline.getBindGroupLayout(0),
|
|
173
|
+
entries: [
|
|
174
|
+
{ binding: 0, resource: { buffer: uniformBuffer } },
|
|
175
|
+
{ binding: 1, resource: { buffer: input.buffer } },
|
|
176
|
+
{ binding: 2, resource: { buffer: output } },
|
|
177
|
+
],
|
|
178
|
+
});
|
|
166
179
|
|
|
167
|
-
|
|
180
|
+
const workgroups = Math.ceil(numElements / WORKGROUP_SIZES.DEFAULT);
|
|
181
|
+
const dispatchSize = calculate2DDispatch(workgroups);
|
|
168
182
|
|
|
169
|
-
|
|
183
|
+
recordDispatch(recorder, pipeline, bindGroup, dispatchSize, 'cast_f32_to_f16');
|
|
184
|
+
return createTensor(output, 'f16', [...input.shape], input.label ? `${input.label}_f16` : 'cast_f32_to_f16_output');
|
|
185
|
+
} catch (error) {
|
|
186
|
+
if (ownsOutput) {
|
|
187
|
+
releaseBuffer(output);
|
|
188
|
+
}
|
|
189
|
+
throw error;
|
|
190
|
+
}
|
|
170
191
|
}
|
|
171
192
|
|
|
172
193
|
|
|
@@ -177,6 +198,7 @@ export async function recordCastF16ToF32(
|
|
|
177
198
|
) {
|
|
178
199
|
const device = recorder.device;
|
|
179
200
|
const { outputBuffer = null } = options;
|
|
201
|
+
const ownsOutput = outputBuffer == null;
|
|
180
202
|
const numElements = input.shape.reduce((a, b) => a * b, 1);
|
|
181
203
|
|
|
182
204
|
const pipeline = await createPipeline('cast', 'f16_to_f32');
|
|
@@ -184,31 +206,37 @@ export async function recordCastF16ToF32(
|
|
|
184
206
|
const outputSize = numElements * DTYPE_SIZES.f32;
|
|
185
207
|
const output = outputBuffer || acquireBuffer(outputSize, undefined, 'cast_f16_to_f32_output');
|
|
186
208
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
view
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
const bindGroup = device.createBindGroup({
|
|
197
|
-
label: 'cast_f16_to_f32_bind_group',
|
|
198
|
-
layout: pipeline.getBindGroupLayout(0),
|
|
199
|
-
entries: [
|
|
200
|
-
{ binding: 0, resource: { buffer: uniformBuffer } },
|
|
201
|
-
{ binding: 1, resource: { buffer: input.buffer } },
|
|
202
|
-
{ binding: 2, resource: { buffer: output } },
|
|
203
|
-
],
|
|
204
|
-
});
|
|
209
|
+
try {
|
|
210
|
+
const uniformBuffer = createUniformBufferWithView(
|
|
211
|
+
'cast_f16_to_f32_uniforms',
|
|
212
|
+
16,
|
|
213
|
+
(view) => {
|
|
214
|
+
view.setUint32(0, numElements, true);
|
|
215
|
+
},
|
|
216
|
+
recorder
|
|
217
|
+
);
|
|
205
218
|
|
|
206
|
-
|
|
207
|
-
|
|
219
|
+
const bindGroup = device.createBindGroup({
|
|
220
|
+
label: 'cast_f16_to_f32_bind_group',
|
|
221
|
+
layout: pipeline.getBindGroupLayout(0),
|
|
222
|
+
entries: [
|
|
223
|
+
{ binding: 0, resource: { buffer: uniformBuffer } },
|
|
224
|
+
{ binding: 1, resource: { buffer: input.buffer } },
|
|
225
|
+
{ binding: 2, resource: { buffer: output } },
|
|
226
|
+
],
|
|
227
|
+
});
|
|
208
228
|
|
|
209
|
-
|
|
229
|
+
const workgroups = Math.ceil(numElements / WORKGROUP_SIZES.DEFAULT);
|
|
230
|
+
const dispatchSize = calculate2DDispatch(workgroups);
|
|
210
231
|
|
|
211
|
-
|
|
232
|
+
recordDispatch(recorder, pipeline, bindGroup, dispatchSize, 'cast_f16_to_f32');
|
|
233
|
+
return createTensor(output, 'f32', [...input.shape], input.label ? `${input.label}_f32` : 'cast_f16_to_f32_output');
|
|
234
|
+
} catch (error) {
|
|
235
|
+
if (ownsOutput) {
|
|
236
|
+
releaseBuffer(output);
|
|
237
|
+
}
|
|
238
|
+
throw error;
|
|
239
|
+
}
|
|
212
240
|
}
|
|
213
241
|
|
|
214
242
|
|
|
@@ -276,11 +304,15 @@ export async function runBF16ToF32(
|
|
|
276
304
|
const dispatchSize = calculate2DDispatch(workgroups);
|
|
277
305
|
|
|
278
306
|
trace.kernels(`BF16ToF32: Dispatching ${dispatchSize[0]}x${dispatchSize[1]} workgroups for ${numPairs} pairs (${numElements} elements)`);
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
307
|
+
try {
|
|
308
|
+
dispatch(device, pipeline, bindGroup, dispatchSize, 'bf16_to_f32');
|
|
309
|
+
return createTensor(output, 'f32', [...shape], name);
|
|
310
|
+
} catch (error) {
|
|
311
|
+
releaseBuffer(output);
|
|
312
|
+
throw error;
|
|
313
|
+
} finally {
|
|
314
|
+
uniformBuffer.destroy();
|
|
315
|
+
}
|
|
284
316
|
}
|
|
285
317
|
|
|
286
318
|
|
|
@@ -337,11 +369,15 @@ export async function runBF16ToF16(
|
|
|
337
369
|
const workgroups = Math.ceil(numPairs / WORKGROUP_SIZES.DEFAULT);
|
|
338
370
|
const dispatchSize = calculate2DDispatch(workgroups);
|
|
339
371
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
372
|
+
try {
|
|
373
|
+
dispatch(device, pipeline, bindGroup, dispatchSize, 'bf16_to_f16');
|
|
374
|
+
return createTensor(output, 'f16', [...shape], name);
|
|
375
|
+
} catch (error) {
|
|
376
|
+
releaseBuffer(output);
|
|
377
|
+
throw error;
|
|
378
|
+
} finally {
|
|
379
|
+
uniformBuffer.destroy();
|
|
380
|
+
}
|
|
345
381
|
}
|
|
346
382
|
|
|
347
383
|
|
|
@@ -375,48 +411,54 @@ async function runBF16ToF32Chunked(
|
|
|
375
411
|
|
|
376
412
|
trace.kernels(`BF16ToF32: Chunking ${numElements} elements in ${numChunks} chunks`);
|
|
377
413
|
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
view
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
414
|
+
try {
|
|
415
|
+
for (let chunkIdx = 0; chunkIdx < numChunks; chunkIdx++) {
|
|
416
|
+
const chunkStart = chunkIdx * maxElementsPerChunk;
|
|
417
|
+
const chunkEnd = Math.min((chunkIdx + 1) * maxElementsPerChunk, numElements);
|
|
418
|
+
const chunkSize = chunkEnd - chunkStart;
|
|
419
|
+
|
|
420
|
+
const uniformBuffer = createUniformBufferWithView(
|
|
421
|
+
`bf16_to_f32_chunk${chunkIdx}_uniforms`,
|
|
422
|
+
16,
|
|
423
|
+
(view) => {
|
|
424
|
+
view.setUint32(0, chunkSize, true);
|
|
425
|
+
view.setUint32(4, 0, true);
|
|
426
|
+
view.setUint32(8, 0, true);
|
|
427
|
+
},
|
|
428
|
+
null,
|
|
429
|
+
device
|
|
430
|
+
);
|
|
431
|
+
|
|
432
|
+
try {
|
|
433
|
+
const inputOffsetBytes = chunkStart * DTYPE_SIZES.bf16;
|
|
434
|
+
const outputOffsetBytes = chunkStart * DTYPE_SIZES.f32;
|
|
435
|
+
const inputPairs = Math.ceil(chunkSize / 2);
|
|
436
|
+
const inputSizeBytes = inputPairs * DTYPE_SIZES.f32;
|
|
437
|
+
const outputSizeBytes = chunkSize * DTYPE_SIZES.f32;
|
|
438
|
+
|
|
439
|
+
const bindGroup = device.createBindGroup({
|
|
440
|
+
label: `bf16_to_f32_chunk${chunkIdx}_bind_group`,
|
|
441
|
+
layout: pipeline.getBindGroupLayout(0),
|
|
442
|
+
entries: [
|
|
443
|
+
{ binding: 0, resource: { buffer: uniformBuffer } },
|
|
444
|
+
{ binding: 1, resource: { buffer: input, offset: inputOffsetBytes, size: inputSizeBytes } },
|
|
445
|
+
{ binding: 2, resource: { buffer: output, offset: outputOffsetBytes, size: outputSizeBytes } },
|
|
446
|
+
],
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
const numPairs = Math.ceil(chunkSize / 2);
|
|
450
|
+
const workgroups = Math.ceil(numPairs / WORKGROUP_SIZES.DEFAULT);
|
|
451
|
+
const dispatchSize = calculate2DDispatch(workgroups);
|
|
452
|
+
|
|
453
|
+
dispatch(device, pipeline, bindGroup, dispatchSize, `bf16_to_f32_chunk${chunkIdx}`);
|
|
454
|
+
} finally {
|
|
455
|
+
uniformBuffer.destroy();
|
|
456
|
+
}
|
|
457
|
+
}
|
|
417
458
|
|
|
418
|
-
|
|
459
|
+
return createTensor(output, 'f32', [...shape], name);
|
|
460
|
+
} catch (error) {
|
|
461
|
+
releaseBuffer(output);
|
|
462
|
+
throw error;
|
|
419
463
|
}
|
|
420
|
-
|
|
421
|
-
return createTensor(output, 'f32', [...shape], name);
|
|
422
464
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
3
|
import { getDevice, getDeviceEpoch } from '../device.js';
|
|
4
|
-
import { acquireBuffer } from '../../memory/buffer-pool.js';
|
|
4
|
+
import { acquireBuffer, readBufferSlice } from '../../memory/buffer-pool.js';
|
|
5
5
|
import { recordDispatch } from './dispatch.js';
|
|
6
6
|
import { createUniformBufferFromData, getOrCreateBindGroupLayout, getOrCreatePipelineLayout } from './utils.js';
|
|
7
7
|
import { allowReadback } from '../perf-guards.js';
|
|
@@ -133,49 +133,38 @@ export async function checkStop(params) {
|
|
|
133
133
|
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC,
|
|
134
134
|
});
|
|
135
135
|
const ownsStopBuffer = !params.shouldStopBuffer;
|
|
136
|
-
if (shouldStopBuffer.size < requiredBytes) {
|
|
137
|
-
throw new Error('[CheckStop] shouldStopBuffer too small for tokenIndex.');
|
|
138
|
-
}
|
|
139
136
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
0
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
stagingBuffer.unmap();
|
|
173
|
-
|
|
174
|
-
uniformBuffer.destroy();
|
|
175
|
-
if (ownsStopBuffer) {
|
|
176
|
-
shouldStopBuffer.destroy();
|
|
137
|
+
try {
|
|
138
|
+
if (shouldStopBuffer.size < requiredBytes) {
|
|
139
|
+
throw new Error('[CheckStop] shouldStopBuffer too small for tokenIndex.');
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const bindGroup = device.createBindGroup({
|
|
143
|
+
layout: getCheckStopBindGroupLayout(device),
|
|
144
|
+
entries: [
|
|
145
|
+
{ binding: 0, resource: { buffer: uniformBuffer } },
|
|
146
|
+
{ binding: 1, resource: { buffer: params.sampledTokenBuffer } },
|
|
147
|
+
{ binding: 2, resource: { buffer: shouldStopBuffer } },
|
|
148
|
+
],
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const encoder = device.createCommandEncoder();
|
|
152
|
+
const pass = encoder.beginComputePass();
|
|
153
|
+
pass.setPipeline(pipeline);
|
|
154
|
+
pass.setBindGroup(0, bindGroup);
|
|
155
|
+
pass.dispatchWorkgroups(1, 1, 1);
|
|
156
|
+
pass.end();
|
|
157
|
+
|
|
158
|
+
device.queue.submit([encoder.finish()]);
|
|
159
|
+
|
|
160
|
+
const result = new Uint32Array(
|
|
161
|
+
await readBufferSlice(shouldStopBuffer, tokenIndex * U32_BYTES, U32_BYTES)
|
|
162
|
+
)[0];
|
|
163
|
+
return result === 1;
|
|
164
|
+
} finally {
|
|
165
|
+
uniformBuffer.destroy();
|
|
166
|
+
if (ownsStopBuffer) {
|
|
167
|
+
shouldStopBuffer.destroy();
|
|
168
|
+
}
|
|
177
169
|
}
|
|
178
|
-
stagingBuffer.destroy();
|
|
179
|
-
|
|
180
|
-
return result === 1;
|
|
181
170
|
}
|
|
@@ -49,27 +49,37 @@ async function _conv2d(target, input, weight, bias, options = {}) {
|
|
|
49
49
|
device.queue.writeBuffer(biasBuffer, 0, new Uint8Array(paddedSize));
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
52
|
+
try {
|
|
53
|
+
await unifiedKernelWrapper(
|
|
54
|
+
'conv2d', target, variant,
|
|
55
|
+
[input, weightBuffer, biasBuffer, output],
|
|
56
|
+
{
|
|
57
|
+
in_channels: inChannels, out_channels: outChannels,
|
|
58
|
+
height, width, out_height: outHeight, out_width: outWidth,
|
|
59
|
+
kernel_h: kernelH, kernel_w: kernelW,
|
|
60
|
+
stride, pad, _pad0: 0, _pad1: 0,
|
|
61
|
+
},
|
|
62
|
+
[Math.ceil((outHeight * outWidth) / WORKGROUP_SIZES.DEFAULT), outChannels, 1]
|
|
63
|
+
);
|
|
63
64
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
65
|
+
if (tempBias) {
|
|
66
|
+
if (recorder) {
|
|
67
|
+
recorder.trackTemporaryBuffer(tempBias);
|
|
68
|
+
} else {
|
|
69
|
+
releaseBuffer(tempBias);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return createTensor(output, input.dtype, [outChannels, outHeight, outWidth], 'conv2d_output');
|
|
74
|
+
} catch (error) {
|
|
75
|
+
if (tempBias) {
|
|
68
76
|
releaseBuffer(tempBias);
|
|
69
77
|
}
|
|
78
|
+
if (!outputBuffer) {
|
|
79
|
+
releaseBuffer(output);
|
|
80
|
+
}
|
|
81
|
+
throw error;
|
|
70
82
|
}
|
|
71
|
-
|
|
72
|
-
return createTensor(output, input.dtype, [outChannels, outHeight, outWidth], 'conv2d_output');
|
|
73
83
|
}
|
|
74
84
|
|
|
75
85
|
export async function runConv2D(input, weight, bias, options = {}) {
|
|
@@ -18,31 +18,37 @@ function resolveDimensions(softmax, options) {
|
|
|
18
18
|
async function _crossEntropyLoss(target, softmax, targets, options = {}) {
|
|
19
19
|
const recorder = target && typeof target.beginComputePass === 'function' ? target : null;
|
|
20
20
|
const { outputBuffer = null } = options;
|
|
21
|
+
const ownsOutput = outputBuffer == null;
|
|
21
22
|
const { numTokens, vocabSize } = resolveDimensions(softmax, options);
|
|
22
23
|
|
|
23
24
|
const inputTensor = softmax.dtype === 'f16'
|
|
24
25
|
? (recorder ? await recordCastF16ToF32(recorder, softmax) : await castF16ToF32(softmax))
|
|
25
26
|
: softmax;
|
|
26
|
-
|
|
27
27
|
const outputSize = numTokens * 4;
|
|
28
28
|
const outputBuf = outputBuffer || acquireBuffer(outputSize, undefined, 'cross_entropy_loss_output');
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
30
|
+
try {
|
|
31
|
+
await unifiedKernelWrapper(
|
|
32
|
+
'cross_entropy_loss', target, 'default',
|
|
33
|
+
[inputTensor, targets, outputBuf],
|
|
34
|
+
{ num_tokens: numTokens, vocab_size: vocabSize },
|
|
35
|
+
Math.ceil(numTokens / WORKGROUP_SIZES.DEFAULT)
|
|
36
|
+
);
|
|
37
|
+
return createTensor(outputBuf, 'f32', [numTokens], 'cross_entropy_loss_output');
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (ownsOutput) {
|
|
40
|
+
releaseBuffer(outputBuf);
|
|
41
|
+
}
|
|
42
|
+
throw error;
|
|
43
|
+
} finally {
|
|
44
|
+
if (inputTensor !== softmax) {
|
|
45
|
+
if (recorder) {
|
|
46
|
+
recorder.trackTemporaryBuffer(inputTensor.buffer);
|
|
47
|
+
} else {
|
|
48
|
+
releaseBuffer(inputTensor.buffer);
|
|
49
|
+
}
|
|
42
50
|
}
|
|
43
51
|
}
|
|
44
|
-
|
|
45
|
-
return createTensor(outputBuf, 'f32', [numTokens], 'cross_entropy_loss_output');
|
|
46
52
|
}
|
|
47
53
|
|
|
48
54
|
export async function runCrossEntropyLoss(softmax, targets, options = {}) {
|