-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathwikitext.ts
More file actions
67 lines (56 loc) · 2.1 KB
/
Copy pathwikitext.ts
File metadata and controls
67 lines (56 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import "@tensorflow/tfjs-node";
import { Disco, fetchTasks, models, Task } from "@epfml/discojs";
import {
saveModelToDisk,
loadModelFromDisk,
loadText,
} from "@epfml/discojs-node";
import { List } from "immutable";
async function main(): Promise<void> {
// Launch a server instance
const url = new URL("http://localhost:8080");
// Fetch the wikitext task from the server
const tasks = await fetchTasks(url);
const task = tasks.get("llm_task") as Task<"text", "federated"> | undefined;
if (task === undefined) {
throw new Error("task not found");
}
let model;
const modelFolder = "./models";
const modelFileName = "model_random.json";
// Toggle TRAIN_MODEL to either train and save a new model from scratch or load an existing model
const TRAIN_MODEL = true;
if (TRAIN_MODEL) {
// Load the wikitext dataset from the `datasets` folder
const dataset = loadText("../../datasets/wikitext/wiki.train.tokens").chain(
loadText("../../datasets/wikitext/wiki.valid.tokens"),
);
// Initialize a Disco instance and start training a language model
const disco = new Disco(task, url, { scheme: "federated" });
await disco.trainFully(dataset);
// Get the model and save the trained model
model = disco.trainer.model as models.GPT;
await saveModelToDisk(model, modelFolder, modelFileName);
await disco.close();
} else {
// Load the trained model
model = (await loadModelFromDisk(
`${modelFolder}/${modelFileName}`,
)) as models.GPT;
}
// Preprocess prompt
const prompt =
"The game began development in 2010 , carrying over a large portion";
const { tokenizer } = task.trainingInformation;
let tokens = tokenizer.tokenize(prompt);
// Predict a few tokens
const numberOfTokens = 10;
for (let i = 0; i < numberOfTokens; i++) {
const next = (await model.predict(List.of(tokens))).first();
if (next === undefined) throw new Error("no prediction");
tokens = tokens.push(next);
}
console.log(tokenizer.decode(tokens.toArray()));
}
// You can run this example with "npm start" from this folder
main().catch(console.error);