fix(mtmd): clean up successful MTMD results after sibling decode failure - #177
Merged
Merged
Conversation
cleanup array at Concurrent Media Decoding.cleanup array at Concurrent Media Decoding.
cleanup array at Concurrent Media Decoding.cleanup array on decoding error.
Owner
|
Could you provide details on how to reproduce the issue you mentioned? |
Author
Reproduce issue"""
Reproduce issue of memory leak
when MTMD has failed with one invalid/multiple valid images
"""
from llama_cpp import Llama
import base64
from PIL import UnidentifiedImageError
import psutil
import os
def image_data_uri(raw: bytes, mime: str = "image/png") -> str:
encoded = base64.b64encode(raw).decode("ascii")
return f"data:{mime};base64,{encoded}"
def get_memory_mb() -> float:
pid = os.getpid()
process = psutil.Process(pid)
memory_bytes = process.memory_info().rss
memory_mb = memory_bytes / (1024 * 1024)
return memory_mb
# Model and multimodal projection paths
MODEL_PATH = r"./gemma-4-E2B-it-Q4_K_M.gguf"
MMPROJ_PATH = r"./mmproj-BF16.gguf"
invalid_image = b"\x89PNG\r\n\x1a\n"
valid_image_path = "images/correct.png"
llm = Llama(
model_path=MODEL_PATH,
mmproj_path=MMPROJ_PATH,
n_gpu_layers=-1,
n_ctx=10240,
verbose=True,
verbosity=2,
chat_handler_kwargs={
"verbose": False,
},
)
llm_messages = [
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": image_data_uri(invalid_image),
},
},
*[
{
"type": "image_url",
"image_url": {
"url": valid_image_path,
},
}
for _ in range(5)
],
{
"type": "text",
"text": "Describe each images in one sentence.",
},
],
}
]
attemps = 100
previous_mb = get_memory_mb()
try:
for i in range(attemps):
prefix = f"[{i+1}/{attemps}]"
print(f"{prefix} create_chat_completion", flush=True)
try:
response = llm.create_chat_completion(
messages=llm_messages,
)
except UnidentifiedImageError as exc:
print(f"{prefix} expected error: {exc}", flush=True)
except Exception:
raise
else:
raise AssertionError("Invalid image unexpectedly succeeded")
finally:
current_mb = get_memory_mb()
print(
f"{prefix} rss={current_mb:.2f} MB "
f"delta={(current_mb - previous_mb):.2f} MB",
flush=True
)
previous_mb = current_mb
finally:
print(f"before close: {get_memory_mb():.2f} MB", flush=True)
llm.close()
print(f"after close: {get_memory_mb():.2f} MB", flush=True)From repo: https://github.com/craftingmod/llama_cpp_python_resetissue/blob/main/reproduce2.py SummaryRequest MTMD messages with 1 invalid & 5 valid images 100 times and
ResultExpected memory usage(After fix) Actual memory usage(Before fix) |
cleanup array on decoding error.
Owner
|
I'll probably check it later. |
Owner
|
LGTM |
Owner
|
I’ve re-optimized this part of the logic; you might want to see how your example performs with it. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When a media is failed to decode in
ThreadPoolExecutor, other succeed media is leaked from cleanup list.This PR fixes leaking succeed media from cleanup list.
AI Disclosure
Codex(GPT 5.6 Luna) is used to analyze and sugessting fix, but I manually reviewed and confirmed entire changes.