feat: speed up first_fit in sequence packing with a segment tree#15563
Open
fangwei123456 wants to merge 2 commits intoNVIDIA-NeMo:mainfrom
Open
feat: speed up first_fit in sequence packing with a segment tree#15563fangwei123456 wants to merge 2 commits intoNVIDIA-NeMo:mainfrom
first_fit in sequence packing with a segment tree#15563fangwei123456 wants to merge 2 commits intoNVIDIA-NeMo:mainfrom
Conversation
Signed-off-by: wei.fang <wei.fang@miromind.ai>
first_fit in sequence packing with a segment treefirst_fit in sequence packing with a segment tree
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.
Speed up
first_fitin sequence packing with a segment treeWhat does this PR do?
Replaces the O(n) linear scan in
find_first_bin_that_fitswith an O(log n) segment tree, reducing the overallfirst_fitpacking complexity from O(n^2) to O(n log n). This significantly speeds up sequence packing for large datasets.A
backendparameter is added tofirst_fitwith two options:"segment_tree"(default) — uses a segment tree for O(log n) per-query lookup"naive"— uses the original O(n) linear scanThe function signature and return type remain backward-compatible. Downstream callers (
first_fit_decreasing,first_fit_shuffle,create_packing_strategy,fill_packing_strategy) require no changes.This modification is particularly crucial for processing large datasets: in our own experiments, the time required to process 50GB of data was reduced from two and a half hours to just one minute.
Changes
nemo/utils/sequence_packing_utils.py_SegmentTreeclass: a 1-indexed flat-array segment tree that stores per-bin remaining capacity, with internal nodes tracking the max of their children. Supportsopen_bin,query(leftmost bin with capacity >= s), andupdatein O(log n).backendparameter ("segment_tree"|"naive") tofirst_fit._first_fit_naiveand_first_fit_segment_treeas the two backend implementations.find_first_bin_that_fitswith a deprecation note for backward compatibility.tests/utils/test_first_fit_backends.py(new)Performance
Benchmarked on 10,000 random sequences (lengths 1–500, pack_size=1024):
naivesegment_treeTests
All 16 tests pass, including correctness (both backends match) and performance (segment tree > 2x faster).
Signed-off-by: Wei Fang wei.fang@miromind.ai