catniff 0.8.5 → 0.8.6

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.
Files changed (3) hide show
  1. package/dist/nn.d.ts +15 -0
  2. package/dist/nn.js +79 -1
  3. package/package.json +1 -1
package/dist/nn.d.ts CHANGED
@@ -50,6 +50,20 @@ export declare class LSTMCell {
50
50
  constructor(inputSize: number, hiddenSize: number, bias?: boolean, device?: string, dtype?: dtype);
51
51
  forward(input: Tensor | TensorValue, hidden: Tensor | TensorValue, cell: Tensor | TensorValue): [Tensor, Tensor];
52
52
  }
53
+ export declare class BatchNorm {
54
+ weight?: Tensor;
55
+ bias?: Tensor;
56
+ runningMean?: Tensor;
57
+ runningVar?: Tensor;
58
+ eps: number;
59
+ momentum: number;
60
+ numFeatures: number;
61
+ affine: boolean;
62
+ trackRunningStats: boolean;
63
+ numBatchesTracked: number;
64
+ constructor(numFeatures: number, eps?: number, momentum?: number, affine?: boolean, trackRunningStats?: boolean, device?: string, dtype?: dtype);
65
+ forward(input: Tensor): Tensor;
66
+ }
53
67
  export declare class LayerNorm {
54
68
  weight?: Tensor;
55
69
  bias?: Tensor;
@@ -91,6 +105,7 @@ export declare const nn: {
91
105
  RNNCell: typeof RNNCell;
92
106
  GRUCell: typeof GRUCell;
93
107
  LSTMCell: typeof LSTMCell;
108
+ BatchNorm: typeof BatchNorm;
94
109
  LayerNorm: typeof LayerNorm;
95
110
  RMSNorm: typeof RMSNorm;
96
111
  Embedding: typeof Embedding;
package/dist/nn.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.nn = exports.MultiheadAttention = exports.Embedding = exports.RMSNorm = exports.LayerNorm = exports.LSTMCell = exports.GRUCell = exports.RNNCell = exports.Linear = void 0;
3
+ exports.nn = exports.MultiheadAttention = exports.Embedding = exports.RMSNorm = exports.LayerNorm = exports.BatchNorm = exports.LSTMCell = exports.GRUCell = exports.RNNCell = exports.Linear = void 0;
4
4
  exports.scaledDotProductAttention = scaledDotProductAttention;
5
5
  const core_1 = require("./core");
6
6
  function linearTransform(input, weight, bias) {
@@ -149,6 +149,83 @@ class LSTMCell {
149
149
  }
150
150
  }
151
151
  exports.LSTMCell = LSTMCell;
152
+ class BatchNorm {
153
+ weight;
154
+ bias;
155
+ runningMean;
156
+ runningVar;
157
+ eps;
158
+ momentum;
159
+ numFeatures;
160
+ affine;
161
+ trackRunningStats;
162
+ numBatchesTracked;
163
+ constructor(numFeatures, eps = 1e-5, momentum = 0.1, affine = true, trackRunningStats = true, device, dtype) {
164
+ this.numFeatures = numFeatures;
165
+ this.eps = eps;
166
+ this.momentum = momentum;
167
+ this.affine = affine;
168
+ this.trackRunningStats = trackRunningStats;
169
+ this.numBatchesTracked = 0;
170
+ if (this.affine) {
171
+ this.weight = core_1.Tensor.ones([numFeatures], { requiresGrad: true, device, dtype });
172
+ this.bias = core_1.Tensor.zeros([numFeatures], { requiresGrad: true, device, dtype });
173
+ }
174
+ if (this.trackRunningStats) {
175
+ this.runningMean = core_1.Tensor.zeros([numFeatures], { requiresGrad: false, device, dtype });
176
+ this.runningVar = core_1.Tensor.ones([numFeatures], { requiresGrad: false, device, dtype });
177
+ }
178
+ }
179
+ forward(input) {
180
+ // Input shape: (N, C, ...) where C = numFeatures
181
+ // Normalize over batch dimension and spatial dimensions (if any)
182
+ if (input.shape.length < 2) {
183
+ throw new Error("Input must have at least 2 dimensions (batch, features)");
184
+ }
185
+ if (input.shape[1] !== this.numFeatures) {
186
+ throw new Error(`Expected ${this.numFeatures} features, got ${input.shape[1]}`);
187
+ }
188
+ let mean;
189
+ let variance;
190
+ if (core_1.Tensor.training || !this.trackRunningStats) {
191
+ // Training or trackRunningStats disabled - calculate mean and variance from scratch
192
+ // Calculate mean and variance over batch and spatial dimensions
193
+ // Keep only the channel dimension
194
+ const dims = [0, ...Array.from({ length: input.shape.length - 2 }, (_, i) => i + 2)];
195
+ mean = input.mean(dims, true);
196
+ variance = input.sub(mean).pow(2).mean(dims, true);
197
+ // Update running statistics if enabled and in training mode
198
+ if (this.trackRunningStats && core_1.Tensor.training) {
199
+ const exponentialAverageFactor = this.momentum;
200
+ this.runningMean = this.runningMean
201
+ .mul(1 - exponentialAverageFactor)
202
+ .add(mean.squeeze().mul(exponentialAverageFactor));
203
+ // Use unbiased variance for running estimate
204
+ const n = input.shape.reduce((acc, val, idx) => idx === 1 ? acc : acc * val, 1);
205
+ const unbiasingFactor = n / (n - 1);
206
+ this.runningVar = this.runningVar
207
+ .mul(1 - exponentialAverageFactor)
208
+ .add(variance.squeeze().mul(exponentialAverageFactor * unbiasingFactor));
209
+ this.numBatchesTracked++;
210
+ }
211
+ }
212
+ else {
213
+ // Inference with trackRunningStats enabled - use running statistics
214
+ mean = this.runningMean.reshape([1, this.numFeatures, ...Array(input.shape.length - 2).fill(1)]);
215
+ variance = this.runningVar.reshape([1, this.numFeatures, ...Array(input.shape.length - 2).fill(1)]);
216
+ }
217
+ // Normalize
218
+ let normalized = input.sub(mean).div(variance.add(this.eps).sqrt());
219
+ // Apply affine transformation
220
+ if (this.affine) {
221
+ const weightReshaped = this.weight.reshape([1, this.numFeatures, ...Array(input.shape.length - 2).fill(1)]);
222
+ const biasReshaped = this.bias.reshape([1, this.numFeatures, ...Array(input.shape.length - 2).fill(1)]);
223
+ normalized = normalized.mul(weightReshaped).add(biasReshaped);
224
+ }
225
+ return normalized;
226
+ }
227
+ }
228
+ exports.BatchNorm = BatchNorm;
152
229
  class LayerNorm {
153
230
  weight;
154
231
  bias;
@@ -383,6 +460,7 @@ exports.nn = {
383
460
  RNNCell,
384
461
  GRUCell,
385
462
  LSTMCell,
463
+ BatchNorm,
386
464
  LayerNorm,
387
465
  RMSNorm,
388
466
  Embedding,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "catniff",
3
- "version": "0.8.5",
3
+ "version": "0.8.6",
4
4
  "description": "Torch-like deep learning framework for Javascript",
5
5
  "main": "index.js",
6
6
  "scripts": {