catniff 0.6.6 → 0.6.8
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 +1 -0
- package/dist/core.js +19 -0
- package/dist/nn.js +5 -5
- package/package.json +1 -1
package/dist/core.d.ts
CHANGED
|
@@ -57,6 +57,7 @@ export declare class Tensor {
|
|
|
57
57
|
indexWithArray(indices: number[]): Tensor;
|
|
58
58
|
index(indices: Tensor | TensorValue): Tensor;
|
|
59
59
|
slice(ranges: number[][]): Tensor;
|
|
60
|
+
chunk(chunks: number, dim?: number): Tensor[];
|
|
60
61
|
squeeze(dims?: number[] | number): Tensor;
|
|
61
62
|
unsqueeze(dim: number): Tensor;
|
|
62
63
|
static reduce(tensor: Tensor, dims: number[] | number | undefined, keepDims: boolean, config: {
|
package/dist/core.js
CHANGED
|
@@ -605,6 +605,25 @@ class Tensor {
|
|
|
605
605
|
}
|
|
606
606
|
return out;
|
|
607
607
|
}
|
|
608
|
+
// Tensor chunk
|
|
609
|
+
chunk(chunks, dim = 0) {
|
|
610
|
+
// Handle negative indices
|
|
611
|
+
if (dim < 0) {
|
|
612
|
+
dim += this.shape.length;
|
|
613
|
+
}
|
|
614
|
+
const sliceOpt = new Array(this.shape.length);
|
|
615
|
+
for (let index = 0; index < sliceOpt.length; index++) {
|
|
616
|
+
sliceOpt[index] = [];
|
|
617
|
+
}
|
|
618
|
+
const dimSize = this.shape[dim];
|
|
619
|
+
const chunkDimSize = Math.ceil(dimSize / chunks);
|
|
620
|
+
const results = [];
|
|
621
|
+
for (let index = 0; index < dimSize; index += chunkDimSize) {
|
|
622
|
+
sliceOpt[dim] = [index, Math.min(index + chunkDimSize, dimSize)];
|
|
623
|
+
results.push(this.slice(sliceOpt));
|
|
624
|
+
}
|
|
625
|
+
return results;
|
|
626
|
+
}
|
|
608
627
|
// Tensor squeeze
|
|
609
628
|
squeeze(dims) {
|
|
610
629
|
if (typeof this.value === "number")
|
package/dist/nn.js
CHANGED
|
@@ -191,7 +191,7 @@ class LayerNorm {
|
|
|
191
191
|
class Embedding {
|
|
192
192
|
weight;
|
|
193
193
|
constructor(numEmbeddings, embeddingDim, device) {
|
|
194
|
-
this.weight = core_1.Tensor.randn([numEmbeddings, embeddingDim], { device });
|
|
194
|
+
this.weight = core_1.Tensor.randn([numEmbeddings, embeddingDim], { requiresGrad: true, device });
|
|
195
195
|
}
|
|
196
196
|
forward(input) {
|
|
197
197
|
return this.weight.index(input);
|
|
@@ -207,10 +207,10 @@ class MultiheadAttention {
|
|
|
207
207
|
headDim;
|
|
208
208
|
dropout;
|
|
209
209
|
constructor(embedDim, numHeads, dropout = 0, bias = true, device) {
|
|
210
|
-
this.qProjection = new
|
|
211
|
-
this.kProjection = new
|
|
212
|
-
this.vProjection = new
|
|
213
|
-
this.oProjection = new
|
|
210
|
+
this.qProjection = new Linear(embedDim, embedDim, bias, device);
|
|
211
|
+
this.kProjection = new Linear(embedDim, embedDim, bias, device);
|
|
212
|
+
this.vProjection = new Linear(embedDim, embedDim, bias, device);
|
|
213
|
+
this.oProjection = new Linear(embedDim, embedDim, bias, device);
|
|
214
214
|
this.embedDim = embedDim;
|
|
215
215
|
this.numHeads = numHeads;
|
|
216
216
|
this.headDim = Math.floor(embedDim / numHeads);
|