@mineproness/tensorloop 1.2.5 → 1.2.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mineproness/tensorloop",
3
- "version": "1.2.5",
3
+ "version": "1.2.7",
4
4
  "description": "The most powerfull Machine Learning Library for AI.",
5
5
  "keywords": [
6
6
  "ai",
package/readme.md CHANGED
@@ -10,6 +10,8 @@ A Library that Supply AI Tools and Large Language model Development tools like E
10
10
  - CrossEntroy for Loss Calculation.
11
11
  - Relu for Stable Layer.
12
12
  - Tokenizer for convert into tokens.
13
+ - Self attention
14
+ - Position Embedding
13
15
 
14
16
  ## Configs
15
17
 
@@ -1,5 +1,5 @@
1
1
  import fs, { existsSync } from 'fs'
2
- import { GetEmbeddingVectors, SaveEmbeddingVectors, SaveVectors } from '../GetConfigs.js'
2
+ import { SaveVectors, LoadVectors } from '../GetConfigs.js'
3
3
  // import { config } from 'process'
4
4
 
5
5
  function GenerateVectors(embeddingSize, vocabSize) {
@@ -22,28 +22,22 @@ 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()
29
29
  }
30
30
  } else {
31
31
  this.vectors = GenerateVectors(embeddingSize, vocabSize)
32
-
32
+
33
33
  }
34
34
  }
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
- SaveEmbeddingVectors(this.vectors, this.configs.save.filename)
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 "fs";
2
- import { join } from "path";
3
-
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, vector.byteOffset, vector.byteLength));
10
- }
11
- } finally {
12
- fs.closeSync(fd);
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 GetVector(filename) {
32
- const buffer = fs.readFileSync(filename)
33
- return new Float32Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / Float32Array.BYTES_PER_ELEMENT)
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 Save(filename, vector) {
37
- return fs.writeFileSync(filename, Buffer.from(vector.buffer))
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 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.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
  }
@@ -1,4 +1,4 @@
1
- import { GetWeights, Save,GetVector , SaveVectors } from '../GetConfigs.js'
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,16 +22,16 @@ 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 = GetWeights(configs.save.filename[0], embeddingSize)
26
- const Bias = GetVector(configs.save.filename[1])
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 {
30
30
  const { vectors, Bias } = GenerateWeightsBias(embeddingSize, vocabSize)
31
31
  this.Weights = vectors;
32
32
  this.Bias = Bias;
33
- SaveVectors(configs.save.filename[0], this.Weights)
34
- Save(configs.save.filename[1], this.Bias)
33
+ SaveVectors( this.Weights,configs.save.filename[0])
34
+ SaveBias(this.Bias,configs.save.filename[1])
35
35
  }
36
36
  } else {
37
37
  const { vectors, Bias } = GenerateWeightsBias(embeddingSize, vocabSize)
@@ -40,8 +40,8 @@ export class Linear {
40
40
  }
41
41
  }
42
42
  Save() {
43
- SaveVectors(this.configs.save.filename[0], this.Weights)
44
- Save(this.configs.save.filename[1], this.Bias)
43
+ SaveVectors(this.Weights,this.configs.save.filename[0])
44
+ SaveBias(this.Bias, this.configs.save.filename[1])
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 { GetEmbeddingVectors, SaveEmbeddingVectors, SaveVectors } from '../GetConfigs.js'
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
- if (!this.vectors[token]) {
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
- SaveEmbeddingVectors(this.vectors, this.configs.save.filename)
51
+ SaveVectors(this.vectors, this.configs.save.filename)
57
52
  }
58
- addPos(PSEM, xEM) {
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 = PSEM.forward(pos)
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
  }