Spaces:
Running
Running
Fix IndentationError in ops.py patch
Browse filesPrevious patch had wrong indentation in replacement string.
Now using line-by-line replacement to preserve exact indentation.
Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
app.py
CHANGED
|
@@ -37,79 +37,44 @@ def _patch_qwen_for_mig_gpu():
|
|
| 37 |
return
|
| 38 |
|
| 39 |
with open(ops_file, 'r') as f:
|
| 40 |
-
|
| 41 |
|
| 42 |
# Check if patch already applied
|
|
|
|
| 43 |
if 'MIG GPU CUBLAS fix' in content:
|
| 44 |
print("[OK] Qwen MIG GPU patch already applied")
|
| 45 |
return
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
if
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
# MIG GPU CUBLAS fix: Force float32 for linear ops to avoid CUBLAS errors
|
| 74 |
-
try:
|
| 75 |
-
return torch.nn.functional.linear(input, weight, bias)
|
| 76 |
-
except RuntimeError as e:
|
| 77 |
-
if 'CUBLAS' in str(e):
|
| 78 |
-
# Force everything to float32 and retry
|
| 79 |
-
input_f32 = input.float()
|
| 80 |
-
weight_f32 = weight.float()
|
| 81 |
-
bias_f32 = bias.float() if bias is not None else None
|
| 82 |
-
result = torch.nn.functional.linear(input_f32, weight_f32, bias_f32)
|
| 83 |
-
return result.to(input.dtype)
|
| 84 |
-
raise'''
|
| 85 |
-
|
| 86 |
-
if original_code in content:
|
| 87 |
-
patched_content = content.replace(original_code, patched_code)
|
| 88 |
with open(ops_file, 'w') as f:
|
| 89 |
-
f.
|
| 90 |
print("[OK] Applied MIG GPU CUBLAS fix to linear operations")
|
| 91 |
else:
|
| 92 |
-
|
| 93 |
-
if 'return torch.nn.functional.linear(input, weight, bias)' in content:
|
| 94 |
-
patched_content = content.replace(
|
| 95 |
-
' return torch.nn.functional.linear(input, weight, bias)',
|
| 96 |
-
''' # MIG GPU CUBLAS fix
|
| 97 |
-
try:
|
| 98 |
-
return torch.nn.functional.linear(input, weight, bias)
|
| 99 |
-
except RuntimeError as e:
|
| 100 |
-
if 'CUBLAS' in str(e):
|
| 101 |
-
input_f32 = input.float()
|
| 102 |
-
weight_f32 = weight.float()
|
| 103 |
-
bias_f32 = bias.float() if bias is not None else None
|
| 104 |
-
result = torch.nn.functional.linear(input_f32, weight_f32, bias_f32)
|
| 105 |
-
return result.to(input.dtype)
|
| 106 |
-
raise'''
|
| 107 |
-
)
|
| 108 |
-
with open(ops_file, 'w') as f:
|
| 109 |
-
f.write(patched_content)
|
| 110 |
-
print("[OK] Applied MIG GPU CUBLAS fix (fallback pattern)")
|
| 111 |
-
else:
|
| 112 |
-
print("[WARN] Could not find patch location in ops.py")
|
| 113 |
|
| 114 |
def setup():
|
| 115 |
"""Environment setup for Hugging Face Space"""
|
|
|
|
| 37 |
return
|
| 38 |
|
| 39 |
with open(ops_file, 'r') as f:
|
| 40 |
+
lines = f.readlines()
|
| 41 |
|
| 42 |
# Check if patch already applied
|
| 43 |
+
content = ''.join(lines)
|
| 44 |
if 'MIG GPU CUBLAS fix' in content:
|
| 45 |
print("[OK] Qwen MIG GPU patch already applied")
|
| 46 |
return
|
| 47 |
|
| 48 |
+
# Find and patch the return statement in forward_comfy_cast_weights
|
| 49 |
+
patched = False
|
| 50 |
+
for i, line in enumerate(lines):
|
| 51 |
+
if 'return torch.nn.functional.linear(input, weight, bias)' in line and not patched:
|
| 52 |
+
indent = len(line) - len(line.lstrip())
|
| 53 |
+
space = ' ' * indent
|
| 54 |
+
# Replace the single return line with try-except block
|
| 55 |
+
new_lines = [
|
| 56 |
+
f'{space}# MIG GPU CUBLAS fix\n',
|
| 57 |
+
f'{space}try:\n',
|
| 58 |
+
f'{space} return torch.nn.functional.linear(input, weight, bias)\n',
|
| 59 |
+
f'{space}except RuntimeError as e:\n',
|
| 60 |
+
f'{space} if "CUBLAS" in str(e):\n',
|
| 61 |
+
f'{space} input_f32 = input.float()\n',
|
| 62 |
+
f'{space} weight_f32 = weight.float()\n',
|
| 63 |
+
f'{space} bias_f32 = bias.float() if bias is not None else None\n',
|
| 64 |
+
f'{space} result = torch.nn.functional.linear(input_f32, weight_f32, bias_f32)\n',
|
| 65 |
+
f'{space} return result.to(input.dtype)\n',
|
| 66 |
+
f'{space} raise\n'
|
| 67 |
+
]
|
| 68 |
+
lines[i:i+1] = new_lines
|
| 69 |
+
patched = True
|
| 70 |
+
break
|
| 71 |
+
|
| 72 |
+
if patched:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
with open(ops_file, 'w') as f:
|
| 74 |
+
f.writelines(lines)
|
| 75 |
print("[OK] Applied MIG GPU CUBLAS fix to linear operations")
|
| 76 |
else:
|
| 77 |
+
print("[WARN] Could not find patch location in ops.py")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
def setup():
|
| 80 |
"""Environment setup for Hugging Face Space"""
|