Skip to content

Commit 2e5365d

Browse files
committed
LanguageModel: Update llama2.c-emscripten build to include support for 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).
1 parent 1ab12bf commit 2e5365d

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

src/LanguageModel/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class LanguageModel extends EventEmitter {
1919
modelUrl: '', // if set, model.bin will be preloaded from provided URL (assumed to be embedded in llama2.data if not)
2020
tokenizerUrl: '', // if set, tokenizer.bin will be preloaded from provided URL (assumed to be embedded in llama2.data if not)
2121
steps: 0, // how many tokens to generate (defaults to model's maximum)
22-
temperature: 0.9, // 0.0 = (deterministic) argmax sampling, 1.0 = baseline
22+
temperature: 1.0, // 0.0 = (deterministic) argmax sampling, 1.0 = baseline, don't set higher
23+
topp: 0.9, // p value in top-p (nucleus) sampling, 0 = off
2324
stopOnBosOrEos: true, // stop when encountering beginning-of-sequence or end-of-sequence token
2425
};
2526

@@ -164,6 +165,7 @@ class LanguageModel extends EventEmitter {
164165
if (typeof optionsOrCb === 'object') {
165166
this.options.steps = (typeof optionsOrCb.steps === 'number') ? optionsOrCb.steps : this.options.steps;
166167
this.options.temperature = (typeof optionsOrCb.temperature === 'number') ? optionsOrCb.temperature : this.options.temperature;
168+
this.options.topp = (typeof optionsOrCb.topp === 'number') ? optionsOrCb.topp : this.options.topp;
167169
this.options.stopOnBosOrEos = (typeof optionsOrCb.stopOnBosOrEos == 'boolean') ? optionsOrCb.stopPropagation : this.options.stopOnBosOrEos;
168170
}
169171
if (typeof cb === 'function') {
@@ -179,7 +181,7 @@ class LanguageModel extends EventEmitter {
179181
this.promiseResolve(this.text);
180182
}
181183

182-
await this.llama2.ccall('set_parameters', null, [ 'number', 'number' ], [ this.options.temperature, this.options.steps ]);
184+
await this.llama2.ccall('set_parameters', null, [ 'number', 'number', 'number' ], [ this.options.temperature, this.options.topp, this.options.steps ]);
183185

184186
this.prompt = prompt;
185187
this.text = '';
@@ -220,6 +222,7 @@ class LanguageModel extends EventEmitter {
220222
if (typeof optionsOrCb === 'object') {
221223
this.options.steps = (typeof optionsOrCb.steps === 'number') ? optionsOrCb.steps : this.options.steps;
222224
this.options.temperature = (typeof optionsOrCb.temperature === 'number') ? optionsOrCb.temperature : this.options.temperature;
225+
this.options.topp = (typeof optionsOrCb.topp === 'number') ? optionsOrCb.topp : this.options.topp;
223226
this.options.stopOnBosOrEos = (typeof optionsOrCb.stopOnBosOrEos == 'boolean') ? optionsOrCb.stopPropagation : this.options.stopOnBosOrEos;
224227
}
225228
if (typeof cb === 'function') {
@@ -235,7 +238,7 @@ class LanguageModel extends EventEmitter {
235238
this.promiseResolve(this.text);
236239
}
237240

238-
await this.llama2.ccall('set_parameters', null, [ 'number', 'number' ], [ this.options.temperature, this.options.steps ]);
241+
await this.llama2.ccall('set_parameters', null, [ 'number', 'number', 'number' ], [ this.options.temperature, this.options.topp, this.options.steps ]);
239242

240243
this.prompt = prompt;
241244
this.text = '';

src/LanguageModel/llama2.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/LanguageModel/llama2.wasm

2.51 KB
Binary file not shown.

0 commit comments

Comments
 (0)