`FMComposedPrompt` still leaked by `stream_response()` and `SystemLanguageModel.token_count()` (follow-up to #17 / #18)
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 35/100
Rechercherichtung
Read src/apple_fm_sdk/session.py at _stream_response_basic(), src/apple_fm_sdk/core.py at SystemLanguageModel.token_count(), and src/apple_fm_sdk/prompt.py at _composed_prompt_from_prompt(). Then inspect tests/test_composed_prompt_cleanup.py and its fake native bindings. Done means the composed prompt is released on success, error, cancellation, and rejected components, and invalid streaming prompts raise to the caller.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Description
#18 releases the native composed prompt in the three respond() paths. Three other places that
build one with _composed_prompt_from_prompt() still never release it:
LanguageModelSession._stream_response_basic(), which builds the prompt inside the stream thread:
https://github.com/apple/python-apple-fm-sdk/blob/0918b60/src/apple_fm_sdk/session.py#L833SystemLanguageModel.token_count(), in the prompt branch:
https://github.com/apple/python-apple-fm-sdk/blob/0918b60/src/apple_fm_sdk/core.py#L440_composed_prompt_from_prompt()itself, when a component is rejected (PromptError,
ImagePromptError): the pointer is created first and never returned to the caller:
https://github.com/apple/python-apple-fm-sdk/blob/0918b60/src/apple_fm_sdk/prompt.py#L300-L319
Each call leaks one ComposedPrompt. With an ImageAttachment it also keeps one file descriptor
open, which is the symptom from #17.
A related effect of (1): because the prompt is built in the stream thread, an invalid prompt
raises there and the exception is lost. stream_response() then waits indefinitely and
asyncio.wait_for() cannot interrupt it:
Exception in thread Thread-1 (_start_stream):
apple_fm_sdk.prompt.PromptError: Unsupported prompt component type <class 'int'>, ...
(still waiting after 10 s, despite asyncio.wait_for(..., 3))
Reproduction
import asyncio, gc, os, tempfile
from pathlib import Path
from PIL import Image
import apple_fm_sdk as fm
def open_fds(): return len(os.listdir("/dev/fd"))
async def main():
img = Path(tempfile.mkdtemp()) / "leak.png"
Image.new("RGB", (32, 32), "white").save(img)
for _ in range(2): # warm-up, then measure
before = open_fds()
for _ in range(15):
async for _ in fm.LanguageModelSession().stream_response(["Reply with OK.", fm.ImageAttachment(img)]):
pass
gc.collect()
print(f"stream_response: open fds {before} -> {open_fds()}")
asyncio.run(main())
Results with 15 calls, after warm-up (macOS 27.2.1, M1, Python 3.14.6):
main @ 0918b60 |
with the fix below | |
|---|---|---|
respond() with image (fixed in #18) |
fds 7 → 7 | fds 7 → 7 |
stream_response() with image |
+15 fds per 15 calls (the snippet prints 22 -> 37; the warm-up round already leaked 15) |
+0 (7 -> 7) |
token_count() with a 200 KB prompt, 60 calls |
RSS +12.7 MB | RSS +1.6 MB |
stream_response(123) |
hangs | raises PromptError immediately |
Proposed fix
Same approach as #18:
_stream_response_basic(): build the composed prompt before starting the stream thread, and
release it in the existingfinallyblock, next to the stream pointer.PromptErrorthen
reaches the caller.token_count(): release the composed prompt in afinallyaround_token_count()._composed_prompt_from_prompt(): release the pointer before re-raising when a component is
rejected.
FMLanguageModelSessionStreamResponse and FMSystemLanguageModelTokenCountForPrompt read
.promptRepresentation synchronously, so releasing afterwards is safe.
The patch extends tests/test_composed_prompt_cleanup.py in the same style as #18 (faked native
bindings, no model needed). It adds 8 tests: success, error and early exit for streaming, the
invalid-prompt case, success, error and cancellation for token_count, and the rejected
component. All 8 fail on main (0918b60) and pass with the fix. The existing 12 tests are
unchanged and still pass. With the on-device model, test_streaming.py, test_token_count.py,
test_prompts.py, test_image_prompts.py, test_session.py and test_respond_options.py also pass
on the branch.
Branch with the fix and tests:
https://github.com/apple/python-apple-fm-sdk/compare/main...luiacuaniello:python-apple-fm-sdk:fix/composed-prompt-leak-stream-token-count
Note: PyPI still serves 0.2.1, which predates #18, so respond() leaks too for anyone who
installs from PyPI.
- Vorherrschende Sprache
- Python
- Sterne
- 1.4k
- Forks
- 95
- Ø Merge
- 1 Min.
- Gemergte PRs (30 T.)
- 1
Entwicklungsumgebung
Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus apple/python-apple-fm-sdk
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 90/100
apple/python-apple-fm-sdk#16 ·
-
Build fails with Command Line Tools only — Xcode.app should not be requiredEvtl. vergeben @siva-acv hat das vor 255 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
apple/python-apple-fm-sdk#6 · 2 Kommentare · 3 Reaktionen ·
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 28/100
apple/python-apple-fm-sdk#21 · 1 Kommentar ·
-
Add support for `LanguageModelFeedback` and `logFeedbackAttachment`Evtl. wieder frei @mkery hat das vor 256 Tagen übernommen, und es ist kein Pull Request offen. Offen
apple/python-apple-fm-sdk#5 · 1 Kommentar · 1 zugewiesene Person ·
Alle Issues in apple/python-apple-fm-sdk
Ähnliche Issues
-
bug triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
ultralytics/ultralytics#27258 ·
Maintainer antworten meist innerhalb von 1 Tag
-
author-page correction move-papers
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
acl-org/acl-anthology#10969 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 86/100
Maintainer antworten meist innerhalb von 1 Tag
-
Pin Python to 3.14 in the era5grib-parallel environment specificationEvtl. vergeben @atteggiani hat das heute übernommen. Offen
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 85/100
ACCESS-NRI/containerised-environments-infra#129 ·
Maintainer antworten meist innerhalb von 2 Tagen
-
priority middle
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 72/100
KATO-Hiro/AtCoderClans#13029 ·
Maintainer antworten meist innerhalb von 1 Tag