#!/usr/bin/env python3 """ Build a tiny slice of Cloudflare/clef (clef 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 5120 token embedding, so this one also narrows the hidden sizes of the trunk, of the vision encoder and of the joint head. usage: slice_clef.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 = 'Cloudflare/clef' AUX = ['tokenizer.json', 'tokenizer_config.json', 'chat_template.jinja', 'processor_config.json', 'generation_config.json', 'LICENSE'] # 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, 6 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 72) V, VFF, VH = 144, 288, 2 VISION_LAYERS = [0, 1] MERGE = 4 # 2 x 2 patches are merged # joint head: width, heads (head_dim 1024 / 16 = 64 -> 16), ffn. the routing and joint layer counts are kept JW, JH, JFF = 32, 2, 64 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('