Download code/tt_dsocr/tests/test_host_config.py from changh95/DeepSeek-OCR: direct link, hf CLI and curl.
- Browser
- Download file 16.9 kB
-
https://huggingface.co/changh95/DeepSeek-OCR/resolve/main/code/tt_dsocr/tests/test_host_config.py
- Command line
-
hf download hf://changh95/DeepSeek-OCR/code/tt_dsocr/tests/test_host_config.py
-
curl -L -o test_host_config.py https://huggingface.co/changh95/DeepSeek-OCR/resolve/main/code/tt_dsocr/tests/test_host_config.py
16.9 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| # Adapted from this workspace's GLM-OCR bundle (tt_glmocr/tests/test_host_config.py, Apache-2.0). | |
| """Host tests of the compile-time configuration (``DSOCR_*``; no device, no ttnn): | |
| TT_VISIBLE_DEVICES=none python -m pytest -q code/tt_dsocr/tests/test_host_config.py | |
| """ | |
| from __future__ import annotations | |
| import os | |
| import sys | |
| import pytest | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) | |
| from tt_dsocr import config as C # noqa: E402 | |
| OCR1 = C.VARIANTS["deepseek-ocr"] | |
| def test_defaults(): | |
| c = C.resolve_config({}) | |
| assert (c.variant, c.backend, c.dispatch, c.revision) == ("deepseek-ocr", "device", "eth", OCR1.revision) | |
| assert c.model_name == "deepseek-ocr" and c.weights_repo == "deepseek-ai/DeepSeek-OCR" | |
| assert c.modes == ("tiny", "small", "base", "large", "gundam") and c.default_mode == "gundam" | |
| assert c.engine == {"vis": "ttnn", "pre": "ttnn", "dec": "ttnn"} # D20: the megakernel is an experimental opt-in | |
| assert c.precision == {"vis": "bf16", "vis_fid": "HiFi4", "pre_mlp": "bfp8", "pre_experts": "bfp8", | |
| "pre_fid": "HiFi2", "mk": "bfp8"} | |
| assert (c.max_crops, c.max_context, c.mk_attk, c.loop_steps, c.k_max, c.max_new_tokens, c.default_max_new_tokens, | |
| c.max_prompt_tokens, c.trace_region_mb) == (9, 8192, 4, 1, 64, 8192, 8192, 512, 1024) | |
| # Gundam's worst grid within 9 crops is 1 x 9 (a tall image): 273 + 11 x 90 = 1263 image tokens | |
| assert c.max_image_tokens == 1263 and c.max_prompt == 1263 + 512 and c.kv_positions == 8192 | |
| assert c.pre_buckets == (128, 256, 384, 512, 640, 896, 1024, 1408, 1792) | |
| assert c.trace and not c.allow_urls and c.queue_depth == 8 and c.cache_dir is None | |
| assert C.resolve_config({"TT_DIT_CACHE_DIR": "/weight-cache"}).cache_dir == "/weight-cache" | |
| assert C.resolve_config({"DSOCR_CACHE_DIR": "/c", "TT_DIT_CACHE_DIR": "/w"}).cache_dir == "/c" | |
| def test_variant_table_mirrors_the_frontend(): | |
| pytest.importorskip("numpy") | |
| from tt_dsocr import frontend as FE | |
| for name, fv in FE.VARIANTS.items(): | |
| v = C.VARIANTS[name] | |
| assert (v.newline, v.tile, v.crop_min, v.crop_max, v.modes) == (fv.newline, fv.tile, fv.crop_min, | |
| fv.crop_max, fv.modes), name | |
| for m, b, s, crop in v.modes: | |
| assert C.mode_params(v, m) == FE.mode_params(fv, m) | |
| grids = [(1, 1)] + C._grids(v.crop_max) if crop else [(1, 1)] | |
| for g in grids: | |
| assert C.image_token_count(v, b, s, crop, g) == FE.image_token_count(fv, b, s, crop, g), (name, m, g) | |
| assert set(C.VARIANTS) >= set(FE.VARIANTS) and FE.IMAGE_TOKEN_ID == C.IMAGE_TOKEN_ID and FE.EOS_ID == C.EOS_ID | |
| assert tuple(FE.TASKS) == C._TASKS and FE.TASKS == C.TASK_PROMPTS["deepseek-ocr"] | |
| assert C.TASK_PROMPTS["deepseek-ocr-2"] == {k: FE.TASKS[k] for k in ("markdown", "free")} | |
| assert C.UOCR2_DEFAULT_PROMPT == FE.UOCR2_DEFAULT_PROMPT and C.PAGE_TOKEN_ID == FE.PAGE_TOKEN_ID | |
| for name, v in C.VARIANTS.items(): | |
| assert v.tasks == tuple(C.TASK_PROMPTS[name]) and v.default_task in v.tasks, name | |
| assert v.multipage == (name == FE.UOCR2.name), name | |
| def test_image_token_counts(): | |
| v = OCR1 | |
| assert [C.mode_image_tokens(v, m, 9) for m in ("tiny", "small", "base", "large")] == [ | |
| (73, 73), (111, 111), (273, 273), (421, 421)] | |
| assert C.mode_image_tokens(v, "gundam", 9) == (273, 1263) | |
| assert C.mode_image_tokens(v, "gundam", 6) == (273, 273 + 11 * 60) # 1 x 6 | |
| assert C.image_token_count(v, 1024, 640, True, (3, 3)) == 1203 | |
| assert C.image_token_count(v, 1024, 640, True, (9, 1)) == 1183 # the line_en golden (9 x 1 tiles) | |
| ocr2 = C.VARIANTS["deepseek-ocr-2"] | |
| assert C.mode_image_tokens(ocr2, "gundam", 6) == (257, 257 + 6 * 144) # 256 + separator, 144 per tile | |
| def test_prefill_buckets_match_the_engine(): | |
| pytest.importorskip("torch") | |
| from tt_dsocr import frontend as FE | |
| from tt_dsocr.tt import model as TM | |
| for env in ({}, {"DSOCR_MODES": "tiny,small"}, {"DSOCR_MODES": "gundam", "DSOCR_MAX_CROPS": "4"}, | |
| {"DSOCR_MAX_PROMPT_TOKENS": "1024"}, {"DSOCR_MODES": "large,base", "DSOCR_MAX_PROMPT_TOKENS": "64"}): | |
| c = C.resolve_config(env) | |
| dc = TM.DeviceConfig(variant=c.variant, modes=c.modes, max_crops=c.max_crops, | |
| max_prompt_text=c.max_prompt_tokens) | |
| assert tuple(TM.default_prefill_buckets(dc, FE.VARIANTS[c.variant])) == c.pre_buckets, env | |
| def test_device_config_mapping(): | |
| pytest.importorskip("torch") | |
| c = C.resolve_config({"DSOCR_PRECISION": "vis=bfp8,vis_fid=HiFi2,pre_mlp=bfp4,pre_experts=bf16,pre_fid=LoFi," | |
| "mk=bfp8h8", "DSOCR_LOOP_STEPS": "1", "DSOCR_MAX_CONTEXT": "4000", | |
| "DSOCR_ENGINE": "dec=ttnn", "DSOCR_K_MAX": "32", "DSOCR_MK_ATTK": "2", | |
| "DSOCR_MODES": "tiny,gundam", "DSOCR_MAX_CROPS": "6", "DSOCR_TRACE": "0", | |
| "DSOCR_MAX_PROMPT_TOKENS": "256"}) | |
| d = c.device_config() | |
| assert (d.variant, d.modes, d.max_crops, d.max_context, d.engine_dec, d.mk_recipe, d.mk_attk, d.mk_batch) == ( | |
| "deepseek-ocr", ("tiny", "gundam"), 6, 4000, "ttnn", "bfp8h8", 2, 1) | |
| assert (d.vis_wdtype, d.vis_fidelity, d.pre_mlp, d.pre_experts, d.pre_fidelity) == ("bfp8", "HiFi2", "bfp4", | |
| "bf16", "LoFi") | |
| assert tuple(d.prefill_buckets) == c.pre_buckets and d.max_prompt_text == 256 and not d.trace and d.k_max == 32 | |
| assert C.resolve_config({}).device_config().engine_dec == "ttnn" | |
| assert C.resolve_config({"DSOCR_ENGINE": "dec=mk"}).device_config().engine_dec == "mk" | |
| from tt_dsocr.tt.mk_moe.program import RECIPES | |
| assert set(C.PRECISION_KEYS["mk"]) == set(RECIPES) # the recipes the megakernel knows | |
| assert "ttnn" not in sys.modules # tt.model / mk_moe import ttnn lazily | |
| def test_modes_and_default_mode(): | |
| c = C.resolve_config({"DSOCR_MODES": "small, TINY"}) | |
| assert c.modes == ("tiny", "small") and c.default_mode == "tiny" and c.max_image_tokens == 111 | |
| assert c.pre_buckets == (128, 256, 640) | |
| c = C.resolve_config({"DSOCR_MODES": "base,gundam", "DSOCR_DEFAULT_MODE": "base", "DSOCR_MAX_CROPS": "2"}) | |
| assert c.default_mode == "base" and c.max_image_tokens == 273 + 11 * 20 # 1 x 2: (10 + 1) x (10 x 2) | |
| assert C.resolve_config({"DSOCR_MODES": "gundam,base"}).modes == ("base", "gundam") | |
| def test_other_variants(): | |
| c = C.resolve_config({"DSOCR_VARIANT": "deepseek-ocr-2", "DSOCR_BACKEND": "fake"}) | |
| assert c.modes == ("gundam", "base", "small") and c.max_crops == 6 and c.default_mode == "gundam" | |
| assert c.revision == C.VARIANTS["deepseek-ocr-2"].revision and c.weights_repo == "deepseek-ai/DeepSeek-OCR-2" | |
| assert c.info.device and not c.info.todo and c.info.license_file == "LICENSE-Apache-2.0-DeepSeek-OCR-2.txt" | |
| assert c.info.tasks == ("markdown", "free") and c.engine["dec"] == "ttnn" and c.max_pages == 1 | |
| # Gundam 1024 global (256 + separator) + up to 6 tiles of 768 (144 each) | |
| assert c.max_image_tokens == 257 + 6 * 144 and c.pre_buckets == (256, 384, 768, 896, 1280, 1664) | |
| with pytest.raises(ValueError): | |
| C.resolve_config({"DSOCR_VARIANT": "deepseek-ocr-2", "DSOCR_MAX_CROPS": "9"}) | |
| with pytest.raises(ValueError): | |
| C.resolve_config({"DSOCR_VARIANT": "deepseek-ocr-2", "DSOCR_MODES": "tiny"}) | |
| with pytest.raises(ValueError, match="DSOCR_MAX_PAGES"): | |
| C.resolve_config({"DSOCR_VARIANT": "deepseek-ocr-2", "DSOCR_MAX_PAGES": "2"}) | |
| def test_unlimited_ocr_2(): | |
| v = C.VARIANTS["unlimited-ocr-2"] | |
| assert (v.weights_repo, v.revision, v.license) == ("baidu/Unlimited-OCR-2", | |
| "cb16b920ec2249c4b280f3aa67942e00b5099122", "MIT") | |
| assert v.device and v.multipage and v.tasks == ("parse",) and v.ngram_presets[0] == "off" | |
| assert not [f for f in v.weight_files if f.endswith(".py") or "wheel" in f or f.endswith(".pdf")] | |
| assert {"model-00001-of-00002.safetensors", "model-00002-of-00002.safetensors", | |
| "generation_config.json"} <= set(v.weight_files) | |
| c = C.resolve_config({"DSOCR_VARIANT": "unlimited-ocr-2"}) | |
| assert c.engine == {"vis": "ttnn", "pre": "ttnn", "dec": "ttnn"} and c.modes == ("gundam", "base") | |
| assert (c.max_crops, c.max_pages, c.max_context, c.max_new_tokens, c.default_mode) == (16, 8, 16384, 16384, | |
| "gundam") | |
| # one page: Gundam's worst grid within 16 tiles is 1 x 16: 273 + 11 x 160 = 2033 image tokens (+ <PAGE>); | |
| # 8 base pages: 8 x (273 + 1) rows | |
| assert C.mode_image_tokens(v, "gundam", 16) == (273, 2033) and C.page_rows(v, "base", 16) == (274, 274) | |
| assert C.image_token_count(v, 1024, 640, True, (4, 4)) == 1913 | |
| assert c.max_image_tokens == 8 * 273 and c.max_page_rows == 8 * 274 and c.max_prompt == 8 * 274 + 512 | |
| assert c.pre_buckets == (384, 640, 896, 1152, 1792, 2176, 2304, 2560, 2816) | |
| assert c.pre_buckets[-1] >= c.max_prompt and c.kv_positions >= c.pre_buckets[-1] + 64 | |
| assert c.as_env()["DSOCR_MAX_PAGES"] == "8" and C.resolve_config(c.as_env()).as_env() == c.as_env() | |
| one = C.resolve_config({"DSOCR_VARIANT": "unlimited-ocr-2", "DSOCR_MAX_PAGES": "1"}) | |
| assert one.pre_buckets == (384, 896, 2176, 2560) and one.max_prompt == 2034 + 512 | |
| gun = C.resolve_config({"DSOCR_VARIANT": "unlimited-ocr-2", "DSOCR_MODES": "gundam"}) | |
| assert gun.max_page_rows == 2034 and gun.pre_buckets == (384, 896, 2176, 2560) # pages: in Gundam, if they fit | |
| big = C.resolve_config({"DSOCR_VARIANT": "unlimited-ocr-2", "DSOCR_MAX_PAGES": "32"}) | |
| assert big.pre_buckets[-1] == C.ceil_to(32 * 274 + 512, 128) and len(big.pre_buckets) <= 24 | |
| for bad in ({"DSOCR_ENGINE": "dec=mk"}, {"DSOCR_ENGINE": "mk"}, {"DSOCR_MAX_PAGES": "0"}, | |
| {"DSOCR_MAX_PAGES": "33"}, {"DSOCR_MAX_CROPS": "17"}, {"DSOCR_MODES": "tiny"}, | |
| {"DSOCR_MAX_PAGES": "32", "DSOCR_MAX_CONTEXT": "8192"}, {"DSOCR_PREFILL_BUCKETS": "2560"}): | |
| with pytest.raises(ValueError): | |
| C.resolve_config(dict(bad, DSOCR_VARIANT="unlimited-ocr-2")) | |
| assert C.resolve_config({"DSOCR_VARIANT": "unlimited-ocr-2", "DSOCR_MAX_PAGES": "1", | |
| "DSOCR_PREFILL_BUCKETS": "2560"}).pre_buckets == (2560,) | |
| def test_unlimited_ocr_2_device_config(): | |
| pytest.importorskip("torch") | |
| from tt_dsocr import frontend as FE | |
| from tt_dsocr.tt import model as TM | |
| c = C.resolve_config({"DSOCR_VARIANT": "unlimited-ocr-2"}) | |
| d = c.device_config() | |
| assert (d.variant, d.engine_dec, d.max_crops, d.max_context, d.modes) == ("unlimited-ocr-2", "ttnn", 16, 16384, | |
| ("gundam", "base")) | |
| assert tuple(d.prefill_buckets) == c.pre_buckets # the configuration's buckets (multi-page rows) | |
| # one page: the engine's own derivation gives the same buckets (the <PAGE> row never crosses a 128 boundary) | |
| one = C.resolve_config({"DSOCR_VARIANT": "unlimited-ocr-2", "DSOCR_MAX_PAGES": "1"}) | |
| dc = TM.DeviceConfig(variant=one.variant, modes=one.modes, max_crops=one.max_crops, | |
| max_prompt_text=one.max_prompt_tokens) | |
| assert tuple(TM.default_prefill_buckets(dc, FE.UOCR2)) == one.pre_buckets | |
| d2 = C.resolve_config({"DSOCR_VARIANT": "deepseek-ocr-2"}).device_config() | |
| assert (d2.variant, d2.engine_dec, d2.max_crops) == ("deepseek-ocr-2", "ttnn", 6) | |
| def test_revision_pointer(): | |
| assert C.resolve_config({"TT_MODEL_WEIGHTS_REVISION": "abc"}).revision == "abc" | |
| assert C.resolve_config({"TT_WEIGHTS_REVISION": "def"}).revision == "def" | |
| assert C.resolve_config({"TT_MODEL_WEIGHTS_REVISION": "abc", "TT_WEIGHTS_REVISION": "def"}).revision == "abc" | |
| def test_megakernel_context_rule(): | |
| with pytest.raises(ValueError, match="DSOCR_MK_ATTK"): | |
| C.resolve_config({"DSOCR_MAX_CONTEXT": "16384", "DSOCR_MK_ATTK": "1", "DSOCR_ENGINE": "dec=mk"}) | |
| assert C.resolve_config({"DSOCR_MAX_CONTEXT": "16384", "DSOCR_MK_ATTK": "2", | |
| "DSOCR_ENGINE": "dec=mk"}).kv_positions == 16384 | |
| assert C.resolve_config({"DSOCR_MAX_CONTEXT": "16384", "DSOCR_MK_ATTK": "1", | |
| "DSOCR_ENGINE": "dec=ttnn"}).kv_positions == 16384 | |
| assert C.resolve_config({"DSOCR_MAX_CONTEXT": "5000"}).kv_positions == 5024 # rounded up to 32 | |
| def test_invalid_values_refuse_to_start(bad): | |
| with pytest.raises(ValueError): | |
| C.resolve_config(bad) | |
| pytest.importorskip("fastapi") | |
| from tt_dsocr.server.app import load_config | |
| with pytest.raises(RuntimeError): | |
| load_config(bad) | |
| def test_explicit_buckets(): | |
| c = C.resolve_config({"DSOCR_PREFILL_BUCKETS": "1792, 128,640"}) | |
| assert c.pre_buckets == (128, 640, 1792) | |
| c = C.resolve_config({"DSOCR_PREFILL_BUCKETS": "96,2048", "DSOCR_MODES": "tiny"}) | |
| assert c.pre_buckets == (96, 2048) | |
| def test_mesh_shape(): | |
| pytest.importorskip("fastapi") | |
| from tt_dsocr.server.app import load_config, parse_mesh_shape | |
| assert parse_mesh_shape("1x1") == parse_mesh_shape("(1, 1)") == parse_mesh_shape("1,1") == (1, 1) | |
| assert parse_mesh_shape(None) == parse_mesh_shape("") == (1, 1) | |
| with pytest.raises(RuntimeError): | |
| parse_mesh_shape("one") | |
| with pytest.raises(RuntimeError): | |
| load_config({"TT_MESH_SHAPE": "1x2"}) | |
| assert load_config({"TT_MESH_SHAPE": "1x1", "DSOCR_BACKEND": "fake"}).backend == "fake" | |
| def test_as_env_round_trip(): | |
| c = C.resolve_config({"DSOCR_MODES": "tiny,base,gundam", "DSOCR_MAX_CROPS": "6", "DSOCR_ENGINE": "dec=ttnn", | |
| "DSOCR_PRECISION": "mk=bf16", "DSOCR_ALLOW_URLS": "1", "DSOCR_QUEUE_TIMEOUT_S": "12.5", | |
| "DSOCR_TRACE": "0", "DSOCR_CACHE_DIR": "/cache"}) | |
| env = c.as_env() | |
| assert env["DSOCR_ENGINE"] == "vis=ttnn,pre=ttnn,dec=ttnn" and env["DSOCR_ALLOW_URLS"] == "1" | |
| assert env["DSOCR_MODES"] == "tiny,base,gundam" and env["DSOCR_TRACE"] == "0" | |
| c2 = C.resolve_config(env) | |
| assert c2.as_env() == env and c2.pre_buckets == c.pre_buckets and c2.modes == c.modes | |
| assert (c2.engine, c2.precision, c2.max_crops, c2.queue_timeout_s, c2.trace) == (c.engine, c.precision, 6, 12.5, | |
| False) | |
| d = c.as_dict() | |
| assert set(d) >= {"prefill_buckets", "kv_positions", "max_prompt", "max_image_tokens", "weights_repo"} | |
| def test_model_dir(tmp_path): | |
| with pytest.raises(RuntimeError): | |
| C.resolve_model_dir(C.resolve_config({"DSOCR_MODEL_DIR": str(tmp_path)})) | |
| (tmp_path / "config.json").write_text("{}") | |
| with pytest.raises(RuntimeError, match="tokenizer.json"): | |
| C.resolve_model_dir(C.resolve_config({"DSOCR_MODEL_DIR": str(tmp_path)})) | |
| (tmp_path / "tokenizer.json").write_text("{}") | |
| assert C.resolve_model_dir(C.resolve_config({"DSOCR_MODEL_DIR": str(tmp_path)})) == str(tmp_path) | |
| def test_weight_files_have_no_code(): | |
| for v in C.VARIANTS.values(): | |
| assert not [f for f in v.weight_files if f.endswith(".py")], v.name | |
| assert "tokenizer.json" in OCR1.weight_files and "model.safetensors.index.json" in OCR1.weight_files | |