Instructions to use anusoft/OpenThai2.2-OCR-Markdown-20261127-mlx-rocm-pascal with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use anusoft/OpenThai2.2-OCR-Markdown-20261127-mlx-rocm-pascal with MLX:
# Make sure mlx-vlm is installed # pip install --upgrade mlx-vlm from mlx_vlm import load, generate from mlx_vlm.prompt_utils import apply_chat_template from mlx_vlm.utils import load_config # Load the model model, processor = load("anusoft/OpenThai2.2-OCR-Markdown-20261127-mlx-rocm-pascal") config = load_config("anusoft/OpenThai2.2-OCR-Markdown-20261127-mlx-rocm-pascal") # Prepare input image = ["http://images.cocodataset.org/val2017/000000039769.jpg"] prompt = "Describe this image." # Apply chat template formatted_prompt = apply_chat_template( processor, config, prompt, num_images=1 ) # Generate output output = generate(model, processor, formatted_prompt, image) print(output) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- Atomic Chat
Download runtime/pascal/bench_loop.py from anusoft/OpenThai2.2-OCR-Markdown-20261127-mlx-rocm-pascal: direct link, hf CLI and curl.
- Browser
- Download file 3.52 kB
-
https://huggingface.co/anusoft/OpenThai2.2-OCR-Markdown-20261127-mlx-rocm-pascal/resolve/main/runtime/pascal/bench_loop.py
- Command line
-
hf download hf://anusoft/OpenThai2.2-OCR-Markdown-20261127-mlx-rocm-pascal/runtime/pascal/bench_loop.py
-
curl -L -o bench_loop.py https://huggingface.co/anusoft/OpenThai2.2-OCR-Markdown-20261127-mlx-rocm-pascal/resolve/main/runtime/pascal/bench_loop.py
3.52 kB
| #!/usr/bin/env python3 | |
| """Time transformers.generate against a tight greedy loop. One median page.""" | |
| from __future__ import annotations | |
| import time | |
| from pathlib import Path | |
| import torch | |
| MODEL = Path(__file__).resolve().parents[2] / "models" / "original" | |
| EOS = 2 | |
| def penalize(logits, seen, penalty): | |
| scores = torch.gather(logits, 0, seen) | |
| scores = torch.where(scores < 0, scores * penalty, scores / penalty) | |
| return logits.scatter(0, seen, scores) | |
| def tight(model, inputs, limit, penalty): | |
| token = torch.empty(1, 1, dtype=torch.long, device="cuda") | |
| seen = torch.empty(9000, dtype=torch.long, device="cuda") | |
| out = model( | |
| input_ids=inputs["input_ids"], | |
| pixel_values=inputs["pixel_values"], | |
| image_sizes=inputs["image_sizes"], | |
| content_sizes=inputs["content_sizes"], | |
| use_cache=True, | |
| ) | |
| cache = out.past_key_values | |
| prompt = inputs["input_ids"][0] | |
| count = prompt.numel() | |
| seen[:count].copy_(prompt) | |
| logits = out.logits[0, -1] | |
| produced = 0 | |
| while produced < limit: | |
| choice = int(penalize(logits, seen[:count], penalty).argmax()) | |
| seen[count] = choice | |
| count += 1 | |
| produced += 1 | |
| if choice == EOS: | |
| break | |
| token.fill_(choice) | |
| out = model(input_ids=token, past_key_values=cache, use_cache=True) | |
| logits = out.logits[0, -1] | |
| return seen[:count].view(1, -1) | |
| def main() -> None: | |
| import sys | |
| from datasets import load_dataset | |
| from transformers import AutoModel, AutoProcessor | |
| from fast_attn import install as install_attn | |
| from int8_gemv import install as install_int8 | |
| sys.path.insert(0, str(MODEL)) | |
| from nemotron_parse.inference import ( | |
| PROMPT, | |
| DocumentParser, | |
| GenerationSettings, | |
| TransformersBackend, | |
| clean_raw_output, | |
| ) | |
| from nemotron_parse.postprocessing import postprocess_document | |
| dataset = load_dataset("scb10x/ThaiOCRBench")["test"] | |
| image = next(row["image"] for row in dataset if row["Id"] == "84DF1F7D").convert("RGB") | |
| model = AutoModel.from_pretrained( | |
| MODEL, trust_remote_code=True, dtype=torch.float32, local_files_only=True | |
| ).to("cuda").eval() | |
| processor = AutoProcessor.from_pretrained(MODEL, trust_remote_code=True, local_files_only=True) | |
| install_attn(model) | |
| install_int8(model) | |
| inputs = processor(images=[image], text=PROMPT, return_tensors="pt", add_special_tokens=False) | |
| inputs = {k: v.to("cuda") if torch.is_tensor(v) else v for k, v in inputs.items()} | |
| parser = DocumentParser( | |
| backend=TransformersBackend(model, processor), | |
| fullpage=GenerationSettings(temperature=0.0, repetition_penalty=1.05, max_tokens=8744), | |
| ) | |
| parser.parse(image, mode="single") | |
| tight(model, inputs, 8744, 1.05) | |
| torch.cuda.synchronize() | |
| started = time.perf_counter() | |
| stock = parser.parse(image, mode="single")["markdown"] | |
| torch.cuda.synchronize() | |
| stock_s = time.perf_counter() - started | |
| torch.cuda.synchronize() | |
| started = time.perf_counter() | |
| ids = tight(model, inputs, 8744, 1.05) | |
| torch.cuda.synchronize() | |
| fast_s = time.perf_counter() - started | |
| text = processor.batch_decode(ids, skip_special_tokens=False)[0] | |
| fast = postprocess_document(clean_raw_output(text), image_size=image.size)["markdown"] | |
| print(f"generate {stock_s:.2f}s loop {fast_s:.2f}s {stock_s / fast_s:.2f}x exact={stock == fast}") | |
| if __name__ == "__main__": | |
| main() | |