Create ReadMe_Surgical_Vocab_Reduction_for_QWEN2B_DotOCRv1.5_V1.0untested_py.md
Browse files
ReadMe_Surgical_Vocab_Reduction_for_QWEN2B_DotOCRv1.5_V1.0untested_py.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Surgical Vocabulary & Parameter Reduction for Qwen2 / dots.ocr-1.5 (V1.0 Untested)
|
| 2 |
+
|
| 3 |
+
This repository contains details and scripts for **Surgical_Vocab_Reduction_for_QWEN2B_DotOCRv1.5_V1.0untested.py**, a pipeline designed to prune the large multilingual vocabulary of the **dots.ocr-1.5** model (and other Qwen2-based Vision-Language Models).
|
| 4 |
+
|
| 5 |
+
By surgically removing non-target Unicode blocks (such as Chinese, Cyrillic, Arabic, Hebrew, Japanese, and Korean) while keeping English, French, Spanish, special layout coordinate symbols, and byte-fallback sequences, this script significantly reduces both the model's disk/VRAM footprint and its output logit calculation latency during local edge inference (such as on Intel Core Ultra NPUs or mobile devices).
|
| 6 |
+
|
| 7 |
+
This implementation is inspired by and built upon the methodology outlined in [Method for Dynamically Reducing Logit Computation in LLMs](https://huggingface.co/MartialTerran/Method_for_Dynamically_Reducing_Logit_Computation_in_LLMs).
|
| 8 |
+
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
## 1. The Core Problem
|
| 12 |
+
|
| 13 |
+
Modern, state-of-the-art multimodal models like **dots.ocr-1.5** are designed for global multilingual coverage. To achieve this, they utilize very large vocabularies (Qwen2 series, for example, features a vocabulary of **151,936 tokens**).
|
| 14 |
+
|
| 15 |
+
While this makes the model highly capable globally, it introduces significant overhead for deployment scenarios limited to Western European scripts (e.g., English, French, Spanish):
|
| 16 |
+
|
| 17 |
+
1. **Parameter Inflation:** The input embedding layer (`model.embed_tokens.weight`) and the final Language Modeling head (`lm_head.weight`) scale linearly with vocabulary size. At $151,936$ tokens, these two layers alone consume hundreds of millions of parameters, adding several gigabytes of unnecessary storage and VRAM.
|
| 18 |
+
2. **Computational Overhead (Logit Latency):** During autoregressive token generation, the final layer performs a matrix multiplication of shape `[batch_size * seq_len, hidden_dim] @ [hidden_dim, vocab_size]` to compute logits. Performing this massive projection on every decoded token introduces latency, particularly on resource-constrained CPUs and edge NPUs.
|
| 19 |
+
|
| 20 |
+
---
|
| 21 |
+
|
| 22 |
+
## 2. The Surgical Reduction Method
|
| 23 |
+
|
| 24 |
+
The script executes a targeted, multi-stage compression process that shrinks the model's text processing layers without degrading its fundamental visual-spatial, OCR, or layout parsing intelligence:
|
| 25 |
+
|
| 26 |
+
```
|
| 27 |
+
[ Original VLM ] ββ> [ 1. Scan Tokenizer & Filter Vocab ] ββ> [ 2. Filter BPE Merges ]
|
| 28 |
+
β (ASCII, Latin-1, & Special Tags Only) β
|
| 29 |
+
βΌ βΌ
|
| 30 |
+
[ Pruned Weights ] <ββ [ 4. Prune Embedding/Head Tensors ] <ββ [ 3. Re-index IDs ]
|
| 31 |
+
```
|
| 32 |
+
|
| 33 |
+
### Stage A: Tokenizer Parsing & Robust Filtering
|
| 34 |
+
The script reads `tokenizer.json` and evaluates each token string against strict, multi-tier criteria to determine which to keep:
|
| 35 |
+
* **Preserve Special & Coordinate Tokens:** Bounding box markers, segment boundaries, and visual layout tags (e.g., `<|image_1|>`, `<|box_start|>`, etc.) are detected and kept to protect the spatial parsing mechanism.
|
| 36 |
+
* **Preserve Byte-Fallback Mappings:** To prevent the tokenizer from failing when encountering out-of-vocabulary characters, any token representing a single byte (Latin-1 range `0x00` - `0xFF` or length-1 strings) is retained.
|
| 37 |
+
* **Targeted Script Deletion:** Multi-character tokens containing Chinese characters, Japanese Kana, Hangul, Cyrillic, Arabic, Devanagari, or Thai are discarded.
|
| 38 |
+
* **Targeted Language Retention:** Standard ASCII punctuation, numbers, and the accented character sets of English, French, and Spanish (using Latin-1 and Latin Extended-A Unicode ranges) are preserved.
|
| 39 |
+
|
| 40 |
+
### Stage B: Re-Indexing & Merge List Sanitization
|
| 41 |
+
* Discarded tokens are stripped, and the remaining tokens are sequentially re-indexed from $0$ to $N_{\text{new}} - 1$.
|
| 42 |
+
* The Byte Pair Encoding (BPE) `"merges"` rules are filtered. If a merge pair contains a sub-token or output token that was deleted, the merge rule is safely omitted. This prevents tokenizer errors or lookup warnings.
|
| 43 |
+
|
| 44 |
+
### Stage C: Surgical Tensor Pruning
|
| 45 |
+
* The script scans the model directory for weights files (both PyTorch `.bin` and Safetensors `.safetensors` formats).
|
| 46 |
+
* It locates the input embedding weights (`model.embed_tokens.weight` or `transformer.wte.weight`) and the output projection head weights (`lm_head.weight`).
|
| 47 |
+
* It extracts only the slices of these matrices corresponding to the indices of the retained vocabulary, updating their shapes dynamically.
|
| 48 |
+
|
| 49 |
+
### Stage D: Configuration Re-alignment
|
| 50 |
+
* Updates `config.json` with the new `"vocab_size"`.
|
| 51 |
+
* Remaps `bos_token_id`, `eos_token_id`, and `pad_token_id` to their new values inside `generation_config.json`.
|
| 52 |
+
|
| 53 |
+
---
|
| 54 |
+
|
| 55 |
+
## 3. Anticipated Benefits
|
| 56 |
+
|
| 57 |
+
* **VRAM and Storage Compression:** Shifting from $151,936$ tokens down to an estimated $30,000 \text{--} 40,000$ tokens significantly shrinks the parameters of the text layers. For a hidden size of $D = 3584$, this reduction strips away over **800 million parameters**, translating to immediate savings of up to **1.6 GB of VRAM/storage** in FP16 (or comparable savings under quantized configurations).
|
| 58 |
+
* **Improved Generation Latency:** Computing output logits against a vocabulary that is roughly $75\%$ smaller reduces the arithmetic intensity of the final `lm_head` projection layer by up to $75\%$, improving token-generation speed on resource-constrained CPUs and edge NPUs.
|
| 59 |
+
* **Codebase & Edge Independence:** By outputting standard, structurally valid Hugging Face files, the resulting compressed model can be loaded natively by downstream optimization toolkits (such as OpenVINO or ONNX Runtime) without requiring custom code layers.
|
| 60 |
+
|
| 61 |
+
---
|
| 62 |
+
|
| 63 |
+
## 4. How to Use
|
| 64 |
+
|
| 65 |
+
### Setup & Dependencies
|
| 66 |
+
Ensure your python environment has the required libraries installed:
|
| 67 |
+
```bash
|
| 68 |
+
pip install torch safetensors transformers
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
### Directory Structure
|
| 72 |
+
Place your original downloaded weights for `dots.ocr-1.5` in a source directory, and designate a destination directory for the pruned output. Example:
|
| 73 |
+
```
|
| 74 |
+
./weights/
|
| 75 |
+
βββ dots_ocr_1_5/ <-- Original weights directory
|
| 76 |
+
β βββ config.json
|
| 77 |
+
β βββ tokenizer.json
|
| 78 |
+
β βββ model-00001-of-00002.safetensors
|
| 79 |
+
β βββ ...
|
| 80 |
+
βββ dots_ocr_1_5_pruned/ <-- Target destination for pruned output
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
### Execution
|
| 84 |
+
Run the script from your terminal:
|
| 85 |
+
```bash
|
| 86 |
+
python Surgical_Vocab_Reduction_for_QWEN2B_DotOCRv1.5_V1.0untested.py
|
| 87 |
+
```
|
| 88 |
+
Upon successful execution, the script will output the pruned weights, tokenizer structures, and updated configurations directly into your target directory. You can then load, quantize (e.g., using OpenVINO for Intel NPUs), or test the compressed model.
|
| 89 |
+
|
| 90 |
+
---
|
| 91 |
+
|
| 92 |
+
## 5. Disclaimer & Untested Notice
|
| 93 |
+
|
| 94 |
+
Please note that this script is marked as **V1.0 Untested**.
|
| 95 |
+
|
| 96 |
+
While designed with strong safeguardsβsuch as preserving all special layout coordinates, maintaining the byte-fallback system, and pruning the BPE merge sequencesβ**empirical validation is required**.
|
| 97 |
+
|
| 98 |
+
Users are encouraged to run evaluations and verification benchmarks (such as measuring layout extraction accuracy, tokenization rate, and logit output accuracy) on their pruned models to confirm that model performance meets expectations for their targeted language profiles.
|