minicpm5-2b-webgpu / src /token-stream.js
ProCreations's picture
Dedicated WebGPU runtime, DSpark acceleration, and automatic tool repair
c51a3e7 verified
Raw History Blame Contribute Delete
827 Bytes
// Incremental ByteLevel decoding keeps long generations linear in output length.
export class TokenStream {
constructor(tokenizer) {
this.tokenizer = tokenizer;
this.decoder = new TextDecoder("utf-8", { ignoreBOM: true });
this.added = new Map(
tokenizer.tokenizer.added_tokens.map((t) => [t.id, t.content]),
);
this.bytes = tokenizer.decoder.byte_decoder;
this.cache = new Map();
}
push(id) {
if (this.added.has(id)) return this.decoder.decode() + this.added.get(id);
let bytes = this.cache.get(id);
if (!bytes) {
bytes = Uint8Array.from(
[...this.tokenizer.model.vocab[id]].map((c) => this.bytes[c]),
);
this.cache.set(id, bytes);
}
return this.decoder.decode(bytes, { stream: true });
}
flush() {
return this.decoder.decode();
}
}