Download code/tt_glmocr/tests/bench_vision_ops.py from changh95/GLM-OCR: direct link, hf CLI and curl.
- Browser
- Download file 6.28 kB
-
https://huggingface.co/changh95/GLM-OCR/resolve/main/code/tt_glmocr/tests/bench_vision_ops.py
- Command line
-
hf download hf://changh95/GLM-OCR/code/tt_glmocr/tests/bench_vision_ops.py
-
curl -L -o bench_vision_ops.py https://huggingface.co/changh95/GLM-OCR/resolve/main/code/tt_glmocr/tests/bench_vision_ops.py
6.28 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| """Micro-benchmark of the vision encoder's op types at a bucket size (eager, host-timed with synchronize, best of 5; | |
| ms-scale ops only): which op dominates, and which configuration (fidelity, weight format, SDPA chunks, exp approx) | |
| is fastest at what accuracy. | |
| bin/devrun -t 600 -- python -u bundles/glm-ocr/code/tt_glmocr/tests/bench_vision_ops.py --n 2048,12288 | |
| """ | |
| import argparse | |
| import json | |
| import os | |
| import sys | |
| import time | |
| import torch | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(HERE))) | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--n", default="2048,12288") | |
| ap.add_argument("--reps", type=int, default=5) | |
| args = ap.parse_args() | |
| import ttnn | |
| from tt_glmocr.ttaw.device import open_device, close_device | |
| from tt_glmocr.tt.ttnn_vision import cu_windows | |
| dev = open_device(0, dispatch="eth") | |
| g = dev.compute_with_storage_grid_size() | |
| grid = ttnn.CoreGrid(y=int(g.y), x=int(g.x)) | |
| out = {} | |
| def cc(f): | |
| return ttnn.init_device_compute_kernel_config(dev.arch(), math_fidelity=getattr(ttnn.MathFidelity, f), | |
| math_approx_mode=False, fp32_dest_acc_en=True, | |
| packer_l1_acc=True) | |
| def t(fn): | |
| fn() | |
| ttnn.synchronize_device(dev) | |
| best = 1e9 | |
| for _ in range(args.reps): | |
| t0 = time.perf_counter() | |
| r = fn() | |
| ttnn.synchronize_device(dev) | |
| best = min(best, time.perf_counter() - t0) | |
| del r | |
| return round(1000 * best, 3) | |
| def up(x, dt=ttnn.bfloat16): | |
| return ttnn.from_torch(x, dtype=dt, layout=ttnn.TILE_LAYOUT, device=dev, memory_config=ttnn.DRAM_MEMORY_CONFIG) | |
| try: | |
| for N in [int(x) for x in args.n.split(",")]: | |
| r = out.setdefault(N, {}) | |
| x = up(torch.randn(1, 1, N, 1024)) | |
| x32 = up(torch.randn(1, 1, N, 1024), ttnn.float32) | |
| h4 = up(torch.randn(1, 1, N, 4096)) | |
| for wd, wn in ((ttnn.bfloat16, "bf16"), (ttnn.bfloat8_b, "bfp8")): | |
| wq = up(torch.randn(1024, 3072) * 0.02, wd) | |
| wg = up(torch.randn(1024, 4096) * 0.02, wd) | |
| wgu = up(torch.randn(1024, 8192) * 0.02, wd) | |
| wd_ = up(torch.randn(4096, 1024) * 0.02, wd) | |
| for f in ("HiFi4", "HiFi2", "LoFi"): | |
| c = cc(f) | |
| r[f"qkv_{wn}_{f}"] = t(lambda: ttnn.linear(x, wq, compute_kernel_config=c, core_grid=grid)) | |
| r[f"gate_{wn}_{f}"] = t(lambda: ttnn.linear(x, wg, compute_kernel_config=c, core_grid=grid)) | |
| r[f"gateup8192_{wn}_{f}"] = t(lambda: ttnn.linear(x, wgu, compute_kernel_config=c, core_grid=grid)) | |
| r[f"down_{wn}_{f}"] = t(lambda: ttnn.linear(h4, wd_, compute_kernel_config=c, core_grid=grid)) | |
| r[f"down_{wn}_{f}_out32"] = t(lambda: ttnn.linear(h4, wd_, compute_kernel_config=c, core_grid=grid, | |
| dtype=ttnn.float32)) | |
| nw = up(torch.ones(1, 1, 1, 1024)) | |
| c4 = cc("HiFi4") | |
| r["rms_bf16"] = t(lambda: ttnn.rms_norm(x, weight=nw, epsilon=1e-5, compute_kernel_config=c4)) | |
| r["rms_fp32"] = t(lambda: ttnn.rms_norm(x32, weight=nw, epsilon=1e-5, compute_kernel_config=c4)) | |
| r["typecast_32_16"] = t(lambda: ttnn.typecast(x32, ttnn.bfloat16)) | |
| r["add_fp32"] = t(lambda: ttnn.add(x32, x32)) | |
| r["mul_4096"] = t(lambda: ttnn.multiply(h4, h4)) | |
| qkv = up(torch.randn(1, 1, N, 3072)) | |
| r["create_heads"] = t(lambda: ttnn.experimental.nlp_create_qkv_heads( | |
| qkv, num_heads=16, num_kv_heads=16, transpose_k_heads=False, memory_config=ttnn.DRAM_MEMORY_CONFIG)) | |
| q, k, v = ttnn.experimental.nlp_create_qkv_heads(qkv, num_heads=16, num_kv_heads=16, | |
| transpose_k_heads=False, | |
| memory_config=ttnn.DRAM_MEMORY_CONFIG) | |
| hw = up(torch.ones(1, 1, 1, 64)) | |
| r["qnorm"] = t(lambda: ttnn.rms_norm(q, weight=hw, epsilon=1e-5, compute_kernel_config=c4)) | |
| cos = up(torch.randn(1, 1, N, 64)) | |
| r["rotary"] = t(lambda: ttnn.experimental.rotary_embedding_hf(q, cos, cos, is_decode_mode=False, | |
| compute_kernel_config=c4)) | |
| cu = ttnn.from_torch(torch.tensor(cu_windows(N, N), dtype=torch.int32), dtype=ttnn.uint32, | |
| layout=ttnn.ROW_MAJOR_LAYOUT, device=dev) | |
| for qc, kc in ((128, 128), (256, 256), (256, 512), (512, 512)): | |
| if N % qc or N % kc: | |
| continue | |
| for f in ("HiFi4", "HiFi2"): | |
| for ea in (False, True): | |
| cfg = ttnn.SDPAProgramConfig(compute_with_storage_grid_size=(grid.x, grid.y), q_chunk_size=qc, | |
| k_chunk_size=kc, exp_approx_mode=ea) | |
| c = cc(f) | |
| try: | |
| r[f"sdpa_{qc}_{kc}_{f}_{'approx' if ea else 'exact'}"] = t( | |
| lambda: ttnn.transformer.scaled_dot_product_attention( | |
| q, k, v, is_causal=False, scale=0.125, compute_kernel_config=c, | |
| program_config=cfg, cu_window_seqlens=cu)) | |
| except Exception as e: # noqa: BLE001 | |
| r[f"sdpa_{qc}_{kc}_{f}_{'approx' if ea else 'exact'}"] = str(e)[:80] | |
| a = up(torch.randn(1, 16, N, 64)) | |
| r["concat_heads"] = t(lambda: ttnn.experimental.nlp_concat_heads(a, memory_config=ttnn.DRAM_MEMORY_CONFIG)) | |
| print(N, json.dumps(r), flush=True) | |
| finally: | |
| close_device(dev) | |
| od = os.path.join(os.environ.get("GLMOCR_ASSETS", "."), "runs") | |
| os.makedirs(od, exist_ok=True) | |
| json.dump(out, open(os.path.join(od, "bench_vision_ops.json"), "w"), indent=1) | |
| if __name__ == "__main__": | |
| main() | |