@genai-fi/nanogpt 0.2.9 → 0.2.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/dist/Generator.d.ts +2 -0
- package/dist/Generator.js +37 -32
- package/dist/NanoGPTModel.d.ts +4 -1
- package/dist/NanoGPTModel.js +33 -25
- package/dist/TeachableLLM.d.ts +4 -0
- package/dist/TeachableLLM.js +32 -15
- package/dist/{complex-Cd8sqiBC.js → complex-CJ-qCcLB.js} +6 -6
- package/dist/{index-Dsg28SG6.js → index-YPKosni4.js} +59 -51
- package/dist/layers/BaseLayer.d.ts +8 -0
- package/dist/layers/BaseLayer.js +18 -0
- package/dist/layers/CausalSelfAttention.d.ts +4 -1
- package/dist/layers/CausalSelfAttention.js +47 -55
- package/dist/layers/MLP.d.ts +2 -1
- package/dist/layers/MLP.js +16 -14
- package/dist/layers/RMSNorm.d.ts +2 -1
- package/dist/layers/RMSNorm.js +13 -11
- package/dist/layers/RoPECache.d.ts +4 -2
- package/dist/layers/RoPECache.js +13 -7
- package/dist/layers/TiedEmbedding.js +16 -15
- package/dist/layers/TransformerBlock.d.ts +4 -1
- package/dist/layers/TransformerBlock.js +9 -5
- package/dist/main.js +18 -16
- package/dist/{mat_mul-BAYDrXvE.js → mat_mul-Bu7bhLms.js} +5 -5
- package/dist/ops/attentionMask.js +31 -25
- package/dist/ops/gatherSub.js +2 -2
- package/dist/ops/node/sparseCrossEntropy.js +1 -1
- package/dist/ops/qkv.d.ts +7 -0
- package/dist/ops/qkv.js +127 -0
- package/dist/ops/rope.d.ts +8 -0
- package/dist/ops/rope.js +153 -0
- package/dist/ops/scatterSub.js +14 -14
- package/dist/reshape-DmnmKT6r.js +25 -0
- package/dist/{stack-1o648CP_.js → stack-BtKpB0Ry.js} +5 -5
- package/dist/sum-D7fu15XL.js +27 -0
- package/dist/training/AdamExt.js +1 -1
- package/dist/training/Trainer.js +30 -29
- package/dist/training/sparseCrossEntropy.js +34 -33
- package/dist/utilities/profile.d.ts +10 -0
- package/dist/utilities/profile.js +29 -0
- package/package.json +1 -1
- package/dist/sum-NWazHI7f.js +0 -49
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { gatherSub as
|
|
2
|
-
import { scatterSub as
|
|
3
|
-
import { o as l,
|
|
4
|
-
import {
|
|
1
|
+
import { gatherSub as _ } from "../ops/gatherSub.js";
|
|
2
|
+
import { scatterSub as v } from "../ops/scatterSub.js";
|
|
3
|
+
import { o as l, d, E as f, M as w, y as I, L as z, z as N, b as E, D as F, F as M, e as m, H as g, I as $, J as S } from "../index-YPKosni4.js";
|
|
4
|
+
import { r as b } from "../reshape-DmnmKT6r.js";
|
|
5
|
+
import { s as T } from "../sum-D7fu15XL.js";
|
|
5
6
|
/**
|
|
6
7
|
* @license
|
|
7
8
|
* Copyright 2017 Google LLC. All Rights Reserved.
|
|
@@ -18,16 +19,16 @@ import { s as F, r as b } from "../sum-NWazHI7f.js";
|
|
|
18
19
|
* limitations under the License.
|
|
19
20
|
* =============================================================================
|
|
20
21
|
*/
|
|
21
|
-
function
|
|
22
|
+
function D(n, s, t) {
|
|
22
23
|
const a = n.length + s.length, e = [];
|
|
23
24
|
let r = 0, c = 0;
|
|
24
25
|
for (let o = 0; o < a; o++)
|
|
25
26
|
t.indexOf(o) === -1 ? e.push(n[r++]) : e.push(s[c++]);
|
|
26
27
|
return e;
|
|
27
28
|
}
|
|
28
|
-
function
|
|
29
|
+
function P(n, s) {
|
|
29
30
|
const t = s.map((a) => 1);
|
|
30
|
-
return
|
|
31
|
+
return D(n, t, s);
|
|
31
32
|
}
|
|
32
33
|
/**
|
|
33
34
|
* @license
|
|
@@ -45,11 +46,11 @@ function A(n, s) {
|
|
|
45
46
|
* limitations under the License.
|
|
46
47
|
* =============================================================================
|
|
47
48
|
*/
|
|
48
|
-
function
|
|
49
|
+
function A(n, s = null, t = !1) {
|
|
49
50
|
const e = { x: d(n, "x", "max") }, r = { reductionIndices: s, keepDims: t };
|
|
50
|
-
return f.runKernel(
|
|
51
|
+
return f.runKernel(w, e, r);
|
|
51
52
|
}
|
|
52
|
-
const L = /* @__PURE__ */ l({ max_:
|
|
53
|
+
const L = /* @__PURE__ */ l({ max_: A });
|
|
53
54
|
/**
|
|
54
55
|
* @license
|
|
55
56
|
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
@@ -66,11 +67,11 @@ const L = /* @__PURE__ */ l({ max_: D });
|
|
|
66
67
|
* limitations under the License.
|
|
67
68
|
* =============================================================================
|
|
68
69
|
*/
|
|
69
|
-
function
|
|
70
|
+
function H(n) {
|
|
70
71
|
const t = { x: d(n, "x", "exp") };
|
|
71
|
-
return f.runKernel(
|
|
72
|
+
return f.runKernel(I, t);
|
|
72
73
|
}
|
|
73
|
-
const
|
|
74
|
+
const J = /* @__PURE__ */ l({ exp_: H });
|
|
74
75
|
/**
|
|
75
76
|
* @license
|
|
76
77
|
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
@@ -87,11 +88,11 @@ const W = /* @__PURE__ */ l({ exp_: O });
|
|
|
87
88
|
* limitations under the License.
|
|
88
89
|
* =============================================================================
|
|
89
90
|
*/
|
|
90
|
-
function
|
|
91
|
+
function O(n) {
|
|
91
92
|
const t = { x: d(n, "x", "log", "float32") };
|
|
92
|
-
return f.runKernel(
|
|
93
|
+
return f.runKernel(z, t);
|
|
93
94
|
}
|
|
94
|
-
const
|
|
95
|
+
const W = /* @__PURE__ */ l({ log_: O });
|
|
95
96
|
/**
|
|
96
97
|
* @license
|
|
97
98
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
@@ -108,20 +109,20 @@ const q = /* @__PURE__ */ l({ log_: j });
|
|
|
108
109
|
* limitations under the License.
|
|
109
110
|
* =============================================================================
|
|
110
111
|
*/
|
|
111
|
-
function
|
|
112
|
+
function j(n, s = null, t = !1) {
|
|
112
113
|
const a = d(n, "x", "logSumExp"), e = N(s, a.shape), r = L(
|
|
113
114
|
a,
|
|
114
115
|
e,
|
|
115
116
|
!0
|
|
116
117
|
/* keepDims */
|
|
117
|
-
), c = E(a, r), o =
|
|
118
|
+
), c = E(a, r), o = J(c), p = T(o, e), u = W(p), x = F(b(r, u.shape), u);
|
|
118
119
|
if (t) {
|
|
119
|
-
const h =
|
|
120
|
-
return b(
|
|
120
|
+
const h = P(x.shape, e);
|
|
121
|
+
return b(x, h);
|
|
121
122
|
}
|
|
122
|
-
return
|
|
123
|
+
return x;
|
|
123
124
|
}
|
|
124
|
-
const
|
|
125
|
+
const q = /* @__PURE__ */ l({ logSumExp_: j });
|
|
125
126
|
/**
|
|
126
127
|
* @license
|
|
127
128
|
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
@@ -138,21 +139,21 @@ const H = /* @__PURE__ */ l({ logSumExp_: B });
|
|
|
138
139
|
* limitations under the License.
|
|
139
140
|
* =============================================================================
|
|
140
141
|
*/
|
|
141
|
-
function
|
|
142
|
+
function B(n, s = -1) {
|
|
142
143
|
const t = d(n, "logits", "softmax", "float32");
|
|
143
144
|
if (s === -1 && (s = t.rank - 1), s !== t.rank - 1)
|
|
144
145
|
throw Error(`Softmax along a non-last dimension is not yet supported. Logits was rank ${t.rank} and dim was ${s}`);
|
|
145
146
|
const a = { logits: t }, e = { dim: s };
|
|
146
|
-
return f.runKernel(
|
|
147
|
+
return f.runKernel(M, a, e);
|
|
147
148
|
}
|
|
148
|
-
const Q = /* @__PURE__ */ l({ softmax_:
|
|
149
|
+
const Q = /* @__PURE__ */ l({ softmax_: B });
|
|
149
150
|
function R(n, s) {
|
|
150
151
|
return $(() => {
|
|
151
|
-
const t = n.shape[n.shape.length - 1], e = n.shape.slice(0, -1).reduce((h,
|
|
152
|
-
return
|
|
152
|
+
const t = n.shape[n.shape.length - 1], e = n.shape.slice(0, -1).reduce((h, i) => h * i, 1), r = n.shape.length > 2 ? n.reshape([e, t]) : n, c = s.shape.length > 1 ? s.reshape([e]).cast("int32") : s.cast("int32"), o = L(r, -1, !0), p = E(r, o), u = q(p, -1);
|
|
153
|
+
return _(u, c, p);
|
|
153
154
|
});
|
|
154
155
|
}
|
|
155
|
-
function
|
|
156
|
+
function ts() {
|
|
156
157
|
return m().backendName === "tensorflow" ? g((s, t, a) => {
|
|
157
158
|
const e = s.shape.length > 2 ? s.reshape([-1, s.shape[s.shape.length - 1]]) : s, r = t.shape.length > 1 ? t.reshape([-1]).cast("int32") : t.cast("int32"), [c, o] = m().runKernel(
|
|
158
159
|
"NativeSparseSoftmaxCrossEntropy",
|
|
@@ -163,15 +164,15 @@ function ss() {
|
|
|
163
164
|
}) : g(
|
|
164
165
|
// @ts-expect-error Invalid params
|
|
165
166
|
(s, t, a) => {
|
|
166
|
-
const e = s.shape[s.shape.length - 1], c = s.shape.slice(0, -1).reduce((h,
|
|
167
|
-
return a([o, p]), o.dispose(), p.dispose(), { value: u, gradFunc: (h,
|
|
168
|
-
const y =
|
|
169
|
-
return [G.reshape(s.shape),
|
|
167
|
+
const e = s.shape[s.shape.length - 1], c = s.shape.slice(0, -1).reduce((h, i) => h * i, 1), o = s.reshape([c, e]), p = t.reshape([c]).cast("int32"), u = R(o, p);
|
|
168
|
+
return a([o, p]), o.dispose(), p.dispose(), { value: u, gradFunc: (h, i) => $(() => {
|
|
169
|
+
const y = i[0], k = i[1], C = Q(y), G = v(C, k, h), K = S(t);
|
|
170
|
+
return [G.reshape(s.shape), K];
|
|
170
171
|
}) };
|
|
171
172
|
}
|
|
172
173
|
);
|
|
173
174
|
}
|
|
174
175
|
export {
|
|
175
|
-
|
|
176
|
+
ts as createSoftmaxCrossEntropyWithGrad,
|
|
176
177
|
R as sparseSoftmaxCrossEntropy
|
|
177
178
|
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { m as s } from "../index-YPKosni4.js";
|
|
2
|
+
const m = 1024 * 1024;
|
|
3
|
+
class i {
|
|
4
|
+
log = /* @__PURE__ */ new Map();
|
|
5
|
+
maxMemory = 0;
|
|
6
|
+
maxLabel;
|
|
7
|
+
lastMemInfo = [];
|
|
8
|
+
peakMemory = 0;
|
|
9
|
+
startMemory() {
|
|
10
|
+
this.lastMemInfo.push(s());
|
|
11
|
+
}
|
|
12
|
+
endMemory(e) {
|
|
13
|
+
if (this.lastMemInfo.length === 0) {
|
|
14
|
+
console.warn("MemoryProfiler: endMemory called without matching startMemory");
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const o = s(), t = o.numBytes - (this.lastMemInfo.pop()?.numBytes || 0);
|
|
18
|
+
this.log.set(e, Math.max(this.log.get(e) || 0, t)), t > this.maxMemory && (this.maxMemory = t, this.maxLabel = e), this.peakMemory = Math.max(this.peakMemory, o.numBytes);
|
|
19
|
+
}
|
|
20
|
+
printSummary() {
|
|
21
|
+
console.log("Memory Usage Summary:");
|
|
22
|
+
for (const [e, o] of this.log.entries())
|
|
23
|
+
console.log(`- ${e}: ${(o / m).toFixed(2)} MB`);
|
|
24
|
+
this.maxLabel && console.log(`Peak Memory Usage: ${(this.maxMemory / m).toFixed(2)} MB at "${this.maxLabel}"`), console.log(`Overall Peak Memory Usage: ${(this.peakMemory / m).toFixed(2)} MB`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export {
|
|
28
|
+
i as default
|
|
29
|
+
};
|
package/package.json
CHANGED
package/dist/sum-NWazHI7f.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { o, c as a, E as u, i, j as p, S as x } from "./index-Dsg28SG6.js";
|
|
2
|
-
/**
|
|
3
|
-
* @license
|
|
4
|
-
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
* =============================================================================
|
|
17
|
-
*/
|
|
18
|
-
function l(n, t) {
|
|
19
|
-
const s = { x: a(n, "x", "reshape", "string_or_numeric") }, r = { shape: t };
|
|
20
|
-
return u.runKernel(i, s, r);
|
|
21
|
-
}
|
|
22
|
-
const h = /* @__PURE__ */ o({ reshape_: l });
|
|
23
|
-
/**
|
|
24
|
-
* @license
|
|
25
|
-
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
26
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
27
|
-
* you may not use this file except in compliance with the License.
|
|
28
|
-
* You may obtain a copy of the License at
|
|
29
|
-
*
|
|
30
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
31
|
-
*
|
|
32
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
33
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
34
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
35
|
-
* See the License for the specific language governing permissions and
|
|
36
|
-
* limitations under the License.
|
|
37
|
-
* =============================================================================
|
|
38
|
-
*/
|
|
39
|
-
function m(n, t = null, e = !1) {
|
|
40
|
-
let s = a(n, "x", "sum");
|
|
41
|
-
s.dtype === "bool" && (s = p(s, "int32"));
|
|
42
|
-
const r = { x: s }, c = { axis: t, keepDims: e };
|
|
43
|
-
return u.runKernel(x, r, c);
|
|
44
|
-
}
|
|
45
|
-
const _ = /* @__PURE__ */ o({ sum_: m });
|
|
46
|
-
export {
|
|
47
|
-
h as r,
|
|
48
|
-
_ as s
|
|
49
|
-
};
|