dennny123 Claude Sonnet 4.5 (1M context) commited on
Commit
fb34b62
·
1 Parent(s): f7908e9

Fix IndentationError in ops.py patch

Browse files

Previous 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>

Files changed (1) hide show
  1. app.py +29 -64
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
- content = f.read()
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
- # Patch the linear operation to use float32 on MIG GPUs
48
- original_code = ''' def forward_comfy_cast_weights(self, input, weight, bias=None, weight_dtype=None, bias_dtype=None):
49
- if weight_dtype is not None:
50
- weight = comfy.model_management.cast_to_device(weight, input.device, weight_dtype)
51
- else:
52
- weight = comfy.model_management.cast_to_device(weight, input.device, torch.float32)
53
-
54
- if bias is not None:
55
- if bias_dtype is not None:
56
- bias = comfy.model_management.cast_to_device(bias, input.device, bias_dtype)
57
- else:
58
- bias = comfy.model_management.cast_to_device(bias, input.device, torch.float32)
59
- return torch.nn.functional.linear(input, weight, bias)'''
60
-
61
- patched_code = ''' def forward_comfy_cast_weights(self, input, weight, bias=None, weight_dtype=None, bias_dtype=None):
62
- if weight_dtype is not None:
63
- weight = comfy.model_management.cast_to_device(weight, input.device, weight_dtype)
64
- else:
65
- weight = comfy.model_management.cast_to_device(weight, input.device, torch.float32)
66
-
67
- if bias is not None:
68
- if bias_dtype is not None:
69
- bias = comfy.model_management.cast_to_device(bias, input.device, bias_dtype)
70
- else:
71
- bias = comfy.model_management.cast_to_device(bias, input.device, torch.float32)
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.write(patched_content)
90
  print("[OK] Applied MIG GPU CUBLAS fix to linear operations")
91
  else:
92
- # Try a simpler pattern match
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"""