-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Emscripten build (demo, quick and dirty) #12
Draft
ggerganov
wants to merge
1
commit into
karpathy:master
Choose a base branch
from
ggerganov:web
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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 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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -383,80 +383,100 @@ int argmax(float* v, int n) { | |||||||
|
||||||||
// ---------------------------------------------------------------------------- | ||||||||
|
||||||||
int main(int argc, char *argv[]) { | ||||||||
setbuf(stdout, NULL); // disable stdout buffering | ||||||||
// poor man's C argparse | ||||||||
char *checkpoint = NULL; | ||||||||
float temperature = 0.9f; | ||||||||
|
||||||||
// poor man's C argparse | ||||||||
char *checkpoint = NULL; | ||||||||
float temperature = 0.9f; | ||||||||
// 'checkpoint' is necessary arg | ||||||||
if (argc < 2) { | ||||||||
printf("Usage: %s <checkpoint_file> [temperature] [seed]\n", argv[0]); | ||||||||
return 1; | ||||||||
} | ||||||||
checkpoint = argv[1]; | ||||||||
// temperature is optional | ||||||||
if (argc >= 3) { | ||||||||
temperature = atof(argv[2]); | ||||||||
} | ||||||||
// seed is optional | ||||||||
if (argc >= 4) { | ||||||||
unsigned int seed = atoi(argv[3]); | ||||||||
srand(seed); | ||||||||
// read in the config header | ||||||||
Config config; | ||||||||
|
||||||||
TransformerWeights weights; | ||||||||
|
||||||||
// create and init the application RunState | ||||||||
RunState state; | ||||||||
|
||||||||
// the current position we are in | ||||||||
int next; | ||||||||
int token = 1; // 1 = BOS token in Llama-2 sentencepiece | ||||||||
int pos = 0; | ||||||||
|
||||||||
char * vocab[32000]; | ||||||||
|
||||||||
void main_loop(void * dummy) { | ||||||||
// forward the transformer to get logits for the next token | ||||||||
transformer(token, pos, &config, &state, &weights); | ||||||||
|
||||||||
// sample the next token | ||||||||
if(temperature == 0.0f) { | ||||||||
// greedy argmax sampling | ||||||||
next = argmax(state.logits, config.vocab_size); | ||||||||
} else { | ||||||||
time_t current_time; | ||||||||
time(¤t_time); | ||||||||
srand((unsigned int)current_time); | ||||||||
// apply the temperature to the logits | ||||||||
for (int q=0; q<config.vocab_size; q++) { state.logits[q] /= temperature; } | ||||||||
// apply softmax to the logits to get the probabilities for next token | ||||||||
softmax(state.logits, config.vocab_size); | ||||||||
// we now want to sample from this distribution to get the next token | ||||||||
next = sample(state.logits, config.vocab_size); | ||||||||
} | ||||||||
//printf("%d\n", next); | ||||||||
printf("%s\n", vocab[next]); | ||||||||
fflush(stdout); | ||||||||
|
||||||||
// read in the config header | ||||||||
Config config; | ||||||||
FILE *file = fopen(checkpoint, "rb"); | ||||||||
if (!file) { | ||||||||
printf("Unable to open file!"); | ||||||||
return 1; | ||||||||
} | ||||||||
fread(&config, sizeof(Config), 1, file); | ||||||||
// advance forward | ||||||||
token = next; | ||||||||
pos++; | ||||||||
} | ||||||||
|
||||||||
// create and init the Transformer | ||||||||
TransformerWeights weights; | ||||||||
malloc_weights(&weights, &config); | ||||||||
checkpoint_init_weights(&weights, &config, file); | ||||||||
fclose(file); | ||||||||
#ifdef __EMSCRIPTEN__ | ||||||||
#include "emscripten.h" | ||||||||
#endif | ||||||||
|
||||||||
// create and init the application RunState | ||||||||
RunState state; | ||||||||
malloc_run_state(&state, &config); | ||||||||
int main() { | ||||||||
setbuf(stdout, NULL); // disable stdout buffering | ||||||||
|
||||||||
|
||||||||
time_t current_time; | ||||||||
time(¤t_time); | ||||||||
srand((unsigned int)current_time); | ||||||||
|
||||||||
// the current position we are in | ||||||||
int next; | ||||||||
int token = 1; // 1 = BOS token in Llama-2 sentencepiece | ||||||||
int pos = 0; | ||||||||
while (pos < config.seq_len) { | ||||||||
|
||||||||
// forward the transformer to get logits for the next token | ||||||||
transformer(token, pos, &config, &state, &weights); | ||||||||
|
||||||||
// sample the next token | ||||||||
if(temperature == 0.0f) { | ||||||||
// greedy argmax sampling | ||||||||
next = argmax(state.logits, config.vocab_size); | ||||||||
} else { | ||||||||
// apply the temperature to the logits | ||||||||
for (int q=0; q<config.vocab_size; q++) { state.logits[q] /= temperature; } | ||||||||
// apply softmax to the logits to get the probabilities for next token | ||||||||
softmax(state.logits, config.vocab_size); | ||||||||
// we now want to sample from this distribution to get the next token | ||||||||
next = sample(state.logits, config.vocab_size); | ||||||||
// model | ||||||||
{ | ||||||||
checkpoint = "model.bin"; | ||||||||
FILE *file = fopen(checkpoint, "rb"); | ||||||||
if (!file) { | ||||||||
printf("Unable to open file!"); | ||||||||
} | ||||||||
printf("%d\n", next); | ||||||||
fread(&config, sizeof(Config), 1, file); | ||||||||
|
||||||||
// advance forward | ||||||||
token = next; | ||||||||
pos++; | ||||||||
// create and init the Transformer | ||||||||
malloc_weights(&weights, &config); | ||||||||
checkpoint_init_weights(&weights, &config, file); | ||||||||
fclose(file); | ||||||||
} | ||||||||
|
||||||||
free_run_state(&state, &config); | ||||||||
free_weights(&weights, &config); | ||||||||
return 0; | ||||||||
// vocab | ||||||||
{ | ||||||||
FILE *file = fopen("vocab.bin", "r"); | ||||||||
if (!file) { | ||||||||
printf("Unable to open file!"); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
} | ||||||||
|
||||||||
int len; | ||||||||
for (int i = 0; i < 32000; i++) { | ||||||||
fread(&len, sizeof(int), 1, file); | ||||||||
vocab[i] = (char *)malloc(len + 1); | ||||||||
fread(vocab[i], len, 1, file); | ||||||||
vocab[i][len] = '\0'; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
malloc_run_state(&state, &config); | ||||||||
|
||||||||
#ifdef __EMSCRIPTEN__ | ||||||||
emscripten_set_main_loop_arg(main_loop, NULL, 0, 1); | ||||||||
#else | ||||||||
while (1) { | ||||||||
main_loop(NULL); | ||||||||
} | ||||||||
#endif | ||||||||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
|
||
<title>llama2.c</title> | ||
|
||
<style> | ||
textarea { | ||
width: 300px; | ||
height: 400px; | ||
font-family: monospace; | ||
font-size: 12px; | ||
background: #fff; | ||
color: #000; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h3><a href="https://github.com/karpathy/llama2.c">llama2.c</a> compiled with Emscripten to run in a web page</h3> | ||
|
||
<p id="msg">loading, please wait ...</p> | ||
<p id="progress"></p> | ||
|
||
<textarea id="output"></textarea> | ||
|
||
<script> | ||
"use strict"; | ||
|
||
function onStdout(text) { | ||
text = Array.prototype.slice.call(arguments).join(' '); | ||
document.getElementById('output').innerHTML += text; | ||
} | ||
|
||
function onRuntimeInitialized() { | ||
document.getElementById('msg').innerHTML = "generating ..."; | ||
} | ||
|
||
function onProgress(text) { | ||
document.getElementById('progress').innerHTML = text; | ||
} | ||
|
||
var Module = { }; | ||
|
||
Module['onRuntimeInitialized'] = onRuntimeInitialized; | ||
Module['setStatus'] = onProgress; | ||
Module['print'] = onStdout; | ||
</script> | ||
<script src="llama2.js"></script> | ||
</body> | ||
</html> |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.