Skip to content

Use shallow copy in _merge_sample_pair#67

Open
nightlessbaron wants to merge 2 commits into
prodfrom
fix/merge-samples-shallow-copy
Open

Use shallow copy in _merge_sample_pair#67
nightlessbaron wants to merge 2 commits into
prodfrom
fix/merge-samples-shallow-copy

Conversation

@nightlessbaron

@nightlessbaron nightlessbaron commented Jul 3, 2026

Copy link
Copy Markdown

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.

@nightlessbaron nightlessbaron requested a review from a team July 3, 2026 08:44
@odp

odp commented Jul 3, 2026

Copy link
Copy Markdown

👀

py-spy on a live run showed the rollout event loop pinned in deepcopy via merge_samples

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