-
Notifications
You must be signed in to change notification settings - Fork 406
feat: Allow for users / kv cache to add aliased I/O for inplace operations #4251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
narendasan
wants to merge
3
commits into
main
Choose a base branch
from
narendasan/aliased_io
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,7 +97,8 @@ TRTEngine::TRTEngine( | |
| bool requires_output_allocator, | ||
| const std::string& serialized_metadata, | ||
| const ResourceAllocationStrategy resource_allocation_strategy, | ||
| RuntimeSettings runtime_settings) | ||
| RuntimeSettings runtime_settings, | ||
| const std::unordered_map<std::string, AliasedIOSpec>& aliased_io) | ||
| : TRTEngine( | ||
| "deserialized_trt", | ||
| serialized_engine, | ||
|
|
@@ -109,7 +110,8 @@ TRTEngine::TRTEngine( | |
| requires_output_allocator, | ||
| serialized_metadata, | ||
| resource_allocation_strategy, | ||
| std::move(runtime_settings)) {} | ||
| std::move(runtime_settings), | ||
| aliased_io) {} | ||
|
|
||
| TRTEngine::TRTEngine(std::vector<std::string> serialized_info) | ||
| : TRTEngine( | ||
|
|
@@ -125,7 +127,8 @@ TRTEngine::TRTEngine(std::vector<std::string> serialized_info) | |
| (static_cast<bool>(std::stoi(serialized_info[RESOURCE_ALLOCATION_STRATEGY_IDX])) | ||
| ? ResourceAllocationStrategy::kDynamic | ||
| : ResourceAllocationStrategy::kStatic), | ||
| RuntimeSettings{}) { | ||
| RuntimeSettings{}, | ||
| deserialize_aliased_io(serialized_info[ALIASED_IO_IDX])) { | ||
| // Single visible marker that this engine was instantiated through the C++ runtime | ||
| // entry point (i.e. torch.classes.tensorrt.Engine), distinguishing it from the Python | ||
| // TRTEngine path. Tests look for this string in captured stderr to verify the | ||
|
|
@@ -148,7 +151,8 @@ TRTEngine::TRTEngine( | |
| bool requires_output_allocator, | ||
| const std::string& serialized_metadata, | ||
| const ResourceAllocationStrategy resource_allocation_strategy, | ||
| RuntimeSettings runtime_settings) { | ||
| RuntimeSettings runtime_settings, | ||
| const std::unordered_map<std::string, AliasedIOSpec>& aliased_io) { | ||
| this->runtime_cfg = TRTRuntimeConfig(std::move(runtime_settings)); | ||
| TORCHTRT_CHECK( | ||
| target_platform._platform != Platform::PlatformEnum::kUNKNOWN, | ||
|
|
@@ -280,9 +284,62 @@ TRTEngine::TRTEngine( | |
| } | ||
|
|
||
| has_dynamic_inputs = engine_has_dynamic_inputs(cuda_engine.get(), in_binding_names); | ||
| // Cache input binding metadata used by the runtime hot path, then reconstruct | ||
| // optimization-profile info from the TRT API so multi-profile selection works | ||
| // for any loaded engine. | ||
|
|
||
| // Store the build-time aliased_io map as the starting point. Then reconcile | ||
| // against ICudaEngine::getAliasedInputTensor — that API is the source of | ||
| // truth for KV-cache-style aliasing (TRT-enforced via IKVCacheUpdateLayer) | ||
| // and may report aliases the build-time path didn't record (e.g. for | ||
| // engines built outside Torch-TensorRT). User-declared aliases (kind=kUser) | ||
| // are preserved as-is since TRT doesn't know about them. | ||
| this->aliased_io = aliased_io; | ||
| for (const auto& out_name : this->out_binding_names) { | ||
| // TRT returns nullptr / empty string for non-aliased outputs; any thrown | ||
| // exception is a real error in the engine state and propagates. | ||
| const char* aliased_in = cuda_engine->getAliasedInputTensor(out_name.c_str()); | ||
| if (aliased_in == nullptr || aliased_in[0] == '\0') { | ||
| continue; | ||
| } | ||
| auto it = this->aliased_io.find(out_name); | ||
| if (it == this->aliased_io.end()) { | ||
| this->aliased_io[out_name] = AliasedIOSpec{std::string(aliased_in), AliasKind::kKVCacheUpdate}; | ||
| LOG_DEBUG("aliased_io reconciliation: discovered " << out_name << " -> " << aliased_in << " (kv_cache_update)"); | ||
| } else if (it->second.kind == AliasKind::kUser) { | ||
| // A kUser alias is declared by Torch-TensorRT, not TRT. If the engine | ||
| // also reports an alias for this output the two views should agree on the | ||
| // source input; keep the kUser kind either way so the provenance recorded | ||
| // at build time is not silently rewritten to kv_cache_update. | ||
| if (it->second.input_binding_name != std::string(aliased_in)) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How would the user and engine declare differently? |
||
| LOG_WARNING( | ||
| "aliased_io: user-declared alias for output " << out_name << " (input: " << it->second.input_binding_name | ||
| << ") disagrees with engine-reported input " << aliased_in | ||
| << "; keeping the user-declared value."); | ||
| } | ||
| } else if (it->second.input_binding_name != std::string(aliased_in)) { | ||
| LOG_WARNING( | ||
| "aliased_io: build-time map disagrees with engine for output " | ||
| << out_name << " (build: " << it->second.input_binding_name << ", engine: " << aliased_in | ||
| << "); using engine value."); | ||
| it->second = AliasedIOSpec{std::string(aliased_in), AliasKind::kKVCacheUpdate}; | ||
| } | ||
| // Validation: aliased outputs must not also require an output allocator, | ||
| // since aliasing requires the output shape to match the input's static | ||
| // shape, which is incompatible with the dynamic-allocation path. | ||
| TORCHTRT_CHECK( | ||
| !this->requires_output_allocator, | ||
| "Aliased output " << out_name | ||
| << " is incompatible with dynamic output allocator. Aliasing requires fixed output shape."); | ||
| } | ||
|
|
||
| // Precompute the set of input binding names that are the alias source of some | ||
| // output so the per-call input-setup loop can test membership in O(1). | ||
| this->aliased_input_binding_names.clear(); | ||
| for (const auto& kv : this->aliased_io) { | ||
| this->aliased_input_binding_names.insert(kv.second.input_binding_name); | ||
| } | ||
|
|
||
| // Cache input binding metadata used by the runtime hot path (depends on | ||
| // aliased_input_binding_names above), then reconstruct optimization-profile | ||
| // info from the TRT API so multi-profile selection works for any loaded engine. | ||
| this->setup_input_binding_infos(); | ||
| this->setup_optimization_profiles(); | ||
|
|
||
|
|
@@ -527,7 +584,8 @@ FlattenedState TRTEngine::__obj_flatten__() { | |
| std::tuple("requires_output_allocator", serialized_info[REQUIRES_OUTPUT_ALLOCATOR_IDX]), | ||
| std::tuple("target_platform", serialized_info[TARGET_PLATFORM_IDX]), | ||
| std::tuple("resource_allocation_strategy", serialized_info[RESOURCE_ALLOCATION_STRATEGY_IDX]), | ||
| std::tuple("requires_native_multidevice", serialized_info[REQUIRES_NATIVE_MULTIDEVICE_IDX])); | ||
| std::tuple("requires_native_multidevice", serialized_info[REQUIRES_NATIVE_MULTIDEVICE_IDX]), | ||
| std::tuple("aliased_io", serialized_info[ALIASED_IO_IDX])); | ||
| } | ||
|
|
||
| std::vector<std::string> TRTEngine::serialize() { | ||
|
|
@@ -553,6 +611,7 @@ std::vector<std::string> TRTEngine::serialize() { | |
| serialized_info[RESOURCE_ALLOCATION_STRATEGY_IDX] = | ||
| this->resource_allocation_strategy == ResourceAllocationStrategy::kDynamic ? "1" : "0"; | ||
| serialized_info[REQUIRES_NATIVE_MULTIDEVICE_IDX] = this->requires_native_multidevice ? "1" : "0"; | ||
| serialized_info[ALIASED_IO_IDX] = serialize_aliased_io(this->aliased_io); | ||
| // rank/world_size are runtime facts (may differ at load time); not serialized. | ||
| // RuntimeSettings are intentionally NOT serialized: they're per-engine, in-memory | ||
| // initialization values, not part of the engine's identity. | ||
|
|
@@ -571,7 +630,8 @@ void TRTEngine::setup_input_binding_infos() { | |
| input_binding_infos.push_back( | ||
| {name, | ||
| util::TRTDataTypeToScalarType(cuda_engine->getTensorDataType(name.c_str())), | ||
| cuda_engine->isShapeInferenceIO(name.c_str())}); | ||
| cuda_engine->isShapeInferenceIO(name.c_str()), | ||
| aliased_input_binding_names.find(name) != aliased_input_binding_names.end()}); | ||
| } | ||
| } | ||
|
|
||
|
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about kUser?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this code is only to detect IKVCacheUpdate bindings basically the internal under the hood loop back case. If its in the map then its a user binding which needs to be returned