Skip to content

Count uncapped words without building a document matrix - #898

Open
snygginghani wants to merge 1 commit into
huggingface:mainfrom
snygginghani:perf/count-words-without-document-matrix
Open

snygginghani wants to merge 1 commit into
huggingface:mainfrom
snygginghani:perf/count-words-without-document-matrix

Conversation

@snygginghani

Copy link
Copy Markdown

Summary

The default word_count path builds a sparse document-term matrix, then only reads its sum and number of columns. Use the same scikit-learn analyzer and count words and distinct tokens directly. A specified max_vocab still uses CountVectorizer's feature selection.

Benchmark

On 33,379 text entries from the SQuAD v2 development set, median public .compute() time fell from 316.58 ms to 203.64 ms across nine alternating runs. Peak traced allocations fell from 13.41 MiB to 6.63 MiB, measured separately. Both revisions reported 328,746 words and 17,939 unique words.

For reproduction, create a list of each paragraph's context, append all questions, then append all annotated answer strings in dataset order. Pass it to evaluate.load("./measurements/word_count", module_type="measurement", keep_in_memory=True).compute(data=texts). Tested on Windows 11, Python 3.13.3, datasets 5.2.0 and scikit-learn 1.6.1.

python -m pytest tests/test_word_count.py -q passed all three cases, covering default and capped counts, case folding, punctuation and empty vocabulary. Black, isort, flake8 and git diff --check passed.

AI assistance was used to prepare the patch, tests and benchmark.

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.

1 participant