AI Agent commited on
Commit
a998b70
·
1 Parent(s): 2b36e91

Monkey-patch torch.autocast to natively support T4 Turing GPUs by intercepting bfloat16 demands

Browse files
Files changed (1) hide show
  1. app.py +11 -0
app.py CHANGED
@@ -31,6 +31,17 @@ if not torch.cuda.is_available():
31
  return __orig_fn(*args, **kwargs)
32
  setattr(torch, name, patched_fn)
33
 
 
 
 
 
 
 
 
 
 
 
 
34
  # ── SAM 3 Imports ────────────────────────────────────────────────
35
  try:
36
  from sam3.model_builder import build_sam3_image_model
 
31
  return __orig_fn(*args, **kwargs)
32
  setattr(torch, name, patched_fn)
33
 
34
+ # Intercept Meta's hardcoded BFloat16 autocast (T4 Turing GPUs don't support BFloat16 hardware math)
35
+ original_autocast = torch.autocast
36
+ class PatchedAutocast(original_autocast):
37
+ def __init__(self, device_type, dtype=None, *args, **kwargs):
38
+ if dtype == torch.bfloat16 and torch.cuda.is_available() and not torch.cuda.is_bf16_supported():
39
+ dtype = torch.float16 # Fallback to fp16, supported perfectly by T4 Turing NVidia cards
40
+ if device_type == 'cuda' and not torch.cuda.is_available():
41
+ device_type = 'cpu'
42
+ super().__init__(device_type, dtype, *args, **kwargs)
43
+ torch.autocast = PatchedAutocast
44
+
45
  # ── SAM 3 Imports ────────────────────────────────────────────────
46
  try:
47
  from sam3.model_builder import build_sam3_image_model