#!/usr/bin/env python3 """ Build a tiny slice of openbmb/MiniCPM-OCR-Jev (openbmb-minicpm-ocr-jev decision model) for testing. Output is gibberish, only the shapes and code paths are real. Adapted from the slice-model skill of llama.cpp (skills/slice-model): tensors are fetched with HTTP range requests and config.json is patched to match, then convert_hf_to_gguf.py does the conversion. The stock script only drops layers, which cannot go below the 248320 x 2048 token embedding, so this one also narrows the hidden sizes of the trunk, of the vision encoder and of the score head. The config.json of the upstream repo declares Qwen3_5ForConditionalGeneration, the slice declares MiniCPMOCRForSequenceClassification so that convert_hf_to_gguf.py picks the sequence classifier (the upstream repo has no LICENSE file, it is Apache-2.0 per its model card). usage: slice_minicpm_ocr_jev.py """ import json, re, struct, sys from collections import defaultdict from concurrent.futures import ThreadPoolExecutor from pathlib import Path import requests import torch from huggingface_hub import hf_hub_url from safetensors.torch import save_file REPO = 'openbmb/MiniCPM-OCR-Jev' AUX = ['tokenizer.json', 'tokenizer_config.json', 'chat_template.jinja', 'processor_config.json', 'preprocessor_config.json', 'generation_config.json'] # text: hidden, ffn, attention heads, kv heads (head_dim stays 256), linear attention key / value heads (head dims stay 128) E, FF, H, KV, LK, LV = 32, 128, 2, 1, 2, 2 # upstream has as many key heads as value heads HD, LKD, LVD = 256, 128, 128 TEXT_LAYERS = [0, 1, 2, 3] # 3 linear attention + 1 full attention # vision: hidden, ffn, heads (head_dim stays 64) V, VFF, VH = 128, 256, 2 VISION_LAYERS = [0, 1] MERGE = 4 # 2 x 2 patches are merged DTYPES = {'BF16': torch.bfloat16, 'F32': torch.float32, 'F16': torch.float16} def read_header(url): def rng(a, b): r = requests.get(url, headers={'Range': f'bytes={a}-{b}'}, timeout=120) r.raise_for_status() return r.content n = struct.unpack('