@genai-fi/nanogpt 0.6.3 → 0.7.1
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.js +17 -13
- package/dist/NanoGPTModel.d.ts +2 -2
- package/dist/NanoGPTModel.js +104 -136
- package/dist/{RealDiv-BYViZwhN.js → RealDiv-CVYNbZxu.js} +30 -29
- package/dist/{Reshape-t7Kcikjk.js → Reshape-CEsEp0AI.js} +5 -5
- package/dist/Reshape-Do18N3gO.js +30 -0
- package/dist/TeachableLLM.js +9 -5
- package/dist/{TiedEmbedding-9WeDwvjO.js → TiedEmbedding-ccLBFiZi.js} +4 -4
- package/dist/{axis_util-Bu4h7XWV.js → axis_util-5DTW2tFV.js} +3 -3
- package/dist/backend.d.ts +1 -0
- package/dist/backend.js +7 -0
- package/dist/backend_util-C9Ut8n0Q.js +749 -0
- package/dist/{broadcast_to-DARN-DBD.js → broadcast_to-Ba9h_8DO.js} +2 -2
- package/dist/{concat-5aPGqw3Z.js → concat-CbXTetof.js} +8 -8
- package/dist/{dataset-pgqp-YfL.js → dataset-U3PrjwgU.js} +7 -7
- package/dist/{dropout-Bciw46HT.js → dropout-DPfPgWWe.js} +8 -8
- package/dist/{gather-DjyCjmOD.js → gather-Bbh8DHhM.js} +1 -1
- package/dist/gelu-BFwVnd1r.js +26 -0
- package/dist/gpgpu_math-DffelNS-.js +2371 -0
- package/dist/index-DYD_yPa-.js +12076 -0
- package/dist/{index-BAzbokzv.js → index-UdZhlibC.js} +414 -398
- package/dist/{kernel_funcs_utils-CUxJCg0g.js → kernel_funcs_utils-CXDy3EN7.js} +31 -30
- package/dist/layers/BaseLayer.js +2 -2
- package/dist/layers/CausalSelfAttention.js +8 -8
- package/dist/layers/MLP.js +5 -5
- package/dist/layers/RMSNorm.js +3 -3
- package/dist/layers/RoPECache.js +4 -4
- package/dist/layers/TiedEmbedding.js +5 -5
- package/dist/layers/TransformerBlock.js +1 -1
- package/dist/loader/loadTransformers.js +1 -1
- package/dist/loader/oldZipLoad.js +11 -7
- package/dist/{log_sum_exp-YEo2h3gb.js → log_sum_exp-BnmCkHWl.js} +16 -16
- package/dist/main.d.ts +13 -0
- package/dist/main.js +44 -23
- package/dist/{mat_mul-7121rsJk.js → mat_mul-dwmZz69e.js} +4 -4
- package/dist/{max-DtlIuVeW.js → max-ByjEGoFx.js} +3 -3
- package/dist/{mulmat_packed_gpu-D4nKF7Je.js → mulmat_packed_gpu-IGPBp6h9.js} +1 -1
- package/dist/non_max_suppression_impl-CsEgBuMA.js +134 -0
- package/dist/{ones-BBlSRqn1.js → ones-C8Mfln6-.js} +2 -2
- package/dist/ops/adamAdjust.d.ts +2 -0
- package/dist/ops/adamAdjust.js +9 -0
- package/dist/ops/adamMoments.d.ts +2 -0
- package/dist/ops/adamMoments.js +9 -0
- package/dist/ops/appendCache.js +3 -3
- package/dist/ops/attentionMask.js +1 -1
- package/dist/ops/cpu/adamAdjust.d.ts +1 -0
- package/dist/ops/cpu/adamAdjust.js +18 -0
- package/dist/ops/cpu/adamMoments.d.ts +1 -0
- package/dist/ops/cpu/adamMoments.js +16 -0
- package/dist/ops/cpu/appendCache.js +8 -8
- package/dist/ops/cpu/attentionMask.js +9 -9
- package/dist/ops/cpu/fusedSoftmax.js +17 -11
- package/dist/ops/cpu/gatherSub.js +7 -7
- package/dist/ops/cpu/gelu.js +13 -13
- package/dist/ops/cpu/matMulGelu.js +36 -24
- package/dist/ops/cpu/matMulMul.js +14 -8
- package/dist/ops/cpu/mulDropout.js +9 -3
- package/dist/ops/cpu/normRMS.js +5 -5
- package/dist/ops/cpu/qkv.js +3 -3
- package/dist/ops/cpu/rope.js +5 -5
- package/dist/ops/cpu/scatterSub.js +11 -11
- package/dist/ops/fusedSoftmax.js +1 -1
- package/dist/ops/gatherSub.js +1 -1
- package/dist/ops/gelu.js +2 -2
- package/dist/ops/grads/attentionMask.js +1 -1
- package/dist/ops/grads/fusedSoftmax.js +2 -2
- package/dist/ops/grads/gelu.js +3 -24
- package/dist/ops/grads/matMulGelu.js +5 -5
- package/dist/ops/grads/normRMS.js +6 -6
- package/dist/ops/grads/qkv.js +1 -1
- package/dist/ops/grads/rope.js +3 -3
- package/dist/ops/matMulGelu.js +1 -1
- package/dist/ops/matMulMul.js +1 -1
- package/dist/ops/mulDrop.js +1 -1
- package/dist/ops/normRMS.js +1 -1
- package/dist/ops/qkv.js +1 -1
- package/dist/ops/rope.js +4 -4
- package/dist/ops/scatterSub.js +1 -1
- package/dist/ops/webgl/adamAdjust.d.ts +1 -0
- package/dist/ops/webgl/adamAdjust.js +50 -0
- package/dist/ops/webgl/adamMoments.d.ts +1 -0
- package/dist/ops/webgl/adamMoments.js +38 -0
- package/dist/ops/webgl/appendCache.js +1 -1
- package/dist/ops/webgl/attentionMask.js +1 -1
- package/dist/ops/webgl/fusedSoftmax.js +4 -4
- package/dist/ops/webgl/gatherSub.js +8 -8
- package/dist/ops/webgl/gelu.js +2 -2
- package/dist/ops/webgl/log.js +5 -5
- package/dist/ops/webgl/matMulGelu.js +17 -17
- package/dist/ops/webgl/matMulMul.js +1 -1
- package/dist/ops/webgl/mulDropout.js +4 -4
- package/dist/ops/webgl/normRMS.js +2 -2
- package/dist/ops/webgl/qkv.js +1 -1
- package/dist/ops/webgl/rope.js +1 -1
- package/dist/ops/webgl/scatterSub.js +1 -1
- package/dist/ops/webgpu/adamAdjust.d.ts +1 -0
- package/dist/ops/webgpu/adamAdjust.js +52 -0
- package/dist/ops/webgpu/adamMoments.d.ts +1 -0
- package/dist/ops/webgpu/adamMoments.js +51 -0
- package/dist/ops/webgpu/appendCache.d.ts +1 -0
- package/dist/ops/webgpu/appendCache.js +57 -0
- package/dist/ops/webgpu/attentionMask.d.ts +1 -0
- package/dist/ops/webgpu/attentionMask.js +65 -0
- package/dist/ops/webgpu/gatherSub.d.ts +1 -0
- package/dist/ops/webgpu/gatherSub.js +52 -0
- package/dist/ops/webgpu/gelu.d.ts +14 -0
- package/dist/ops/webgpu/gelu.js +87 -0
- package/dist/ops/webgpu/index.d.ts +0 -0
- package/dist/ops/webgpu/index.js +11 -0
- package/dist/ops/webgpu/normRMS.d.ts +1 -0
- package/dist/ops/webgpu/normRMS.js +41 -0
- package/dist/ops/webgpu/normRMSGrad.d.ts +1 -0
- package/dist/ops/webgpu/normRMSGrad.js +128 -0
- package/dist/ops/webgpu/qkv.d.ts +1 -0
- package/dist/ops/webgpu/qkv.js +57 -0
- package/dist/ops/webgpu/rope.d.ts +1 -0
- package/dist/ops/webgpu/rope.js +69 -0
- package/dist/ops/webgpu/scatterSub.d.ts +1 -0
- package/dist/ops/webgpu/scatterSub.js +38 -0
- package/dist/ops/webgpu/utils/reductions.d.ts +9 -0
- package/dist/ops/webgpu/utils/reductions.js +68 -0
- package/dist/{ops-C0sQEcPw.js → ops-aRTXR2Sr.js} +433 -508
- package/dist/{random_width-DWzaOgrn.js → random_width-DbSpgl4o.js} +144 -144
- package/dist/{range-DYsrnfiy.js → range-D9CZhVlR.js} +1 -1
- package/dist/{reciprocal-CJQeasVa.js → reciprocal-CGB48wZB.js} +1 -1
- package/dist/{register_all_kernels-BfFCQAqs.js → register_all_kernels-DnbAyBXt.js} +203 -200
- package/dist/{reshape-krWGKraP.js → reshape-BR0eoLYN.js} +1 -1
- package/dist/{scatter_nd_util-93ln7Hut.js → scatter_nd_util-OjyAxku2.js} +3 -3
- package/dist/{selu_util-sntGesxr.js → selu_util-Ce6pu9IM.js} +44 -44
- package/dist/{shared-Ca6iDobD.js → shared-Czipaeb6.js} +541 -606
- package/dist/shared-DS5waSIY.js +69 -0
- package/dist/{sin-D_h-qCSx.js → sin-CiBxrDqX.js} +1 -1
- package/dist/slice-BHbDHObE.js +28 -0
- package/dist/{softmax-fsdtf6JC.js → softmax-JMEIUo2J.js} +1 -1
- package/dist/{split-eiktj-6L.js → split-CRU0PjVV.js} +4 -4
- package/dist/{stack-dfEEz2OY.js → stack-ikk2Y8_P.js} +2 -2
- package/dist/{sum-BE_Irnim.js → sum-NLYbiDag.js} +3 -3
- package/dist/{tensor-Xyi595sG.js → tensor-Do9PKbIE.js} +1 -1
- package/dist/{tensor2d-CPEkynbH.js → tensor2d-CWHxHpLh.js} +1 -1
- package/dist/training/Adam.d.ts +22 -0
- package/dist/training/Adam.js +93 -0
- package/dist/training/AdamExt.d.ts +1 -1
- package/dist/training/AdamExt.js +13 -12
- package/dist/training/DatasetBuilder.js +2 -2
- package/dist/training/Evaluator.js +1 -1
- package/dist/training/FullTrainer.js +27 -27
- package/dist/training/Trainer.d.ts +5 -6
- package/dist/training/Trainer.js +54 -55
- package/dist/training/sparseCrossEntropy.d.ts +0 -4
- package/dist/training/sparseCrossEntropy.js +7 -7
- package/dist/utilities/arrayClose.d.ts +1 -0
- package/dist/utilities/arrayClose.js +11 -0
- package/dist/utilities/dummy.js +19 -19
- package/dist/utilities/generate.js +15 -16
- package/dist/utilities/multinomialCPU.d.ts +2 -0
- package/dist/utilities/multinomialCPU.js +13 -0
- package/dist/utilities/performance.d.ts +2 -0
- package/dist/utilities/performance.js +16 -0
- package/dist/utilities/profile.d.ts +1 -0
- package/dist/utilities/profile.js +9 -6
- package/dist/utilities/safetensors.js +2 -2
- package/dist/utilities/weights.js +2 -2
- package/dist/{variable-wSS22xj5.js → variable-BTBkayv_.js} +1 -1
- package/dist/webgpu_program-WaoMq-WD.js +548 -0
- package/dist/webgpu_util-DhSeP4b6.js +80 -0
- package/dist/{zeros-YJDE7oRb.js → zeros-DnPT2nD4.js} +10 -10
- package/package.json +2 -1
- package/dist/gpgpu_math-CNslybmD.js +0 -3115
- package/dist/norm-CzltS9Fz.js +0 -86
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { s as l } from "./shared-Czipaeb6.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
|
+
const { addImpl: m, bincountImpl: s, bincountReduceImpl: a, bitwiseAndImpl: I, castImpl: e, ceilImpl: t, concatImpl: r, equalImpl: i, expImpl: C, expm1Impl: P, floorImpl: U, gatherNdImpl: n, gatherV2Impl: g, greaterImpl: o, greaterEqualImpl: c, lessImpl: u, lessEqualImpl: d, linSpaceImpl: q, logImpl: R, maxImpl: h, maximumImpl: x, minimumImpl: b, multiplyImpl: E, negImpl: S, notEqualImpl: T, prodImpl: w, raggedGatherImpl: y, raggedRangeImpl: A, raggedTensorToTensorImpl: F, rangeImpl: G, rsqrtImpl: N, scatterImpl: f, sigmoidImpl: k, simpleAbsImpl: B, sliceImpl: H, sparseFillEmptyRowsImpl: K, sparseReshapeImpl: V, sparseSegmentReductionImpl: j, sqrtImpl: v, staticRegexReplaceImpl: z, stridedSliceImpl: D, stringNGramsImpl: J, stringSplitImpl: L, stringToHashBucketFastImpl: M, subImpl: O, tileImpl: Q, topKImpl: W, transposeImpl: X, uniqueImpl: Y } = l;
|
|
19
|
+
export {
|
|
20
|
+
b as A,
|
|
21
|
+
O as B,
|
|
22
|
+
S as C,
|
|
23
|
+
w as D,
|
|
24
|
+
y as E,
|
|
25
|
+
A as F,
|
|
26
|
+
F as G,
|
|
27
|
+
G as H,
|
|
28
|
+
N as I,
|
|
29
|
+
k as J,
|
|
30
|
+
K,
|
|
31
|
+
V as L,
|
|
32
|
+
j as M,
|
|
33
|
+
f as N,
|
|
34
|
+
v as O,
|
|
35
|
+
z as P,
|
|
36
|
+
D as Q,
|
|
37
|
+
J as R,
|
|
38
|
+
L as S,
|
|
39
|
+
M as T,
|
|
40
|
+
Q as U,
|
|
41
|
+
W as V,
|
|
42
|
+
Y as W,
|
|
43
|
+
E as a,
|
|
44
|
+
m as b,
|
|
45
|
+
H as c,
|
|
46
|
+
s as d,
|
|
47
|
+
I as e,
|
|
48
|
+
e as f,
|
|
49
|
+
t as g,
|
|
50
|
+
r as h,
|
|
51
|
+
a as i,
|
|
52
|
+
i as j,
|
|
53
|
+
C as k,
|
|
54
|
+
P as l,
|
|
55
|
+
h as m,
|
|
56
|
+
T as n,
|
|
57
|
+
U as o,
|
|
58
|
+
n as p,
|
|
59
|
+
g as q,
|
|
60
|
+
o as r,
|
|
61
|
+
B as s,
|
|
62
|
+
X as t,
|
|
63
|
+
c as u,
|
|
64
|
+
u as v,
|
|
65
|
+
d as w,
|
|
66
|
+
q as x,
|
|
67
|
+
R as y,
|
|
68
|
+
x as z
|
|
69
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { B as c, C as e, E as a, W as l } from "./index-UdZhlibC.js";
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright 2018 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 p(r, n, o) {
|
|
19
|
+
const s = e(r, "x", "slice", "string_or_numeric");
|
|
20
|
+
if (s.rank === 0)
|
|
21
|
+
throw new Error("Slicing scalar is not possible");
|
|
22
|
+
const t = { x: s }, i = { begin: n, size: o };
|
|
23
|
+
return a.runKernel(l, t, i);
|
|
24
|
+
}
|
|
25
|
+
const x = /* @__PURE__ */ c({ slice_: p });
|
|
26
|
+
export {
|
|
27
|
+
x as s
|
|
28
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { B as p, C as i, E as a, L as c } from "./index-UdZhlibC.js";
|
|
2
2
|
/**
|
|
3
3
|
* @license
|
|
4
4
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
@@ -15,9 +15,9 @@ import { o as p, q as i, E as a, F as c } from "./index-BAzbokzv.js";
|
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
* =============================================================================
|
|
17
17
|
*/
|
|
18
|
-
function e(t, s,
|
|
19
|
-
const
|
|
20
|
-
return a.runKernel(c,
|
|
18
|
+
function e(t, s, n = 0) {
|
|
19
|
+
const o = { x: i(t, "x", "split") }, r = { numOrSizeSplits: s, axis: n };
|
|
20
|
+
return a.runKernel(c, o, r);
|
|
21
21
|
}
|
|
22
22
|
const u = /* @__PURE__ */ p({ split_: e });
|
|
23
23
|
export {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { B as e, D as c, l as n, E as k, P as i } from "./index-UdZhlibC.js";
|
|
2
2
|
/**
|
|
3
3
|
* @license
|
|
4
4
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
@@ -19,7 +19,7 @@ function u(r, t = 0) {
|
|
|
19
19
|
const s = c(r, "tensors", "stack", "string_or_numeric");
|
|
20
20
|
n(s.length >= 1, () => "Pass at least one tensor to tf.stack"), s.length > 0 && n(t <= s[0].rank, () => "Axis must be <= rank of the tensor");
|
|
21
21
|
const o = s, a = { axis: t };
|
|
22
|
-
return
|
|
22
|
+
return k.runKernel(i, o, a);
|
|
23
23
|
}
|
|
24
24
|
const l = /* @__PURE__ */ e({ stack_: u });
|
|
25
25
|
export {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { B as e, C as u, X as c, E as l, Y as m } from "./index-UdZhlibC.js";
|
|
2
2
|
/**
|
|
3
3
|
* @license
|
|
4
4
|
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
@@ -15,10 +15,10 @@ import { o as e, q as u, L as c, E as l, N as m } from "./index-BAzbokzv.js";
|
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
* =============================================================================
|
|
17
17
|
*/
|
|
18
|
-
function i(t,
|
|
18
|
+
function i(t, n = null, o = !1) {
|
|
19
19
|
let s = u(t, "x", "sum");
|
|
20
20
|
s.dtype === "bool" && (s = c(s, "int32"));
|
|
21
|
-
const r = { x: s }, a = { axis:
|
|
21
|
+
const r = { x: s }, a = { axis: n, keepDims: o };
|
|
22
22
|
return l.runKernel(m, r, a);
|
|
23
23
|
}
|
|
24
24
|
const f = /* @__PURE__ */ e({ sum_: i });
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Optimizer } from '@tensorflow/tfjs-core';
|
|
2
|
+
import { ConfigDict, Serializable, SerializableConstructor } from '@tensorflow/tfjs-core/dist/serialization';
|
|
3
|
+
import { NamedTensor, NamedVariableMap } from '@tensorflow/tfjs-core/dist/tensor_types';
|
|
4
|
+
export declare class AdamOptimizer extends Optimizer {
|
|
5
|
+
protected learningRate: number;
|
|
6
|
+
protected beta1: number;
|
|
7
|
+
protected beta2: number;
|
|
8
|
+
protected epsilon: number | null;
|
|
9
|
+
/** @nocollapse */
|
|
10
|
+
static get className(): string;
|
|
11
|
+
private accBeta1;
|
|
12
|
+
private accBeta2;
|
|
13
|
+
private accumulatedMoments;
|
|
14
|
+
constructor(learningRate: number, beta1: number, beta2: number, epsilon?: number | null);
|
|
15
|
+
applyGradients(variableGradients: NamedVariableMap | NamedTensor[]): void;
|
|
16
|
+
dispose(): void;
|
|
17
|
+
getWeights(): Promise<NamedTensor[]>;
|
|
18
|
+
setWeights(weightValues: NamedTensor[]): Promise<void>;
|
|
19
|
+
getConfig(): ConfigDict;
|
|
20
|
+
/** @nocollapse */
|
|
21
|
+
static fromConfig<T extends Serializable>(cls: SerializableConstructor<T>, config: ConfigDict): T;
|
|
22
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { adamAdjust as b } from "../ops/adamAdjust.js";
|
|
2
|
+
import { adamMoments as d } from "../ops/adamMoments.js";
|
|
3
|
+
import { O as g, e as h, t as o, d as B } from "../index-UdZhlibC.js";
|
|
4
|
+
import { z as M } from "../zeros-DnPT2nD4.js";
|
|
5
|
+
/**
|
|
6
|
+
* @license
|
|
7
|
+
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
8
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9
|
+
* you may not use this file except in compliance with the License.
|
|
10
|
+
* You may obtain a copy of the License at
|
|
11
|
+
*
|
|
12
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
*
|
|
14
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
15
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17
|
+
* See the License for the specific language governing permissions and
|
|
18
|
+
* limitations under the License.
|
|
19
|
+
* =============================================================================
|
|
20
|
+
*/
|
|
21
|
+
class R extends g {
|
|
22
|
+
constructor(t, a, e, s = null) {
|
|
23
|
+
super(), this.learningRate = t, this.beta1 = a, this.beta2 = e, this.epsilon = s, this.accBeta1 = a, this.accBeta2 = e, s === null && (this.epsilon = h().backend.epsilon());
|
|
24
|
+
}
|
|
25
|
+
/** @nocollapse */
|
|
26
|
+
static get className() {
|
|
27
|
+
return "Adam";
|
|
28
|
+
}
|
|
29
|
+
accBeta1 = 0;
|
|
30
|
+
accBeta2 = 0;
|
|
31
|
+
accumulatedMoments = [];
|
|
32
|
+
applyGradients(t) {
|
|
33
|
+
const a = Array.isArray(t) ? t.map((e) => e.name) : Object.keys(t);
|
|
34
|
+
o(() => {
|
|
35
|
+
const e = 1 - this.accBeta1, s = 1 - this.accBeta2;
|
|
36
|
+
a.forEach((n, i) => {
|
|
37
|
+
const c = h().registeredVariables[n], u = !1;
|
|
38
|
+
this.accumulatedMoments[i] == null && (this.accumulatedMoments[i] = {
|
|
39
|
+
originalName: `${n}/m`,
|
|
40
|
+
variable: o(() => M([...c.shape, 2]).variable(u))
|
|
41
|
+
});
|
|
42
|
+
const r = Array.isArray(t) ? t[i].tensor : t[n];
|
|
43
|
+
if (r == null)
|
|
44
|
+
return;
|
|
45
|
+
const m = this.accumulatedMoments[i].variable, l = d(m, r, this.beta1, this.beta2);
|
|
46
|
+
m.assign(l);
|
|
47
|
+
const p = b(
|
|
48
|
+
l,
|
|
49
|
+
c,
|
|
50
|
+
e,
|
|
51
|
+
s,
|
|
52
|
+
this.epsilon ?? 1e-8,
|
|
53
|
+
this.learningRate
|
|
54
|
+
);
|
|
55
|
+
c.assign(p);
|
|
56
|
+
}), this.accBeta1 = this.accBeta1 * this.beta1, this.accBeta2 = this.accBeta2 * this.beta2;
|
|
57
|
+
}), this.incrementIterations();
|
|
58
|
+
}
|
|
59
|
+
dispose() {
|
|
60
|
+
this.accumulatedMoments != null && B(this.accumulatedMoments.map((t) => t.variable));
|
|
61
|
+
}
|
|
62
|
+
async getWeights() {
|
|
63
|
+
const t = [...this.accumulatedMoments];
|
|
64
|
+
return [await this.saveIterations()].concat(
|
|
65
|
+
t.map((a) => ({ name: a.originalName, tensor: a.variable }))
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
async setWeights(t) {
|
|
69
|
+
t = await this.extractIterations(t), o(() => {
|
|
70
|
+
this.accBeta1 = Math.pow(this.beta1, this.iterations_ + 1), this.accBeta2 = Math.pow(this.beta2, this.iterations_ + 1);
|
|
71
|
+
});
|
|
72
|
+
const a = t.length / 2, e = !1;
|
|
73
|
+
this.accumulatedMoments = t.slice(0, a).map((s) => ({
|
|
74
|
+
originalName: s.name,
|
|
75
|
+
variable: s.tensor.variable(e)
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
getConfig() {
|
|
79
|
+
return {
|
|
80
|
+
learningRate: this.learningRate,
|
|
81
|
+
beta1: this.beta1,
|
|
82
|
+
beta2: this.beta2,
|
|
83
|
+
epsilon: this.epsilon
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/** @nocollapse */
|
|
87
|
+
static fromConfig(t, a) {
|
|
88
|
+
return new t(a.learningRate, a.beta1, a.beta2, a.epsilon);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
export {
|
|
92
|
+
R as AdamOptimizer
|
|
93
|
+
};
|
package/dist/training/AdamExt.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { a as r, b as c, c as h, e as o } from "../index-UdZhlibC.js";
|
|
2
|
+
import { AdamOptimizer as g } from "./Adam.js";
|
|
3
|
+
class y extends g {
|
|
4
|
+
constructor(t, e, s, i, a) {
|
|
5
|
+
super(t, e, s, i), this.config = a, this.startLearningRate = t;
|
|
5
6
|
}
|
|
6
7
|
step = 0;
|
|
7
8
|
startLearningRate;
|
|
@@ -23,21 +24,21 @@ class u extends r {
|
|
|
23
24
|
}
|
|
24
25
|
decayVariable(t, e, s) {
|
|
25
26
|
if (t && t.shape.length >= 2) {
|
|
26
|
-
const
|
|
27
|
-
t.assign(
|
|
27
|
+
const i = r(t, c(s * e));
|
|
28
|
+
t.assign(h(t, i)), i.dispose();
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
31
|
applyWeightDecay(t) {
|
|
31
|
-
const e = this.config.weightDecay, s = this.learningRate,
|
|
32
|
-
Array.isArray(t) ? t.forEach(({ name:
|
|
33
|
-
const n = a
|
|
32
|
+
const e = this.config.weightDecay, s = this.learningRate, i = o().registeredVariables;
|
|
33
|
+
Array.isArray(t) ? t.forEach(({ name: a }) => {
|
|
34
|
+
const n = i[a];
|
|
34
35
|
this.decayVariable(n, e, s);
|
|
35
|
-
}) : Object.keys(t).forEach((
|
|
36
|
-
const n = a
|
|
36
|
+
}) : Object.keys(t).forEach((a) => {
|
|
37
|
+
const n = i[a];
|
|
37
38
|
this.decayVariable(n, e, s);
|
|
38
39
|
});
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
42
|
export {
|
|
42
|
-
|
|
43
|
+
y as default
|
|
43
44
|
};
|
|
@@ -11,7 +11,7 @@ class p {
|
|
|
11
11
|
if (e.done) break;
|
|
12
12
|
const c = e.value, { xs: r, ys: i } = c, [u, l] = this.model.forward({ training: !1 }, r, i);
|
|
13
13
|
u.dispose(), r.dispose(), i.dispose();
|
|
14
|
-
const d = l.
|
|
14
|
+
const d = await l.array();
|
|
15
15
|
l.dispose(), t += d, o++;
|
|
16
16
|
}
|
|
17
17
|
return t / o;
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import { generateText as
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import { d as h } from "../index-
|
|
5
|
-
import
|
|
6
|
-
const
|
|
1
|
+
import { generateText as w } from "../utilities/generate.js";
|
|
2
|
+
import T from "./Trainer.js";
|
|
3
|
+
import L from "./Evaluator.js";
|
|
4
|
+
import { d as h } from "../index-UdZhlibC.js";
|
|
5
|
+
import x from "../utilities/profile.js";
|
|
6
|
+
const y = {
|
|
7
7
|
desiredLoss: 0.01,
|
|
8
8
|
logInterval: 1,
|
|
9
9
|
maxSteps: 1e3
|
|
10
10
|
};
|
|
11
|
-
class
|
|
12
|
-
constructor(i, e,
|
|
13
|
-
super(i, e,
|
|
11
|
+
class E extends T {
|
|
12
|
+
constructor(i, e, r = 3e-4) {
|
|
13
|
+
super(i, e, r);
|
|
14
14
|
}
|
|
15
15
|
// Train for multiple epochs using Dataset API - FIXED memory leaks
|
|
16
|
-
async trainOnDataset(i, e,
|
|
17
|
-
const {
|
|
18
|
-
...
|
|
16
|
+
async trainOnDataset(i, e, r) {
|
|
17
|
+
const { logInterval: g, onStep: l, prompt: c, maxSteps: u } = {
|
|
18
|
+
...y,
|
|
19
19
|
...e
|
|
20
20
|
}, n = Date.now(), t = {
|
|
21
21
|
step: 0,
|
|
@@ -27,22 +27,22 @@ class I extends L {
|
|
|
27
27
|
trainingDuration: 0,
|
|
28
28
|
...this.lastState || {}
|
|
29
29
|
};
|
|
30
|
-
this.lastState = t, this.dummyPass(), this.model.trainable = !0, e?.advancedMetrics && (this.model.getProfiler() || (this.model.config.layerConfig.profiler = new
|
|
31
|
-
const m =
|
|
30
|
+
this.lastState = t, await this.dummyPass(), this.model.trainable = !0, e?.advancedMetrics && (this.model.getProfiler() || (this.model.config.layerConfig.profiler = new x())), this.running = !0, t.logStartTime = n;
|
|
31
|
+
const m = r ? new L(this.model, r) : void 0, f = await i.iterator();
|
|
32
32
|
try {
|
|
33
|
-
for (; this.running
|
|
34
|
-
const
|
|
35
|
-
if (
|
|
36
|
-
const d =
|
|
33
|
+
for (; this.running; ) {
|
|
34
|
+
const o = await f.next();
|
|
35
|
+
if (o.done) break;
|
|
36
|
+
const d = o.value, p = this.trainBatch(t, d), s = {
|
|
37
37
|
loss: t.lastLoss,
|
|
38
38
|
step: t.step,
|
|
39
39
|
time: Date.now() - n,
|
|
40
40
|
batchSize: d.xs.shape[0],
|
|
41
|
-
learningRate: e?.advancedMetrics ? this.optimizer.lr : void 0
|
|
42
|
-
gradientNorm:
|
|
41
|
+
learningRate: e?.advancedMetrics ? this.optimizer.lr : void 0
|
|
42
|
+
//gradientNorm: options?.advancedMetrics ? await state.gradientNorm : undefined,
|
|
43
43
|
};
|
|
44
44
|
if (this.model.log.push(s), t.step % g === 0) {
|
|
45
|
-
await
|
|
45
|
+
await p.data();
|
|
46
46
|
const S = Date.now();
|
|
47
47
|
if (t.trainingDuration += S - t.logStartTime, m)
|
|
48
48
|
try {
|
|
@@ -53,10 +53,10 @@ class I extends L {
|
|
|
53
53
|
}
|
|
54
54
|
if (l) {
|
|
55
55
|
if (c) {
|
|
56
|
-
const
|
|
56
|
+
const v = await w(this.tokenizer, this.model, c, 100, {
|
|
57
57
|
temperature: 0.8
|
|
58
58
|
});
|
|
59
|
-
s.example =
|
|
59
|
+
s.example = v;
|
|
60
60
|
}
|
|
61
61
|
const a = {
|
|
62
62
|
duration: t.trainingDuration,
|
|
@@ -68,14 +68,14 @@ class I extends L {
|
|
|
68
68
|
}
|
|
69
69
|
t.logStartTime = Date.now();
|
|
70
70
|
}
|
|
71
|
-
t.step >= u && this.stop();
|
|
71
|
+
p.dispose(), t.step >= u && this.stop();
|
|
72
72
|
}
|
|
73
|
-
} catch (
|
|
74
|
-
throw console.error("Training error:",
|
|
73
|
+
} catch (o) {
|
|
74
|
+
throw console.error("Training error:", o), h(), o;
|
|
75
75
|
}
|
|
76
76
|
return h(), this.running = !1, { losses: t.losses, validationLosses: t.validationLosses };
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
export {
|
|
80
|
-
|
|
80
|
+
E as default
|
|
81
81
|
};
|
|
@@ -11,7 +11,7 @@ export interface TrainingState {
|
|
|
11
11
|
totalSteps: number;
|
|
12
12
|
losses: number[];
|
|
13
13
|
validationLosses: number[];
|
|
14
|
-
gradientNorm?: number
|
|
14
|
+
gradientNorm?: Promise<number>;
|
|
15
15
|
}
|
|
16
16
|
export interface TrainingProgress {
|
|
17
17
|
duration: number;
|
|
@@ -47,16 +47,15 @@ export default abstract class GPTTrainer {
|
|
|
47
47
|
stop(): void;
|
|
48
48
|
getOptimizer(): AdamExt;
|
|
49
49
|
resetOptimizer(config?: AdamConfig): void;
|
|
50
|
-
|
|
51
|
-
protected trainStep(state: Partial<TrainingState>, batch: {
|
|
50
|
+
protected trainStep(_state: Partial<TrainingState>, batch: {
|
|
52
51
|
xs: Tensor;
|
|
53
52
|
ys: Tensor;
|
|
54
|
-
}, dummy?: boolean
|
|
55
|
-
protected dummyPass(): void
|
|
53
|
+
}, dummy?: boolean): Scalar;
|
|
54
|
+
protected dummyPass(): Promise<void>;
|
|
56
55
|
protected trainBatch(state: TrainingState, batch: {
|
|
57
56
|
xs: Tensor;
|
|
58
57
|
ys: Tensor;
|
|
59
|
-
}
|
|
58
|
+
}): Scalar;
|
|
60
59
|
abstract trainOnDataset(dataset: Dataset<{
|
|
61
60
|
xs: Tensor;
|
|
62
61
|
ys: Tensor;
|
package/dist/training/Trainer.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { DatasetBuilder as
|
|
2
|
-
import
|
|
3
|
-
import { t as
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
this.tokenizer = e, this.model = t, this.learningRate = s, this.resetOptimizer(), this.datasetBuilder = new g(e, t.config.gpt.blockSize);
|
|
1
|
+
import { DatasetBuilder as h, flattenTokens as p, PAGE_FACTOR as g } from "./DatasetBuilder.js";
|
|
2
|
+
import u from "./AdamExt.js";
|
|
3
|
+
import { t as f, v as y, d as c } from "../index-UdZhlibC.js";
|
|
4
|
+
import { z as m } from "../zeros-DnPT2nD4.js";
|
|
5
|
+
class x {
|
|
6
|
+
constructor(t, e, a = 1e-3) {
|
|
7
|
+
this.tokenizer = e, this.model = t, this.learningRate = a, this.resetOptimizer(), this.datasetBuilder = new h(e, t.config.gpt.blockSize);
|
|
9
8
|
}
|
|
10
9
|
model;
|
|
11
10
|
optimizer;
|
|
@@ -27,7 +26,7 @@ class R {
|
|
|
27
26
|
}
|
|
28
27
|
resetOptimizer(t = { learningRateFactor: 1, beta1: 0.9, beta2: 0.99, epsilon: 1e-8 }) {
|
|
29
28
|
this.optimizer && this.optimizer.dispose();
|
|
30
|
-
const e = new
|
|
29
|
+
const e = new u(
|
|
31
30
|
t.learningRateFactor * this.learningRate,
|
|
32
31
|
t.beta1,
|
|
33
32
|
t.beta2,
|
|
@@ -41,76 +40,76 @@ class R {
|
|
|
41
40
|
);
|
|
42
41
|
this.optimizer = e;
|
|
43
42
|
}
|
|
44
|
-
maxGradNorm(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
43
|
+
/*private async maxGradNorm(grads: NamedVariableMap): Promise<number> {
|
|
44
|
+
let maxNorm = 0;
|
|
45
|
+
// Print all gradients
|
|
46
|
+
await Promise.all(
|
|
47
|
+
Object.keys(grads).map(async (varName) => {
|
|
48
|
+
const grad = grads[varName];
|
|
49
|
+
const temp = norm(grad);
|
|
50
|
+
const gradNorm = (await temp.data())[0];
|
|
51
|
+
temp.dispose();
|
|
52
|
+
if (gradNorm > maxNorm) {
|
|
53
|
+
maxNorm = gradNorm;
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
);
|
|
57
|
+
return maxNorm;
|
|
58
|
+
}*/
|
|
59
|
+
trainStep(t, e, a = !1) {
|
|
60
|
+
return f(() => {
|
|
53
61
|
this.model.getProfiler()?.startMemory();
|
|
54
|
-
const { xs:
|
|
55
|
-
const [
|
|
56
|
-
return
|
|
57
|
-
}, { value:
|
|
58
|
-
|
|
59
|
-
this.model.getProfiler()?.endMemory("Training");
|
|
60
|
-
else {
|
|
61
|
-
if (a) {
|
|
62
|
-
const n = this.maxGradNorm(o);
|
|
63
|
-
t.gradientNorm = n;
|
|
64
|
-
}
|
|
65
|
-
this.optimizer.applyGradients(o), this.model.getProfiler()?.endMemory("Training"), c(o);
|
|
66
|
-
}
|
|
67
|
-
return l;
|
|
62
|
+
const { xs: s, ys: i } = e, o = () => {
|
|
63
|
+
const [l, d] = this.model.forward({ training: !0 }, s, i);
|
|
64
|
+
return l.dispose(), d;
|
|
65
|
+
}, { value: n, grads: r } = y(o);
|
|
66
|
+
return a ? this.model.getProfiler()?.endMemory("Training") : (this.optimizer.applyGradients(r), this.model.getProfiler()?.endMemory("Training"), c(r)), n;
|
|
68
67
|
});
|
|
69
68
|
}
|
|
70
|
-
dummyPass() {
|
|
71
|
-
const t =
|
|
69
|
+
async dummyPass() {
|
|
70
|
+
const t = m([1, this.model.config.gpt.blockSize], "int32"), e = m([1, this.model.config.gpt.blockSize], "int32");
|
|
72
71
|
try {
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
} catch (
|
|
76
|
-
console.error("Error during dummy pass:",
|
|
72
|
+
const a = this.trainStep({}, { xs: t, ys: e }, !0);
|
|
73
|
+
await a.data(), a.dispose();
|
|
74
|
+
} catch (a) {
|
|
75
|
+
console.error("Error during dummy pass:", a);
|
|
77
76
|
} finally {
|
|
78
77
|
t.dispose(), e.dispose();
|
|
79
78
|
}
|
|
80
79
|
}
|
|
81
|
-
|
|
80
|
+
trainBatch(t, e) {
|
|
82
81
|
try {
|
|
83
|
-
const a = this.trainStep(t, e, !1
|
|
84
|
-
return e.xs.dispose(), e.ys.dispose(), t.step++, t.totalSteps++, a
|
|
82
|
+
const a = this.trainStep(t, e, !1);
|
|
83
|
+
return e.xs.dispose(), e.ys.dispose(), t.step++, t.totalSteps++, a;
|
|
85
84
|
} catch (a) {
|
|
86
85
|
throw console.error(`Error processing batch at step ${t.step}:`, a), c(), a;
|
|
87
86
|
}
|
|
88
87
|
}
|
|
89
|
-
async createTrainValidationSplit(t, e = 32,
|
|
90
|
-
const
|
|
91
|
-
if (
|
|
92
|
-
const
|
|
93
|
-
for (;
|
|
94
|
-
const
|
|
95
|
-
|
|
88
|
+
async createTrainValidationSplit(t, e = 32, a = 0.1) {
|
|
89
|
+
const s = await p(t, this.tokenizer), i = /* @__PURE__ */ new Set();
|
|
90
|
+
if (a > 0) {
|
|
91
|
+
const r = Math.floor(s.length / (this.datasetBuilder.blockSize * g)), l = Math.max(1, Math.floor(r * a));
|
|
92
|
+
for (; i.size < l; ) {
|
|
93
|
+
const d = Math.floor(Math.random() * r);
|
|
94
|
+
i.add(d);
|
|
96
95
|
}
|
|
97
96
|
}
|
|
98
|
-
const
|
|
99
|
-
|
|
97
|
+
const o = await this.datasetBuilder.createTextDataset(s, e, i, !1), n = await this.datasetBuilder.createTextDataset(
|
|
98
|
+
s,
|
|
100
99
|
e,
|
|
101
|
-
|
|
100
|
+
i,
|
|
102
101
|
!0
|
|
103
102
|
);
|
|
104
|
-
return { trainDataset:
|
|
103
|
+
return { trainDataset: o, validationDataset: n };
|
|
105
104
|
}
|
|
106
105
|
async createDataset(t, e = 32) {
|
|
107
|
-
const
|
|
108
|
-
return await this.datasetBuilder.createTextDataset(
|
|
106
|
+
const a = await p(t, this.tokenizer);
|
|
107
|
+
return await this.datasetBuilder.createTextDataset(a, e);
|
|
109
108
|
}
|
|
110
109
|
dispose() {
|
|
111
110
|
this.optimizer && this.optimizer.dispose();
|
|
112
111
|
}
|
|
113
112
|
}
|
|
114
113
|
export {
|
|
115
|
-
|
|
114
|
+
x as default
|
|
116
115
|
};
|
|
@@ -4,8 +4,4 @@ import * as tf from '@tensorflow/tfjs-core';
|
|
|
4
4
|
* This version handles potential numerical issues better
|
|
5
5
|
*/
|
|
6
6
|
export declare function sparseSoftmaxCrossEntropy(logits: tf.Tensor, labels: tf.Tensor): tf.Tensor;
|
|
7
|
-
/**
|
|
8
|
-
* Custom gradient implementation for sparse cross-entropy
|
|
9
|
-
* This ensures proper backpropagation
|
|
10
|
-
*/
|
|
11
7
|
export declare function createSoftmaxCrossEntropyWithGrad(): (...args: tf.Tensor[]) => tf.Tensor<tf.Rank>;
|