Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ from threading import Thread
|
|
| 7 |
import transformers.modeling_rope_utils as rope_utils
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
|
| 10 |
-
# 1. Monkey-patch dynamic_rope_update if
|
| 11 |
if not hasattr(rope_utils, 'dynamic_rope_update'):
|
| 12 |
def dynamic_rope_update(rope_forward):
|
| 13 |
return rope_forward
|
|
@@ -18,7 +18,7 @@ model_id = "deepgrove/maple-preview"
|
|
| 18 |
print("🔄 Loading tokenizer...")
|
| 19 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 20 |
|
| 21 |
-
# 2. Explicitly load
|
| 22 |
try:
|
| 23 |
template_path = hf_hub_download(repo_id=model_id, filename="chat_template.jinja")
|
| 24 |
with open(template_path, "r", encoding="utf-8") as f:
|
|
@@ -36,7 +36,7 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 36 |
attn_implementation="eager",
|
| 37 |
)
|
| 38 |
|
| 39 |
-
# 3. Patch the cu_seq_lens mismatch bug in fa3.py
|
| 40 |
def patch_fa3_module():
|
| 41 |
for name, module in list(sys.modules.items()):
|
| 42 |
if 'fa3' in name and 'maple' in name:
|
|
@@ -44,21 +44,40 @@ def patch_fa3_module():
|
|
| 44 |
orig_fa_forward = module._flash_attention_forward
|
| 45 |
|
| 46 |
def patched_fa_forward(
|
| 47 |
-
query_states,
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
):
|
| 53 |
-
# FIX:
|
| 54 |
-
if cu_seq_lens_q is not None and query_states is not None:
|
| 55 |
-
# Flash Attention
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
return orig_fa_forward(
|
| 64 |
query_states, key_states, value_states, attention_mask, query_length, is_causal,
|
|
@@ -115,4 +134,4 @@ demo = gr.ChatInterface(
|
|
| 115 |
)
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|
| 118 |
-
demo.queue().launch()
|
|
|
|
| 7 |
import transformers.modeling_rope_utils as rope_utils
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
|
| 10 |
+
# 1. Monkey-patch dynamic_rope_update if missing
|
| 11 |
if not hasattr(rope_utils, 'dynamic_rope_update'):
|
| 12 |
def dynamic_rope_update(rope_forward):
|
| 13 |
return rope_forward
|
|
|
|
| 18 |
print("🔄 Loading tokenizer...")
|
| 19 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 20 |
|
| 21 |
+
# 2. Explicitly load chat template
|
| 22 |
try:
|
| 23 |
template_path = hf_hub_download(repo_id=model_id, filename="chat_template.jinja")
|
| 24 |
with open(template_path, "r", encoding="utf-8") as f:
|
|
|
|
| 36 |
attn_implementation="eager",
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# 3. Patch the cu_seq_lens mismatch bug in fa3.py
|
| 40 |
def patch_fa3_module():
|
| 41 |
for name, module in list(sys.modules.items()):
|
| 42 |
if 'fa3' in name and 'maple' in name:
|
|
|
|
| 44 |
orig_fa_forward = module._flash_attention_forward
|
| 45 |
|
| 46 |
def patched_fa_forward(
|
| 47 |
+
query_states,
|
| 48 |
+
key_states,
|
| 49 |
+
value_states,
|
| 50 |
+
attention_mask,
|
| 51 |
+
query_length,
|
| 52 |
+
is_causal,
|
| 53 |
+
dropout=0.0,
|
| 54 |
+
position_ids=None,
|
| 55 |
+
softmax_scale=None,
|
| 56 |
+
sliding_window=None,
|
| 57 |
+
use_top_left_mask=False,
|
| 58 |
+
softcap=None,
|
| 59 |
+
deterministic=None,
|
| 60 |
+
cu_seq_lens_q=None,
|
| 61 |
+
cu_seq_lens_k=None,
|
| 62 |
+
max_length_q=None,
|
| 63 |
+
max_length_k=None,
|
| 64 |
+
target_dtype=None,
|
| 65 |
+
**kwargs,
|
| 66 |
):
|
| 67 |
+
# FIX: Derive cu_seq_lens dynamically from query and key sequence lengths
|
| 68 |
+
if cu_seq_lens_q is not None and query_states is not None and key_states is not None:
|
| 69 |
+
# Flash Attention shape: (batch_size, seq_len, num_heads, head_dim)
|
| 70 |
+
bsz = query_states.shape[0]
|
| 71 |
+
q_len = query_states.shape[1]
|
| 72 |
+
k_len = key_states.shape[1]
|
| 73 |
|
| 74 |
+
q_tokens = bsz * q_len
|
| 75 |
+
k_tokens = bsz * k_len
|
| 76 |
+
|
| 77 |
+
cu_seq_lens_q = torch.tensor([0, q_tokens], device=query_states.device, dtype=torch.int32)
|
| 78 |
+
cu_seq_lens_k = torch.tensor([0, k_tokens], device=key_states.device, dtype=torch.int32)
|
| 79 |
+
max_length_q = q_len
|
| 80 |
+
max_length_k = k_len
|
| 81 |
|
| 82 |
return orig_fa_forward(
|
| 83 |
query_states, key_states, value_states, attention_mask, query_length, is_causal,
|
|
|
|
| 134 |
)
|
| 135 |
|
| 136 |
if __name__ == "__main__":
|
| 137 |
+
demo.queue().launch(ssr=False)
|