-
Notifications
You must be signed in to change notification settings - Fork 31
ONNX to Tensorflow.js conversion of GPT-2 #927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
750350c
discojs/models/gpt: update layer names to be consistent with Python v…
JulienVig 07f2145
onnx-converter: new npm workspace to convert GPT2 from ONNX to TFJS
JulienVig 0560425
*: remove v-html tags
JulienVig a97dd78
Re-generate package-lock.json
JulienVig f6ae706
discojs-node: cache hellaswag file locally
JulienVig 9fbe6b3
cli/hellaswag: remove logging to file and string casting
JulienVig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,95 @@ | ||
| import fsPromise from 'node:fs/promises'; | ||
| import { dirname } from 'path'; | ||
| import { fileURLToPath } from 'url'; | ||
| import { parse } from 'ts-command-line-args' | ||
|
|
||
| import '@tensorflow/tfjs-node'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { Tokenizer, models } from '@epfml/discojs'; | ||
| import { models, serialization, Tokenizer } from '@epfml/discojs'; | ||
| import { loadHellaSwag } from '@epfml/discojs-node'; | ||
|
|
||
| const logFile = path.join('..', 'datasets', 'LogFile_hellaswag.txt'); | ||
| const logLines: string[] = []; | ||
|
|
||
| function log(message: string) { | ||
| console.log(message); | ||
| logLines.push(message); | ||
| } | ||
| const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| const hellaswagDataset: models.HellaSwagDataset = await loadHellaSwag(-1) | ||
|
|
||
| async function evaluateTFJS(tokenizer: Tokenizer) { | ||
| const model = new models.GPT({ seed: 42 }); | ||
| log('Evaluating TFJS GPT on HellaSwag...'); | ||
| async function evaluateModel(model: models.GPT | models.ONNXModel, numDataPoints = -1) { | ||
| const hellaswagDataset: models.HellaSwagDataset = await loadHellaSwag(numDataPoints) | ||
| const tokenizer = await Tokenizer.from_pretrained('Xenova/gpt2'); | ||
| console.log('Starting the HellaSwag benchmark...'); | ||
|
|
||
| const start = Date.now(); | ||
| const accuracy = await models.evaluate_hellaswag(model, tokenizer, hellaswagDataset, false); | ||
| const accuracy = await models.evaluate_hellaswag(model, tokenizer, hellaswagDataset, true); | ||
| const duration = ((Date.now() - start) / 1000).toFixed(2); | ||
|
|
||
| log(`TFJS GPT Accuracy: ${(accuracy * 100).toFixed(2)}%`); | ||
| log(`TFJS GPT Evaluation Time: ${duration} seconds`); | ||
| console.log(`Final accuracy: ${(accuracy * 100).toFixed(2)}%`); | ||
| console.log(`Evaluation Time: ${duration} seconds`); | ||
| } | ||
|
|
||
| async function evaluateXenova(tokenizer: Tokenizer) { | ||
| const model = await models.ONNXModel.init_pretrained('Xenova/gpt2'); | ||
| log('Evaluating Xenova GPT-2 (ONNX) on HellaSwag...'); | ||
| const ModelTypes = ['onnx', 'gpt-tfjs-random', 'gpt-tfjs-pretrained'] as const; | ||
| type ModelType = typeof ModelTypes[number]; | ||
|
|
||
| const start = Date.now(); | ||
| const accuracy = await models.evaluate_hellaswag(model, tokenizer, hellaswagDataset, false); | ||
| const duration = ((Date.now() - start) / 1000).toFixed(2); | ||
| interface HellaSwagArgs { | ||
| model: ModelType | ||
| numDataPoints: number | ||
| logFile: string | ||
| pretrainedModelPath: string | ||
| help?: boolean | ||
| } | ||
|
|
||
| log(`Xenova GPT-2 Accuracy: ${(accuracy * 100).toFixed(2)}%`); | ||
| log(`Xenova GPT-2 Evaluation Time: ${duration} seconds`); | ||
| function castModelType(raw: string): ModelType { | ||
| for (const t of ModelTypes) if (raw === t) return t | ||
| throw new Error(`Invalid model type: ${raw}`) | ||
| } | ||
|
|
||
| async function main(): Promise<void> { | ||
| fs.writeFileSync(logFile, '', 'utf-8'); // Clear old log file | ||
| const args = parse<HellaSwagArgs>({ | ||
| model: { | ||
| type: (raw: string) => castModelType(raw), | ||
| description: `Model type, one of ${ModelTypes.toString()}`, | ||
| defaultValue: 'onnx' | ||
| }, | ||
| numDataPoints: { | ||
| type: Number, | ||
| description: 'Number of HellaSwag datapoints to evaluate, set -1 for the whole benchmark', | ||
| defaultValue: -1 | ||
| }, | ||
| logFile: { | ||
| type: String, | ||
| description: 'Relative path to the log file, default to ./hellaswag.log', defaultValue: 'hellaswag.log' | ||
| }, | ||
| pretrainedModelPath: { | ||
| type: String, | ||
| description: 'If specifying gpt-tfjs-pretrained, provide the relative path to the TF.js pretrained model', | ||
| defaultValue: path.join(__dirname, "..", "..", "onnx-converter", "assets", "model.json") | ||
| }, | ||
| help: { | ||
| type: Boolean, | ||
| optional: true, | ||
| alias: 'h', | ||
| description: 'Prints this usage guide' | ||
| } | ||
| }, { helpArg: 'help' }) | ||
|
|
||
| const tokenizer = await Tokenizer.from_pretrained('Xenova/gpt2'); | ||
| await evaluateTFJS(tokenizer); | ||
| log('\n---\n'); | ||
| await evaluateXenova(tokenizer); | ||
| let model: models.GPT | models.ONNXModel | undefined; | ||
| switch (args.model) { | ||
| case 'onnx': | ||
| console.log("Using ONNX pretrained model Xenova/gpt2") | ||
| model = await models.ONNXModel.init_pretrained('Xenova/gpt2'); | ||
| break; | ||
| case 'gpt-tfjs-random': | ||
| console.log("Using GPT-TFJS with random initialization") | ||
| model = new models.GPT({ seed: 42 }); | ||
| break; | ||
| case 'gpt-tfjs-pretrained': | ||
| console.log("Using GPT-TFJS with pretrained weights") | ||
| if (args.pretrainedModelPath === undefined) { | ||
| throw new Error("If choosing gpt-tfjs-pretrained, provide the relative path to the TF.js pretrained model `pretrainedModelPath") | ||
| } | ||
| const encodedModel = await fsPromise.readFile(args.pretrainedModelPath); | ||
| model = await serialization.model.decode(encodedModel) as models.GPT; | ||
| break; | ||
| } | ||
| await evaluateModel(model, args.numDataPoints); | ||
|
|
||
| fs.writeFileSync(logFile, logLines.join('\n'), 'utf-8'); | ||
| console.log(`\nResults written to ${logFile}`); | ||
| console.log("Benchmark completed!") | ||
| } | ||
|
|
||
| main().catch(console.error); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,3 +20,6 @@ | |
|
|
||
| # GDHF demo | ||
| /tinder_dog/ | ||
|
|
||
| # HellaSwag benchmark | ||
| hellaswag* | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.