@mineproness/tensorloop 1.2.5 → 1.2.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.
- package/package.json +1 -1
- package/readme.md +2 -0
- package/src/Embedding/index.js +3 -9
- package/src/GetConfigs.js +28 -55
- package/src/Linear/index.js +5 -5
- package/src/PositionEmbedding/index.js +6 -11
- package/src/SelfAttention/index.d.ts +6 -1
package/package.json
CHANGED
package/readme.md
CHANGED
package/src/Embedding/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import fs, { existsSync } from 'fs'
|
|
2
|
-
import {
|
|
2
|
+
import { SaveVectors, LoadVectors } from '../GetConfigs.js'
|
|
3
3
|
// import { config } from 'process'
|
|
4
4
|
|
|
5
5
|
function GenerateVectors(embeddingSize, vocabSize) {
|
|
@@ -22,7 +22,7 @@ export class Embedding {
|
|
|
22
22
|
this.embeddingSize = embeddingSize
|
|
23
23
|
if (configs && configs.save) {
|
|
24
24
|
if (existsSync(configs.save.filename)) {
|
|
25
|
-
this.vectors =
|
|
25
|
+
this.vectors = LoadVectors(configs.save.filename, embeddingSize)
|
|
26
26
|
} else {
|
|
27
27
|
this.vectors = GenerateVectors(embeddingSize, vocabSize)
|
|
28
28
|
this.Save()
|
|
@@ -35,15 +35,9 @@ export class Embedding {
|
|
|
35
35
|
forward(token) {
|
|
36
36
|
if (Array.isArray(token)) {
|
|
37
37
|
return token.map((e) => {
|
|
38
|
-
const id = this.vectors[e]
|
|
39
|
-
if (id) return id
|
|
40
|
-
this.vectors[e] = GetEmbeddingVectors(e, this.embeddingSize, this.configs.save.filename)
|
|
41
38
|
return this.vectors[e]
|
|
42
39
|
})
|
|
43
40
|
} else {
|
|
44
|
-
if (!this.vectors[token]) {
|
|
45
|
-
this.vectors[token] = GetEmbeddingVectors(token, this.embeddingSize, this.configs.save.filename)
|
|
46
|
-
}
|
|
47
41
|
return this.vectors[token]
|
|
48
42
|
}
|
|
49
43
|
}
|
|
@@ -53,6 +47,6 @@ export class Embedding {
|
|
|
53
47
|
}
|
|
54
48
|
}
|
|
55
49
|
Save() {
|
|
56
|
-
|
|
50
|
+
SaveVectors(this.vectors, this.configs.save.filename)
|
|
57
51
|
}
|
|
58
52
|
}
|
package/src/GetConfigs.js
CHANGED
|
@@ -1,62 +1,35 @@
|
|
|
1
|
-
import fs from
|
|
2
|
-
import { join } from
|
|
3
|
-
|
|
4
|
-
export function SaveVectors(
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function GetWeights(filename, embeddingSize) {
|
|
17
|
-
const fileBuffer = fs.readFileSync(filename)
|
|
18
|
-
const ELEMENTS_PER_ARRAY = embeddingSize;
|
|
19
|
-
const BYTES_PER_ELEMENT = Float32Array.BYTES_PER_ELEMENT
|
|
20
|
-
const BYTES_PER_ARRAY = ELEMENTS_PER_ARRAY * BYTES_PER_ELEMENT
|
|
21
|
-
const totalArraySaved = fileBuffer.byteLength / BYTES_PER_ARRAY;
|
|
22
|
-
const totalArray = []
|
|
23
|
-
for (let i = 0; i < totalArraySaved; i++) {
|
|
24
|
-
const currentOffset = fileBuffer.byteOffset + (i * BYTES_PER_ARRAY)
|
|
25
|
-
const f32 = new Float32Array(fileBuffer.buffer, currentOffset, ELEMENTS_PER_ARRAY)
|
|
26
|
-
totalArray.push(f32)
|
|
27
|
-
}
|
|
28
|
-
return totalArray
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import { join } from 'path'
|
|
3
|
+
|
|
4
|
+
export function SaveVectors(vectors, path){
|
|
5
|
+
const rows = vectors.length;
|
|
6
|
+
const cols = vectors[0].length;
|
|
7
|
+
const flatArray = new Float32Array(rows * cols)
|
|
8
|
+
vectors.forEach((e,i)=>{
|
|
9
|
+
flatArray.set(e, i * cols)
|
|
10
|
+
})
|
|
11
|
+
const bufferArray = Buffer.from(flatArray.buffer, flatArray.byteOffset, flatArray.byteLength)
|
|
12
|
+
fs.writeFileSync(path, bufferArray)
|
|
29
13
|
}
|
|
30
14
|
|
|
31
|
-
export function
|
|
32
|
-
|
|
33
|
-
|
|
15
|
+
export function LoadVectors(path, cols){
|
|
16
|
+
const buffer = fs.readFileSync(path)
|
|
17
|
+
const flatArray = new Float32Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / 4 )
|
|
18
|
+
const vectors = []
|
|
19
|
+
const rows = flatArray.length / cols;
|
|
20
|
+
for(let a = 0; a < rows; a++){
|
|
21
|
+
vectors.push(flatArray.slice(a * cols, (a+1) * cols))
|
|
22
|
+
}
|
|
23
|
+
return vectors
|
|
34
24
|
}
|
|
35
25
|
|
|
36
|
-
export function
|
|
37
|
-
|
|
26
|
+
export function SaveBias(vectors, path){
|
|
27
|
+
const bufferArray = Buffer.from(vectors.buffer, vectors.byteOffset, vectors.byteLength)
|
|
28
|
+
fs.writeFileSync(path, bufferArray)
|
|
38
29
|
}
|
|
39
30
|
|
|
40
|
-
export function
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
fs.readSync(fd, buffer, 0, PER_BTYES_VECTOR, token * PER_BTYES_VECTOR)
|
|
45
|
-
fs.closeSync(fd)
|
|
46
|
-
return new Float32Array(buffer.buffer, buffer.byteOffset, embeddingSize)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function SaveEmbeddingVectors(vectors = [new Float32Array([1, 2, 3])], filename) {
|
|
50
|
-
const fd = fs.openSync(filename, "w")
|
|
51
|
-
try {
|
|
52
|
-
for (let a = 0; a < vectors.length; a++) {
|
|
53
|
-
const vector = vectors[a]
|
|
54
|
-
if (vector == undefined) continue;
|
|
55
|
-
const offset = a * vector.length * Float32Array.BYTES_PER_ELEMENT;
|
|
56
|
-
fs.writeSync(fd, Buffer.from(vector.buffer, vector.byteOffset, vector.byteLength) , 0 , vector.byteLength, offset)
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
}finally{
|
|
60
|
-
fs.closeSync(fd)
|
|
61
|
-
}
|
|
31
|
+
export function LoadBias(path){
|
|
32
|
+
const buffer = fs.readFileSync(path)
|
|
33
|
+
const flatArray = new Float32Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / 4 )
|
|
34
|
+
return vectors
|
|
62
35
|
}
|
package/src/Linear/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LoadBias,LoadVectors,SaveBias,SaveVectors } from '../GetConfigs.js'
|
|
2
2
|
import { existsSync } from 'fs'
|
|
3
3
|
function GenerateWeightsBias(embeddingSize, vocabSize) {
|
|
4
4
|
const vectors = []
|
|
@@ -22,8 +22,8 @@ export class Linear {
|
|
|
22
22
|
if (configs && configs.save) {
|
|
23
23
|
this.configs = configs
|
|
24
24
|
if (existsSync(configs.save.filename[0]) && existsSync(configs.save.filename[1])) {
|
|
25
|
-
const Vectors =
|
|
26
|
-
const Bias =
|
|
25
|
+
const Vectors = LoadVectors(configs.save.filename[0], embeddingSize)
|
|
26
|
+
const Bias = LoadBias(configs.save.filename[1])
|
|
27
27
|
this.Weights = Vectors;
|
|
28
28
|
this.Bias = Bias
|
|
29
29
|
} else {
|
|
@@ -31,7 +31,7 @@ export class Linear {
|
|
|
31
31
|
this.Weights = vectors;
|
|
32
32
|
this.Bias = Bias;
|
|
33
33
|
SaveVectors(configs.save.filename[0], this.Weights)
|
|
34
|
-
|
|
34
|
+
SaveBias(configs.save.filename[1], this.Bias)
|
|
35
35
|
}
|
|
36
36
|
} else {
|
|
37
37
|
const { vectors, Bias } = GenerateWeightsBias(embeddingSize, vocabSize)
|
|
@@ -41,7 +41,7 @@ export class Linear {
|
|
|
41
41
|
}
|
|
42
42
|
Save() {
|
|
43
43
|
SaveVectors(this.configs.save.filename[0], this.Weights)
|
|
44
|
-
|
|
44
|
+
SaveBias(this.configs.save.filename[1], this.Bias)
|
|
45
45
|
}
|
|
46
46
|
forward(input) {
|
|
47
47
|
this.input.push(input)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import fs, { existsSync } from 'fs'
|
|
2
|
-
import {
|
|
2
|
+
import { LoadVectors, SaveVectors } from '../GetConfigs.js'
|
|
3
3
|
// import { config } from 'process'
|
|
4
4
|
|
|
5
5
|
function GenerateVectors(embeddingSize, vocabSize) {
|
|
@@ -22,7 +22,7 @@ export class PositionEmbedding {
|
|
|
22
22
|
this.embeddingSize = embeddingSize
|
|
23
23
|
if (configs && configs.save) {
|
|
24
24
|
if (existsSync(configs.save.filename)) {
|
|
25
|
-
this.vectors =
|
|
25
|
+
this.vectors = LoadVectors(onfigs.save.filename, embeddingSize)
|
|
26
26
|
} else {
|
|
27
27
|
this.vectors = GenerateVectors(embeddingSize, vocabSize)
|
|
28
28
|
this.Save()
|
|
@@ -35,15 +35,10 @@ export class PositionEmbedding {
|
|
|
35
35
|
forward(token) {
|
|
36
36
|
if (Array.isArray(token)) {
|
|
37
37
|
return token.map((e) => {
|
|
38
|
-
const id = this.vectors[e]
|
|
39
|
-
if (id) return id
|
|
40
|
-
this.vectors[e] = GetEmbeddingVectors(e, this.embeddingSize, this.configs.save.filename)
|
|
41
38
|
return this.vectors[e]
|
|
42
39
|
})
|
|
43
40
|
} else {
|
|
44
|
-
|
|
45
|
-
this.vectors[token] = GetEmbeddingVectors(token, this.embeddingSize, this.configs.save.filename)
|
|
46
|
-
}
|
|
41
|
+
|
|
47
42
|
return this.vectors[token]
|
|
48
43
|
}
|
|
49
44
|
}
|
|
@@ -53,14 +48,14 @@ export class PositionEmbedding {
|
|
|
53
48
|
}
|
|
54
49
|
}
|
|
55
50
|
Save() {
|
|
56
|
-
|
|
51
|
+
SaveVectors(this.vectors, this.configs.save.filename)
|
|
57
52
|
}
|
|
58
|
-
addPos(
|
|
53
|
+
addPos(xEM) {
|
|
59
54
|
const out = []
|
|
60
55
|
|
|
61
56
|
for (let pos = 0; pos < xEM.length; pos++) {
|
|
62
57
|
const tokenVector = xEM[pos]
|
|
63
|
-
const posVector =
|
|
58
|
+
const posVector = this.forward(pos)
|
|
64
59
|
const x = new Float32Array(xEM[0].length)
|
|
65
60
|
for (let a = 0; a < tokenVector.length; a++) {
|
|
66
61
|
x[a] = tokenVector[a] + posVector[a]
|
|
@@ -3,9 +3,14 @@ export class SelfAttention{
|
|
|
3
3
|
v : Float32Array[]
|
|
4
4
|
k : Float32Array[]
|
|
5
5
|
input;
|
|
6
|
+
constructor(embeddingSize : number, configs: {
|
|
7
|
+
save: {
|
|
8
|
+
dirname: string
|
|
9
|
+
}
|
|
10
|
+
})
|
|
6
11
|
weights : Float32Array[]
|
|
7
12
|
score : number
|
|
8
|
-
forward(vectors: Float32Array[])
|
|
13
|
+
forward(vectors: Float32Array[][])
|
|
9
14
|
Save() : void
|
|
10
15
|
backward(dOutput, learningRate)
|
|
11
16
|
}
|