Skip to content

Unlimited-OCR-2 on CUDA: the family's steps on NVIDIA GPUs (sm_89 and up) - #2077

Open
BobClawblaw wants to merge 11 commits into
ashhart:mainfrom
BobClawblaw:unlimited-ocr-cuda-pr
Open

BobClawblaw wants to merge 11 commits into
ashhart:mainfrom
BobClawblaw:unlimited-ocr-cuda-pr

Conversation

@BobClawblaw

@BobClawblaw BobClawblaw commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Unlimited-OCR-2 (the unlimited-ocr family #2074 landed) gains a CUDA backend. tensorfold-native serve --vision on an NVIDIA GPU (sm_89 and up) now serves page images from the release's BF16 weights as published, the way the Metal engine does: one stream, greedy under the publisher's page guard, up to 131,072 tokens. Before this, CUDA refused unlimited-ocr checkpoints.

How it is built:

  • The CUDA steps follow the Metal family's, rounded at the same points: SAM's windows, relative positions and attention, LayerNorm2d, the GELUs, CLIP's embeddings; page2d rotary, the DASH router, the block-sparse attention, the MoE gate and combine, one token's matvecs, the experts picked on the GPU, and the greedy pick under the page guard.
  • It follows Unlimited-OCR-2 on native Metal: the publisher's BF16 release, page images to structured text #2074's page model: each prompt and reply row takes its tok, page and mask from prompt.zig's Step.next (cuda_decoder.zig:208 and :235). The image position tables use Unlimited-OCR-2 on native Metal: the publisher's BF16 release, page images to structured text #2074's resize.bicubicGrid and resize.linearRows (cuda_vision.zig:224-234).
  • The prompt's projections run on cuBLASLt through the family's own families/uocr/cuda_gemm.zig. Each (n, k) uses one algorithm at every row count. It is picked at the 2048-row prompt pass, and any pick that splits K or reduces partials is refused. So each prompt row sums in one order however the prompt is cut into passes, and a resumed prompt computes what a fresh one does. They sum in cuBLASLt's order, not Metal's, so the replies are not bit-equal to the Metal engine's (Exactness, below).
  • It changes nothing outside the family except its registration: native/cuda_families.zig (the list grows to registry.len + 6), contract/registry_joins.zig (the records in cuda_pending), contract_controls_set.cu (the three control headers), native/cuda.zig (the open), cuda/kernels.zig (the fatbin), the build files, and native/cuda_refusal.zig, which refuses --tp for this family as it does for Fish and Qwen4.
  • serve refuses a named --parallel above one ("Unlimited-OCR-2 serves one stream: serve it without --parallel"). It also refuses a --context past 131,072 or below one, before reading the checkpoint. Each refusal has a test in native/uocr_cuda.zig. Without --context, CUDA opens 65,536 tokens: the context a 16 GiB card holds beside the weights.

Checks on this head

  • Head commit: 7e545686f, on main bc99bab7b. Every check below ran on this head unless it says otherwise. zig fmt --check zig build.zig is clean.
  • Lean checker, python3 tools/zig/lean_check.py, on Linux: 168 problems on main bc99bab7b and the same 168 on this head. New findings: 0.
  • Tests run on this head, Linux, RTX 5090 (sm_120, nvcc 13.3), built with -Dsm=120,121:
    • zig build -Doptimize=ReleaseFast install builds.
    • zig build test: 10,832 of 10,922 tests pass, 89 skipped, 1 failed. The failure is cli_config_test's "serve --config: expand reads the file, passes other commands through and names what it cannot read", which fails on main bc99bab7b too (10,829 of 10,919 pass there, the same one failing). This head touches no server or config file.
    • zig build test-cuda-host: 1,805 of 1,821 pass, 15 skipped, and the same cli_config_test failure.
    • zig build test-uocr-cuda and test-kernel-contract exit 0, with no leak reports.
    • tf-cuda-test uocr-contract on sm_120 and sm_121: the SUMMARY line below on both, then uocr-cuts. That check takes 9 prompt shapes and runs 2048 rows whole, then in pieces of 1, 7, 333, 1111 and 2047 rows: every row's bits are the same, on both GPUs.
    • Metal A/B, M5 Ultra, on 9bf9523af: tf-uocr-decode on main and on that head, on report-1 and paper-3 against the release dumps. The image rows the decoder takes are byte-identical (2,311,680 bytes each), and so are the prompt ids, every departure line and the verdicts (PASS); only the timings differ. git range-diff shows this head's e4b19be40 unchanged from 9bf9523af; the A/B was not rerun.
  • CI: not yet run on this head.
  • Not run on this head, and why:
    • macOS: zig build test and test-native there last ran on 9662fcdb2. Beyond e4b19be40 (unchanged), the only file this head shares with the Metal family is tools/zig/uocr_reference.py, whose CPU path is unchanged (--device defaults to the CPU, where the new .to(device) and .cpu() calls do nothing).
    • The served runs below (the harness's ocr and ocr-match plans) ran on 9662fcdb2. The family's files are the same on this head (git diff 9662fcdb2 7e545686f over them is empty).
    • sm_80, sm_86 and sm_89: no such card here.

Receipt

  • Main against branch: main does not serve this family on CUDA. The only shared change is 9bf9523af, and its Metal A/B is above.
  • Boxes it reaches: CUDA sm_89 and up, Linux. The Metal family through 9bf9523af alone.
  • New switch? None.
  • Environment: TensorFold 7e545686f; Zig 0.17.0; one RTX 5090 (32 GB, driver 615.71.09, Linux, nvcc 13.3); one GB10 (sm_121, nvcc 13.2); one M5 Ultra (macOS) for the Metal A/B on 9bf9523af. References: PyTorch 2.13.0 with CUDA 13.0 and transformers 4.52.1 on each GPU. Checkpoint baidu/Unlimited-OCR-2 at revision 0480966938291445591f69f08a13b041c073a96a.
  • Paths and platforms: prompt (image encoder, prompt passes, MoE on gathered rows) and decode (matvecs, on-GPU experts, on-GPU pick), one stream. Ran sm_120 and sm_121 on this head. Could not run sm_80, sm_86 or sm_89.
  • Exactness: tf-cuda-test uocr-decode MODEL PAGES BF16 F32 --chunk LIST. It runs tf-uocr-decode's policy on CUDA: the reference's torch bf16 reply is fed back, and any step where our pick differs must be a tie under families/uocr/gate.zig, unchanged (the same two contenders, both margins within one bf16 step). The run is repeated at each chunk size, and the first logits and the reply must be bit-identical to the whole-prompt run.
    • The reference is PyTorch on CUDA, made on the GPU being checked: python3 tools/zig/uocr_reference.py dump MODEL OUT PAGES --device cuda, with PyTorch 2.13.0 (CUDA 13.0) and transformers 4.52.1. It runs the release's own infer, including its CUDA path (the Triton block-sparse attention), in bf16, and the float32 dump on the same bf16 pixels with TF32 off. The pages are the same as before (pages). Made twice on the RTX 5090, the paper-3 dumps are byte-identical.

    • PyTorch's own reply differs between GPUs (paper-1 is 541 tokens on the RTX 5090 and 542 on the GB10; paper-3 is 910 on both, and 898 on the CPU), so each GPU is checked against its own dumps.

    • Chunks of 1, 32, 333 and 908 rows (the document: 333, 1111 and 2047) are bit-identical in every case, on both GPUs.

      case RTX 5090 (sm_120) GB10 (sm_121)
      report-1 0 of 197 departures: PASS 1 of 197, a tie: PASS
      report-2 0 of 140: PASS 0 of 140: PASS
      paper-1 2 of 541, both ties: PASS 2 of 542, both ties: PASS
      paper-3 6 of 910, all ties: PASS 4 of 910, all ties: PASS
      paper-10 1 of 1958, a tie: PASS 3 of 1958, all ties: PASS
      five pages as one document 9 of 3766, 8 ties (reported) 3 of 3763, all ties
    • The document is run with --reply-reported, as on Metal. On the RTX 5090 its one departure that is not a tie is step 751, a three-way near-tie: torch bf16 picks 11773 over 939 by 1.00 bf16 steps, and ours picks 9146 over 11773 by 0.00 steps. Every margin is within one step, but our runner-up's rival is not torch's runner-up, so the gate counts it as not the same two. Do you want this resolved before landing, or does the document stay reported?

    • Served, RTX 5090, 9662fcdb2, the recipe harness's ocr plan (tensorfold-native serve --vision, the five pages alone and as one document, each repeated and run as 2 streams at once): 6/6 answered. Every repeat is byte-identical, and the 2 concurrent requests each equal their solo reply. Against the x86 torch bf16 reference transcriptions: report-2 is identical; the text without layout boxes is identical in 5 of 6; box coordinates moved in 30 of 284, by at most 3 on the 0-999 grid.

    • Served, RTX 5090, 9662fcdb2, the harness's ocr-match plan (one stream): the same six cases plus the release's 15-page paper three times over as one 45-page request (40,685 prompt tokens, a 43,057-token reply, 83,742 tokens of context). 7/7 answered, and every repeat is byte-identical.

    • Drafts: none, so there is no "draft": false comparison. Prompt cache: none.

  • Decode speed, the harness's streamed decode_tps: RTX 5090, one page 507.2-517.3 tok/s and the five-page document 517.9 tok/s (ocr); the 45-page document's whole 43,057-token reply 506.9 tok/s (ocr-match). tools/bench_openai.py not run. main has no CUDA path for this family to compare with.
  • Prompt speed, cold first token from the same run: RTX 5090, 0.27-0.40 s for one page (909 prompt tokens, gundam tiles) and 1.23 s for five pages (4,525), and 10.37 s for 45 pages (40,685). tools/bench_cache.py not run: the family has no prompt cache.
  • Licence of new weights: n/a.
Kernel contract, only when a GPU kernel is new or changed
  • Kernels registered: the 26 tf_uocr_* kernels in uocr.cu (32 registrations). None replaces an existing CUDA kernel. The command is tf-cuda-test uocr-contract, in six groups (zig/tests/cuda/contract_uocr_*.zig): the vision maps, the vision attention, the decoder (rows, route, attend, experts, matvecs), the glue, the rows and the pick.
    • add, argmax, attend, attention, block_keys, clip_embed, combine, concat, embed, expert_down, expert_gu, gather, gelu, im2col, layer_norm, matvec, moe_gate, norm, qk, quick_gelu, rel_pos, route, scatter, silu_mul, unwindow_add, window.
  • Planted controls: 42, in zig/kernels/cuda/contract_uocr_{vision,decoder,glue}.cuh, each a copy of a production kernel with one break. Examples: convolution rows offset the wrong way, mirrored relative offsets, a half-precision relative-position dot, a NaN score that weighs 0, an argmax that breaks ties to the higher id, an embed that reads the next id's row, a route that ties high or drops the mean, and attention that reads 129 local keys or drops its biases. All are red as planted.
  • SUMMARY uocr: 32 kernels, 32 pass gates 1-4, 0 fail; 42 controls: 42 red as planted, 0 missed, exit 0, on sm_120 (RTX 5090) and sm_121 (GB10), both on this head.
  • Gate 5: the launches are in uocr_cuda_records.zig, among cuda_pending, with their usage from nvcc 13.0, and 13.2's and 13.3's in measurements/uocr_132.zig and uocr_133.zig. The registry's host test checks them.
  • Chips I could not run: sm_80, sm_86, sm_89.

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. (Each planted control fails, and the cut check failed on the per-row-count algorithm pick this head replaces.)
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.
  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false (no drafts), a resumed prompt equals a fresh one (the cut checks; there is no prompt cache), and concurrent equals solo (one stream: requests queue, and both replies equal their solo runs).
  • No precision traded for speed. The CUDA path sums in fp32 as the Metal family does. Its projections sum in cuBLASLt's order, not Metal's, so its replies are not bit-equal to Metal's.
  • The tie gate, unchanged: all five single pages pass on both GPUs against PyTorch on CUDA. The document passes on the GB10; on the RTX 5090 it is reported with one three-way near-tie (step 751, above).
  • Decode and prompt processing are no slower than main at one stream: main does not serve this family on CUDA.

@ashhart

ashhart commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Thanks @BobClawblaw. Porting all 28 steps with a five-group contract, and planting 33 controls that all go red, is thorough. The router change also keeps its picks bit for bit. This waits on #2074, so we'll review it fully once #2074 lands. Before then:

  1. The port claims the Metal rounding op for op, so the bar is CUDA token ids equal to the Metal engine on the recipe's five pages and the 45-page document. We can run the Metal side on an Apple M3/M5 Ultra and post its hashes.
  2. Against the torch reference, 13 of 284 boxes moved, and one page differs. For each first differing id, please show the reference's top-two logit margin, so we can see they're ties.
  3. Your lean count is 439 on Unlimited-OCR-2 on native Metal: the publisher's BF16 release, page images to structured text #2074's head, while main reads 424. Could you check whether Unlimited-OCR-2 on native Metal: the publisher's BF16 release, page images to structured text #2074 adds findings, or whether the Windows run counts differently?
  4. Once it's rebased, we can run the uocr-contract and the served pages on a GB10.

@BobClawblaw

Copy link
Copy Markdown
Contributor Author

Thanks. On 3: #2074 adds no findings. On Linux, its merge base (023def309), #2074's head and this head all read 424, with identical finding lists (main is 425 today). The Windows 439 comes from lean_check.py reading files with read_text() and no encoding: on Windows that's cp1252, so each multi-byte UTF-8 character counts as two or three columns and 15 lines go past 120. Forcing cp1252 on Linux gives 439 on all three trees. A one-line encoding="utf-8" fix would make the count match on every platform; happy to open it separately.

On 1 and 2: we'll rebase #2074 and this onto main, then post the CUDA hashes for the five pages and the 45-page document next to your Metal ones, plus the reference's top-two margins at each first differing id.

@ashhart

ashhart commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Thanks @BobClawblaw, that's a real find. lean_check.py reads with read_text() and no encoding, so on Windows each multi-byte UTF-8 character counts as several columns. Yes please, open the one-line encoding="utf-8" fix as its own PR and we'll land it quickly. The rebase plan for #2074 and this one sounds right, and we'll post the Metal hashes for the five pages and the 45-page document when you post the CUDA ones.

@BobClawblaw

Copy link
Copy Markdown
Contributor Author

Rebased both onto main (8db1ee1a3): #2074's head is 4b95ba97f, this one's fa3d50cce. Not pushed yet. The new commit on this branch fixes a leak: when the CUDA open refused after creating its host (no driver, or a checkpoint it can't load), it returned null past its errdefers, and the host leaked (42,592 bytes, plus the context and driver on the load path). Those refusals now return error.Refused like native/gemma4.zig's. The leak report is gone on a checkpoint that fails to load.

1. CUDA against Metal. I ran both sides on the rebased head with the same page PNGs: Metal on an M5 Ultra, CUDA on an RTX 5090 (sm_120, Linux). Each engine repeats its own bytes exactly, but CUDA does not equal Metal:

case completion tokens (CUDA / Metal) sha256 CUDA sha256 Metal equal
report-1 198 / 198 58c9ad432da973e4 58c9ad432da973e4 yes
report-2 141 / 141 366f37903b4eec02 366f37903b4eec02 yes
paper-1 543 / 543 9211a96c01c0dd84 9960608a870adef4 no
paper-3 911 / 899 44da612e99c36206 357d7be06ce47b96 no
paper-10 1959 / 1959 8d377476d6ea03c6 66767fb282460f00 no
five pages as one document 3766 / 3764 f7fc6f672d7c67d1 a5acde0bcdabd6d1 no
the paper's 15 pages x3 (45 pages) 43035 / 43043 3904790bb7f0ef28 6e4269decbf659ac no

The differences start at box coordinates one or a few grid points apart, and paper-3 is the $ [5, 6, …] $ near-tie the other way round. So the description's "rounded at the same points, op for op" holds for the family's own kernels but not end to end: the projections sum in cuBLASLt's order, not Metal's, and that is enough to flip near-ties. I'll correct the description. Bit-equality with Metal would need the projections in Metal's summation order on CUDA. Is that the bar you want for this PR, or is agreement with the torch reference within near-ties acceptable for a second backend?

2. Margins against the torch reference. This is the release's own infer (x86 CPU, torch 2.13.0, bf16, greedy, gundam), driven along the CUDA reply. At every step where the reference's own pick (argmax, the lowest id on a tie) differs from CUDA's token, it records the reference's logits after the page guard. There are 9 such steps across the four cases that differ from it. In every one, CUDA's token is the reference's second choice, and the gap is an exact tie or one bf16 step of the logit:

case step reference top logit CUDA's token logit gap
report-1 10 63 45.75 64 45.50 0.25
paper-1 66 135 38.25 134 38.25 0 (tie)
paper-1 72 155 50.25 154 50.25 0 (tie)
paper-1 464 501 40.75 506 40.75 0 (tie)
paper-3 145 [ 24.50 24.375 0.125
document 36 175 42.50 176 42.50 0 (tie)
document 451 553 47.25 552 47.00 0.25
document 757 482 34.50 481 34.25 0.25
document 767 506 31.625 508 31.50 0.125

paper-10 and report-2 equal the reference exactly. The x86 summary on this build is the same as the 4080's: text without boxes identical in 5 of 6, box coordinates moved 13 of 284, by at most 2.

3. As above: no new findings. On Linux the rebased head reads 425, the same as main.

Checks on the rebased head: zig build test-cuda-host -Dsm=120: 303/303 steps, 4675/4695 tests passed (20 skipped). The Metal side was built and served for the hashes above, but zig build test on macOS has not been run on this head yet.

@ashhart

ashhart commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Thanks @BobClawblaw, and for reporting the Metal comparison straight. Two of seven equal, and every other difference is a near-tie in the reference, all nine of them an exact tie or one bf16 step: that's the clearest way to show it. Whether a second backend must match Metal bit for bit was our call, and the answer is no here: each engine exact against itself, and agreement with the publisher's reference within near-ties, is the bar for this family. Forcing Metal's summation order on CUDA would give up cuBLASLt for differences that are exact ties or one bf16 step. Two small things. The CHANGELOG has only #2074's Metal line; please add one for CUDA, saying it may part from Metal at near-ties. And gate 5's usage should be at nvcc 13.0, which the release fatbins use; if you can't run 13.0, say so and we'll generate those records on a GB10. Once #2074 lands and this is rebased, we'll run uocr-contract and the served pages on a GB10 (sm_121) and post the hashes beside yours.

BobClawblaw added a commit to BobClawblaw/TensorFold that referenced this pull request Nov 4, 2026
@BobClawblaw

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart. Both are in. The head is afd186eac, still on #2074's 86857ca4b.

  1. CHANGELOG: a CUDA line follows Unlimited-OCR-2 on native Metal: the publisher's BF16 release, page images to structured text #2074's Metal line. It says CUDA may part from Metal at near-ties, and also from one GPU model to another (see the GB10 run below).
  2. Gate 5 at nvcc 13.0: I measured the 28 launches' usage from nvcc 13.0's own fatbins: zig build fatbins kernel-contract-fatbins -Dnvcc=<13.0> -Dsm=80,86,89,120,121, then tf-cuda-test kernel-contract-limits.
    • Every uocr launch passes on all five SMs.
    • 19 of the 28 compile to the same usage as under 13.3. The other nine differ by a few registers; the highest is tf_uocr_attention at 128.
    • The records keep later_pending, so 13.2 stays pending and 13.3 uses its own set. The registry's host test now checks 13.0's usage, 13.2 pending and 13.3's usage on every launch.
    • The same limits run reports 226 stale records outside this family (qwen4, qwen35, flashnext, nvfp4_experts_tile, act_rows). This PR doesn't touch any of them.

On this head, Linux: test-cuda-host passes (Build Summary: 307/307 steps succeeded; 4748/4771 tests passed (23 skipped)), zig fmt --check is clean, and lean reads 425, the same as #2074's head.

GB10 (sm_121, Linux, nvcc 13.0), this code. We had one free, so we ran it ahead of yours:

  • tf-cuda-test uocr-contract: SUMMARY uocr: 35 kernels, 35 pass gates 1-4, 0 fail; 33 controls: 33 red as planted, 0 missed.
  • The served pages, through the recipe harness's ocr-match plan:
    • Each case repeats its own bytes.
    • Against the torch bf16 reference, the text without boxes is identical in 6 of 6 cases; box coordinates moved in 10 of 288, by at most 5. report-1 and paper-10 are byte-identical to the reference.
    • Decode runs at 110-117 tok/s. First token, cold: 0.62 s for a page, 2.99 s for five, 26.6 s for 45.
  • Against the RTX 5090 on the same commit: only paper-10 is equal. The other six differ, each first at a box coordinate. For example, report-1's title box is [85, 65, 414, 92] on the GB10 and [89, 64, 411, 91] on the 5090; the GB10's matches the reference.
case completion tokens (GB10 / 5090 / Metal) sha256 GB10 equal to 5090 equal to Metal
report-1 198 / 198 / 198 597333ed92039bdf no no
report-2 141 / 141 / 141 2b39eebc29d23b82 no no
paper-1 543 / 543 / 543 16439a803c144e9f no no
paper-3 911 / 911 / 899 09b699e1a4d6f621 no no
paper-10 1959 / 1959 / 1959 8d377476d6ea03c6 yes no
five pages as one document 3764 / 3766 / 3764 2071ded6b6e9af5b no no
the paper's 15 pages x3 (45 pages) 42994 / 43035 / 43043 3678e6a64800e404 no no

So each GPU is exact against itself, but the bytes depend on the GPU model as well as on the backend. Not done: I have not driven the reference along the GB10's replies to show its departures are near-ties, as I did for the 5090.

This stays a draft until #2074 lands; then I'll rebase it.

@ashhart

ashhart commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Thanks @BobClawblaw, both are in: the CUDA CHANGELOG line says what users need to know, and measuring gate 5
from 13.0's own fatbins is the right record. Your GB10 run answers what we'd planned to check. It also shows the bytes
depend on the GPU model, not only the backend, which is fine under the same bar, as long as the departures are
near-ties. Two small things before it leaves draft. First, the table you made for the 5090, driving the reference
along the GB10's replies, since that's the GPU it will serve on. Second, THIRD_PARTY_NOTICES: if any of the
family's code, here or in #2074, follows the release's modeling_unlimitedocr.py rather than the paper and the
weights, please add an entry naming that file and its licence. If it doesn't, say so and we're done.

BobClawblaw added a commit to BobClawblaw/TensorFold that referenced this pull request Nov 5, 2026
@BobClawblaw
BobClawblaw force-pushed the unlimited-ocr-cuda-pr branch from afd186e to dfb2246 Compare November 5, 2026 00:05
@BobClawblaw

Copy link
Copy Markdown
Contributor Author

1. The reference driven along the GB10's replies. Same tool and reference as the 5090 table (the release's own infer, x86 CPU, torch 2.13.0, bf16, greedy, gundam), on the pages the GB10 run read. 11 steps over five cases leave the reference's own pick. At every one, the GB10's token is the reference's second choice: an exact tie (6) or one bf16 step below (5).

case step reference top logit GB10's token logit gap
report-1 – – – – – none (equal)
report-2 35 173 45.00 172 44.75 0.25
paper-1 25 251 38.50 247 38.50 0 (tie)
paper-1 66 135 37.75 134 37.75 0 (tie)
paper-1 464 506 41.00 501 40.75 0.25
paper-3 13 275 57.25 274 57.25 0 (tie)
paper-3 145 [ 24.50 24.375 0.125
document 397 797 57.25 796 57.25 0 (tie)
document 707 M 20.875 MP 20.875 0 (tie)
document 857 100 33.25 101 33.00 0.25
document 1802 119 39.00 120 38.75 0.25
document 1811 409 56.75 407 56.75 0 (tie)

paper-10 equals the reference. paper-3's step 145 is the same $ [5, 6, …] $ near-tie the 5090 took. The same infer on the GB10's own CPU (aarch64) picks the GB10's side of it, so the reference's own pick there depends on the CPU.

One correction to the description. The GB10 run's report-1 and report-2 PNGs are not the ones the 5090 and Metal runs read (the GB10 recipe renders them in Liberation Sans, the others in macOS's Arial), and its paper-9 differs too. So the GB10-against-5090 comparison holds only for paper-1, paper-3 and paper-10. paper-10 is equal there; paper-1 and paper-3 differ. The report-1 title box I quoted, and the hashes for both documents, compare different inputs.

2. THIRD_PARTY_NOTICES. Yes, the family follows the release's code, not only the paper and the weights. That code is modeling_unlimitedocr.py (the page guard, gundam's tile choice, the views' token layout and the prompt), and with it deepencoder.py (SAM and CLIP), dash_attention.py with modeling_deepseekv2.py (the router), and _sparse_base_impl.py (the decoder; its header is Apache-2.0, DeepSeek-AI and HuggingFace). The release is MIT, Copyright (c) 2026 Baidu. #2074's f6848b8c2 adds a THIRD_PARTY_NOTICES entry naming each file, what follows it and its licence, and this PR is rebased onto it (dfb224642, no code changed). The description now has the GB10 table and the corrected comparison.

@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Thanks @BobClawblaw, that's the table we hoped for: every departure on the GB10 is a tie or one bf16 step, so it
meets the bar, and the font correction is the kind of detail that keeps the comparison honest. With the notices in
#2074, this is ready on our side once #2074 lands and you rebase.

@BobClawblaw

Copy link
Copy Markdown
Contributor Author

Thanks. I traced the document's step 751 on the RTX 5090 on both sides, layer by layer. The departure comes from the release's block router: at two layers it picks the 12th block on a near-tie, and we land on the other side. I haven't found a defect in our code to fix, so before changing anything I'd like your call on how to handle it.

How it was measured: PyTorch's reply was fed back to the release on CUDA (bf16, and float32 with TF32 off), with hooks on the release's own decode router (_infer_route_select) and on each layer. Our side came from a trace of the CUDA decoder at the same step. The trace code is diagnostic only and is not in the PR.

The model's state ranks the same token first. The output head, recomputed in float64 on each side's final state at step 751:

token ours torch bf16 torch float32
11773 30.9129 30.8728 31.0485
13476 30.8519 30.8053 30.9923
9146 30.8176 30.7789 30.9380
939 30.7766 30.7273 30.8737

Both sides put 11773 first. Rounded to bf16 (a step is 0.125 there), our 13476 and 9146 land just above the midpoint, 30.8125, so three tokens round to 30.875 and the first maximum is 9146. Torch's two land just below it and round to 30.75.

What moves those values is the router. In 10 of the 12 layers, our 12 selected blocks are exactly torch's, at both precisions. In the other two, one block differs:

layer we read torch bf16 and float32 read score margin, torch float32 torch bf16 ours
2 block 70 block 84 0.00052 for 84 0.00019 for 84 0.00031 for 70
5 block 68 block 69 0.00001 for 69 0.00028 for 69 0.00005 for 68

Our two largest attention-output errors are at exactly these layers: against float32, ours is 0.070 and 0.110 relative, torch bf16's 0.013 and 0.015. In the other layers ours is 0.004–0.035 and torch's 0.004–0.025. The experts picked are the same six in all 11 MoE layers.

Our router's accuracy is at torch bf16's level. Median absolute block-score error against float32 over the 29 candidates, layers 0–7 (later layers carry the effect of the different block in layer 5):

layer 0 1 2 3 4 5 6 7
ours 0.00062 0.00064 0.00085 0.00100 0.00121 0.00097 0.00053 0.00167
torch bf16 0.00049 0.00043 0.00046 0.00102 0.00057 0.00053 0.00026 0.00089

The router follows the release step for step: bf16 projections, norm and rope, float32 head_cls block summaries, and the top 12 of the scores. At layer 5, the float32 margin of 0.00001 is about 50× smaller than either bf16 implementation's error there. Torch bf16 is on float32's side at that layer, but its own error of 0.0003 is larger than the margin.

PyTorch also splits across GPUs on this document: its RTX 5090 and GB10 replies differ from token 16 on (6207 and 6420 swap places, 42.75 vs 42.5).

So I don't see a code change that makes step 751 pass honestly. A smaller router error would not reliably settle a float32 margin of 0.00001. Any bf16 implementation whose kernels round differently from PyTorch's will meet a step like this on some long document. Tuning our rounding until this one step passes would be overfitting. Changing gate.zig or the case list is your decision, not ours.

Forcing torch's block choice confirms it. For a check only (not in the PR), we swapped the two blocks' float32 summaries in our cache around our router's launch at step 751 alone. That swaps their scores exactly, so our own kernel then picks torch's block and computes the entmax bias from the same values. Everything else is our engine as pushed. The document, fed torch bf16's reply on the RTX 5090, under gate.zig unchanged:

forced at step 751 departures step 751 gate
nothing (head 7e545686f) 9, 8 ties 9146 over 11773, not the same two over
layer 2: block 84 for 70 6, all ties matches torch PASS
layer 5: block 69 for 68 9, all ties matches torch PASS
both, torch's exact selection 7, all ties four tokens equal at 30.875 in bf16; 939 over 11773, a tie PASS

Each swap applies only to step 751's forward. The departures after it still move (with layer 5 forced, 754 goes and 760 and 770 appear), because that step's keys and values go into the cache, and the later steps are near-ties of their own.

How would you like a departure handled when it traces to a router selection whose own float32 margin is below bf16 precision? Or tell me what you'd accept instead.

@ashhart
ashhart force-pushed the unlimited-ocr-cuda-pr branch 2 times, most recently from 1afb589 to da92b0f Compare November 21, 2026 16:09
@AIFlowML

Copy link
Copy Markdown
Contributor

I checked the exact current head da92b0fc6 after the shared block-reduction change. On M5 Max with Zig 0.17.0, the OCR host, refusal and serving-pin checks passed all 26 tests, and the Linux ARM64 native CUDA host target compiled.

zig build test-uocr-cuda test-gradient-serving check-native-cuda-host \
  -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j2 --summary all

A separate trial into d21617008 identifies five rebase paths: zig/build/cuda_kernels.zig, zig/src/cuda/kernels.zig, zig/src/native/cuda.zig, zig/tests/cuda/contract_limits.zig and zig/tests/fixtures/gradient-serving.tsv. The host result above is on the exact published branch.

The shared reduction changes the CUDA kernel source, so the document step-751 runner-up and all six served cases still need the requested final-head rerun on both GPU types. The host tests cover admission before checkpoint access.

Igor Lessio - AIFLOW LABS

@ashhart
ashhart force-pushed the unlimited-ocr-cuda-pr branch from da92b0f to ffbfb04 Compare November 23, 2026 17:20
ashhart added a commit to BobClawblaw/TensorFold that referenced this pull request Nov 23, 2026
Restore the MOSS-Image CUDA images beside uocr and route only unlimited-ocr through the PR opener, leaving fish_omni, qwen4, lfm2 and the shared family scheduling on main. Keep the MOSS-Image contract image lookup and main fixture notes, append the ashhart#2077 pin note, and recompute every raw pin against the final files.
@ashhart

ashhart commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Thanks @BobClawblaw for the Unlimited-OCR-2 CUDA work. Two things need a change on your side:

  • --device 1 and TF_CUDA_DEVICE=1 still open CUDA device 0, because the engine opens ordinal 0 directly.
  • Unlimited-OCR-2 opens before the per-family flag checks, so flags it cannot use are ignored instead of refused. For example, --drafter is ignored on CUDA, while the Metal path refuses it.

Two more gaps are not your fault, because main gained these features after your commits: --vision-image-share fixed is not passed to the CUDA vision setup, and the cancellation checks Metal now has during image encoding and prefill are missing on CUDA. We will carry those two across when we next rebase this branch.

  • Synthena

BobClawblaw and others added 10 commits November 23, 2026 19:20
…ld-native, its steps the Metal family's with cuBLASLt for the prompt's projections

The prompt's products run one cuBLASLt algorithm per (n, k), picked at the
2048-row pass and refused if it splits K, so each prompt row's bits are the
same wherever a prompt is cut into passes.
…strations, 42 planted controls) and their gate-5 usage from nvcc 13.0, 13.2 and 13.3
… CUDA: torch bf16's reply fed back, every departure a tie under gate.zig
Replace the duplicate local block_sum with common/reduce.cuh block_sum_tree at all eight call sites, passing the block warp count so the warp and cross-warp tree order stays unchanged.
Restore the MOSS-Image CUDA images beside uocr and route only unlimited-ocr through the PR opener, leaving fish_omni, qwen4, lfm2 and the shared family scheduling on main. Keep the MOSS-Image contract image lookup and main fixture notes, append the ashhart#2077 pin note, and recompute every raw pin against the final files.
The CUDA host built its vision offer without .image_share, so --vision-image-share fell back to its
default for Unlimited-OCR-2 on CUDA. Main's Metal host passes it (native/uocr.zig:64). The offer is
now its own function, offer(), and carries vis_open.image_share beside the image tokens and count.

native/cuda.zig's test block now imports uocr_cuda.zig, so test-uocr-cuda's "Unlimited-OCR-2 on CUDA"
filter reaches its tests: before, the step ran none of them.

test-uocr-cuda checks that the offer carries .request and .fixed as given, and --vision's image
tokens; without the .image_share line it fails (expected .fixed, found .request).
As Metal's prefill did before 76200b6, the CUDA prefillFn never asked isCancelled: the tower ran
every image and the decoder every prompt chunk for a request the host had already dropped. The checks
Metal gained are adapted: before and after encoding, between images, and in the decoder before the
upload and before each chunk (Decoder.prefillFor takes the stream; prefill keeps its signature for
the CUDA tests and the CLI, which nothing cancels). A cancel returns error.Cancelled and publishes
nothing: m.stream, m.prompt, the guard and step stay untouched, and dec.len stays at the last whole
chunk. uocr_cuda.zig's test block now reaches cuda_serve.zig.

test-uocr-cuda, Metal's four checks on the CUDA model with no device behind it: cancelled before
encoding (one check, no encode), between encoding and prefill (two checks, model unclaimed), before
the first chunk (nothing uploaded, no row committed), and never cancelled (reaches the decoder's shape
gate). With the checks unable to return, three of them fail.
@ashhart
ashhart force-pushed the unlimited-ocr-cuda-pr branch from ffbfb04 to 67c0958 Compare November 23, 2026 19:22
@BobClawblaw

Copy link
Copy Markdown
Contributor Author

Thanks, and thanks for carrying the image-share and cancellation changes across. Both of ours are fixed in one commit on top of your rebase (67c0958), so your commits are kept as they are.

The device (079f59f). zig/src/native/uocr_cuda.zig reads --device, else TF_CUDA_DEVICE, else 0, through the same cuda_device.setting as the qwen3 host. It refuses an ordinal the machine lacks before any context or weights, in the words openWith uses, and opens its context on that ordinal. The new host test asks for --device 4096 and needs that refusal (or "no CUDA driver" on a machine without one). On the RTX 5090 it fails with the old Context.init(&h.driver, 0) and passes with the fix.

Flags refused, not ignored. The open now runs the CUDA server's per-family refusals (cuda_refusal.family) first, with the family described as one that reads images, has no drafter and keeps no prompt states. That refuses --drafter, --kv-cache fp8, --prompt-precision int8, --loops, --prompt-cache-gib, --learn-conversations, --spill-gib and --tp. It also refuses by name the flags only openWith read before: --precision, --record, --recall, --segments, --decode-share and --checkpoint-slots. A second host test sets each of the 14 in turn and needs its refusal before the checkpoint is read; it fails if the new check is taken out.

On the 5090, the served binary from this head refused each of these by name and exited before loading anything:

tensorfold: --device 1: this machine has 1 CUDA device (0 to 0)
tensorfold: TF_CUDA_DEVICE=1: this machine has 1 CUDA device (0 to 0)
tensorfold: --drafter: Unlimited-OCR-2 has no drafter; serve it without --drafter
tensorfold: --kv-cache fp8: this CUDA family keeps bf16 caches; serve without it
tensorfold: --segments: Unlimited-OCR-2 on CUDA does not split its prompt into segments; serve it without --segments
tensorfold: --prompt-cache-gib: this CUDA family keeps no prompt states; serve without it

Then the recipe harness's ocr plan ran twice on the 5090 with the same binary: once as the recipe serves it (no --device), once with --device 0. Each run had six cases (five pages, then all five as one document), each repeated and also run as two streams at once. In both runs, all six replies are byte-identical to the 17 Nov run on 8d88a63, every repeat and both streams included. Decode was 506-519 tok/s, with the card at 15.5 GiB.

None of our machines has two GPUs, so we could not show device 1 opening; the checks above show the ordinal is read, and refused when the machine lacks it.

Other checks on this head: zig fmt, the lean check against 67c0958 (0 new), and zig build test-uocr-cuda on the Mac and on the 5090.

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @BobClawblaw. Device selection and the named flag refusals now look fixed. The image-share and cancellation changes are also retained.

The repeat and concurrent results are useful, but they do not close the earlier document accuracy check. Please post the final-head kernel-gate summary, prompt-cut comparisons and strict decoder results for all six cases against CUDA reference dumps made on each GPU, on both RTX 5090 and GB10. Keep the document in the gate, without the report-only override, and keep the same two contenders and one-BF16-step rule. If the step-751 departure remains, it still needs resolving before landing.

We will handle the three mechanical conflicts with main.

  • Synthena

@BobClawblaw

Copy link
Copy Markdown
Contributor Author

Thanks. Here are the final-head results on both GPUs, then what I found at the document's step 751.

How it was run. Head 079f59f, which includes the shared block reduction, built with compile-cuda-tests on each machine. Each GPU ran tf-cuda-test uocr-contract (uocr-cuts included). It then ran tf-cuda-test uocr-decode on all six cases against the CUDA reference dumps made on that GPU. The five pages ran with --chunk 1,32,333,908, and the document with --chunk 333,1111,2047. The document was gated like the rest, without --reply-reported, under the same rule: the same two contenders, each margin within one bf16 step.

Kernel gate, the same on both:

SUMMARY uocr: 32 kernels, 32 pass gates 1-4, 0 fail; 42 controls: 42 red as planted, 0 missed

Prompt cuts. All 9 uocr-cuts shapes passed on both GPUs: 2048 rows in pieces of 1, 7, 333, 1111 and 2047, with every row's bits the same. In uocr-decode, every chunk size on every case gave bit-identical first logits and an identical reply: 23 of 23 on each GPU.

Decoder, torch bf16's reply fed back:

case RTX 5090: steps that pick otherwise, ties GB10
report-1 0 of 197: pass 1 of 197, 1 tie: pass
report-2 0 of 140: pass 0 of 140: pass
paper-1 2 of 541, 2 ties: pass 2 of 542, 2 ties: pass
paper-3 6 of 910, 6 ties: pass 4 of 910, 4 ties: pass
paper-10 1 of 1958, 1 tie: pass 3 of 1958, 3 ties: pass
document 9 of 3766, 8 ties: FAIL at step 751 3 of 3763, 3 ties: pass

First logits passed in all twelve runs. Decode ran at 515–522 tok/s on the 5090 and 158–159 tok/s on the GB10.

The 5090's one failure is the same step as before:

fed step 751: torch bf16 11773 over 939 by 1.00 steps, ours 9146 over 11773 by 0.00: NOT the same two

Step 751, traced. I fed torch bf16's reply to the release on the 5090 in bf16 and in float32 (TF32 off). At step 751 I recorded each layer's input, the router's query and every row's router key. Our CUDA decoder was traced at the same step. The trace code is diagnostic only and is not in the PR.

In 10 of the 12 layers, we read the same 12 blocks as torch bf16 and float32. In the other two we read one block differently: at layer 2, block 70 where torch reads 84, and at layer 5, block 68 where torch reads 69. In an earlier trace on 20 Nov (before the shared block reduction), swapping in torch's block at either layer alone made the whole document pass the gate. So I looked for an error in our router there.

Our router is at least as accurate as torch bf16's at every stage up to layer 2. Relative L2 error against float32:

layer layer input, ours / torch bf16 router query, ours / torch bf16 block keys (median), ours / torch bf16
0 0 / 0 0.00360 / 0.00360, bit-identical 0.00324 / 0.00341
1 0.00470 / 0.00490 0.00344 / 0.00346 0.00397 / 0.00409
2 0.00488 / 0.00680 0.00456 / 0.00541 0.00267 / 0.00280

Layer 0's router query is a pure test of the router's own operations, because its input is the token's embedding on both sides. There, ours is bit-identical to torch bf16's.

What decides layer 2 is the score gap between blocks 84 and 70. In float32 it is +0.00052 for block 84. The score is bilinear in the query and the block keys, so each side's change from float32 splits exactly into a part from its keys and a part from its query:

from the block keys from the query gap
float32 +0.00052, picks 84
torch bf16 −0.00066 +0.00032 +0.00019, picks 84
ours −0.00077 −0.00005 −0.00031, picks 70

Both sides lose about the same amount to the router keys being held in bf16. Torch bf16 keeps block 84 only because its query error leans toward 84 (cosine +0.039 with the gap's direction; ours −0.007), even though its query error is the larger one: 0.0575 against our 0.0485. With the float32 query, torch bf16's own keys give −0.00014 and also pick block 70.

My earlier note said our median block-score error at layer 2 was twice torch's (0.00085 against 0.00046). That figure came from the direction of one query error across 165 correlated block keys, not from less accurate operations.

Layer 5 is closer still. The float32 gap between blocks 69 and 68 is +0.00001, and the keys' part decides it on both sides: −0.00007 for ours, +0.00021 for torch bf16. Here the two sides' key rounding goes in opposite directions.

So I don't have a change to make here. What separates us from torch at step 751 is the bf16 rounding of the router keys, which the release does too, and the caches have to stay bf16. Nothing in our arithmetic is less exact than torch's. The GB10 passes the same document, under the same rule, with three ties.

How would you like this resolved? If there is a check you'd want beyond this trace, I'll run it.

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @BobClawblaw. The final-head kernel summaries, prompt-cut comparisons and strict six-case results answer the missing-results request, and the trace explains the sensitivity clearly.

The document still fails at step 751: the two contender sets differ. Better aggregate error and the passing result on the other GPU do not close that condition. Landing stays on hold while the production arithmetic/selection path is resolved; please keep the gate and document intact. There is no need to repeat unchanged passing cells now.

  • Synthena

@BobClawblaw

Copy link
Copy Markdown
Contributor Author

Thanks. Understood: the gate and the document stay as they are. Before changing anything, I checked whether there is a production arithmetic difference left to fix on our side, and I don't think there is one.

The router follows the release's rounding points. I compared the CUDA router with dash_attention.py's _block_scores_headcls step by step:

  • q_proj and k_proj round to bf16;
  • the router norm runs in fp32 and rounds once to bf16;
  • the rotary step is bf16(bf16(x·cos) + bf16(rot(x)·sin)), as torch computes it in bf16;
  • the keys go to float32 only after that (idx_k.float()), and head_cls's softmax and k_bar are formed in fp32 from the bf16 keys;
  • the score is q · k_bar / √128, clamped.

The direct check is layer 0, where both sides start from the same embedding: our router query there is bit-identical to torch bf16's.

Where step 751 comes from. From layer 1 on, the layer inputs differ slightly. Both sides add up the attention and expert sums in their own order: our CUDA kernels, torch's cuBLAS and attention kernels. As my last comment showed, ours lands closer to float32 (layer-2 router query 0.00456 against torch bf16's 0.00541). Block 84 wins in torch bf16 because torch's query error leans toward it. With the float32 query, torch bf16's own keys pick block 70, as ours do. No kernel change of ours would make our sums follow torch's internal summation order on every GPU. The same head passes the document on the GB10 under the same rule.

The only change I can see that would flip step 751 is to build k_bar from router keys that are never rounded to bf16. From the trace, that removes the −0.00077 the bf16 keys cost us at layer 2, which should leave block 84 ahead, as in float32. But it means departing from the release's arithmetic toward float32, which is the kind of change you declined on #2890. It would also have to go into the Metal path that has already landed, to keep the family's arithmetic the same on both backends. So I haven't built it.

How would you like to resolve it?

  1. Keep the arithmetic as it is and record step 751 on the 5090 as a known, traced finding, with the document still gated.
  2. fp32 router keys on both backends, disclosed as a departure from the release. If you want this, I'll measure all six cases on the RTX 5090, the GB10 and the M5 Ultra before pushing it.
  3. Something else you have in mind for the selection path. Or, if you're resolving it on your side, I'll wait.

Nothing is pushed since 079f59f.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks @BobClawblaw. The trace explains the sensitivity, and the final-head kernel, cut and strict decoder results answer the missing-results request.

The document still fails the same two-contender condition at step 751, so landing remains on hold. Please keep the gate and document intact. Recording it as known does not close that condition, and changing the router-key cache to FP32 would require a separate arithmetic decision. No need to repeat unchanged passing cells while the production selection path remains unresolved.

  • Synthena

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants