-
Notifications
You must be signed in to change notification settings - Fork 886
<EXPERIMENTAL - DO NOT REVIEW> DYNAMIC_UNBOUND support for portable runtime: lazy KV cache allocation #18350
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
Draft
psiddh
wants to merge
1
commit into
main
Choose a base branch
from
dynamic_unbound_kv_cache
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.
Draft
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| from typing import List, Optional | ||
|
|
||
| import torch | ||
| from executorch.exir.pass_base import ExportPass, PassResult | ||
| from executorch.exir.schema import TensorShapeDynamism | ||
| from executorch.exir.tensor import TensorSpec | ||
|
|
||
|
|
||
| class MarkDynamicUnboundPass(ExportPass): | ||
| """ | ||
| Marks mutable buffer TensorSpecs matching given name patterns as | ||
| DYNAMIC_UNBOUND. This causes the memory planner to skip them (no | ||
| allocation_info in the flatbuffer), and the runtime will allocate their | ||
| memory lazily via DynamicAllocator. | ||
|
|
||
| Typical usage: mark KV cache buffers so they start unallocated and grow | ||
| on demand, avoiding the full upfront memory cost of max_context_length. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| name_patterns: Optional[List[str]] = None, | ||
| ) -> None: | ||
| super().__init__() | ||
| self.name_patterns = name_patterns or ["k_cache", "v_cache"] | ||
|
|
||
| def call(self, graph_module: torch.fx.GraphModule) -> PassResult: | ||
| modified = False | ||
| for node in graph_module.graph.nodes: | ||
| if node.op != "placeholder": | ||
| continue | ||
| spec = node.meta.get("spec") | ||
| if not isinstance(spec, TensorSpec): | ||
| continue | ||
| if not spec.const: | ||
| # Only process mutable buffers (const=False means mutable). | ||
| name = node.name | ||
| if any(pattern in name for pattern in self.name_patterns): | ||
| spec.shape_dynamism = TensorShapeDynamism.DYNAMIC_UNBOUND | ||
| modified = True | ||
| return PassResult(graph_module, modified) |
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
|
|
||
| namespace executorch { | ||
| namespace runtime { | ||
|
|
||
| /** | ||
| * Interface for dynamic memory allocation used by DYNAMIC_UNBOUND tensors. | ||
| * | ||
| * Tensors marked as DYNAMIC_UNBOUND have their memory allocated lazily at | ||
| * runtime rather than at load time. This interface allows plugging in custom | ||
| * allocation strategies (e.g., page-aligned, tracking, virtual memory). | ||
| */ | ||
| class DynamicAllocator { | ||
| public: | ||
| virtual ~DynamicAllocator() = default; | ||
|
|
||
| /** | ||
| * Allocate memory. | ||
| * | ||
| * @param[in] size Minimum number of bytes to allocate. | ||
| * @param[in] alignment Required alignment of the returned pointer. | ||
| * @param[out] actual_size If non-null, receives the actual allocation size | ||
| * (may be larger than requested, e.g., due to growth policy). | ||
| * @returns Pointer to allocated memory, or nullptr on failure. | ||
| */ | ||
| virtual void* allocate( | ||
| size_t size, | ||
| size_t alignment, | ||
| size_t* actual_size) = 0; | ||
|
|
||
| /** | ||
| * Reallocate memory, potentially growing the buffer. | ||
| * | ||
| * The allocator may implement a growth policy (e.g., 2x) so that the | ||
| * actual allocation exceeds new_size. Old data up to min(old_size, new_size) | ||
| * is preserved. | ||
| * | ||
| * @param[in] ptr Pointer previously returned by allocate() or reallocate(), | ||
| * or nullptr (in which case this behaves like allocate()). | ||
| * @param[in] old_size Size of the existing allocation at ptr. | ||
| * @param[in] new_size Minimum number of bytes needed. | ||
| * @param[in] alignment Required alignment of the returned pointer. | ||
| * @param[out] actual_size If non-null, receives the actual allocation size. | ||
| * @returns Pointer to reallocated memory, or nullptr on failure. On failure, | ||
| * the old allocation at ptr remains valid. | ||
| */ | ||
| virtual void* reallocate( | ||
| void* ptr, | ||
| size_t old_size, | ||
| size_t new_size, | ||
| size_t alignment, | ||
| size_t* actual_size) = 0; | ||
|
|
||
| /** | ||
| * Free memory previously returned by allocate() or reallocate(). | ||
| * | ||
| * @param[in] ptr Pointer to free. May be nullptr (no-op). | ||
| */ | ||
| virtual void free(void* ptr) = 0; | ||
| }; | ||
|
|
||
| } // namespace runtime | ||
| } // namespace executorch |
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.
is this because we do actually touch the full memory during attention?