@genai-fi/nanogpt 0.15.13 → 0.16.1

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.
@@ -1,6 +1,8 @@
1
1
  import { Task as t } from "./Task.js";
2
+ import { shuffle as s } from "../DatasetBuilder.js";
2
3
  class a extends t {
3
4
  rawConvo;
5
+ shuffledIndices = null;
4
6
  index = 0;
5
7
  get length() {
6
8
  return this.rawConvo.length;
@@ -14,20 +16,20 @@ class a extends t {
14
16
  nextConversation() {
15
17
  if (this.index >= this.rawConvo.length)
16
18
  return null;
17
- const n = this.rawConvo[this.index];
19
+ const n = this.rawConvo[this.shuffledIndices ? this.shuffledIndices[this.index] : this.index];
18
20
  return this.index++, n;
19
21
  }
20
22
  nextTokens(n) {
21
- const o = this.nextConversation();
22
- return o ? n.encodeConversation(o) : null;
23
- }
24
- getRandomConversation() {
25
- const n = Math.floor(Math.random() * this.rawConvo.length);
26
- return this.rawConvo[n];
27
- }
28
- getRandomTokens(n) {
29
- const o = Math.floor(Math.random() * this.rawConvo.length);
30
- return n.encodeConversation(this.rawConvo[o]);
23
+ const e = this.nextConversation();
24
+ return e ? n.encodeConversation(e) : null;
25
+ }
26
+ shuffle() {
27
+ if (!this.shuffledIndices) {
28
+ this.shuffledIndices = new Uint32Array(this.rawConvo.length);
29
+ for (let n = 0; n < this.rawConvo.length; n++)
30
+ this.shuffledIndices[n] = n;
31
+ }
32
+ s(this.shuffledIndices), this.index = 0;
31
33
  }
32
34
  async estimateTokens(n) {
33
35
  return (await n.encodeConversation(this.rawConvo[0])).length * this.length;
@@ -8,7 +8,6 @@ export default class PretrainingTask extends Task {
8
8
  hasMoreConversations(): boolean;
9
9
  nextConversation(): Conversation[] | null;
10
10
  nextTokens(tokeniser: ITokeniser): number[] | null;
11
- getRandomConversation(): Conversation[];
12
- getRandomTokens(tokeniser: ITokeniser): number[];
11
+ shuffle(): void;
13
12
  estimateTokens(tokeniser: ITokeniser): Promise<number>;
14
13
  }
@@ -1,5 +1,5 @@
1
1
  import { Task as n } from "./Task.js";
2
- class i extends n {
2
+ class r extends n {
3
3
  rawText;
4
4
  index = 0;
5
5
  get length() {
@@ -26,18 +26,8 @@ class i extends n {
26
26
  const e = t.encodeSequence(this.rawText[this.index]);
27
27
  return this.index++, e;
28
28
  }
29
- getRandomConversation() {
30
- const t = Math.floor(Math.random() * this.rawText.length);
31
- return [
32
- {
33
- role: "assistant",
34
- content: this.rawText[t]
35
- }
36
- ];
37
- }
38
- getRandomTokens(t) {
39
- const e = Math.floor(Math.random() * this.rawText.length);
40
- return t.encodeSequence(this.rawText[e]);
29
+ shuffle() {
30
+ this.index = 0;
41
31
  }
42
32
  async estimateTokens(t) {
43
33
  return (await t.encodeConversation([
@@ -49,5 +39,5 @@ class i extends n {
49
39
  }
50
40
  }
51
41
  export {
52
- i as default
42
+ r as default
53
43
  };
@@ -8,8 +8,7 @@ export default class StartSentenceTask extends Task {
8
8
  hasMoreConversations(): boolean;
9
9
  nextConversation(): Conversation[] | null;
10
10
  nextTokens(tokeniser: ITokeniser): number[] | null;
11
- getRandomConversation(): Conversation[];
12
- getRandomTokens(tokeniser: ITokeniser): number[];
11
+ shuffle(): void;
13
12
  private conversationFromString;
14
13
  estimateTokens(tokeniser: ITokeniser): Promise<number>;
15
14
  }
@@ -21,13 +21,8 @@ class a extends e {
21
21
  const n = this.nextConversation();
22
22
  return n ? t.encodeConversation(n) : null;
23
23
  }
24
- getRandomConversation() {
25
- const t = Math.floor(Math.random() * this.rawText.length);
26
- return this.conversationFromString(this.rawText[t]);
27
- }
28
- getRandomTokens(t) {
29
- const n = this.getRandomConversation();
30
- return t.encodeConversation(n);
24
+ shuffle() {
25
+ this.index = 0;
31
26
  }
32
27
  conversationFromString(t) {
33
28
  const n = t.indexOf(".");
@@ -5,7 +5,6 @@ export declare abstract class Task {
5
5
  abstract nextConversation(): Conversation[] | null;
6
6
  abstract nextTokens(tokeniser: ITokeniser): number[] | null;
7
7
  abstract estimateTokens(tokeniser: ITokeniser): Promise<number>;
8
- abstract getRandomConversation(): Conversation[];
9
- abstract getRandomTokens(tokeniser: ITokeniser): number[];
8
+ abstract shuffle(): void;
10
9
  }
11
10
  export declare function tokensFromTasks(tasks: Task[], tokenizer: ITokeniser, cb?: (tokens: number) => void): Promise<Uint16Array>;
@@ -0,0 +1,5 @@
1
+ import { Task } from './Task';
2
+ export default function splitValidation(tasks: Task[], validationSplit: number): {
3
+ training: Task;
4
+ validation: Task;
5
+ };
@@ -0,0 +1,21 @@
1
+ import s from "./ConversationTask.js";
2
+ function f(e, o) {
3
+ if (o <= 0 || o >= 1)
4
+ throw new Error("validationSplit must be between 0 and 1");
5
+ e.forEach((n) => n.shuffle());
6
+ const r = [], a = [];
7
+ for (const n of e)
8
+ for (; n.hasMoreConversations(); ) {
9
+ const t = n.nextConversation();
10
+ if (!t)
11
+ break;
12
+ Math.random() < o ? a.push(t) : r.push(t);
13
+ }
14
+ return {
15
+ training: new s(r),
16
+ validation: new s(a)
17
+ };
18
+ }
19
+ export {
20
+ f as default
21
+ };
@@ -39,8 +39,8 @@ import "../ops/webgl/adamAdjust.js";
39
39
  import "../ops/cpu/adamMoments.js";
40
40
  import "../ops/webgl/adamMoments.js";
41
41
  import { PAGE_FACTOR as m, shuffle as h } from "./DatasetBuilder.js";
42
- import "../papaparse.min-C0cScC2i.js";
43
42
  import { tokensFromTasks as k } from "./tasks/Task.js";
43
+ import "../papaparse.min-C0cScC2i.js";
44
44
  import "../ops/cpu/matMulGelu.js";
45
45
  import "../matMulGelu-JNLZqKQp.js";
46
46
  import "../ops/grads/matMulGelu.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@genai-fi/nanogpt",
3
- "version": "0.15.13",
3
+ "version": "0.16.1",
4
4
  "type": "module",
5
5
  "main": "dist/main.js",
6
6
  "types": "dist/main.d.ts",