-
Notifications
You must be signed in to change notification settings - Fork 700
[ET-VK][AOT] Serialize constant tensors via NamedDataMap #13473
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
Merged
Merged
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ | |
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| import ctypes | ||
| import hashlib | ||
| import logging | ||
| import operator | ||
| from types import NoneType | ||
|
|
@@ -25,6 +27,7 @@ | |
| is_symint_node, | ||
| TensorRepr, | ||
| ) | ||
| from executorch.exir._serialize._named_data_store import NamedDataStore | ||
| from executorch.exir.backend.utils import DelegateMappingBuilder | ||
|
|
||
| from executorch.exir.tensor import TensorSpec | ||
|
|
@@ -56,6 +59,7 @@ def __init__( | |
| self.input_ids = [] | ||
| self.output_ids = [] | ||
| self.const_tensors = [] | ||
| self.named_data_store = NamedDataStore() | ||
|
|
||
| # Mapping from Node to VkValue id | ||
| self.node_to_value_ids = {} | ||
|
|
@@ -129,8 +133,36 @@ def get_param_tensor(self, node: Node) -> torch.Tensor: | |
| def maybe_add_constant_tensor(self, node: Node) -> int: | ||
| constant_id = -1 | ||
| if is_param_node(self.program, node): | ||
| constant_id = len(self.const_tensors) | ||
| self.const_tensors.append(self.get_param_tensor(node)) | ||
| tensor = self.get_param_tensor(node) | ||
|
|
||
| # Serialize tensor data to bytes | ||
| tensor = tensor.contiguous() | ||
| size = tensor.untyped_storage().nbytes() | ||
|
|
||
| if size > 0: | ||
| array_type = ctypes.c_char * size | ||
| array = ctypes.cast( | ||
| tensor.untyped_storage().data_ptr(), | ||
| ctypes.POINTER(array_type), | ||
| ).contents | ||
|
|
||
| # Generate SHA256 hash as the named key | ||
| tensor_bytes = bytes(array) | ||
| sha256_hash = hashlib.sha256(tensor_bytes) | ||
| named_key = sha256_hash.hexdigest() | ||
|
|
||
| # Add to named data store with 16-byte alignment (matching XNNPACK) | ||
|
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. +1 |
||
| self.named_data_store.add_named_data( | ||
| named_key, tensor_bytes, alignment=16 | ||
| ) | ||
|
|
||
| # Create VkBytes entry with named_key and set offset to indicate named data usage | ||
| constant_id = len(self.const_tensors) | ||
| self.const_tensors.append((named_key, size)) | ||
| else: | ||
| # Handle empty tensors | ||
| constant_id = len(self.const_tensors) | ||
| self.const_tensors.append(None) | ||
|
|
||
| return constant_id | ||
|
|
||
|
|
||
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
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.
we deduplicate the data in the pte using the value, and not the key right?