-
Notifications
You must be signed in to change notification settings - Fork 265
Multimodal improve #951
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
shihaobai
wants to merge
8
commits into
main
Choose a base branch
from
multimodal_improve
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.
+368
−42
Open
Multimodal improve #951
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d498aaf
vit and llm inference at a single process
691b89c
fix
8624e8f
fix
3e04c7f
remove print
06c38a0
fix
d21ccaa
Merge branch 'main' into multimodal_improve
2ab9dfc
Merge branch 'main' into multimodal_improve
shihaobai b0768e2
mutli infer detect need prefill objs
hiworldwzj 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
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,76 @@ | ||
from collections import OrderedDict | ||
from lightllm.utils.dist_utils import get_current_device_id | ||
|
||
|
||
class ImageCacheManager: | ||
def __init__(self): | ||
""" | ||
Initialize the image cache manager with a simple GPU cache and an LRU CPU cache. | ||
""" | ||
self._gpu_cache = dict() | ||
self._cpu_cache = OrderedDict() | ||
|
||
def set_max_size(self, max_size: int): | ||
""" | ||
Set the maximum number of items to keep in the CPU cache. | ||
:param max_size: Maximum number of items to keep in the CPU cache. | ||
""" | ||
if max_size <= 0: | ||
raise ValueError("max_size must be greater than 0") | ||
self._max_size = max_size | ||
|
||
def set_embed(self, uuid, embed): | ||
""" | ||
Store the embedding for the given uuid in the GPU cache. | ||
:param uuid: Unique identifier for the image | ||
:param embed: Embedding vector for the image (on GPU) | ||
""" | ||
self._gpu_cache[uuid] = embed | ||
|
||
def get_embed(self, uuid): | ||
""" | ||
Retrieve the embedding for the given uuid. Prefer GPU cache, | ||
otherwise return CPU cache and move to GPU (simulate .cuda()). | ||
:param uuid: Unique identifier for the image | ||
:return: Embedding vector (on GPU if possible, else move from CPU to GPU) | ||
""" | ||
if uuid in self._gpu_cache: | ||
return self._gpu_cache[uuid] | ||
elif uuid in self._cpu_cache: | ||
self._cpu_cache.move_to_end(uuid) | ||
embed = self._cpu_cache[uuid].cuda(get_current_device_id()) | ||
return embed | ||
return None | ||
|
||
def query_embed(self, uuid): | ||
""" | ||
Query if the embedding for the given uuid is in the cache. | ||
:param uuid: Unique identifier for the image | ||
:return: True if the embedding is in the cache, False otherwise | ||
""" | ||
return uuid in self._gpu_cache or uuid in self._cpu_cache | ||
|
||
def filter(self, uuid_list): | ||
""" | ||
Given a list of uuids, move their embeddings from GPU cache to CPU cache if present, | ||
and return a dict of those found in the cache and their embeddings (on CPU). | ||
:param uuid_list: List of uuids | ||
""" | ||
for uuid in uuid_list: | ||
if uuid in self._gpu_cache: | ||
embed_cpu = self._gpu_cache[uuid].cpu() | ||
# Move to CPU cache and remove from GPU cache | ||
self._gpu_cache.pop(uuid) | ||
if uuid in self._cpu_cache: | ||
self._cpu_cache.move_to_end(uuid) | ||
self._cpu_cache[uuid] = embed_cpu | ||
if len(self._cpu_cache) > self._max_size: | ||
self._cpu_cache.popitem(last=False) | ||
elif uuid in self._cpu_cache: | ||
self._cpu_cache.move_to_end(uuid) | ||
print(self._gpu_cache.keys()) | ||
print(self._cpu_cache.keys()) | ||
Comment on lines
+71
to
+72
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. |
||
return | ||
|
||
|
||
image_cache_manager = ImageCacheManager() |
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.
The
_max_size
attribute is used in thefilter
method but is not initialized in the__init__
method, which can lead to anAttributeError
iffilter()
is called beforeset_max_size()
.