@mineproness/tensorloop 2.1.44 → 2.1.46

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": "2.1.44",
3
+ "version": "2.1.46",
4
4
  "description": "Raw-JAVASCRIPT Based Matirx and AI LLM Package called Tensorloop.",
5
5
  "keywords": [
6
6
  "ai",
@@ -6,6 +6,6 @@ export class Embedding{
6
6
  }
7
7
  })
8
8
  forward(token: number | number[]) : Float32Array[] | Float32Array[][]
9
- backward(token: number , inputGradient: number[] | Float32Array[], learingRate: number) : void
9
+ backward(inputGradient: number[] | Float32Array[], learingRate: number) : void
10
10
  Save() : void
11
11
  }
@@ -0,0 +1,7 @@
1
+ export default class Model{
2
+ constructor(embeddingSize: number, contextSize: number, configs: {
3
+ dirname: string,
4
+ activation: "gelu" | "relu",
5
+ })
6
+ train(text: string, batchSize: number, batchExecution: number, epochs: number, learningRate: number) : void
7
+ }
@@ -0,0 +1,89 @@
1
+ import { Embedding, PositionEmbedding, Linear, Tokenizer, TransformerBlock, Softmax } from '../../index.js'
2
+ import path, { join } from 'path'
3
+ export default class Model {
4
+ embedding;
5
+ linear;
6
+ posEmbedding;
7
+ softmax;
8
+ tokenizer;
9
+ block1;
10
+ block2;
11
+ contextSize;
12
+ constructor(embeddingSize, contextSize, configs) {
13
+ this.tokenizer = new Tokenizer()
14
+ this.contextSize = contextSize
15
+ this.embedding = new Embedding(embeddingSize, this.tokenizer.vocabSize, contextSize, {
16
+ save: {
17
+ filename: path.join(configs.dirname, "vectors.bin")
18
+ }
19
+ })
20
+ this.posEmbedding = new PositionEmbedding(embeddingSize, contextSize, {
21
+ save: {
22
+ filename: join(configs.dirname, 'posVec.bin')
23
+ }
24
+ })
25
+ this.linear = new Linear(embeddingSize, this.tokenizer.vocabSize, {
26
+ save: {
27
+ filename: [join(configs.dirname, 'weights.bin'), join(configs.dirname, 'bias.bin')]
28
+ }
29
+ })
30
+ this.softmax = new Softmax()
31
+ this.block1 = new TransformerBlock(embeddingSize, {
32
+ dirname: join(configs.dirname, "block1"),
33
+ activation: configs.activation
34
+ })
35
+ this.block2 = new TransformerBlock(embeddingSize, {
36
+ dirname: join(configs.dirname, "block2"),
37
+ activation: configs.activation
38
+ })
39
+ }
40
+ train(text = "", epochs = 500, learningRate = 0.01) {
41
+ process.stdout.write("\x1Bc")
42
+ console.log("Preparing Training Pairs....")
43
+ const tokens = text.split(/\r?\n/).map((e) => e.trim()).filter((e) => e.length > 0).map((e)=> e + " <EOS>").map((e) => this.tokenizer.encoder(e).tokenIDs).flat()
44
+ const trainingPair = []
45
+ for (let a = 0; a + this.contextSize < tokens.length; a++) {
46
+ trainingPair.push({
47
+ inputIDX: tokens.slice(a, a + this.contextSize),
48
+ targetIDX: tokens.slice(a + 1, a + this.contextSize + 1)
49
+ })
50
+ }
51
+ let firstRun = true;
52
+ console.log(`TotalPairs: ${trainingPair.length}`)
53
+ if (trainingPair.length == 0 || !trainingPair) throw new Error(`The Batch Execution ${batchExecution} is doesn't exist.`);
54
+ for (let i = 0; i < epochs; i++) {
55
+ let totalLoss = 0;
56
+ for (let a = 0; a < trainingPair.length; a++) {
57
+ const input = trainingPair[a].inputIDX
58
+ const target = trainingPair[a].targetIDX
59
+ let vector = this.embedding.forward(input)
60
+ vector = this.posEmbedding.forward(vector)
61
+ vector = this.block1.forward(vector)
62
+ vector = this.block2.forward(vector)
63
+ const logits = this.linear.forward(vector)
64
+ const probs = this.softmax.forward(logits)
65
+ const loss = this.softmax.crossEntropy(probs, target)[0]
66
+ const dLogits = this.softmax.backward(probs, target)
67
+ totalLoss += loss;
68
+ let dOutput = this.linear.backward(dLogits, learningRate);
69
+ dOutput = this.block2.backward(dOutput, learningRate)
70
+ dOutput = this.block1.backward(dOutput, learningRate)
71
+ this.posEmbedding.backward(dOutput, learningRate)
72
+ this.embedding.backward(dOutput, learningRate)
73
+ if (firstRun) {
74
+ firstRun = null;
75
+ console.log("")
76
+ console.log("")
77
+ }
78
+ const percentense = (((a + 1) / trainingPair.length) * 100).toFixed(2)
79
+ const filted = Math.round((percentense / 100) * 20);
80
+ const empty = 20 - filted
81
+ process.stdout.write(`\r[${"=".repeat(filted)}${" ".repeat(empty)}] ${percentense}% (${a + 1}/${trainingPair.length}) | loss ${(totalLoss / (a + 1)).toFixed(2)}`)
82
+ }
83
+ this.embedding.Save()
84
+ this.linear.Save()
85
+ this.block1.Save()
86
+ this.block2.Save()
87
+ }
88
+ }
89
+ }
@@ -27,22 +27,23 @@ export class PositionEmbedding {
27
27
  this.vectors = LoadVectors(configs.save.filename, embeddingSize)
28
28
  } else {
29
29
  this.vectors = GenerateVectors(embeddingSize, vocabSize)
30
- this.Save()
30
+ SaveVectors(this.vectors, this.configs.save.filename)
31
+
31
32
  }
32
33
  } else {
33
34
  this.vectors = GenerateVectors(embeddingSize, vocabSize)
34
- this.gpu = new GPUPosition(this.vectors, embeddingSize, vocabSize)
35
35
  }
36
+ this.gpu = new GPUPosition(this.vectors, embeddingSize, vocabSize)
36
37
  }
37
38
  forward(vec) {
38
39
  return this.gpu.forward(vec)
39
40
  }
40
41
  backward(inputGradient, learingRate) {
41
- this.gpu.backward(inputGradient, learingRate)
42
+ this.gpu.backward(inputGradient, learingRate)
42
43
  }
43
44
  Save() {
44
45
  const save = this.gpu.Save()
45
46
  SaveVectors(save, this.configs.save.filename)
46
-
47
+
47
48
  }
48
49
  }