Download code/tt_glmocr/tests/test_host_config.py from changh95/GLM-OCR: direct link, hf CLI and curl.
- Browser
- Download file 7.52 kB
-
https://huggingface.co/changh95/GLM-OCR/resolve/main/code/tt_glmocr/tests/test_host_config.py
- Command line
-
hf download hf://changh95/GLM-OCR/code/tt_glmocr/tests/test_host_config.py
-
curl -L -o test_host_config.py https://huggingface.co/changh95/GLM-OCR/resolve/main/code/tt_glmocr/tests/test_host_config.py
7.52 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| """Host tests of the compile-time configuration (``GLMOCR_*``; no device): | |
| TT_VISIBLE_DEVICES=none python -m pytest bundles/glm-ocr/code/tt_glmocr/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_glmocr import config as C # noqa: E402 | |
| def test_defaults(): | |
| c = C.resolve_config({}) | |
| assert c.backend == "device" and c.dispatch == "eth" and c.revision == C.WEIGHTS_REVISION | |
| assert c.engine == {"vis": "ttnn", "pre": "ttnn", "dec": "mk"} | |
| assert c.precision == {"vis": "bf16", "vis_fid": "HiFi4", "attn": "bf16", "mlp": "bfp8", "head": "bf16", | |
| "pre_fid": "HiFi4", "mk": "bfp8", "vis_sdpa": "approx"} | |
| assert c.cache_dir is None and C.resolve_config({"TT_DIT_CACHE_DIR": "/weight-cache"}).cache_dir == "/weight-cache" | |
| assert C.resolve_config({"GLMOCR_CACHE_DIR": "/c", "TT_DIT_CACHE_DIR": "/w"}).cache_dir == "/c" | |
| assert (c.max_image_tokens, c.max_context, c.max_new_tokens, c.default_max_new_tokens, c.max_prompt_tokens, | |
| c.k_max, c.loop_steps) == (6144, 10240, 8192, 4096, 1024, 64, 8) | |
| assert c.vis_buckets == (256, 512, 1024, 2048, 4096, 8192, 12288, 16384, 24576) | |
| assert c.max_prompt == 8 + 6144 + 1024 and c.pre_buckets == (128, 256, 512, 1024, 2048, 3072, 4096, 6400, 7296) | |
| assert c.kv_positions == 10240 and c.max_pixels_cap == C.OFFICIAL_MAX_PIXELS == c.default_max_pixels | |
| assert not c.allow_urls and c.queue_depth == 8 | |
| def test_vision_buckets_match_the_engine(): | |
| pytest.importorskip("torch") | |
| from tt_glmocr.tt.ttnn_vision import default_buckets | |
| for t in (256, 1000, 2048, 3000, 6144): | |
| assert list(C.default_vision_buckets(t)) == default_buckets(t), t | |
| def test_small_configuration(): | |
| c = C.resolve_config({"GLMOCR_MAX_IMAGE_TOKENS": "1024", "GLMOCR_MAX_PROMPT_TOKENS": "256", | |
| "GLMOCR_MAX_CONTEXT": "4096", "GLMOCR_MAX_NEW_TOKENS": "2048", | |
| "GLMOCR_DEFAULT_MAX_NEW_TOKENS": "1024"}) | |
| assert c.vis_buckets == (256, 512, 1024, 2048, 4096) | |
| assert c.max_prompt == 1288 and c.pre_buckets == (128, 256, 512, 1024, 1408) | |
| assert c.max_pixels_cap == 2 * 784 * 1024 and c.default_max_pixels == c.max_pixels_cap | |
| def test_explicit_buckets_engine_precision(): | |
| c = C.resolve_config({"GLMOCR_VISION_BUCKETS": "24576,1024,4096", "GLMOCR_PREFILL_BUCKETS": "7296, 512", | |
| "GLMOCR_ENGINE": "dec=mk", "GLMOCR_PRECISION": "mlp=bfp4,vis_fid=HiFi2", | |
| "GLMOCR_DISPATCH": "worker", "GLMOCR_ALLOW_URLS": "true", "TT_DEVICE_ID": "1"}) | |
| assert c.vis_buckets == (1024, 4096, 24576) and c.pre_buckets == (512, 7296) | |
| assert c.engine == {"vis": "ttnn", "pre": "ttnn", "dec": "mk"} | |
| assert c.precision["mlp"] == "bfp4" and c.precision["vis_fid"] == "HiFi2" and c.precision["attn"] == "bf16" | |
| assert c.dispatch == "worker" and c.allow_urls and c.device_id == 1 | |
| assert C.parse_engine("mk") == {"vis": "ttnn", "pre": "ttnn", "dec": "mk"} | |
| assert C.parse_engine("vis=ttnn,pre=ttnn,dec=mk") == C.DEFAULT_ENGINE | |
| 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_invalid_values_refuse_to_start(bad): | |
| with pytest.raises(ValueError): | |
| C.resolve_config(bad) | |
| from tt_glmocr.server.app import load_config | |
| with pytest.raises(RuntimeError): | |
| load_config(bad) | |
| def test_mesh_shape(): | |
| from tt_glmocr.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", "GLMOCR_BACKEND": "fake"}).backend == "fake" | |
| def test_as_env_round_trip(): | |
| c = C.resolve_config({"GLMOCR_MAX_IMAGE_TOKENS": "2048", "GLMOCR_ENGINE": "dec=mk", "GLMOCR_PRECISION": "head=bfp8", | |
| "GLMOCR_ALLOW_URLS": "1", "GLMOCR_QUEUE_TIMEOUT_S": "12.5"}) | |
| env = c.as_env() | |
| assert env["GLMOCR_ENGINE"] == "vis=ttnn,pre=ttnn,dec=mk" and env["GLMOCR_ALLOW_URLS"] == "1" | |
| c2 = C.resolve_config(env) | |
| assert c2.as_env() == env and c2.vis_buckets == c.vis_buckets and c2.pre_buckets == c.pre_buckets | |
| assert (c2.engine, c2.precision, c2.max_image_tokens, c2.queue_timeout_s) == (c.engine, c.precision, 2048, 12.5) | |
| assert set(c.as_dict()) >= {"vision_buckets", "prefill_buckets", "kv_positions", "max_prompt"} | |
| def test_engine_config_mapping(): | |
| pytest.importorskip("torch") | |
| c = C.resolve_config({"GLMOCR_PRECISION": "vis=bfp8,vis_fid=HiFi2,attn=bfp8,mlp=bfp4,head=bfp8,pre_fid=LoFi," | |
| "mk=bfp8h8", "GLMOCR_LOOP_STEPS": "16", "GLMOCR_MAX_CONTEXT": "10000", | |
| "GLMOCR_ENGINE": "dec=mk", "GLMOCR_K_MAX": "32"}) | |
| e = c.engine_config() | |
| assert tuple(e.vision_buckets) == c.vis_buckets and tuple(e.prefill_buckets) == c.pre_buckets | |
| assert e.max_context == 10048 and e.loop_steps == 16 and e.dec == "mk" | |
| assert (e.vision_wdtype, e.vision_fidelity, e.attn_dtype, e.mlp_dtype, e.head_dtype, e.pre_fidelity) == ( | |
| "bfp8", "HiFi2", "bfp8", "bfp4", "bfp8", "LoFi") | |
| assert (e.mk_recipe, e.mk_batch, e.k_max) == ("bfp8h8", 16, 32) | |
| from tt_glmocr.tt.mk_dec.weights import RECIPES | |
| assert set(C.PRECISION_KEYS["mk"]) == set(RECIPES) # the recipes the megakernel knows | |
| def test_model_dir(tmp_path): | |
| with pytest.raises(RuntimeError): | |
| C.resolve_model_dir(C.resolve_config({"GLMOCR_MODEL_DIR": str(tmp_path)})) | |
| (tmp_path / "config.json").write_text("{}") | |
| assert C.resolve_model_dir(C.resolve_config({"GLMOCR_MODEL_DIR": str(tmp_path)})) == str(tmp_path) | |
| def test_pixel_units(): | |
| assert C.tokens_to_max_pixels(6144) == 9633792 and C.max_pixels_to_tokens(9633792) == 6144 | |
| assert C.max_pixels_to_tokens(C.OFFICIAL_MIN_PIXELS) == 8 | |