doppler-gpu 0.4.9 → 0.4.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/CHANGELOG.md +53 -0
- package/README.md +89 -12
- package/models/catalog.json +612 -7
- package/package.json +30 -1
- package/src/client/doppler-registry.js +6 -0
- package/src/client/doppler-registry.json +126 -4
- package/src/client/runtime/lora.d.ts +10 -0
- package/src/client/runtime/lora.js +64 -0
- package/src/client/runtime/model-manager.js +6 -167
- package/src/client/runtime/model-session.d.ts +3 -1
- package/src/client/runtime/model-session.js +6 -0
- package/src/client/wrap-pipeline-handle.js +7 -0
- package/src/config/conversion/amplify/amplify-120m-f16-af32.json +516 -0
- package/src/config/conversion/esm/esm2-t12-35m-ur50d-f32-af32.json +571 -0
- package/src/config/conversion/esmc/esmc-300m-f32-af32.json +342 -0
- package/src/config/conversion/nucleotide-transformer/nucleotide-transformer-v2-50m-f32-af32.json +388 -0
- package/src/config/kernels/registry.json +52 -9
- package/src/config/merge.js +24 -0
- package/src/config/required-inference-fields-contract-check.js +2 -0
- package/src/config/schema/converter.schema.d.ts +7 -0
- package/src/config/schema/converter.schema.js +15 -0
- package/src/config/schema/inference.schema.d.ts +8 -0
- package/src/config/schema/manifest.schema.d.ts +32 -1
- package/src/config/schema/manifest.schema.js +26 -0
- package/src/converter/conversion-plan.js +15 -1
- package/src/converter/core.js +5 -4
- package/src/converter/source-tensor-rules.d.ts +50 -0
- package/src/converter/source-tensor-rules.js +180 -0
- package/src/converter/source-tokenizer.d.ts +45 -0
- package/src/converter/source-tokenizer.js +155 -0
- package/src/experimental/adapters/lora-loader.js +15 -0
- package/src/experimental/bridge/trainer-artifact-bridge.d.ts +2 -1
- package/src/experimental/bridge/trainer-artifact-bridge.js +35 -13
- package/src/gpu/device.js +100 -32
- package/src/inference/pipelines/text/attention/attn-config.js +2 -0
- package/src/inference/pipelines/text/attention/projections.js +161 -24
- package/src/inference/pipelines/text/attention/record.js +67 -9
- package/src/inference/pipelines/text/attention/run.js +60 -7
- package/src/inference/pipelines/text/attention/types.js +18 -9
- package/src/inference/pipelines/text/config.d.ts +5 -0
- package/src/inference/pipelines/text/config.js +91 -0
- package/src/inference/pipelines/text/ffn/dense.js +189 -2
- package/src/inference/pipelines/text/ffn/standard.js +50 -3
- package/src/inference/pipelines/text/generator-helpers.js +11 -1
- package/src/inference/pipelines/text/generator-runtime.js +63 -15
- package/src/inference/pipelines/text/generator-steps.js +3 -1
- package/src/inference/pipelines/text/generator.js +90 -3
- package/src/inference/pipelines/text/init-qkv-fusion.js +85 -1
- package/src/inference/pipelines/text/init.d.ts +2 -0
- package/src/inference/pipelines/text/init.js +2 -0
- package/src/inference/pipelines/text/layer.js +6 -0
- package/src/inference/pipelines/text/logits/cpu.d.ts +6 -0
- package/src/inference/pipelines/text/logits/cpu.js +22 -0
- package/src/inference/pipelines/text/logits/index.d.ts +1 -1
- package/src/inference/pipelines/text/logits/index.js +85 -11
- package/src/inference/pipelines/text/logits/types.d.ts +4 -0
- package/src/inference/pipelines/text/ops.js +32 -0
- package/src/inference/pipelines/text/types.d.ts +38 -0
- package/src/inference/pipelines/text/weights.d.ts +10 -0
- package/src/inference/pipelines/text/weights.js +33 -1
- package/src/inference/pipelines/text.d.ts +4 -2
- package/src/inference/pipelines/text.js +112 -0
- package/src/inference/tokenizers/behavior-flags.js +5 -1
- package/src/inference/tokenizers/bundled.js +99 -8
- package/src/loader/doppler-loader.d.ts +2 -0
- package/src/loader/doppler-loader.js +9 -0
- package/src/loader/final-weights-loader.d.ts +8 -0
- package/src/loader/final-weights-loader.js +42 -0
- package/src/loader/layer-loader.d.ts +6 -0
- package/src/loader/layer-loader.js +102 -3
- package/src/loader/loader-types.d.ts +15 -2
- package/src/tooling/node-converter.js +33 -1
- package/src/tooling/node-webgpu.js +85 -2
- package/src/tooling/runtime-optimization.d.ts +109 -0
- package/src/tooling/runtime-optimization.js +872 -0
- package/src/tooling-exports.shared.d.ts +19 -0
- package/src/tooling-exports.shared.js +11 -0
- package/src/version.js +1 -1
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
function assertPlainObject(value, label) {
|
|
2
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
3
|
+
throw new Error(`${label} must be an object.`);
|
|
4
|
+
}
|
|
5
|
+
return value;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function requireToken(value, label) {
|
|
9
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
10
|
+
throw new Error(`${label} must be a non-empty string.`);
|
|
11
|
+
}
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function validateRelativeFile(value) {
|
|
16
|
+
const file = requireToken(value, 'sourceTokenizer.vocabFile');
|
|
17
|
+
if (file.startsWith('/') || file.includes('\\') || file.split('/').includes('..')) {
|
|
18
|
+
throw new Error('sourceTokenizer.vocabFile must stay inside the source model directory.');
|
|
19
|
+
}
|
|
20
|
+
return file;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function parseVocab(text) {
|
|
24
|
+
if (typeof text !== 'string') {
|
|
25
|
+
throw new Error('Source vocabulary must be text.');
|
|
26
|
+
}
|
|
27
|
+
const tokens = text.replace(/\r/gu, '').split('\n');
|
|
28
|
+
if (tokens.at(-1) === '') tokens.pop();
|
|
29
|
+
if (tokens.length === 0 || tokens.some((token) => token.length === 0)) {
|
|
30
|
+
throw new Error('Source vocabulary must contain one non-empty token per line.');
|
|
31
|
+
}
|
|
32
|
+
const vocab = {};
|
|
33
|
+
for (let id = 0; id < tokens.length; id += 1) {
|
|
34
|
+
const token = tokens[id];
|
|
35
|
+
if (Object.prototype.hasOwnProperty.call(vocab, token)) {
|
|
36
|
+
throw new Error(`Source vocabulary contains duplicate token "${token}".`);
|
|
37
|
+
}
|
|
38
|
+
vocab[token] = id;
|
|
39
|
+
}
|
|
40
|
+
return { tokens, vocab };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function validateSourceTokenizerPolicy(policy) {
|
|
44
|
+
if (policy == null) return null;
|
|
45
|
+
const value = assertPlainObject(policy, 'sourceTokenizer');
|
|
46
|
+
if (value.kind !== 'character_vocab' && value.kind !== 'greedy_vocab') {
|
|
47
|
+
throw new Error(`Unsupported sourceTokenizer.kind "${String(value.kind)}".`);
|
|
48
|
+
}
|
|
49
|
+
const specialTokens = assertPlainObject(value.specialTokens, 'sourceTokenizer.specialTokens');
|
|
50
|
+
const normalized = {
|
|
51
|
+
kind: value.kind,
|
|
52
|
+
vocabFile: validateRelativeFile(value.vocabFile),
|
|
53
|
+
specialTokens: {
|
|
54
|
+
pad: requireToken(specialTokens.pad, 'sourceTokenizer.specialTokens.pad'),
|
|
55
|
+
bos: requireToken(specialTokens.bos, 'sourceTokenizer.specialTokens.bos'),
|
|
56
|
+
eos: value.kind === 'character_vocab'
|
|
57
|
+
? requireToken(specialTokens.eos, 'sourceTokenizer.specialTokens.eos')
|
|
58
|
+
: specialTokens.eos == null
|
|
59
|
+
? null
|
|
60
|
+
: requireToken(specialTokens.eos, 'sourceTokenizer.specialTokens.eos'),
|
|
61
|
+
unk: requireToken(specialTokens.unk, 'sourceTokenizer.specialTokens.unk'),
|
|
62
|
+
mask: specialTokens.mask == null
|
|
63
|
+
? null
|
|
64
|
+
: requireToken(specialTokens.mask, 'sourceTokenizer.specialTokens.mask'),
|
|
65
|
+
},
|
|
66
|
+
addBosToken: value.addBosToken === true,
|
|
67
|
+
addEosToken: value.addEosToken === true,
|
|
68
|
+
};
|
|
69
|
+
if (typeof value.addBosToken !== 'boolean' || typeof value.addEosToken !== 'boolean') {
|
|
70
|
+
throw new Error('sourceTokenizer.addBosToken and addEosToken must be booleans.');
|
|
71
|
+
}
|
|
72
|
+
return normalized;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function buildSourceTokenizerJson(policy, vocabText) {
|
|
76
|
+
const normalized = validateSourceTokenizerPolicy(policy);
|
|
77
|
+
if (!normalized) throw new Error('sourceTokenizer policy is required.');
|
|
78
|
+
const { tokens, vocab } = parseVocab(vocabText);
|
|
79
|
+
for (const [role, token] of Object.entries(normalized.specialTokens)) {
|
|
80
|
+
if (token != null && !Object.prototype.hasOwnProperty.call(vocab, token)) {
|
|
81
|
+
throw new Error(`Source vocabulary is missing ${role} token "${token}".`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const uniqueSpecialTokens = [...new Set(Object.values(normalized.specialTokens).filter(Boolean))];
|
|
85
|
+
const characterVocab = normalized.kind === 'character_vocab';
|
|
86
|
+
return {
|
|
87
|
+
version: '1.0',
|
|
88
|
+
truncation: null,
|
|
89
|
+
padding: null,
|
|
90
|
+
added_tokens: uniqueSpecialTokens.map((token) => ({
|
|
91
|
+
id: vocab[token],
|
|
92
|
+
content: token,
|
|
93
|
+
single_word: false,
|
|
94
|
+
lstrip: false,
|
|
95
|
+
rstrip: false,
|
|
96
|
+
normalized: false,
|
|
97
|
+
special: true,
|
|
98
|
+
})),
|
|
99
|
+
normalizer: null,
|
|
100
|
+
pre_tokenizer: characterVocab
|
|
101
|
+
? {
|
|
102
|
+
type: 'Split',
|
|
103
|
+
pattern: { String: '' },
|
|
104
|
+
behavior: 'Removed',
|
|
105
|
+
invert: false,
|
|
106
|
+
}
|
|
107
|
+
: null,
|
|
108
|
+
post_processor: null,
|
|
109
|
+
decoder: characterVocab
|
|
110
|
+
? {
|
|
111
|
+
type: 'WordPiece',
|
|
112
|
+
prefix: '##',
|
|
113
|
+
cleanup: false,
|
|
114
|
+
}
|
|
115
|
+
: null,
|
|
116
|
+
model: characterVocab
|
|
117
|
+
? {
|
|
118
|
+
type: 'WordPiece',
|
|
119
|
+
dropout: null,
|
|
120
|
+
unk_token: normalized.specialTokens.unk,
|
|
121
|
+
continuing_subword_prefix: '##',
|
|
122
|
+
max_input_chars_per_word: 100,
|
|
123
|
+
vocab,
|
|
124
|
+
}
|
|
125
|
+
: {
|
|
126
|
+
type: 'BPE',
|
|
127
|
+
dropout: null,
|
|
128
|
+
unk_token: normalized.specialTokens.unk,
|
|
129
|
+
continuing_subword_prefix: null,
|
|
130
|
+
end_of_word_suffix: null,
|
|
131
|
+
fuse_unk: false,
|
|
132
|
+
byte_fallback: false,
|
|
133
|
+
vocab,
|
|
134
|
+
merges: [],
|
|
135
|
+
},
|
|
136
|
+
special_tokens_map: {
|
|
137
|
+
pad_token: normalized.specialTokens.pad,
|
|
138
|
+
bos_token: normalized.specialTokens.bos,
|
|
139
|
+
...(normalized.specialTokens.eos ? { eos_token: normalized.specialTokens.eos } : {}),
|
|
140
|
+
unk_token: normalized.specialTokens.unk,
|
|
141
|
+
...(normalized.specialTokens.mask ? { mask_token: normalized.specialTokens.mask } : {}),
|
|
142
|
+
},
|
|
143
|
+
add_bos_token: normalized.addBosToken,
|
|
144
|
+
add_eos_token: normalized.addEosToken,
|
|
145
|
+
source_vocab_size: tokens.length,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function buildCharacterTokenizerJson(policy, vocabText) {
|
|
150
|
+
const normalized = validateSourceTokenizerPolicy(policy);
|
|
151
|
+
if (normalized?.kind !== 'character_vocab') {
|
|
152
|
+
throw new Error('buildCharacterTokenizerJson requires sourceTokenizer.kind="character_vocab".');
|
|
153
|
+
}
|
|
154
|
+
return buildSourceTokenizerJson(normalized, vocabText);
|
|
155
|
+
}
|
|
@@ -149,8 +149,15 @@ async function loadExternalWeights(manifest, options = {}) {
|
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
function assertCompleteAdapterLayers(adapter) {
|
|
152
|
+
const targetModules = new Set(adapter.targetModules || []);
|
|
153
|
+
const loadedModules = new Set();
|
|
152
154
|
for (const [layerIndex, layer] of adapter.layers.entries()) {
|
|
153
155
|
for (const [moduleName, weights] of Object.entries(layer)) {
|
|
156
|
+
if (!targetModules.has(moduleName)) {
|
|
157
|
+
throw new Error(
|
|
158
|
+
`LoRA adapter layer ${layerIndex} contains module ${moduleName} outside targetModules.`
|
|
159
|
+
);
|
|
160
|
+
}
|
|
154
161
|
const hasA = weights?.a instanceof Float32Array && weights.a.length > 0;
|
|
155
162
|
const hasB = weights?.b instanceof Float32Array && weights.b.length > 0;
|
|
156
163
|
if (!hasA || !hasB) {
|
|
@@ -158,6 +165,14 @@ function assertCompleteAdapterLayers(adapter) {
|
|
|
158
165
|
`LoRA adapter layer ${layerIndex} module ${moduleName} is incomplete; both lora_a and lora_b tensors are required.`
|
|
159
166
|
);
|
|
160
167
|
}
|
|
168
|
+
loadedModules.add(moduleName);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
for (const moduleName of targetModules) {
|
|
172
|
+
if (!loadedModules.has(moduleName)) {
|
|
173
|
+
throw new Error(
|
|
174
|
+
`LoRA adapter targetModules declares ${moduleName}, but no complete tensors were loaded for it.`
|
|
175
|
+
);
|
|
161
176
|
}
|
|
162
177
|
}
|
|
163
178
|
}
|
|
@@ -7,6 +7,7 @@ export declare const TRAINER_ARTIFACT_KIND_PEFT_ADAPTER: 'peft_adapter';
|
|
|
7
7
|
|
|
8
8
|
export declare const TRANSLATION_FULL_CHECKPOINT_PARITY_CHECKS: readonly string[];
|
|
9
9
|
export declare const COLUMBO_QWEN_ADAPTER_PARITY_CHECKS: readonly string[];
|
|
10
|
+
export declare const TINKER_PEFT_BROWSER_ADAPTER_PARITY_CHECKS: readonly string[];
|
|
10
11
|
|
|
11
12
|
export interface TrainerArtifactFileIdentity {
|
|
12
13
|
id: string;
|
|
@@ -67,7 +68,7 @@ export interface TrainerArtifactBridgeDescriptor {
|
|
|
67
68
|
receipt: string | null;
|
|
68
69
|
};
|
|
69
70
|
parity: {
|
|
70
|
-
profile: 'translation_full_checkpoint' | 'columbo_qwen_adapter';
|
|
71
|
+
profile: 'translation_full_checkpoint' | 'columbo_qwen_adapter' | 'tinker_peft_browser_adapter';
|
|
71
72
|
requiredChecks: string[];
|
|
72
73
|
};
|
|
73
74
|
claimBoundary: string;
|
|
@@ -29,6 +29,19 @@ export const COLUMBO_QWEN_ADAPTER_PARITY_CHECKS = Object.freeze([
|
|
|
29
29
|
'browser_capability_evaluation',
|
|
30
30
|
]);
|
|
31
31
|
|
|
32
|
+
export const TINKER_PEFT_BROWSER_ADAPTER_PARITY_CHECKS = Object.freeze([
|
|
33
|
+
'source_artifact_byte_identity',
|
|
34
|
+
'tokenizer_and_prompt_identity',
|
|
35
|
+
'architecture_and_conversion_lineage',
|
|
36
|
+
'evaluation_input_identity',
|
|
37
|
+
'tinker_checkpoint_vs_peft_reference',
|
|
38
|
+
'peft_reference_vs_doppler_f16',
|
|
39
|
+
'doppler_f16_vs_quantized_browser',
|
|
40
|
+
'gamma_sealed_task_gain',
|
|
41
|
+
'gamma_retention_floor',
|
|
42
|
+
'browser_capability_evaluation',
|
|
43
|
+
]);
|
|
44
|
+
|
|
32
45
|
const SHA256_PATTERN = /^[0-9a-f]{64}$/;
|
|
33
46
|
const FILE_ROLES_BY_KIND = Object.freeze({
|
|
34
47
|
[TRAINER_ARTIFACT_KIND_FULL_CHECKPOINT]: Object.freeze([
|
|
@@ -44,9 +57,22 @@ const FILE_ROLES_BY_KIND = Object.freeze({
|
|
|
44
57
|
'training_report',
|
|
45
58
|
]),
|
|
46
59
|
});
|
|
47
|
-
const
|
|
48
|
-
translation_full_checkpoint:
|
|
49
|
-
|
|
60
|
+
const PARITY_PROFILES = Object.freeze({
|
|
61
|
+
translation_full_checkpoint: Object.freeze({
|
|
62
|
+
artifactKind: TRAINER_ARTIFACT_KIND_FULL_CHECKPOINT,
|
|
63
|
+
selectionAuthority: 'clocksmith/gamma',
|
|
64
|
+
requiredChecks: TRANSLATION_FULL_CHECKPOINT_PARITY_CHECKS,
|
|
65
|
+
}),
|
|
66
|
+
columbo_qwen_adapter: Object.freeze({
|
|
67
|
+
artifactKind: TRAINER_ARTIFACT_KIND_PEFT_ADAPTER,
|
|
68
|
+
selectionAuthority: 'clocksmith/columbo',
|
|
69
|
+
requiredChecks: COLUMBO_QWEN_ADAPTER_PARITY_CHECKS,
|
|
70
|
+
}),
|
|
71
|
+
tinker_peft_browser_adapter: Object.freeze({
|
|
72
|
+
artifactKind: TRAINER_ARTIFACT_KIND_PEFT_ADAPTER,
|
|
73
|
+
selectionAuthority: 'clocksmith/gamma',
|
|
74
|
+
requiredChecks: TINKER_PEFT_BROWSER_ADAPTER_PARITY_CHECKS,
|
|
75
|
+
}),
|
|
50
76
|
});
|
|
51
77
|
|
|
52
78
|
function stableJson(value) {
|
|
@@ -181,18 +207,16 @@ function normalizeParity(parity, artifactKind) {
|
|
|
181
207
|
throw new Error('trainer artifact bridge: parity must be an object.');
|
|
182
208
|
}
|
|
183
209
|
const profile = requireText(parity.profile, 'parity.profile');
|
|
184
|
-
const
|
|
185
|
-
if (!
|
|
210
|
+
const profileContract = PARITY_PROFILES[profile];
|
|
211
|
+
if (!profileContract) {
|
|
186
212
|
throw new Error(`trainer artifact bridge: unsupported parity profile "${profile}".`);
|
|
187
213
|
}
|
|
188
|
-
|
|
189
|
-
? 'translation_full_checkpoint'
|
|
190
|
-
: 'columbo_qwen_adapter';
|
|
191
|
-
if (profile !== expectedProfile) {
|
|
214
|
+
if (profileContract.artifactKind !== artifactKind) {
|
|
192
215
|
throw new Error(
|
|
193
|
-
`trainer artifact bridge:
|
|
216
|
+
`trainer artifact bridge: parity profile "${profile}" does not support artifact kind "${artifactKind}".`
|
|
194
217
|
);
|
|
195
218
|
}
|
|
219
|
+
const canonicalChecks = profileContract.requiredChecks;
|
|
196
220
|
const requiredChecks = Array.isArray(parity.requiredChecks) ? parity.requiredChecks : [];
|
|
197
221
|
if (stableJson(requiredChecks) !== stableJson(canonicalChecks)) {
|
|
198
222
|
throw new Error(`trainer artifact bridge: parity.requiredChecks must match profile "${profile}".`);
|
|
@@ -435,9 +459,7 @@ export function assertTrainerArtifactCandidateEntry(descriptor) {
|
|
|
435
459
|
if (normalized.selection.status !== 'selected' || !normalized.selection.receipt) {
|
|
436
460
|
throw new Error('trainer artifact bridge: candidate entry denied; selection receipt is required.');
|
|
437
461
|
}
|
|
438
|
-
const expectedAuthority = normalized.
|
|
439
|
-
? 'clocksmith/gamma'
|
|
440
|
-
: 'clocksmith/columbo';
|
|
462
|
+
const expectedAuthority = PARITY_PROFILES[normalized.parity.profile].selectionAuthority;
|
|
441
463
|
if (normalized.selection.authority !== expectedAuthority) {
|
|
442
464
|
throw new Error(
|
|
443
465
|
`trainer artifact bridge: candidate entry denied; selection authority must be "${expectedAuthority}".`
|
package/src/gpu/device.js
CHANGED
|
@@ -14,6 +14,12 @@ const SHARED_DEVICE_STATE_KEY = '__dopplerGpuDeviceState';
|
|
|
14
14
|
function getSharedDeviceState() {
|
|
15
15
|
const existing = globalThis[SHARED_DEVICE_STATE_KEY];
|
|
16
16
|
if (existing && typeof existing === 'object') {
|
|
17
|
+
if (!(existing.bufferOwners instanceof WeakMap)) {
|
|
18
|
+
existing.bufferOwners = new WeakMap();
|
|
19
|
+
}
|
|
20
|
+
if (!('deviceInitPromise' in existing)) {
|
|
21
|
+
existing.deviceInitPromise = null;
|
|
22
|
+
}
|
|
17
23
|
return existing;
|
|
18
24
|
}
|
|
19
25
|
const created = {
|
|
@@ -23,6 +29,8 @@ function getSharedDeviceState() {
|
|
|
23
29
|
lastDeviceLossInfo: null,
|
|
24
30
|
platformInitialized: false,
|
|
25
31
|
deviceEpoch: 0,
|
|
32
|
+
bufferOwners: new WeakMap(),
|
|
33
|
+
deviceInitPromise: null,
|
|
26
34
|
};
|
|
27
35
|
Object.defineProperty(globalThis, SHARED_DEVICE_STATE_KEY, {
|
|
28
36
|
value: created,
|
|
@@ -58,14 +66,14 @@ function syncSharedDeviceState() {
|
|
|
58
66
|
}
|
|
59
67
|
|
|
60
68
|
function hydrateDeviceState() {
|
|
61
|
-
gpuDevice =
|
|
62
|
-
kernelCapabilities =
|
|
63
|
-
resolvedPlatformConfig =
|
|
64
|
-
lastDeviceLossInfo =
|
|
65
|
-
platformInitialized =
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
+
gpuDevice = sharedDeviceState.gpuDevice ?? null;
|
|
70
|
+
kernelCapabilities = sharedDeviceState.kernelCapabilities ?? null;
|
|
71
|
+
resolvedPlatformConfig = sharedDeviceState.resolvedPlatformConfig ?? null;
|
|
72
|
+
lastDeviceLossInfo = sharedDeviceState.lastDeviceLossInfo ?? null;
|
|
73
|
+
platformInitialized = sharedDeviceState.platformInitialized === true;
|
|
74
|
+
deviceEpoch = Number.isInteger(sharedDeviceState.deviceEpoch)
|
|
75
|
+
? sharedDeviceState.deviceEpoch
|
|
76
|
+
: 0;
|
|
69
77
|
}
|
|
70
78
|
|
|
71
79
|
function advanceDeviceEpoch() {
|
|
@@ -185,7 +193,7 @@ function describeBindGroupBufferValue(value) {
|
|
|
185
193
|
return typeof value;
|
|
186
194
|
}
|
|
187
195
|
|
|
188
|
-
function validateBindGroupDescriptor(descriptor) {
|
|
196
|
+
function validateBindGroupDescriptor(descriptor, device) {
|
|
189
197
|
const label = descriptor?.label || 'unlabeled_bind_group';
|
|
190
198
|
const entries = Array.isArray(descriptor?.entries) ? descriptor.entries : [];
|
|
191
199
|
for (const entry of entries) {
|
|
@@ -193,13 +201,18 @@ function validateBindGroupDescriptor(descriptor) {
|
|
|
193
201
|
if (!resource || typeof resource !== 'object' || !('buffer' in resource)) {
|
|
194
202
|
continue;
|
|
195
203
|
}
|
|
196
|
-
if (isValidGPUBuffer(resource.buffer)) {
|
|
197
|
-
|
|
204
|
+
if (!isValidGPUBuffer(resource.buffer)) {
|
|
205
|
+
throw new Error(
|
|
206
|
+
`[${label}] binding ${entry.binding} requires a GPUBuffer; ` +
|
|
207
|
+
`got ${describeBindGroupBufferValue(resource.buffer)}.`
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
const owner = sharedDeviceState.bufferOwners.get(resource.buffer);
|
|
211
|
+
if (owner && owner !== device) {
|
|
212
|
+
throw new Error(
|
|
213
|
+
`[${label}] binding ${entry.binding} uses a GPUBuffer created by a different GPUDevice.`
|
|
214
|
+
);
|
|
198
215
|
}
|
|
199
|
-
throw new Error(
|
|
200
|
-
`[${label}] binding ${entry.binding} requires a GPUBuffer; ` +
|
|
201
|
-
`got ${describeBindGroupBufferValue(resource.buffer)}.`
|
|
202
|
-
);
|
|
203
216
|
}
|
|
204
217
|
}
|
|
205
218
|
|
|
@@ -211,16 +224,49 @@ function shouldValidateBindGroupDescriptors() {
|
|
|
211
224
|
return false;
|
|
212
225
|
}
|
|
213
226
|
|
|
214
|
-
function
|
|
215
|
-
if (
|
|
227
|
+
function wrapDeviceCreateBuffer(device) {
|
|
228
|
+
if (
|
|
229
|
+
!device
|
|
230
|
+
|| device.__dopplerBufferOwnershipWrapped
|
|
231
|
+
|| typeof device.createBuffer !== 'function'
|
|
232
|
+
) {
|
|
216
233
|
return device;
|
|
217
234
|
}
|
|
218
|
-
|
|
235
|
+
const originalCreateBuffer = device.createBuffer.bind(device);
|
|
236
|
+
device.createBuffer = (descriptor) => {
|
|
237
|
+
const buffer = originalCreateBuffer(descriptor);
|
|
238
|
+
if (buffer && (typeof buffer === 'object' || typeof buffer === 'function')) {
|
|
239
|
+
sharedDeviceState.bufferOwners.set(buffer, device);
|
|
240
|
+
}
|
|
241
|
+
return buffer;
|
|
242
|
+
};
|
|
243
|
+
Object.defineProperty(device, '__dopplerBufferOwnershipWrapped', {
|
|
244
|
+
value: true,
|
|
245
|
+
configurable: true,
|
|
246
|
+
enumerable: false,
|
|
247
|
+
writable: false,
|
|
248
|
+
});
|
|
249
|
+
return device;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function wrapDeviceCreateBindGroup(device) {
|
|
253
|
+
if (!device || device.__dopplerBindGroupValidationWrapped) {
|
|
219
254
|
return device;
|
|
220
255
|
}
|
|
221
256
|
const originalCreateBindGroup = device.createBindGroup.bind(device);
|
|
222
257
|
device.createBindGroup = (descriptor) => {
|
|
223
|
-
|
|
258
|
+
if (shouldValidateBindGroupDescriptors()) {
|
|
259
|
+
validateBindGroupDescriptor(descriptor, device);
|
|
260
|
+
} else {
|
|
261
|
+
const entries = Array.isArray(descriptor?.entries) ? descriptor.entries : [];
|
|
262
|
+
for (const entry of entries) {
|
|
263
|
+
const buffer = entry?.resource?.buffer;
|
|
264
|
+
if (buffer && sharedDeviceState.bufferOwners.get(buffer) !== undefined) {
|
|
265
|
+
validateBindGroupDescriptor(descriptor, device);
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
224
270
|
return originalCreateBindGroup(descriptor);
|
|
225
271
|
};
|
|
226
272
|
Object.defineProperty(device, '__dopplerBindGroupValidationWrapped', {
|
|
@@ -240,9 +286,10 @@ function registerDeviceLostHandler(device) {
|
|
|
240
286
|
if (device.lost && typeof device.lost.then === 'function') {
|
|
241
287
|
const trackedDevice = device;
|
|
242
288
|
device.lost.then((info) => {
|
|
243
|
-
if (gpuDevice !== trackedDevice) {
|
|
289
|
+
if (sharedDeviceState.gpuDevice !== trackedDevice) {
|
|
244
290
|
return;
|
|
245
291
|
}
|
|
292
|
+
hydrateDeviceState();
|
|
246
293
|
lastDeviceLossInfo = {
|
|
247
294
|
message: info?.message ?? '',
|
|
248
295
|
reason: info?.reason ?? 'unknown',
|
|
@@ -254,9 +301,10 @@ function registerDeviceLostHandler(device) {
|
|
|
254
301
|
clearActiveDeviceState();
|
|
255
302
|
advanceDeviceEpoch();
|
|
256
303
|
}).catch((error) => {
|
|
257
|
-
if (gpuDevice !== trackedDevice) {
|
|
304
|
+
if (sharedDeviceState.gpuDevice !== trackedDevice) {
|
|
258
305
|
return;
|
|
259
306
|
}
|
|
307
|
+
hydrateDeviceState();
|
|
260
308
|
lastDeviceLossInfo = {
|
|
261
309
|
message: error?.message ?? String(error),
|
|
262
310
|
reason: 'device_lost_handler_failed',
|
|
@@ -478,17 +526,7 @@ async function initializePlatformAndRegistry(adapter) {
|
|
|
478
526
|
}
|
|
479
527
|
|
|
480
528
|
|
|
481
|
-
|
|
482
|
-
hydrateDeviceState();
|
|
483
|
-
// Return cached device if available
|
|
484
|
-
if (gpuDevice) {
|
|
485
|
-
if (isUsableGPUDevice(gpuDevice)) {
|
|
486
|
-
return gpuDevice;
|
|
487
|
-
}
|
|
488
|
-
clearActiveDeviceState();
|
|
489
|
-
advanceDeviceEpoch();
|
|
490
|
-
}
|
|
491
|
-
|
|
529
|
+
async function initializeDevice() {
|
|
492
530
|
if (!isWebGPUAvailable()) {
|
|
493
531
|
throw createDopplerError(ERROR_CODES.GPU_UNAVAILABLE, 'WebGPU is not available in this browser');
|
|
494
532
|
}
|
|
@@ -517,6 +555,7 @@ export async function initDevice() {
|
|
|
517
555
|
}
|
|
518
556
|
lastDeviceLossInfo = null;
|
|
519
557
|
ensureGpuBufferConstructor(gpuDevice);
|
|
558
|
+
wrapDeviceCreateBuffer(gpuDevice);
|
|
520
559
|
wrapDeviceCreateBindGroup(gpuDevice);
|
|
521
560
|
registerDeviceLostHandler(gpuDevice);
|
|
522
561
|
advanceDeviceEpoch();
|
|
@@ -563,6 +602,34 @@ export async function initDevice() {
|
|
|
563
602
|
return gpuDevice;
|
|
564
603
|
}
|
|
565
604
|
|
|
605
|
+
export async function initDevice() {
|
|
606
|
+
hydrateDeviceState();
|
|
607
|
+
// Return cached device if available
|
|
608
|
+
if (gpuDevice) {
|
|
609
|
+
if (isUsableGPUDevice(gpuDevice)) {
|
|
610
|
+
return gpuDevice;
|
|
611
|
+
}
|
|
612
|
+
clearActiveDeviceState();
|
|
613
|
+
advanceDeviceEpoch();
|
|
614
|
+
}
|
|
615
|
+
if (sharedDeviceState.deviceInitPromise) {
|
|
616
|
+
const device = await sharedDeviceState.deviceInitPromise;
|
|
617
|
+
hydrateDeviceState();
|
|
618
|
+
return device;
|
|
619
|
+
}
|
|
620
|
+
const initialization = initializeDevice();
|
|
621
|
+
sharedDeviceState.deviceInitPromise = initialization;
|
|
622
|
+
try {
|
|
623
|
+
const device = await initialization;
|
|
624
|
+
hydrateDeviceState();
|
|
625
|
+
return device;
|
|
626
|
+
} finally {
|
|
627
|
+
if (sharedDeviceState.deviceInitPromise === initialization) {
|
|
628
|
+
sharedDeviceState.deviceInitPromise = null;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
|
|
566
633
|
export function setDevice(device, options = {}) {
|
|
567
634
|
hydrateDeviceState();
|
|
568
635
|
if (!device) {
|
|
@@ -574,6 +641,7 @@ export function setDevice(device, options = {}) {
|
|
|
574
641
|
gpuDevice = device;
|
|
575
642
|
lastDeviceLossInfo = null;
|
|
576
643
|
ensureGpuBufferConstructor(gpuDevice);
|
|
644
|
+
wrapDeviceCreateBuffer(gpuDevice);
|
|
577
645
|
wrapDeviceCreateBindGroup(gpuDevice);
|
|
578
646
|
registerDeviceLostHandler(gpuDevice);
|
|
579
647
|
advanceDeviceEpoch();
|