Image-Text-to-Text
MLX
Safetensors
Chinese
English
qwen2_5_vl
ocr
document-parsing
multimodal
mxfp8
apple-silicon
conversational
custom_code
8-bit precision
Instructions to use sahilchachra/TeleOCR-MXFP8 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use sahilchachra/TeleOCR-MXFP8 with MLX:
# Make sure mlx-vlm is installed # pip install --upgrade mlx-vlm from mlx_vlm import load, generate from mlx_vlm.prompt_utils import apply_chat_template from mlx_vlm.utils import load_config # Load the model model, processor = load("sahilchachra/TeleOCR-MXFP8") config = load_config("sahilchachra/TeleOCR-MXFP8") # Prepare input image = ["http://images.cocodataset.org/val2017/000000039769.jpg"] prompt = "Describe this image." # Apply chat template formatted_prompt = apply_chat_template( processor, config, prompt, num_images=1 ) # Generate output output = generate(model, processor, formatted_prompt, image) print(output) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- Atomic Chat
Upload patch_qwen25vl_qknorm.py with huggingface_hub
Browse files- patch_qwen25vl_qknorm.py +117 -0
patch_qwen25vl_qknorm.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""TeleOCR uses a Qwen2.5-VL vision tower + Qwen2.5-VL mrope, but its text
|
| 2 |
+
attention is patched with per-head QK-RMSNorm (Qwen3-style) and no qkv bias
|
| 3 |
+
(confirmed against raw/modeling_naviocr.py: Qwen2_5_VLAttention, lines ~615-711).
|
| 4 |
+
mlx-vlm's stock qwen2_5_vl.language.Attention has neither. Monkeypatch it here,
|
| 5 |
+
import before any load_model()/load_config() call for this model.
|
| 6 |
+
"""
|
| 7 |
+
from dataclasses import dataclass
|
| 8 |
+
from typing import Optional
|
| 9 |
+
|
| 10 |
+
import mlx.core as mx
|
| 11 |
+
import mlx.nn as nn
|
| 12 |
+
|
| 13 |
+
import mlx_vlm.models.qwen2_5_vl as qwen2_5_vl_pkg
|
| 14 |
+
import mlx_vlm.models.qwen2_5_vl.config as qconfig
|
| 15 |
+
import mlx_vlm.models.qwen2_5_vl.language as language
|
| 16 |
+
import mlx_vlm.models.qwen2_5_vl.qwen2_5_vl as qwen2_5_vl_module
|
| 17 |
+
from mlx_vlm.models.cache import KVCache
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@dataclass
|
| 21 |
+
class TextConfigWithHeadDim(qconfig.TextConfig):
|
| 22 |
+
head_dim: Optional[int] = None
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
qconfig.TextConfig = TextConfigWithHeadDim
|
| 26 |
+
qwen2_5_vl_module.TextConfig = TextConfigWithHeadDim
|
| 27 |
+
qwen2_5_vl_pkg.TextConfig = TextConfigWithHeadDim
|
| 28 |
+
language.TextConfig = TextConfigWithHeadDim
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class QKNormAttention(nn.Module):
|
| 32 |
+
def __init__(self, args):
|
| 33 |
+
super().__init__()
|
| 34 |
+
|
| 35 |
+
dim = args.hidden_size
|
| 36 |
+
self.n_heads = n_heads = args.num_attention_heads
|
| 37 |
+
assert args.num_key_value_heads is not None
|
| 38 |
+
self.n_kv_heads = n_kv_heads = args.num_key_value_heads
|
| 39 |
+
|
| 40 |
+
self.head_dim = head_dim = getattr(
|
| 41 |
+
args, "head_dim", args.hidden_size // n_heads
|
| 42 |
+
)
|
| 43 |
+
self.scale = head_dim**-0.5
|
| 44 |
+
|
| 45 |
+
self.q_proj = nn.Linear(dim, n_heads * head_dim, bias=False)
|
| 46 |
+
self.k_proj = nn.Linear(dim, n_kv_heads * head_dim, bias=False)
|
| 47 |
+
self.v_proj = nn.Linear(dim, n_kv_heads * head_dim, bias=False)
|
| 48 |
+
self.o_proj = nn.Linear(n_heads * head_dim, dim, bias=False)
|
| 49 |
+
|
| 50 |
+
self.q_norm = nn.RMSNorm(head_dim, eps=args.rms_norm_eps)
|
| 51 |
+
self.k_norm = nn.RMSNorm(head_dim, eps=args.rms_norm_eps)
|
| 52 |
+
|
| 53 |
+
self.rope_scaling = args.rope_scaling
|
| 54 |
+
|
| 55 |
+
self.rotary_emb = language.Qwen2RotaryEmbedding(
|
| 56 |
+
head_dim,
|
| 57 |
+
max_position_embeddings=args.max_position_embeddings,
|
| 58 |
+
base=args.rope_theta,
|
| 59 |
+
rope_scaling=args.rope_scaling,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
def __call__(
|
| 63 |
+
self,
|
| 64 |
+
x: mx.array,
|
| 65 |
+
mask: Optional[mx.array] = None,
|
| 66 |
+
cache: Optional[KVCache] = None,
|
| 67 |
+
position_ids: Optional[mx.array] = None,
|
| 68 |
+
position_embeddings: Optional[tuple] = None,
|
| 69 |
+
) -> mx.array:
|
| 70 |
+
B, L, D = x.shape
|
| 71 |
+
|
| 72 |
+
queries, keys, values = self.q_proj(x), self.k_proj(x), self.v_proj(x)
|
| 73 |
+
|
| 74 |
+
queries = self.q_norm(
|
| 75 |
+
queries.reshape(B, L, self.n_heads, self.head_dim)
|
| 76 |
+
).transpose(0, 2, 1, 3)
|
| 77 |
+
keys = self.k_norm(
|
| 78 |
+
keys.reshape(B, L, self.n_kv_heads, self.head_dim)
|
| 79 |
+
).transpose(0, 2, 1, 3)
|
| 80 |
+
values = values.reshape(B, L, self.n_kv_heads, self.head_dim).transpose(
|
| 81 |
+
0, 2, 1, 3
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
kv_seq_len = keys.shape[-2]
|
| 85 |
+
|
| 86 |
+
if position_ids is None:
|
| 87 |
+
kv_seq_len += cache.offset + 1
|
| 88 |
+
position_ids = mx.arange(cache.offset, cache.offset + L)
|
| 89 |
+
position_ids = mx.expand_dims(position_ids, axis=0)
|
| 90 |
+
position_ids = mx.tile(position_ids, (3, B, 1))
|
| 91 |
+
else:
|
| 92 |
+
kv_seq_len += cache.offset + 1 if cache is not None else 0
|
| 93 |
+
|
| 94 |
+
if position_embeddings is None:
|
| 95 |
+
queries, keys = self.rotary_emb.apply_rotary(
|
| 96 |
+
queries, keys, position_ids, unsqueeze_dim=1,
|
| 97 |
+
)
|
| 98 |
+
else:
|
| 99 |
+
cos, sin = position_embeddings
|
| 100 |
+
queries, keys = language.apply_multimodal_rotary_pos_emb(
|
| 101 |
+
queries, keys, cos, sin, unqueeze_dim=1
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
if mask is not None and isinstance(mask, mx.array):
|
| 105 |
+
mask = mask[..., : keys.shape[-2]]
|
| 106 |
+
|
| 107 |
+
if cache is not None:
|
| 108 |
+
keys, values = cache.update_and_fetch(keys, values)
|
| 109 |
+
|
| 110 |
+
output = language.scaled_dot_product_attention(
|
| 111 |
+
queries, keys, values, cache, scale=self.scale, mask=mask
|
| 112 |
+
)
|
| 113 |
+
output = output.transpose(0, 2, 1, 3).reshape(B, L, -1)
|
| 114 |
+
return self.o_proj(output)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
language.Attention = QKNormAttention
|