mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
perf(token_counter): find drawn PDF images with a byte scan instead of parsing every operator
pypdf parsed every operator of a page's content stream to find the images it draws, so a compressed page of a million operators took about 30 seconds to price for an OpenAI model. A linear scan of the decoded stream for name Do and inline images finds the same draws.
This commit is contained in:
parent
24c18e66d5
commit
6ac4e23fbd
2 changed files with 58 additions and 14 deletions
|
|
@ -3,11 +3,12 @@
|
|||
import base64
|
||||
import io
|
||||
import math
|
||||
import re
|
||||
import struct
|
||||
from collections.abc import Awaitable, Callable, Iterable, Mapping, Sequence
|
||||
from dataclasses import dataclass
|
||||
from functools import cached_property
|
||||
from itertools import accumulate, chain
|
||||
from itertools import accumulate, chain, islice
|
||||
from types import MappingProxyType
|
||||
from typing import TYPE_CHECKING, Final, Literal, Protocol, TypeAlias, cast
|
||||
|
||||
|
|
@ -406,8 +407,7 @@ class _PdfBox(Protocol):
|
|||
|
||||
|
||||
class _PdfContent(Protocol):
|
||||
@property
|
||||
def operations(self) -> Sequence[tuple[object, bytes]]: ...
|
||||
def get_data(self) -> bytes: ...
|
||||
|
||||
|
||||
class _PdfPage(Protocol):
|
||||
|
|
@ -1043,26 +1043,44 @@ def _pdf_xobjects(container: Mapping[str, object], inherited: _PdfXObjects) -> _
|
|||
return _pdf_dictionary(_pdf_value(resources, "/XObject"))
|
||||
|
||||
|
||||
_PDF_WHITESPACE: Final = rb"\x00\t\n\x0c\r "
|
||||
_PDF_TOKEN_END: Final = _PDF_WHITESPACE + rb"()<>\[\]{}/%"
|
||||
_PDF_XOBJECT_DRAW: Final = re.compile(
|
||||
rb"/([^" + _PDF_TOKEN_END + rb"]*)[" + _PDF_WHITESPACE + rb"]+Do(?![^" + _PDF_TOKEN_END + rb"])"
|
||||
)
|
||||
_PDF_INLINE_IMAGE: Final = re.compile(rb"(?<![^" + _PDF_WHITESPACE + rb"])BI(?![^" + _PDF_WHITESPACE + rb"/])")
|
||||
_PDF_NAME_ESCAPE: Final = re.compile(rb"#([0-9A-Fa-f]{2})")
|
||||
_PDF_NAME_CHARSETS: Final = ("utf-8", "gbk")
|
||||
|
||||
|
||||
def _decoded(raw: bytes, charset: str) -> str | None:
|
||||
try:
|
||||
return raw.decode(charset)
|
||||
except UnicodeDecodeError:
|
||||
return None
|
||||
|
||||
|
||||
def _pdf_name(escaped: bytes) -> str:
|
||||
raw: Final = _PDF_NAME_ESCAPE.sub(lambda escape: bytes.fromhex(escape[1].decode("ascii")), escaped)
|
||||
decoded: Final = (_decoded(raw, charset) for charset in _PDF_NAME_CHARSETS)
|
||||
return "/" + next((name for name in decoded if name is not None), raw.decode("latin-1"))
|
||||
|
||||
|
||||
def _pdf_content_summary(content: _PdfContent) -> _PdfContentSummary:
|
||||
operations: Final = content.operations
|
||||
operand_lists: Final = (operands for operands, operator in operations if operator == b"Do")
|
||||
names: Final[Iterable[object]] = (
|
||||
cast(Sequence[object], operands)[0] # cast-ok: pypdf parses an operator's operands into a list
|
||||
for operands in operand_lists
|
||||
if isinstance(operands, list) and operands
|
||||
)
|
||||
data: Final = content.get_data()
|
||||
draws: Final = islice(_PDF_XOBJECT_DRAW.finditer(data), PDF_MAX_XOBJECT_DRAWS_PER_PAGE + 1)
|
||||
return _PdfContentSummary(
|
||||
drawn_names=tuple(name for name in names if isinstance(name, str)),
|
||||
draws_inline_image=any(operator == b"INLINE IMAGE" for _, operator in operations),
|
||||
drawn_names=tuple(_pdf_name(draw[1]) for draw in draws),
|
||||
draws_inline_image=_PDF_INLINE_IMAGE.search(data) is not None,
|
||||
)
|
||||
|
||||
|
||||
def _pdf_form_summary(form: Mapping[str, object]) -> _PdfContentSummary | None:
|
||||
from pypdf.generic import ContentStream, StreamObject
|
||||
from pypdf.generic import StreamObject
|
||||
|
||||
if not isinstance(form, StreamObject):
|
||||
return None
|
||||
return _pdf_content_summary(cast(_PdfContent, ContentStream(form, None))) # cast-ok: pypdf is untyped
|
||||
return _pdf_content_summary(cast(_PdfContent, form)) # cast-ok: pypdf is untyped
|
||||
|
||||
|
||||
def _pdf_drawn_xobjects(drawing: _PdfDrawing) -> tuple[_PdfXObjectUse, ...]:
|
||||
|
|
|
|||
|
|
@ -2282,6 +2282,32 @@ def test_openai_model_finds_an_image_a_shared_form_without_resources_draws_from_
|
|||
) == _rendered_page_cost()
|
||||
|
||||
|
||||
@pytest.mark.timeout(10)
|
||||
def test_openai_model_prices_a_page_of_a_million_drawing_operators_promptly():
|
||||
"""A page of a million drawing operators and no image took about 30 seconds to price, since every operator was
|
||||
parsed to find the images it draws, while the stream compresses into a request a few kilobytes long."""
|
||||
model: Final = "gpt-4o"
|
||||
|
||||
assert _pdf_cost(model, _pdf_base64(("",), drawing=b" q Q" * 1_000_000, page_resources="<< >>")) == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize("escaped_name", ["/Im#231", "/#C4#E3", "/caf#C3#A9"], ids=["hex_escape", "gbk", "utf8"])
|
||||
def test_openai_model_finds_an_image_drawn_by_an_escaped_name(escaped_name: str):
|
||||
"""A name written with #xx escapes reads the same in the content stream and in /Resources, decoded the way pypdf
|
||||
decodes it, so an image drawn under such a name still prices the page as rendered."""
|
||||
model: Final = "gpt-4o"
|
||||
|
||||
assert _pdf_cost(
|
||||
model,
|
||||
_pdf_base64(
|
||||
("",),
|
||||
xobjects=(_ONE_PIXEL_IMAGE,),
|
||||
drawing=f" {escaped_name} Do".encode(),
|
||||
page_resources=f"<< /XObject << {escaped_name} 6 0 R >> >>",
|
||||
),
|
||||
) == _rendered_page_cost()
|
||||
|
||||
|
||||
@pytest.mark.timeout(10)
|
||||
def test_openai_model_reads_resources_shared_by_many_forms_once():
|
||||
"""3,000 forms that all list the same /Resources took 17 seconds to price before each dictionary was read once."""
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue