@mineproness/tensorloop 1.1.5 → 1.1.7
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/src/Embedding/index.js +23 -13
- package/src/GetConfigs.js +30 -6
- package/src/Linear/index.js +2 -2
package/package.json
CHANGED
package/src/Embedding/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import fs, { existsSync } from 'fs'
|
|
2
|
-
import {
|
|
2
|
+
import { GetEmbeddingVectors, SaveEmbeddingVectors, SaveVectors } from '../GetConfigs.js'
|
|
3
3
|
// import { config } from 'process'
|
|
4
4
|
|
|
5
5
|
function GenerateVectors(embeddingSize, vocabSize) {
|
|
@@ -16,14 +16,16 @@ function GenerateVectors(embeddingSize, vocabSize) {
|
|
|
16
16
|
export class Embedding {
|
|
17
17
|
vectors = []
|
|
18
18
|
configs;
|
|
19
|
+
embeddingSize = 0
|
|
19
20
|
constructor(embeddingSize, vocabSize, configs) {
|
|
20
21
|
this.configs = configs
|
|
22
|
+
this.embeddingSize = embeddingSize
|
|
21
23
|
if (configs && configs.save) {
|
|
22
24
|
if (existsSync(configs.save.filename)) {
|
|
23
|
-
|
|
25
|
+
this.vectors = []
|
|
24
26
|
} else {
|
|
25
27
|
this.vectors = GenerateVectors(embeddingSize, vocabSize)
|
|
26
|
-
|
|
28
|
+
this.Save()
|
|
27
29
|
}
|
|
28
30
|
} else {
|
|
29
31
|
this.vectors = GenerateVectors(embeddingSize, vocabSize)
|
|
@@ -31,18 +33,26 @@ export class Embedding {
|
|
|
31
33
|
}
|
|
32
34
|
}
|
|
33
35
|
forward(token) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
if (Array.isArray(token)) {
|
|
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
|
+
return this.vectors[e]
|
|
42
|
+
})
|
|
43
|
+
} else {
|
|
44
|
+
if (!this.vectors[token]) {
|
|
45
|
+
this.vectors[token] = GetEmbeddingVectors(token, this.embeddingSize, this.configs.save.filename)
|
|
46
|
+
}
|
|
47
|
+
return this.vectors[token]
|
|
48
|
+
}
|
|
39
49
|
}
|
|
40
|
-
backward(token, inputGradient, learingRate){
|
|
41
|
-
for(let a = 0; a < this.vectors[token].length; a++){
|
|
42
|
-
|
|
50
|
+
backward(token, inputGradient, learingRate) {
|
|
51
|
+
for (let a = 0; a < this.vectors[token].length; a++) {
|
|
52
|
+
this.vectors[token][a] -= learingRate * inputGradient[a]
|
|
43
53
|
}
|
|
44
54
|
}
|
|
45
|
-
Save(){
|
|
46
|
-
|
|
55
|
+
Save() {
|
|
56
|
+
SaveEmbeddingVectors(this.vectors, this.configs.save.filename)
|
|
47
57
|
}
|
|
48
58
|
}
|
package/src/GetConfigs.js
CHANGED
|
@@ -13,14 +13,14 @@ export function SaveVectors(filename, vectors) {
|
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
export function
|
|
16
|
+
export function GetWeights(filename, embeddingSize) {
|
|
17
17
|
const fileBuffer = fs.readFileSync(filename)
|
|
18
|
-
const ELEMENTS_PER_ARRAY= embeddingSize;
|
|
19
|
-
const BYTES_PER_ELEMENT= Float32Array.BYTES_PER_ELEMENT
|
|
18
|
+
const ELEMENTS_PER_ARRAY = embeddingSize;
|
|
19
|
+
const BYTES_PER_ELEMENT = Float32Array.BYTES_PER_ELEMENT
|
|
20
20
|
const BYTES_PER_ARRAY = ELEMENTS_PER_ARRAY * BYTES_PER_ELEMENT
|
|
21
21
|
const totalArraySaved = fileBuffer.byteLength / BYTES_PER_ARRAY;
|
|
22
22
|
const totalArray = []
|
|
23
|
-
for(let i = 0; i < totalArraySaved; i++){
|
|
23
|
+
for (let i = 0; i < totalArraySaved; i++) {
|
|
24
24
|
const currentOffset = fileBuffer.byteOffset + (i * BYTES_PER_ARRAY)
|
|
25
25
|
const f32 = new Float32Array(fileBuffer.buffer, currentOffset, ELEMENTS_PER_ARRAY)
|
|
26
26
|
totalArray.push(f32)
|
|
@@ -28,11 +28,35 @@ export function GetVectors(filename, embeddingSize){
|
|
|
28
28
|
return totalArray
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
export function GetVector(filename){
|
|
31
|
+
export function GetVector(filename) {
|
|
32
32
|
const buffer = fs.readFileSync(filename)
|
|
33
33
|
return new Float32Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / Float32Array.BYTES_PER_ELEMENT)
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
export function Save(filename, vector){
|
|
36
|
+
export function Save(filename, vector) {
|
|
37
37
|
return fs.writeFileSync(filename, Buffer.from(vector.buffer))
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function GetEmbeddingVectors(token, embeddingSize, filename) {
|
|
41
|
+
const fd = fs.openSync(filename, "r")
|
|
42
|
+
const PER_BTYES_VECTOR = embeddingSize * Float32Array.BYTES_PER_ELEMENT
|
|
43
|
+
const buffer = Buffer.allocUnsafe(PER_BTYES_VECTOR)
|
|
44
|
+
fs.readSync(fd, buffer, 0, PER_BTYES_VECTOR, token * PER_BTYES_VECTOR)
|
|
45
|
+
fs.closeSync(fd)
|
|
46
|
+
return new Float32Array(buffer)
|
|
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) , 0 , vector.byteLength, offset)
|
|
57
|
+
|
|
58
|
+
}
|
|
59
|
+
}finally{
|
|
60
|
+
fs.closeSync(fd)
|
|
61
|
+
}
|
|
38
62
|
}
|
package/src/Linear/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GetWeights, Save,GetVector , SaveVectors } from '../GetConfigs.js'
|
|
2
2
|
import { existsSync } from 'fs'
|
|
3
3
|
function GenerateWeightsBias(embeddingSize, vocabSize) {
|
|
4
4
|
const vectors = []
|
|
@@ -22,7 +22,7 @@ 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 =
|
|
25
|
+
const Vectors = GetWeights(configs.save.filename[0], embeddingSize)
|
|
26
26
|
const Bias = GetVector(configs.save.filename[1])
|
|
27
27
|
this.Weights = Vectors;
|
|
28
28
|
this.Bias = Bias
|