[Draft][Core] Refactor _prepare_model_input_tensors #5972
Closed
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.
NOTE: This PR will be rebased after the following PRs are merged: #4628 #5942.
Meanwhile, reviews and comments are welcome.
This PR refactors
_prepare_model_input_tensors
. Specifically, we introduceModelRunnerInputBuilder
mainly for logic isolation and modularization. Specifically,ModelRunnerInputBuilder
manages all processed input data, including token IDs, positions, sequence length, etc, in one place, and isolates the following logic:Note that the purpose of this PR is to enable follow-up refactoring and optimizations, so we don't expect an obvious performance improvement at this moment, although the following optimizations may be slightly helpful:
.extend()
.With this isolation, we could further have follow-up optimizations:
AttentionMetadata
to only include on-device tensors, and move all related logic fromModelRunnerInputBuilder
.for seq_id in seq_ids
inModelRunnerInputBuilder._add_decode_seq_group()
by leveraging tensor processing.for seq_group_metadata in seq_group_metadata_list
.