Repository navigation
MAINT: P311-19 hash pinned classifier datasets with hashlib.file_digest - #3392
Open
ARJUN K (arjun-k047) wants to merge 1 commit into
Open
ARJUN K (arjun-k047) wants to merge 1 commit into
ARJUN K (arjun-k047) wants to merge 1 commit into
Conversation
compute_dataset_hashes() on LocalViolenceClassifierScorer and LocalRefusalClassifierScorer hashed each pinned training CSV by reading the whole file into memory first. It now streams each file through hashlib.file_digest, so the digest no longer needs the full contents in memory. The returned mapping is unchanged. _load_training_rows() keeps hashing the bytes it already read, because it parses the same bytes it verifies; reading the file a second time would split the check from the content used for training. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
|
@microsoft-github-policy-service agree |
4 of 25 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Addresses P311-19 from #3166.
compute_dataset_hashes()onLocalViolenceClassifierScorerandLocalRefusalClassifierScorerread each pinned training CSV fully into memory before hashing it. It now streams each file through
hashlib.file_digest, so hashing no longer holds the whole file in memory. The returneddict[str, str]is unchanged.Hashing a 64 MiB file: peak Python memory 64.0 MiB before, 0.4 MiB after. The shipped CSVs are
small, so the benefit matters mainly for larger files.
Sites investigated and left unchanged:
_load_training_rows()in both scorers hashes the bytes it then parses. Hashing separately wouldread the file twice and decouple the verified bytes from the trained-on bytes.
LocalFileSurfaceSource._read_entryis a bounded, cancellable read loop that retains content(excluded by the item's own guidance).
DataTypeSerializer.get_sha256andMemoryInterfacemedia digests run on async paths throughstorage I/O that may be remote, and reuse the content.
FileDocumentStorage._compute_versionhashes bytes the caller already holds.Tests and Documentation
test_compute_dataset_hashes_reads_current_filesto both scorer test modules: it writes~700 KB files (larger than
file_digest's internal buffer) under a patched dataset path and checksthe result against
hashlib.sha256of the same bytes. Existing pin tests still pass. These testsalso pass on main, since this is a refactor; they guard that the returned hashes are unchanged.
uv run -m pytest tests/unit/score/test_local_violence_classifier_scorer.py tests/unit/score/test_local_refusal_classifier_scorer.py:165 passed (main: 163 passed).
uv run pre-commit run --files <changed files>: all hooks passed.AI assistance: I used Claude Code (Claude Opus 5.5) to find the hashing call sites, draft the
change and tests, and run the checks. I reviewed every changed line, the reasoning for each site
left unchanged, and the test results, and I can explain the change.