Use shallow copy in _merge_sample_pair#67
Open
nightlessbaron wants to merge 2 commits into
Open
Conversation
…merging quadratic
|
👀
|
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.
merge_samples folds per-turn samples pairwise, and _merge_sample_pair deepcopies the accumulator on every merge. The accumulator's token/logprob/loss-mask lists grow toward full trajectory length, so a 70-turn 150k-token trajectory spends 5-10s of GIL-held CPU just copying lists, and a 4k-sample rollout spends hours in the drain (py-spy on a live run showed the rollout event loop pinned in deepcopy via merge_samples).
A shallow copy is enough here: the function only reassigns fields on its local copies and builds the merged Sample from fresh concatenations, so the inputs' lists are never mutated. 70 merges drop from ~7s to <1ms.
Second commit applies the same treatment to _compute_sample_from_openai_record, which deepcopied the input sample once per record (~100x per trajectory, dominated by the task metadata dict). Only the three fields mutated in place downstream (metadata, weight_versions, prefix_cache_info) get fresh copies; everything else is reassigned right after.