Spaces:
Paused
Paused
aeb56
commited on
Commit
Β·
2900b36
1
Parent(s):
b705945
Monkey-patch transformers to disable flash attention via wrapper script
Browse files
app.py
CHANGED
|
@@ -215,80 +215,54 @@ class ChatBot:
|
|
| 215 |
logs += f"β±οΈ Estimated time: 30-60 minutes\n\n"
|
| 216 |
yield status_table, logs
|
| 217 |
|
| 218 |
-
# Create a
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
f.write("""
|
| 225 |
-
# Fake flash_attn module that falls back to standard PyTorch attention
|
| 226 |
-
import torch
|
| 227 |
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
if causal:
|
| 237 |
-
seq_len = attn_weights.size(-1)
|
| 238 |
-
causal_mask = torch.triu(torch.ones(seq_len, seq_len, device=attn_weights.device), diagonal=1).bool()
|
| 239 |
-
attn_weights = attn_weights.masked_fill(causal_mask, float('-inf'))
|
| 240 |
-
|
| 241 |
-
attn_weights = torch.softmax(attn_weights, dim=-1)
|
| 242 |
-
|
| 243 |
-
if dropout_p > 0:
|
| 244 |
-
attn_weights = torch.nn.functional.dropout(attn_weights, p=dropout_p)
|
| 245 |
-
|
| 246 |
-
output = torch.matmul(attn_weights, v)
|
| 247 |
-
return output, None # Return None for attention weights
|
| 248 |
|
| 249 |
-
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
-
|
|
|
|
| 253 |
""")
|
| 254 |
|
| 255 |
-
|
| 256 |
-
import sys
|
| 257 |
-
if f"/tmp" not in sys.path:
|
| 258 |
-
sys.path.insert(0, "/tmp")
|
| 259 |
-
|
| 260 |
-
# Set PYTHONPATH environment variable so subprocess can find fake flash_attn
|
| 261 |
-
env = os.environ.copy()
|
| 262 |
-
pythonpath = env.get('PYTHONPATH', '')
|
| 263 |
-
env['PYTHONPATH'] = f"/tmp:{pythonpath}" if pythonpath else "/tmp"
|
| 264 |
-
|
| 265 |
-
logs += "β οΈ **Note:** Using fallback PyTorch attention (slower than flash-attn)\n\n"
|
| 266 |
yield status_table, logs
|
| 267 |
|
| 268 |
-
# Run lm_eval
|
| 269 |
-
cmd = [
|
| 270 |
-
"lm_eval",
|
| 271 |
-
"--model", "hf",
|
| 272 |
-
"--model_args", f"pretrained={MODEL_NAME},trust_remote_code=True,dtype=bfloat16,low_cpu_mem_usage=True,parallelize=True",
|
| 273 |
-
"--tasks", task_string,
|
| 274 |
-
"--batch_size", "1",
|
| 275 |
-
"--output_path", output_dir,
|
| 276 |
-
"--log_samples"
|
| 277 |
-
]
|
| 278 |
|
| 279 |
status_table = self._create_status_table(tasks_to_run, "π Running")
|
| 280 |
-
logs += f"π **Running lm_eval...**\n\
|
| 281 |
logs += "---\n\n### π Live Logs (last 15 lines):\n\n```\n"
|
| 282 |
yield status_table, logs
|
| 283 |
|
| 284 |
-
# Run evaluation
|
| 285 |
process = subprocess.Popen(
|
| 286 |
cmd,
|
| 287 |
stdout=subprocess.PIPE,
|
| 288 |
stderr=subprocess.STDOUT,
|
| 289 |
text=True,
|
| 290 |
-
bufsize=1
|
| 291 |
-
env=env # Pass custom environment with PYTHONPATH
|
| 292 |
)
|
| 293 |
|
| 294 |
output_lines = []
|
|
|
|
| 215 |
logs += f"β±οΈ Estimated time: 30-60 minutes\n\n"
|
| 216 |
yield status_table, logs
|
| 217 |
|
| 218 |
+
# Create a wrapper script that disables flash attention before running lm_eval
|
| 219 |
+
wrapper_script = f"/tmp/run_eval_{timestamp}.py"
|
| 220 |
+
with open(wrapper_script, 'w') as f:
|
| 221 |
+
f.write(f"""
|
| 222 |
+
import sys
|
| 223 |
+
import os
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
+
# Monkey-patch transformers to disable flash attention
|
| 226 |
+
import transformers.modeling_flash_attention_utils as flash_utils
|
| 227 |
+
|
| 228 |
+
def disabled_lazy_import(*args, **kwargs):
|
| 229 |
+
raise ImportError("Flash attention disabled - using eager attention")
|
| 230 |
+
|
| 231 |
+
flash_utils.lazy_import_flash_attention = disabled_lazy_import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
+
# Now run lm_eval
|
| 234 |
+
sys.argv = [
|
| 235 |
+
'lm_eval',
|
| 236 |
+
'--model', 'hf',
|
| 237 |
+
'--model_args', 'pretrained={MODEL_NAME},trust_remote_code=True,dtype=bfloat16,low_cpu_mem_usage=True,parallelize=True,attn_implementation=eager',
|
| 238 |
+
'--tasks', '{task_string}',
|
| 239 |
+
'--batch_size', '1',
|
| 240 |
+
'--output_path', '{output_dir}',
|
| 241 |
+
'--log_samples'
|
| 242 |
+
]
|
| 243 |
|
| 244 |
+
from lm_eval.__main__ import cli_evaluate
|
| 245 |
+
cli_evaluate()
|
| 246 |
""")
|
| 247 |
|
| 248 |
+
logs += "β οΈ **Note:** Flash attention disabled, using eager attention (slower but compatible)\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
yield status_table, logs
|
| 250 |
|
| 251 |
+
# Run lm_eval via wrapper script
|
| 252 |
+
cmd = ["python3", wrapper_script]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
status_table = self._create_status_table(tasks_to_run, "π Running")
|
| 255 |
+
logs += f"π **Running lm_eval...**\n\nTasks: {task_string}\n\n"
|
| 256 |
logs += "---\n\n### π Live Logs (last 15 lines):\n\n```\n"
|
| 257 |
yield status_table, logs
|
| 258 |
|
| 259 |
+
# Run evaluation
|
| 260 |
process = subprocess.Popen(
|
| 261 |
cmd,
|
| 262 |
stdout=subprocess.PIPE,
|
| 263 |
stderr=subprocess.STDOUT,
|
| 264 |
text=True,
|
| 265 |
+
bufsize=1
|
|
|
|
| 266 |
)
|
| 267 |
|
| 268 |
output_lines = []
|