Skip to content
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
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 85 additions & 65 deletions run.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(&current_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(&current_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!");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
printf("Unable to open file!");
printf("Unable to open file!");
return 1;

}
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!");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
printf("Unable to open file!");
printf("Unable to open file!");
return 1;

}

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
}
52 changes: 52 additions & 0 deletions web/index.html
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>