Forward reduce_batch_size_fn through the find_executable_batch_size decorator form - #4160
Open
vineethsaivs wants to merge 1 commit into
Open
Conversation
find_executable_batch_size supports being used as a decorator with arguments,
which is the form its own docstring example uses. That path goes through
if function is None:
return functools.partial(find_executable_batch_size,
starting_batch_size=starting_batch_size)
and reduce_batch_size_fn is not forwarded, so a custom back-off is bound on the
first call, dropped by the partial, and replaced by the default 0.9 multiplier on
the second.
Starting at 256 with a halving strategy, the decorator tries 26 batch sizes
instead of 5, which is 21 extra out-of-memory-and-retry cycles per search. Both
forms now agree.
Signed-off-by: Vineeth Sai <vineethsai4444@gmail.com>
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.
Problem
find_executable_batch_sizesupports being used as a decorator with arguments, and that is the form its own docstring example uses. That path goes through:reduce_batch_size_fnis not forwarded. It is bound on the first call, dropped by the partial, and on the second call it isNoneagain, so the default multiply-by-0.9 back-off runs instead.Passing the function directly,
find_executable_batch_size(train, starting_batch_size=256, reduce_batch_size_fn=halve), already works, so the two documented forms disagree.Every one of those extra attempts is a real out-of-memory-and-retry cycle: allocate, fail,
clear_device_cache(garbage_collection=True), try again. A halving strategy from 256 should cost 5 of them and costs 26. Nothing errors and nothing warns, so the only symptom is a slow start-up and a batch size that walks down in 10% steps rather than the steps the caller asked for.reduce_batch_size_fnwas added in #3071 and documented in #4051; only the forwarding through the partial was missed.Fix
Forward the third argument.
Test
Three cases added to
tests/test_memory_utils.py, next to the existingfind_executable_batch_sizetests, which all use the decorator form withstarting_batch_sizeonly:pytest tests/test_memory_utils.pygives 2 failed / 8 passed againstmainand 10 passed with this change, on CPU. Both runs usesrc/from a tree checked out at16cb6eb, verified by printingaccelerate.utils.memory.__file__, because an editable install elsewhere on the machine will otherwise shadow the checkout and make the before and after runs test the same code.ruff checkandruff format --checkare clean on both changed files. Note thatruff --fixalso wants to remove a pre-existingpassattests/test_memory_utils.py:143; I put it back so this diff is only the change described here.