Skip to content

Streaming bounding boxes out of Nemotron Parse

Nemotron 3.5 Parse returns a page in one piece. Streaming the tokens gets the first box out in 0.19 s.

Published
Reading time
About 3 min
Author
Mohamed Tahrioui

Nemotron 3.5 Parse needs 4 to 20 seconds per page on my DGX Spark, and vLLM returns the page in one piece. The header is decoded in the first half second. You get it when the last table cell is done.

The output format

The model writes each region as one block:

<x_0.0527><y_0.0133>THE GOLDMAN SACHS GROUP, INC. ...<x_0.4277><y_0.0445><class_Page-header>
<x_0.0518><y_0.0672>\begin{tabular}{ccccccc} ... \end{tabular}<x_0.4707><y_0.7344><class_Table>

Two coordinates, the content, two coordinates, a class. Each coordinate and each class is a single token.

One block of Nemotron Parse output, token by token: start x, start y, content, end x, end y, class.
One block of Nemotron Parse output, token by token: start x, start y, content, end x, end y, class.

Nothing after the class token refers back to the block, so the block is done when the class arrives. Set stream: true and skip_special_tokens: false, and vLLM streams the tags.

The parser

vLLM emits SSE deltas, a tokenizer splits them into tags and text, the state machine completes blocks, each block becomes one JSON line.
vLLM emits SSE deltas, a tokenizer splits them into tags and text, the state machine completes blocks, each block becomes one JSON line.

A tokenizer splits the deltas into x, y, class and text tokens. It holds a half-received tag like <cla until the > arrives, up to the length of the longest tag. Past that, it's text, so a < b in a sentence stays text.

A state machine with five states consumes the tokens:

python
class State(StrEnum):
    OUTSIDE = "outside"
    AFTER_X0 = "after_x0"
    TEXT = "text"
    AFTER_X1 = "after_x1"
    AFTER_Y1 = "after_y1"
The five-state machine. Solid edges follow the grammar; dashed edges are the recovery cases.
The five-state machine. Solid edges follow the grammar; dashed edges are the recovery cases.
python
def consume(self, token: Token) -> Iterator[Block]:
    if self.state is State.OUTSIDE:
        if token.kind is Kind.X:
            self.pending = [token]
            self.state = State.AFTER_X0
        return
    if self.state is State.AFTER_X0:
        if token.kind is Kind.Y:
            self.start = (self.pending[0].value, token.value)
            self.pending, self.text, self.state = [], [], State.TEXT
            return
        self.pending, self.state = [], State.OUTSIDE
        yield from self.consume(token)
        return
    if self.state is State.TEXT:
        if token.kind is Kind.X:
            self.pending = [token]
            self.state = State.AFTER_X1
            return
        if token.kind is Kind.CLASS and not self.text:
            self.state = State.OUTSIDE
            return
        self.text.append(token.raw)
        return
    if self.state is State.AFTER_X1:
        if token.kind is Kind.Y:
            self.pending.append(token)
            self.state = State.AFTER_Y1
            return
        self.demote_pending()
        yield from self.consume(token)
        return
    if token.kind is Kind.CLASS:
        yield self.close(token.label)
        return
    self.demote_pending()
    yield from self.consume(token)

Two cases come up in real output. A coordinate token in the middle of content, or an x without a y: demote_pending puts the held tokens back into the text, and parsing stays inside the block. And on 5 of the 503 ParseBench table pages, the model writes <x><y><class_Page-footer> with no content, then the real block. NVIDIA's postprocessing strips that first, so the parser drops a class token that arrives before any content.

Fed seven characters at a time, all 503 pages produce the same blocks as NVIDIA's pipeline. Each block goes out as one JSON line.

One page

A half-year report page with two tables side by side, one request:

Boxes arrive at 0.19 s, 3.03 s, 6.57 s and 6.62 s; without streaming everything arrives at 6.62 s.
Boxes arrive at 0.19 s, 3.03 s, 6.57 s and 6.62 s; without streaming everything arrives at 6.62 s.
BoxArrivedClass
10.19 sPage-header
23.03 sTable, left
36.57 sTable, right
4, 56.62 sPage-footer

The header arrives 6.4 s early, the left table 3.6 s early. The GPU does the same work.

Repetition loops

12 of the 503 pages ran to the 8,744-token cap, repeating a row until cut off. They are 2.4% of the pages and 12.8% of the generated tokens.

2.4% of pages hit the token cap and account for 12.8% of output tokens; pages over 4,000 tokens account for 25.7%.
2.4% of pages hit the token cap and account for 12.8% of output tokens; pages over 4,000 tokens account for 25.7%.

A streaming client sees the second identical block and cancels, and vLLM hands the slot to the next page. NVIDIA ships a logits processor that breaks the loop server-side after ten repeats. I run both.

Limits

Throughput doesn't change.

A table completes after its whole LaTeX body, so big tables still arrive in one piece. Splitting on \\ rows would fix that. I haven't needed it.

JSON through structured output is out. The model never saw JSON, and a grammar would make it spell coordinates digit by digit. Coordinates and classes are 2.7% of the output anyway. Table bodies are 69%.

Get new posts by email

One email when I publish. Unsubscribe anytime.