# Copyright (c) 2026 LlamaIndex, Inc. All rights reserved. # Licensed under the OCRia-1 Model and Code License (LICENSE at the root of this repository). # This is not open-source software: see the license for permitted uses. """CUDA smoke: synthetic text/table pages, fast pixel isolation, and legacy stability. Run from the repository root with PYTHONPATH=. python tests/check_cuda_layout.py. """ import argparse import json from pathlib import Path def check(model: str) -> dict: import time import numpy as np import torch from PIL import Image, ImageDraw, ImageFont from ocria_grounding import ( load, load_fast_layout, ground_layout, transcribe, to_layout_items_page, ) import ocria_grounding.layout as layout import ocria_grounding.ground as legacy_ground t = time.perf_counter() g = load(model) backend = load_fast_layout() load_seconds = time.perf_counter() - t result = { "device": torch.cuda.get_device_name(), "model_load_seconds": load_seconds, "pages": [], } for index, text in enumerate( [ "FAST LAYOUT TEST\n\nA short paragraph on a test page.\nThe second line has more words.", "INVOICE\n\nItem Quantity Price\nApples 3 6.00\nPears 2 4.00\n\nTotal 10.00", ] ): image = Image.new("RGB", (640, 480), "white") draw = ImageDraw.Draw(image) draw.multiline_text( (35, 35), text, fill="black", font=ImageFont.load_default(size=20), spacing=14, ) started = time.perf_counter() answer = transcribe(g, image, max_new_tokens=256) transcription = time.perf_counter() - started old = ground_layout(g, image, answer) old_page = layout.Page def reject_pixels(*args: object, **kwargs: object) -> None: raise AssertionError("fast-layout touched pixel postprocessing") layout.Page = reject_pixels legacy_ground.Page = reject_pixels try: fast = ground_layout( g, image, answer, method="fast-layout", fast_backend=backend ) finally: layout.Page = old_page legacy_ground.Page = old_page after = ground_layout(g, image, answer) assert old == after, "Fast layout changed subsequent default output" serialized = to_layout_items_page(fast) for item in fast["items"]: assert np.isfinite(item["box"]).all() assert item["box"][2] >= item["box"][0] and item["box"][3] >= item["box"][1] assert fast["items"], "No fast layout items" result["pages"].append( { "index": index, "answer": answer, "default_items": len(old["items"]), "fast_items": len(fast["items"]), "default_before_after_equal": True, "transcription_seconds": transcription, "timings_ms": fast["timings_ms"], "serialization_items": len(serialized["items"]), } ) result["peak_gpu_memory_bytes"] = torch.cuda.max_memory_allocated() return result if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--model", default=str(Path(__file__).resolve().parents[1])) args = parser.parse_args() print(json.dumps(check(args.model), indent=2))