catniff 0.8.0 → 0.8.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/core.d.ts +2 -3
- package/dist/core.js +7 -12
- package/package.json +1 -1
package/dist/core.d.ts
CHANGED
|
@@ -72,16 +72,15 @@ export declare class Tensor {
|
|
|
72
72
|
static reduce(tensor: Tensor, dims: number[] | number | undefined, keepDims: boolean, config: {
|
|
73
73
|
identity: number;
|
|
74
74
|
operation: (accumulator: number, value: number) => number;
|
|
75
|
-
needsCounters?: boolean;
|
|
76
75
|
postProcess?: (options: {
|
|
77
76
|
values: MemoryBuffer;
|
|
78
|
-
|
|
77
|
+
dimSize: number;
|
|
79
78
|
}) => void;
|
|
80
79
|
needsShareCounts?: boolean;
|
|
81
80
|
gradientFn: (options: {
|
|
82
81
|
outputValue: MemoryBuffer;
|
|
83
82
|
originalValue: MemoryBuffer;
|
|
84
|
-
|
|
83
|
+
dimSize: number;
|
|
85
84
|
shareCounts: MemoryBuffer;
|
|
86
85
|
realIndex: number;
|
|
87
86
|
outIndex: number;
|
package/dist/core.js
CHANGED
|
@@ -953,11 +953,11 @@ class Tensor {
|
|
|
953
953
|
}
|
|
954
954
|
return keepDims ? reducedThis : reducedThis.squeeze(dims);
|
|
955
955
|
}
|
|
956
|
+
const dimSize = tensor.shape[dims];
|
|
956
957
|
const outputShape = tensor.shape.map((dim, i) => dims === i ? 1 : dim);
|
|
957
958
|
const outputStrides = Tensor.getStrides(outputShape);
|
|
958
|
-
const outputSize =
|
|
959
|
+
const outputSize = tensor.numel / dimSize;
|
|
959
960
|
const outputValue = new dtype_1.TypedArray[tensor.dtype](outputSize).fill(config.identity);
|
|
960
|
-
const outputCounters = config.needsCounters ? new dtype_1.TypedArray[tensor.dtype](outputSize).fill(0) : new dtype_1.TypedArray[tensor.dtype]();
|
|
961
961
|
const originalSize = tensor.numel;
|
|
962
962
|
const originalValue = tensor.value;
|
|
963
963
|
const linearStrides = Tensor.getStrides(tensor.shape);
|
|
@@ -972,14 +972,10 @@ class Tensor {
|
|
|
972
972
|
const outFlatIndex = Tensor.coordsToIndex(coords, outputStrides);
|
|
973
973
|
// Apply op
|
|
974
974
|
outputValue[outFlatIndex] = config.operation(outputValue[outFlatIndex], originalValue[realFlatIndex]);
|
|
975
|
-
// Count el if needed
|
|
976
|
-
if (config.needsCounters) {
|
|
977
|
-
outputCounters[outFlatIndex]++;
|
|
978
|
-
}
|
|
979
975
|
}
|
|
980
976
|
// Post-process if needed (e.g., divide by count for mean)
|
|
981
977
|
if (config.postProcess) {
|
|
982
|
-
config.postProcess({ values: outputValue,
|
|
978
|
+
config.postProcess({ values: outputValue, dimSize });
|
|
983
979
|
}
|
|
984
980
|
const out = new Tensor(outputValue, {
|
|
985
981
|
shape: outputShape,
|
|
@@ -1021,7 +1017,7 @@ class Tensor {
|
|
|
1021
1017
|
gradValue[flatIndex] = config.gradientFn({
|
|
1022
1018
|
outputValue,
|
|
1023
1019
|
originalValue: tensor.value,
|
|
1024
|
-
|
|
1020
|
+
dimSize,
|
|
1025
1021
|
shareCounts,
|
|
1026
1022
|
realIndex: realFlatIndex,
|
|
1027
1023
|
outIndex: outFlatIndex
|
|
@@ -1058,13 +1054,12 @@ class Tensor {
|
|
|
1058
1054
|
return Tensor.reduce(this, dims, keepDims, {
|
|
1059
1055
|
identity: 0,
|
|
1060
1056
|
operation: (a, b) => a + b,
|
|
1061
|
-
|
|
1062
|
-
postProcess: ({ values, counters }) => {
|
|
1057
|
+
postProcess: ({ values, dimSize }) => {
|
|
1063
1058
|
for (let i = 0; i < values.length; i++) {
|
|
1064
|
-
values[i] /=
|
|
1059
|
+
values[i] /= dimSize;
|
|
1065
1060
|
}
|
|
1066
1061
|
},
|
|
1067
|
-
gradientFn: ({
|
|
1062
|
+
gradientFn: ({ dimSize }) => 1 / dimSize
|
|
1068
1063
|
});
|
|
1069
1064
|
}
|
|
1070
1065
|
max(dims, keepDims = false) {
|