dennny123 Claude Sonnet 4.5 (1M context) commited on
Commit
e90ff32
·
1 Parent(s): 5c8c826

Comprehensive MIG GPU fix - patch BOTH llama.py and ops.py

Browse files

The CUBLAS error occurs in TWO locations:
1. llama.py:301 - frequency computation (freqs = matmul)
2. ops.py:157 - linear operations (q_proj/k_proj/v_proj)

Both now have CPU fallback on CUBLAS errors.
This should finally fix the MIG GPU incompatibility.

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>

Files changed (1) hide show
  1. app.py +64 -41
app.py CHANGED
@@ -31,48 +31,71 @@ COMFYUI_DIR = os.path.join(ROOT_DIR, "ComfyUI")
31
  BYPASS_REPO_DIR = os.path.join(ROOT_DIR, "reference_repo")
32
 
33
  def _patch_qwen_for_mig_gpu():
34
- """Force Qwen text encoder to CPU - MIG GPU incompatible"""
35
- llama_file = os.path.join(COMFYUI_DIR, "comfy/text_encoders/llama.py")
36
- if not os.path.exists(llama_file):
37
- return
38
-
39
- with open(llama_file, 'r') as f:
40
- lines = f.readlines()
41
 
42
- # Check if patch already applied
43
- content = ''.join(lines)
44
- if 'MIG GPU: force CPU' in content:
45
- print("[OK] Qwen CPU fallback already applied")
46
- return
47
-
48
- # Patch the problematic matmul at line ~301
49
- patched = False
50
- for i, line in enumerate(lines):
51
- if 'freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2)' in line and not patched:
52
- indent = len(line) - len(line.lstrip())
53
- space = ' ' * indent
54
- # Force this operation to CPU to avoid CUBLAS errors on MIG GPUs
55
- new_lines = [
56
- f'{space}# MIG GPU: force CPU for matmul to avoid CUBLAS errors\n',
57
- f'{space}try:\n',
58
- f'{space} freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2)\n',
59
- f'{space}except RuntimeError as e:\n',
60
- f'{space} if "CUBLAS" in str(e):\n',
61
- f'{space} device = inv_freq_expanded.device\n',
62
- f'{space} freqs = (inv_freq_expanded.float().cpu() @ position_ids_expanded.float().cpu()).transpose(1, 2).to(device)\n',
63
- f'{space} else:\n',
64
- f'{space} raise\n'
65
- ]
66
- lines[i:i+1] = new_lines
67
- patched = True
68
- break
69
-
70
- if patched:
71
- with open(llama_file, 'w') as f:
72
- f.writelines(lines)
73
- print("[OK] Applied Qwen CPU fallback for MIG GPU")
74
- else:
75
- print("[WARN] Could not find freqs computation in llama.py")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  def setup():
78
  """Environment setup for Hugging Face Space"""
 
31
  BYPASS_REPO_DIR = os.path.join(ROOT_DIR, "reference_repo")
32
 
33
  def _patch_qwen_for_mig_gpu():
34
+ """Patch Qwen text encoder for MIG GPU - force CPU fallback on CUBLAS errors"""
 
 
 
 
 
 
35
 
36
+ # Patch 1: llama.py - frequency computation
37
+ llama_file = os.path.join(COMFYUI_DIR, "comfy/text_encoders/llama.py")
38
+ if os.path.exists(llama_file):
39
+ with open(llama_file, 'r') as f:
40
+ lines = f.readlines()
41
+ content = ''.join(lines)
42
+
43
+ if 'MIG GPU: force CPU' not in content:
44
+ patched = False
45
+ for i, line in enumerate(lines):
46
+ if 'freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2)' in line and not patched:
47
+ indent = len(line) - len(line.lstrip())
48
+ space = ' ' * indent
49
+ new_lines = [
50
+ f'{space}# MIG GPU: force CPU for matmul\n',
51
+ f'{space}try:\n',
52
+ f'{space} freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2)\n',
53
+ f'{space}except RuntimeError as e:\n',
54
+ f'{space} if "CUBLAS" in str(e):\n',
55
+ f'{space} device = inv_freq_expanded.device\n',
56
+ f'{space} freqs = (inv_freq_expanded.float().cpu() @ position_ids_expanded.float().cpu()).transpose(1, 2).to(device)\n',
57
+ f'{space} else:\n',
58
+ f'{space} raise\n'
59
+ ]
60
+ lines[i:i+1] = new_lines
61
+ patched = True
62
+ break
63
+ if patched:
64
+ with open(llama_file, 'w') as f:
65
+ f.writelines(lines)
66
+ print("[OK] Patched llama.py freqs computation")
67
+
68
+ # Patch 2: ops.py - all linear operations
69
+ ops_file = os.path.join(COMFYUI_DIR, "comfy/ops.py")
70
+ if os.path.exists(ops_file):
71
+ with open(ops_file, 'r') as f:
72
+ lines = f.readlines()
73
+ content = ''.join(lines)
74
+
75
+ if 'MIG GPU CUBLAS' not in content:
76
+ patched = False
77
+ for i, line in enumerate(lines):
78
+ if 'x = torch.nn.functional.linear(input, weight, bias)' in line and 'forward_comfy_cast_weights' in ''.join(lines[max(0,i-10):i]) and not patched:
79
+ indent = len(line) - len(line.lstrip())
80
+ space = ' ' * indent
81
+ new_lines = [
82
+ f'{space}# MIG GPU CUBLAS fix\n',
83
+ f'{space}try:\n',
84
+ f'{space} x = torch.nn.functional.linear(input, weight, bias)\n',
85
+ f'{space}except RuntimeError as e:\n',
86
+ f'{space} if "CUBLAS" in str(e):\n',
87
+ f'{space} x = torch.nn.functional.linear(input.float(), weight.float(), bias.float() if bias is not None else None)\n',
88
+ f'{space} x = x.to(input.dtype)\n',
89
+ f'{space} else:\n',
90
+ f'{space} raise\n'
91
+ ]
92
+ lines[i:i+1] = new_lines
93
+ patched = True
94
+ break
95
+ if patched:
96
+ with open(ops_file, 'w') as f:
97
+ f.writelines(lines)
98
+ print("[OK] Patched ops.py linear operations")
99
 
100
  def setup():
101
  """Environment setup for Hugging Face Space"""