@mineproness/tensorloop 1.1.4 → 1.1.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mineproness/tensorloop",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "description": "The most powerfull Machine Learning Library for AI.",
5
5
  "keywords": [
6
6
  "ai",
@@ -1,5 +1,5 @@
1
1
  import fs, { existsSync } from 'fs'
2
- import { GetVectors, SaveVectors } from '../GetConfigs.js'
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
- this.vectors = GetVectors(configs.save.filename, embeddingSize)
25
+ this.vectors = []
24
26
  } else {
25
27
  this.vectors = GenerateVectors(embeddingSize, vocabSize)
26
- SaveVectors(configs.save.filename, this.vectors)
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
- if(Array.isArray(token)){
35
- return token.map((e)=> this.vectors[e])
36
- }else{
37
- return this.vectors[token]
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
- this.vectors[token][a] -= learingRate * inputGradient[a]
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
- SaveVectors(this.configs.save.filename , this.vectors)
55
+ Save() {
56
+ SaveEmbeddingVectors(this.vectors, this.configs.save.filename)
47
57
  }
48
58
  }
package/src/GetConfigs.js CHANGED
@@ -1,23 +1,26 @@
1
1
  import fs from "fs";
2
2
  import { join } from "path";
3
3
 
4
- export function SaveVectors (filename, vectors){
5
- const sizeOfArray = vectors.length;
6
- const sizeOfEmbedding = vectors[0].length;
7
- const floatArray = vectors.map((e)=> Buffer.from(e.buffer))
8
- const combinedBuffer = Buffer.concat(floatArray)
9
- fs.writeFileSync(filename , combinedBuffer)
10
-
4
+ export function SaveVectors(filename, vectors) {
5
+ const fd = fs.openSync(filename, "w");
6
+
7
+ try {
8
+ for (const vector of vectors) {
9
+ fs.writeSync(fd, Buffer.from(vector.buffer));
10
+ }
11
+ } finally {
12
+ fs.closeSync(fd);
13
+ }
11
14
  }
12
15
 
13
- export function GetVectors(filename, embeddingSize){
16
+ export function GetWeights(filename, embeddingSize) {
14
17
  const fileBuffer = fs.readFileSync(filename)
15
- const ELEMENTS_PER_ARRAY= embeddingSize;
16
- const BYTES_PER_ELEMENT= Float32Array.BYTES_PER_ELEMENT
18
+ const ELEMENTS_PER_ARRAY = embeddingSize;
19
+ const BYTES_PER_ELEMENT = Float32Array.BYTES_PER_ELEMENT
17
20
  const BYTES_PER_ARRAY = ELEMENTS_PER_ARRAY * BYTES_PER_ELEMENT
18
21
  const totalArraySaved = fileBuffer.byteLength / BYTES_PER_ARRAY;
19
22
  const totalArray = []
20
- for(let i = 0; i < totalArraySaved; i++){
23
+ for (let i = 0; i < totalArraySaved; i++) {
21
24
  const currentOffset = fileBuffer.byteOffset + (i * BYTES_PER_ARRAY)
22
25
  const f32 = new Float32Array(fileBuffer.buffer, currentOffset, ELEMENTS_PER_ARRAY)
23
26
  totalArray.push(f32)
@@ -25,11 +28,35 @@ export function GetVectors(filename, embeddingSize){
25
28
  return totalArray
26
29
  }
27
30
 
28
- export function GetVector(filename){
31
+ export function GetVector(filename) {
29
32
  const buffer = fs.readFileSync(filename)
30
33
  return new Float32Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / Float32Array.BYTES_PER_ELEMENT)
31
34
  }
32
35
 
33
- export function Save(filename, vector){
36
+ export function Save(filename, vector) {
34
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.readSync(fd, Buffer.from(vector.buffer) , 0 , vector.byteLength, offset)
57
+
58
+ }
59
+ }finally{
60
+ fs.closeSync(fd)
61
+ }
35
62
  }
@@ -1,4 +1,4 @@
1
- import { GetVector, GetVectors, Save, SaveVectors } from '../GetConfigs.js'
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 = GetVectors(configs.save.filename[0], embeddingSize)
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