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

fix memory leak in BPE #962

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

fix memory leak in BPE #962

wants to merge 1 commit into from

Conversation

tmm1
Copy link

@tmm1 tmm1 commented Oct 5, 2024

turns out this isn't much of a cache. just a memory leak in disguise.

> let {LlamaTokenizer} = await import('./src/transformers.js')
> var tok = await LlamaTokenizer.from_pretrained('TinyLlama/TinyLlama-1.1B-Chat-v0.4')
> tok.encode("very long user input ".repeat(10))
[
     1, 1407, 1472, 1404, 1881,  1407,
  1472, 1404, 1881, 1407, 1472,  1404,
  1881, 1407, 1472, 1404, 1881,  1407,
  1472, 1404, 1881, 1407, 1472,  1404,
  1881, 1407, 1472, 1404, 1881,  1407,
  1472, 1404, 1881, 1407, 1472,  1404,
  1881, 1407, 1472, 1404, 1881, 29871
]
> tok.model.cache
Map(1) {
  '▁very▁long▁user▁input▁very▁long▁user▁input▁very▁long▁user▁input▁very▁long▁user▁input▁very▁long▁user▁input▁very▁long▁user▁input▁very▁long▁user▁input▁very▁long▁user▁input▁very▁long▁user▁input▁very▁long▁user▁input▁' => [
    '▁very', '▁long', '▁user', '▁input',
    '▁very', '▁long', '▁user', '▁input',
    '▁very', '▁long', '▁user', '▁input',
    '▁very', '▁long', '▁user', '▁input',
    '▁very', '▁long', '▁user', '▁input',
    '▁very', '▁long', '▁user', '▁input',
    '▁very', '▁long', '▁user', '▁input',
    '▁very', '▁long', '▁user', '▁input',
    '▁very', '▁long', '▁user', '▁input',
    '▁very', '▁long', '▁user', '▁input',
    '▁'
  ]
}

@xenova
Copy link
Collaborator

xenova commented Nov 16, 2024

Hi there 👋 Indeed, this BPE cache is more useful to tokenizers that use a GPT-like pretokenizer:

import { AutoTokenizer } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.2';
    
const tok = await AutoTokenizer.from_pretrained('Xenova/gpt-4');
tok.encode("very long user input ".repeat(10));
console.log(tok.model.cache);

image

A similar note is present here:

https://github.com/huggingface/transformers/blob/13493215abceafc1653af88b045120014fb4c1fc/src/transformers/models/qwen2/tokenization_qwen2.py#L186-L190

However, it might be worth disabling it for LlamaTokenizer (and similar) or defining a max input length/max size/lru cache).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants