Skip to content

Commit

Permalink
LanguageModel: Update llama2.c-emscripten build to include support fo…
Browse files Browse the repository at this point in the history
…r top-p sampling (used by default)

From @karpathy's commit message: Quick note on sampling, the recommendation for good results is to use `-t 1.0 -p 0.9`, i.e. top-p sampling at 0.9 with temperature 1.0 (this is the default). To control the diversity of samples use either the temperature (i.e. vary `-t` between 0 and 1 and keep top-p off with `-p 0`) or the top-p value (i.e. vary `-p` between 0 and 1 and keep `-t 1`), but not both. Nice explainers on LLM sampling strategies include [this](https://peterchng.com/blog/2023/05/02/token-selection-strategies-top-k-top-p-and-temperature/), [this](https://docs.cohere.com/docs/controlling-generation-with-top-k-top-p) or [this](https://huggingface.co/blog/how-to-generate).
  • Loading branch information
gohai committed Aug 9, 2023
1 parent 1ab12bf commit 2e5365d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/LanguageModel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class LanguageModel extends EventEmitter {
modelUrl: '', // if set, model.bin will be preloaded from provided URL (assumed to be embedded in llama2.data if not)
tokenizerUrl: '', // if set, tokenizer.bin will be preloaded from provided URL (assumed to be embedded in llama2.data if not)
steps: 0, // how many tokens to generate (defaults to model's maximum)
temperature: 0.9, // 0.0 = (deterministic) argmax sampling, 1.0 = baseline
temperature: 1.0, // 0.0 = (deterministic) argmax sampling, 1.0 = baseline, don't set higher
topp: 0.9, // p value in top-p (nucleus) sampling, 0 = off
stopOnBosOrEos: true, // stop when encountering beginning-of-sequence or end-of-sequence token
};

Expand Down Expand Up @@ -164,6 +165,7 @@ class LanguageModel extends EventEmitter {
if (typeof optionsOrCb === 'object') {
this.options.steps = (typeof optionsOrCb.steps === 'number') ? optionsOrCb.steps : this.options.steps;
this.options.temperature = (typeof optionsOrCb.temperature === 'number') ? optionsOrCb.temperature : this.options.temperature;
this.options.topp = (typeof optionsOrCb.topp === 'number') ? optionsOrCb.topp : this.options.topp;
this.options.stopOnBosOrEos = (typeof optionsOrCb.stopOnBosOrEos == 'boolean') ? optionsOrCb.stopPropagation : this.options.stopOnBosOrEos;
}
if (typeof cb === 'function') {
Expand All @@ -179,7 +181,7 @@ class LanguageModel extends EventEmitter {
this.promiseResolve(this.text);
}

await this.llama2.ccall('set_parameters', null, [ 'number', 'number' ], [ this.options.temperature, this.options.steps ]);
await this.llama2.ccall('set_parameters', null, [ 'number', 'number', 'number' ], [ this.options.temperature, this.options.topp, this.options.steps ]);

this.prompt = prompt;
this.text = '';
Expand Down Expand Up @@ -220,6 +222,7 @@ class LanguageModel extends EventEmitter {
if (typeof optionsOrCb === 'object') {
this.options.steps = (typeof optionsOrCb.steps === 'number') ? optionsOrCb.steps : this.options.steps;
this.options.temperature = (typeof optionsOrCb.temperature === 'number') ? optionsOrCb.temperature : this.options.temperature;
this.options.topp = (typeof optionsOrCb.topp === 'number') ? optionsOrCb.topp : this.options.topp;
this.options.stopOnBosOrEos = (typeof optionsOrCb.stopOnBosOrEos == 'boolean') ? optionsOrCb.stopPropagation : this.options.stopOnBosOrEos;
}
if (typeof cb === 'function') {
Expand All @@ -235,7 +238,7 @@ class LanguageModel extends EventEmitter {
this.promiseResolve(this.text);
}

await this.llama2.ccall('set_parameters', null, [ 'number', 'number' ], [ this.options.temperature, this.options.steps ]);
await this.llama2.ccall('set_parameters', null, [ 'number', 'number', 'number' ], [ this.options.temperature, this.options.topp, this.options.steps ]);

this.prompt = prompt;
this.text = '';
Expand Down
2 changes: 1 addition & 1 deletion src/LanguageModel/llama2.js

Large diffs are not rendered by default.

Binary file modified src/LanguageModel/llama2.wasm
Binary file not shown.

0 comments on commit 2e5365d

Please sign in to comment.