@danielsimonjr/mathts-autograd 0.3.21 → 0.4.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/index.js +24 -1
- package/dist/tape.d.ts +22 -0
- package/dist/tape.d.ts.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -221,6 +221,11 @@ function forwardGrad(fn, x) {
|
|
|
221
221
|
}
|
|
222
222
|
//#endregion
|
|
223
223
|
//#region src/tape.ts
|
|
224
|
+
function assertSameTape(a, b) {
|
|
225
|
+
if (a.tape !== b.tape) throw new Error("TapedTensor: operands belong to different Tapes");
|
|
226
|
+
if (a.epoch !== void 0) a.tape.assertCurrentEpoch(a.epoch, "TapedTensor");
|
|
227
|
+
if (b.epoch !== void 0) b.tape.assertCurrentEpoch(b.epoch, "TapedTensor");
|
|
228
|
+
}
|
|
224
229
|
var Tape = class {
|
|
225
230
|
nodes = [];
|
|
226
231
|
inputGradSlots = /* @__PURE__ */ new Map();
|
|
@@ -253,7 +258,19 @@ var Tape = class {
|
|
|
253
258
|
const slot = this.inputGradSlots.get(outputId);
|
|
254
259
|
if (!slot) throw new Error(`Tape.backward: unknown outputId ${outputId}`);
|
|
255
260
|
slot.set(outputGrad);
|
|
256
|
-
|
|
261
|
+
const start = outputId >= 0 ? outputId : this.nodes.length - 1;
|
|
262
|
+
for (let n = start; n >= 0; n--) this.nodes[n].backward(this.nodes[n].outputGradSlot);
|
|
263
|
+
}
|
|
264
|
+
reset() {
|
|
265
|
+
this.nodes = [];
|
|
266
|
+
this.inputGradSlots.clear();
|
|
267
|
+
this.nextOpId = 0;
|
|
268
|
+
this.nextInputId = -1;
|
|
269
|
+
this.epoch++;
|
|
270
|
+
}
|
|
271
|
+
epoch = 0;
|
|
272
|
+
assertCurrentEpoch(tensorEpoch, what) {
|
|
273
|
+
if (tensorEpoch !== this.epoch) throw new Error(`${what}: this TapedTensor was created before Tape.reset() and is no longer valid`);
|
|
257
274
|
}
|
|
258
275
|
getInputGrad(id) {
|
|
259
276
|
return this.inputGradSlots.get(id);
|
|
@@ -265,11 +282,13 @@ var TapedTensor = class TapedTensor {
|
|
|
265
282
|
tape;
|
|
266
283
|
id;
|
|
267
284
|
axisLabels;
|
|
285
|
+
epoch;
|
|
268
286
|
constructor(shape, primal, tape, id, axisLabels) {
|
|
269
287
|
this.shape = shape;
|
|
270
288
|
this.primal = primal;
|
|
271
289
|
this.tape = tape;
|
|
272
290
|
this.id = id;
|
|
291
|
+
this.epoch = tape.epoch;
|
|
273
292
|
if (axisLabels !== void 0) this.axisLabels = [...axisLabels];
|
|
274
293
|
}
|
|
275
294
|
get data() {
|
|
@@ -360,6 +379,7 @@ var TapedTensor = class TapedTensor {
|
|
|
360
379
|
return new TapedTensor(this.shape, out, this.tape, id);
|
|
361
380
|
}
|
|
362
381
|
contract(other) {
|
|
382
|
+
assertSameTape(this, other);
|
|
363
383
|
const aPrimal = this.toPrimalTensorWith(this.axisLabels);
|
|
364
384
|
const bPrimal = other.toPrimalTensorWith(other.axisLabels);
|
|
365
385
|
const yPrimal = aPrimal.contract(bPrimal);
|
|
@@ -402,6 +422,7 @@ var TapedTensor = class TapedTensor {
|
|
|
402
422
|
return new TapedTensor([...yPrimal.shape], new Float64Array(yPrimal.data), this.tape, id, yLabels);
|
|
403
423
|
}
|
|
404
424
|
matmul(other) {
|
|
425
|
+
assertSameTape(this, other);
|
|
405
426
|
const rankA = this.shape.length;
|
|
406
427
|
const rankB = other.shape.length;
|
|
407
428
|
if (rankA < 2) throw new Error(`TapedTensor.matmul: 'this' operand must have rank ≥ 2, got rank ${rankA}`);
|
|
@@ -436,6 +457,7 @@ var TapedTensor = class TapedTensor {
|
|
|
436
457
|
return new TapedTensor(yShape, new Float64Array(yData), this.tape, id);
|
|
437
458
|
}
|
|
438
459
|
checkSameShape(other, op) {
|
|
460
|
+
assertSameTape(this, other);
|
|
439
461
|
assertSameShape(this.shape, other.shape, "TapedTensor", op);
|
|
440
462
|
}
|
|
441
463
|
sum(axis, opts) {
|
|
@@ -838,6 +860,7 @@ var TapedTensor = class TapedTensor {
|
|
|
838
860
|
return new TapedTensor(this.shape, out, this.tape, id);
|
|
839
861
|
}
|
|
840
862
|
tensordot(other, axes) {
|
|
863
|
+
assertSameTape(this, other);
|
|
841
864
|
const aShape = [...this.shape];
|
|
842
865
|
const bShape = [...other.shape];
|
|
843
866
|
const aRank = aShape.length;
|
package/dist/tape.d.ts
CHANGED
|
@@ -12,6 +12,17 @@
|
|
|
12
12
|
*/
|
|
13
13
|
import { Tensor, Index } from '@danielsimonjr/mathts-tensor';
|
|
14
14
|
type BackwardFn = (outputGrad: Float64Array) => void;
|
|
15
|
+
/**
|
|
16
|
+
* Throw when two TapedTensors come from different Tapes: their ids index
|
|
17
|
+
* different gradient-slot tables, so combining them would corrupt gradients.
|
|
18
|
+
*/
|
|
19
|
+
export declare function assertSameTape(a: {
|
|
20
|
+
readonly tape: Tape;
|
|
21
|
+
readonly epoch?: number;
|
|
22
|
+
}, b: {
|
|
23
|
+
readonly tape: Tape;
|
|
24
|
+
readonly epoch?: number;
|
|
25
|
+
}): void;
|
|
15
26
|
/**
|
|
16
27
|
* Records the operations of a forward pass so that `backward` can replay them in reverse.
|
|
17
28
|
*
|
|
@@ -37,6 +48,15 @@ export declare class Tape {
|
|
|
37
48
|
};
|
|
38
49
|
/** Seed the final output's gradient slot, then replay in reverse. */
|
|
39
50
|
backward(outputId: number, outputGrad: Float64Array): void;
|
|
51
|
+
/**
|
|
52
|
+
* Drop every recorded node, closure and gradient slot and restart id allocation.
|
|
53
|
+
* TapedTensors created before the reset are invalid afterwards: do not use them.
|
|
54
|
+
*/
|
|
55
|
+
reset(): void;
|
|
56
|
+
/** Incremented by {@link reset}; a TapedTensor records the epoch it was created in. */
|
|
57
|
+
epoch: number;
|
|
58
|
+
/** Throw when `tensorEpoch` predates the last {@link reset} (its id may now name another slot). */
|
|
59
|
+
assertCurrentEpoch(tensorEpoch: number, what: string): void;
|
|
40
60
|
getInputGrad(id: number): Float64Array | undefined;
|
|
41
61
|
}
|
|
42
62
|
/**
|
|
@@ -56,6 +76,8 @@ export declare class TapedTensor {
|
|
|
56
76
|
* Must have the same length as `shape` when present.
|
|
57
77
|
*/
|
|
58
78
|
readonly axisLabels?: ReadonlyArray<Index>;
|
|
79
|
+
/** The tape epoch this tensor was recorded in; stale after {@link Tape.reset}. */
|
|
80
|
+
readonly epoch: number;
|
|
59
81
|
constructor(shape: ReadonlyArray<number>, primal: Float64Array, tape: Tape, id: number, axisLabels?: ReadonlyArray<Index>);
|
|
60
82
|
/**
|
|
61
83
|
* S5 fix: existing engine ops (e.g. lower, pderiv, contract) reach into
|
package/dist/tape.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tape.d.ts","sourceRoot":"","sources":["../src/tape.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,MAAM,EAAE,KAAK,EAAwB,MAAM,8BAA8B,CAAC;AAGnF,KAAK,UAAU,GAAG,CAAC,UAAU,EAAE,YAAY,KAAK,IAAI,CAAC;AAQrD;;;;;;;;GAQG;AACH,qBAAa,IAAI;IACf,OAAO,CAAC,KAAK,CAAkB;IAC/B,OAAO,CAAC,cAAc,CAAmC;IAKzD,OAAO,CAAC,QAAQ,CAAK;IACrB,OAAO,CAAC,WAAW,CAAM;IAEzB,wDAAwD;IACxD,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,YAAY,CAAA;KAAE,CAK7D;IAED,MAAM,CACJ,QAAQ,EAAE,aAAa,CAAC,MAAM,CAAC,EAC/B,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,UAAU,GACnB;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,YAAY,CAAA;KAAE,CAMxC;IAED,qEAAqE;IACrE,QAAQ,CAAC,QAAQ,EAA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;;;;;;;;;OAUG;IACH,KAAK,CAAC,KAAK,EAAE,WAAW,GAAG,WAAW,CAkBrC;IAMD;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4BG;IACH,SAAS,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,aAAa,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,WAAW,CA6KzF;IAMD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA0CG;IACH,GAAG,IAAI;QAAE,CAAC,EAAE,WAAW,CAAC;QAAC,CAAC,EAAE,WAAW,CAAC;QAAC,CAAC,EAAE,WAAW,CAAA;KAAE,CAkGxD;IAMD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAgCG;IACH,GAAG,CAAC,IAAI,EAAE;QAAE,SAAS,EAAE,OAAO,CAAA;KAAE,GAAG;QAAE,OAAO,EAAE,WAAW,CAAC;QAAC,OAAO,EAAE,WAAW,CAAA;KAAE,CAgBhF;IAED,OAAO,CAAC,aAAa;IAkCrB,OAAO,CAAC,WAAW;CA+DpB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@danielsimonjr/mathts-autograd",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Forward + reverse-mode automatic differentiation for MathTS rank-N Tensor",
|
|
5
5
|
"author": "Daniel Simon Jr.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -42,8 +42,8 @@
|
|
|
42
42
|
}
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@danielsimonjr/mathts-core": "^0.
|
|
46
|
-
"@danielsimonjr/mathts-tensor": "^0.
|
|
45
|
+
"@danielsimonjr/mathts-core": "^0.22.0",
|
|
46
|
+
"@danielsimonjr/mathts-tensor": "^0.3.1"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@types/node": "^26.4.0",
|