@genai-fi/nanogpt 1.1.1 → 1.1.3

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.
@@ -11,6 +11,7 @@ import { default as Training } from './api/training';
11
11
  import { GPUOptions } from './patches/webgpu_base';
12
12
  type TeachableLLMStatus = 'warmup' | 'awaitingTokens' | 'ready' | 'training' | 'loading' | 'busy' | 'error';
13
13
  export default class TeachableLLM {
14
+ static instances: Set<TeachableLLM>;
14
15
  private ee;
15
16
  private _config?;
16
17
  private _model?;
@@ -10,8 +10,10 @@ import c from "./utilities/profile.js";
10
10
  import l from "./api/responses.js";
11
11
  import u from "./api/training.js";
12
12
  import { selectBackend as d } from "./backend.js";
13
+ import { getBackendDevice as f } from "./patches/webgpu_base.js";
13
14
  //#region lib/TeachableLLM.ts
14
- var f = class f {
15
+ var p = class p {
16
+ static instances = /* @__PURE__ */ new Set();
15
17
  ee = new e();
16
18
  _config;
17
19
  _model;
@@ -24,11 +26,18 @@ var f = class f {
24
26
  version: 2,
25
27
  application: "@genai-fi/nanogpt"
26
28
  };
27
- static selectBackend(e, t) {
28
- return d(e, t);
29
+ static async selectBackend(e, t) {
30
+ if (await d(e, t), e === "webgpu") {
31
+ let e = f();
32
+ e && e.lost.then(() => {
33
+ console.warn("WebGPU device lost"), p.instances.forEach((e) => {
34
+ e.setStatus("error"), e.ee.emit("lost");
35
+ });
36
+ });
37
+ }
29
38
  }
30
39
  constructor(e, t) {
31
- this._config = t?.config, this._tokeniser = e, this._model = t, t?.metaData && (this.meta = t.metaData);
40
+ this._config = t?.config, this._tokeniser = e, this._model = t, t?.metaData && (this.meta = t.metaData), p.instances.add(this);
32
41
  }
33
42
  get vocab() {
34
43
  return this._tokeniser?.getVocab() || [];
@@ -115,7 +124,7 @@ var f = class f {
115
124
  } : void 0);
116
125
  }
117
126
  static loadModel(e, t) {
118
- let n = new f();
127
+ let n = new p();
119
128
  return o(e, t).then(({ model: e, tokeniser: t, metaData: a, optimizer: o, log: s }) => {
120
129
  r(e.config), n._model = e, n._tokeniser = t, n._config = e.config, a && (n.meta = a), n.setStatus("warmup"), i(e).then((t) => {
121
130
  n._memoryRequirements = t, o && e.metaData.pretrainingSettings && e.metaData.pretrainingData && n.training.restore(e.metaData.pretrainingSettings, s || [], o, e.metaData.pretrainingData), n.setStatus("ready"), n.ee.emit("loaded"), n.ee.emit("mode", n.mode);
@@ -128,7 +137,7 @@ var f = class f {
128
137
  }
129
138
  static create(e, o) {
130
139
  r(o);
131
- let s = o, c = e === "char" ? new t(s.vocabSize) : e === "bpe" ? new n(s.vocabSize) : e, l = a(s), u = new f(c, l);
140
+ let s = o, c = e === "char" ? new t(s.vocabSize) : e === "bpe" ? new n(s.vocabSize) : e, l = a(s), u = new p(c, l);
132
141
  return u.setStatus("warmup"), i(l).then((e) => {
133
142
  u._memoryRequirements = e, u.tokeniser.trained ? (u.setStatus("ready"), u.ee.emit("loaded"), u.ee.emit("mode", u.mode)) : (u.setStatus("awaitingTokens"), u.ee.emit("loaded"), u.ee.emit("mode", u.mode), u.tokeniser.once("trainStatus", (e) => {
134
143
  e === "trained" && u.setStatus("ready");
@@ -184,7 +193,7 @@ var f = class f {
184
193
  return this._training;
185
194
  }
186
195
  dispose() {
187
- this._responses &&= (this._responses.dispose(), null), this._training &&= (this._training.dispose(), null), this._model?.dispose(), this.ee.removeAllListeners();
196
+ this._responses &&= (this._responses.dispose(), null), this._training &&= (this._training.dispose(), null), this._model?.dispose(), this.ee.removeAllListeners(), p.instances.delete(this);
188
197
  }
189
198
  on(e, t) {
190
199
  if (e === "loaded" && this.loaded) {
@@ -198,4 +207,4 @@ var f = class f {
198
207
  }
199
208
  };
200
209
  //#endregion
201
- export { f as default };
210
+ export { p as default };
package/dist/backend.js CHANGED
@@ -4,7 +4,7 @@ async function r(r, i) {
4
4
  if (e() !== r) {
5
5
  if (r === "webgpu") {
6
6
  let { registerWebGPUBackend: e } = await import("./patches/webgpu_base.js");
7
- e(i), await import("./dist-9wGF4ci9.js"), await import("./ops/webgpu/index.js");
7
+ e(i), await import("./dist-BGVm64AY.js"), await import("./ops/webgpu/index.js");
8
8
  }
9
9
  await n(r), await t(), console.log(`Backend set to ${r}`);
10
10
  }