File size: 10,626 Bytes
c51a3e7
 
 
 
 
a9a3715
c51a3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
594cfa2
c51a3e7
 
 
 
594cfa2
c51a3e7
594cfa2
 
c51a3e7
 
594cfa2
c51a3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
594cfa2
 
c51a3e7
594cfa2
c51a3e7
 
 
 
 
 
 
 
594cfa2
c51a3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9a3715
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import { MODEL, DSPARK, DEFAULTS } from "./core.js";
import { WeightCache } from "./cache.js";
import { downloadForSession } from "./session-download.js";
import { startRuntime } from "./startup.js";
import { splitStreamContent } from "./stream.js";
export class Engine {
  constructor(onStatus) {
    this.onStatus = onStatus;
    this.logs = [];
    this.ready = false;
    this.loading = false;
    this.adapter = null;
    this.gpuLayers = 0;
    this.cache = new WeightCache();
    this.sessionFiles = new Map();
  }
  async probe() {
    if (!navigator.gpu)
      return {
        ok: false,
        message: "WebGPU is unavailable. Use a browser with WebGPU enabled.",
      };
    this.adapter = await navigator.gpu.requestAdapter({
      powerPreference: "high-performance",
    });
    if (!this.adapter)
      return {
        ok: false,
        message:
          "No WebGPU adapter found. Enable hardware acceleration in your browser, then reopen this page.",
      };
    const info = this.adapter.info;
    return {
      ok: true,
      name:
        info?.description || info?.device || info?.vendor || "WebGPU graphics",
      maxBuffer: this.adapter.limits.maxStorageBufferBindingSize,
      features: [...this.adapter.features],
      mobile: /Android|iPhone|iPad/i.test(navigator.userAgent),
    };
  }
  get url() {
    if (
      import.meta.env.DEV &&
      new URLSearchParams(location.search).get("qa-weights") === "local"
    )
      return location.origin + "/__qa/model/" + MODEL.file;
    return `https://huggingface.co/${MODEL.weights}/resolve/${MODEL.revision}/${MODEL.file}`;
  }
  get urls() {
    return Array.from({ length: 6 }, (_, i) =>
      this.url.replace("-00001-of-", `-${String(i + 1).padStart(5, "0")}-of-`),
    );
  }
  get draftURL() {
    return import.meta.env.DEV &&
      new URLSearchParams(location.search).get("qa-weights") === "local"
      ? location.origin + "/__qa/dspark"
      : `https://huggingface.co/${DSPARK.repo}/resolve/${DSPARK.revision}/${DSPARK.file}`;
  }
  assets(settings) {
    const files = this.urls.map((url, index) => ({
      url,
      size: MODEL.shardSizes[index],
      type: "model",
    }));
    if (settings.dspark !== "off")
      files.push({ url: this.draftURL, size: DSPARK.bytes, type: "draft" });
    return files;
  }
  async cacheState(settings = this.settings || DEFAULTS) {
    const entries = await this.cache.list(),
      files = this.assets(settings),
      complete = files.map((f) =>
        entries.find(
          (e) =>
            e.metadata?.originalURL === f.url &&
            e.size === f.size &&
            e.metadata.originalSize === f.size,
        ),
      );
    return {
      entries,
      complete,
      cached: complete.every(Boolean),
      bytes: complete.reduce((n, f) => n + (f?.size || 0), 0),
    };
  }
  async load(settings) {
    if (this.loading) throw new Error("Model is already loading.");
    this.loading = true;
    this.loadController = new AbortController();
    const signal = this.loadController.signal,
      start = performance.now();
    this.logs = [];
    this.gpuLayers = 0;
    this.downloadMetrics = { requests: 0, retries: 0 };
    try {
      if (this.ready) await this.unload();
      const gpu = await this.probe();
      if (!gpu.ok) throw new Error(gpu.message);
      const files = this.assets(settings),
        total = files.reduce((n, f) => n + f.size, 0),
        cached = settings.saveModel
          ? await this.cacheState(settings)
          : { complete: [], cached: false };
      const blobs = await Promise.all(
        files.map(
          (file, i) =>
            this.sessionFiles.get(file.url) ||
            (cached.complete[i]
              ? this.cache.open(cached.complete[i].name)
              : null),
        ),
      );
      const restored = blobs.reduce((sum, blob, index) => {
        if (blob?.size !== files[index].size) blobs[index] = null;
        return sum + (blobs[index]?.size || 0);
      }, 0);
      this.onStatus({
        phase: restored === total ? "compile" : "download",
        progress: restored / total,
        cached: cached.cached,
        label:
          restored === total
            ? "Restoring this model"
            : "Downloading Q8 model" +
              (settings.dspark !== "off" ? " + DSpark" : ""),
      });
      if (settings.saveModel) navigator.storage?.persist?.().catch(() => {});
      const missing = files
        .map((file, index) => ({ ...file, index }))
        .filter((f) => !blobs[f.index]);
      if (missing.length)
        await downloadForSession(
          missing.map((f) => f.url),
          {
            signal,
            total,
            fileSizes: missing.map((f) => f.size),
            parallel: gpu.mobile ? 4 : 8,
            onProgress: ({ loaded, requests, retries }) => {
              this.downloadMetrics = { requests, retries };
              this.onStatus({
                phase: "download",
                progress: (loaded + restored) / total,
                loaded: loaded + restored,
                total,
                elapsed: (performance.now() - start) / 1000,
                label:
                  settings.dspark !== "off"
                    ? "Downloading Q8 model + DSpark"
                    : "Downloading Q8 model",
                persistent: settings.saveModel,
              });
            },
            onFileComplete: async (blob, index) => {
              const file = missing[index];
              if (
                file.type === "model" &&
                new TextDecoder().decode(
                  await blob.slice(0, 4).arrayBuffer(),
                ) !== "GGUF"
              )
                throw new Error(
                  "The weight download was not a valid GGUF file. Please retry.",
                );
              this.sessionFiles.set(file.url, blob);
              blobs[file.index] = blob;
              if (settings.saveModel) {
                signal.throwIfAborted();
                await this.cache.write(
                  await this.cache.getNameFromURL(file.url),
                  blob.stream(),
                  { originalURL: file.url, originalSize: blob.size, etag: "" },
                );
              }
            },
          },
        );
      signal.throwIfAborted();
      this.downloadSeconds = (performance.now() - start) / 1000;
      this.onStatus({
        phase: "compile",
        progress: 1,
        label: "Preparing dedicated WebGPU kernels",
      });
      const report = (event) => {
        if (event.diagnostic) {
          this.logs.push({
            level: event.level || "info",
            line: event.diagnostic,
          });
          if (this.logs.length > 100) this.logs.shift();
        }
        this.onStatus(event);
      };
      const { MiniCPMRuntime } = await import("./minicpm-runtime.js");
      this.dedicated = new MiniCPMRuntime(report);
      const runtime = this.dedicated;
      await startRuntime(
        async () => {
          await runtime.load(blobs.slice(0, 6), settings, signal);
          if (settings.dspark !== "off") {
            report({
              diagnostic:
                "Preparing DSpark draft model and target verification",
              level: "info",
            });
            const { DSparkRuntime } = await import("./dspark-runtime.js");
            const draft = new DSparkRuntime(report);
            runtime.draft = draft;
            await draft.initialize(runtime, blobs[6], signal);
          }
        },
        { signal, stop: () => runtime.exit(), timeoutMs: 120000 },
      );
      this.info = {
        n_ctx: settings.context,
        n_layer: 42,
        n_embd: 2048,
        n_vocab: 130560,
      };
      this.gpuLayers = 43;
      this.ready = true;
      this.settings = { ...settings };
      this.sessionFiles.clear();
      this.loadSeconds = (performance.now() - start) / 1000;
      this.gpuSeconds = this.loadSeconds - this.downloadSeconds;
      this.runtimeName = "MiniCPM5 dedicated WebGPU";
      this.runtimeCapabilities = {
        subgroups: runtime.subgroups,
        speculative: !!runtime.draft,
      };
      report({
        diagnostic: `Dedicated WebGPU ready: ${runtime.subgroups ? "subgroup" : "portable"} kernels${runtime.draft ? ", DSpark enabled" : ""}. All 43 layers on the GPU.`,
        level: "info",
      });
      this.onStatus({
        phase: "ready",
        label: "Model ready · dedicated WebGPU",
        seconds: this.loadSeconds,
        cached: cached.cached,
        info: this.info,
      });
    } catch (error) {
      signal.aborted || this.loadController.abort();
      await this.dedicated?.exit().catch(() => {});
      this.dedicated = null;
      this.ready = false;
      throw error;
    } finally {
      this.loading = false;
    }
  }
  async chat(messages, settings, tools, signal, onChunk) {
    if (!this.ready) throw new Error("Load the model first.");
    const prompt = this.dedicated.renderPrompt(messages, settings, tools),
      splitter = { thinking: settings.thinking, pending: "" };
    let last;
    await this.dedicated.generate(prompt, settings, signal, async (token) => {
      if (token.progress) {
        await onChunk({ choices: [], prompt_progress: token.progress });
        return;
      }
      last = token;
      await onChunk({
        choices: [
          {
            delta: splitStreamContent(splitter, token.delta),
            finish_reason: token.finish,
          },
        ],
        timings: {
          predicted_n: token.index,
          predicted_per_second:
            token.index > 1
              ? (token.index - 1) /
                Math.max(0.001, (token.elapsed - token.prefillMs) / 1000)
              : 0,
          prompt_n: token.promptTokens,
          prompt_ms: token.prefillMs,
        },
      });
    });
    if (last)
      await onChunk({
        choices: [
          {
            delta: splitStreamContent(splitter, "", true),
            finish_reason: last.finish || "stop",
          },
        ],
        usage: { completion_tokens: last.index },
        speculation: this.dedicated.specStats,
      });
  }
  async unload() {
    try {
      await this.dedicated?.exit();
    } finally {
      this.ready = false;
      this.dedicated = null;
    }
  }
  async clearCache() {
    await this.cache.deleteMany(
      (f) =>
        f.metadata?.originalURL?.includes(MODEL.weights) ||
        f.metadata?.originalURL?.includes(DSPARK.repo) ||
        f.name.includes("MiniCPM5-2B-Q8_0") ||
        f.metadata?.originalURL?.endsWith("/__qa/dspark"),
    );
  }
}