catniff 0.2.8 → 0.2.9

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/core.d.ts CHANGED
@@ -26,7 +26,7 @@ export declare class Tensor {
26
26
  readonly number[]
27
27
  ];
28
28
  static broadcastShapes(shapeA: readonly number[], shapeB: readonly number[]): readonly number[];
29
- static indexToCoords(index: number, shape: readonly number[], strides: readonly number[]): number[];
29
+ static indexToCoords(index: number, strides: readonly number[]): number[];
30
30
  static coordsToUnbroadcastedIndex(coords: number[], shape: readonly number[], strides: readonly number[]): number;
31
31
  static coordsToIndex(coords: number[], strides: readonly number[]): number;
32
32
  static shapeToSize(shape: readonly number[]): number;
package/dist/core.js CHANGED
@@ -94,12 +94,10 @@ class Tensor {
94
94
  return newShape;
95
95
  }
96
96
  // Utility to convert flat index to array of coordinates
97
- static indexToCoords(index, shape, strides) {
98
- const coords = new Array(shape.length);
97
+ static indexToCoords(index, strides) {
98
+ const coords = new Array(strides.length);
99
99
  let remaining = index;
100
- // Sort dimensions by stride (largest first) for correct decomposition
101
- const sortedDims = shape.map((_, i) => i).sort((a, b) => strides[b] - strides[a]);
102
- for (const dim of sortedDims) {
100
+ for (let dim = 0; dim < strides.length; dim++) {
103
101
  coords[dim] = Math.floor(remaining / strides[dim]);
104
102
  remaining %= strides[dim];
105
103
  }
@@ -152,7 +150,7 @@ class Tensor {
152
150
  const outputValue = new Array(outputSize);
153
151
  for (let i = 0; i < outputSize; i++) {
154
152
  // Get coordinates from 1D index
155
- const coordsOutput = Tensor.indexToCoords(i, outputShape, outputStrides);
153
+ const coordsOutput = Tensor.indexToCoords(i, outputStrides);
156
154
  // Convert the coordinates to 1D index of flattened A with respect to A's shape
157
155
  const indexA = Tensor.coordsToUnbroadcastedIndex(coordsOutput, paddedAShape, paddedAStrides);
158
156
  // Convert the coordinates to 1D index of flattened B with respect to B's shape
@@ -267,14 +265,19 @@ class Tensor {
267
265
  }
268
266
  }
269
267
  // Remove size-1 dims only
270
- const outShape = this.shape.filter((dim, i) => {
271
- const shouldSqueeze = dims.includes(i);
272
- if (shouldSqueeze && dim !== 1)
273
- throw new Error(`Can not squeeze dim with size ${dim}`);
274
- return !shouldSqueeze;
275
- });
276
- // Remove strides of size-1 dims
277
- const outStrides = this.strides.filter((stride, i) => !dims.includes(i));
268
+ const outShape = [], outStrides = [];
269
+ for (let index = 0; index < this.shape.length; index++) {
270
+ const dim = this.shape[index];
271
+ const stride = this.strides[index];
272
+ if (dims.includes(index)) {
273
+ if (dim !== 1)
274
+ throw new Error(`Can not squeeze dim with size ${dim}`);
275
+ }
276
+ else {
277
+ outShape.push(dim);
278
+ outStrides.push(stride);
279
+ }
280
+ }
278
281
  const outValue = outShape.length === 0 ? this.value[0] : this.value;
279
282
  const out = new Tensor(outValue, {
280
283
  shape: outShape,
@@ -353,7 +356,7 @@ class Tensor {
353
356
  }
354
357
  // Calculate new value after sum
355
358
  for (let index = 0; index < originalSize; index++) {
356
- const coords = Tensor.indexToCoords(index, this.shape, this.strides);
359
+ const coords = Tensor.indexToCoords(index, this.strides);
357
360
  // Force 0 on reduced axes to collapse into size-1 dims
358
361
  const outCoords = coords.map((val, i) => dims.includes(i) ? 0 : val);
359
362
  // Convert output coordinates to flat index
@@ -407,7 +410,7 @@ class Tensor {
407
410
  }
408
411
  // Calculate new value after multiplying
409
412
  for (let index = 0; index < originalSize; index++) {
410
- const coords = Tensor.indexToCoords(index, this.shape, this.strides);
413
+ const coords = Tensor.indexToCoords(index, this.strides);
411
414
  // Force 0 on reduced axes to collapse into size-1 dims
412
415
  const outCoords = coords.map((val, i) => dims.includes(i) ? 0 : val);
413
416
  // Convert output coordinates to flat index
@@ -424,7 +427,7 @@ class Tensor {
424
427
  if (this.requiresGrad) {
425
428
  // Grad is the product of other elements of the same axis, which is product of all els divided by the current value
426
429
  for (let index = 0; index < originalSize; index++) {
427
- const coords = Tensor.indexToCoords(index, this.shape, this.strides);
430
+ const coords = Tensor.indexToCoords(index, this.strides);
428
431
  // Force 0 on reduced axes to collapse into size-1 dims
429
432
  const outCoords = coords.map((val, i) => dims.includes(i) ? 0 : val);
430
433
  // Convert output coordinates to flat index
@@ -470,7 +473,7 @@ class Tensor {
470
473
  }
471
474
  // Calculate sums and how many elements contribute to specific positions
472
475
  for (let index = 0; index < originalSize; index++) {
473
- const coords = Tensor.indexToCoords(index, this.shape, this.strides);
476
+ const coords = Tensor.indexToCoords(index, this.strides);
474
477
  // Force 0 on reduced axes to collapse into size-1 dims
475
478
  const outCoords = coords.map((val, i) => dims.includes(i) ? 0 : val);
476
479
  // Convert output coordinates to flat index
@@ -492,7 +495,7 @@ class Tensor {
492
495
  if (this.requiresGrad) {
493
496
  // Calculate grad by assiging 1 divide by the number of contributors to the position
494
497
  for (let index = 0; index < originalSize; index++) {
495
- const coords = Tensor.indexToCoords(index, this.shape, this.strides);
498
+ const coords = Tensor.indexToCoords(index, this.strides);
496
499
  // Force 0 on reduced axes to collapse into size-1 dims
497
500
  const outCoords = coords.map((val, i) => dims.includes(i) ? 0 : val);
498
501
  // Convert output coordinates to flat index
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "catniff",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
4
4
  "description": "A cute autograd engine for Javascript",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -26,7 +26,9 @@
26
26
  "neural-network",
27
27
  "machine-learning",
28
28
  "deep-learning",
29
- "micrograd"
29
+ "micrograd",
30
+ "torch",
31
+ "pytorch"
30
32
  ],
31
33
  "author": "nguyenphuminh",
32
34
  "license": "GPL-3.0",