dagloop5 commited on
Commit
267ab70
·
verified ·
1 Parent(s): 6565fcd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -8
app.py CHANGED
@@ -39,20 +39,39 @@ import shutil
39
 
40
  import spaces
41
  import torch
 
42
  torch._dynamo.config.suppress_errors = True
43
  torch._dynamo.config.disable = True
44
 
45
  _original_tensor_to = torch.Tensor.to
46
- def _coerce_cuda_str(self, *args, **kwargs):
47
- if args and isinstance(args[0], torch.device) and args[0].type == "cuda":
48
- args = ("cuda", *args[1:])
49
- if kwargs.get("device") is not None:
50
- dev = kwargs["device"]
51
- if isinstance(dev, torch.device) and dev.type == "cuda":
52
- kwargs = {**kwargs, "device": "cuda"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  return _original_tensor_to(self, *args, **kwargs)
54
 
55
- torch.Tensor.to = _coerce_cuda_str
 
56
 
57
  import gradio as gr
58
  import numpy as np
 
39
 
40
  import spaces
41
  import torch
42
+
43
  torch._dynamo.config.suppress_errors = True
44
  torch._dynamo.config.disable = True
45
 
46
  _original_tensor_to = torch.Tensor.to
47
+
48
+
49
+ def _is_cuda_target(x):
50
+ return (
51
+ x == "cuda"
52
+ or (isinstance(x, torch.device) and x.type == "cuda")
53
+ or (isinstance(x, str) and x.startswith("cuda"))
54
+ or (isinstance(x, int) and x == 0)
55
+ )
56
+
57
+
58
+ def _spaces_safe_to(self, *args, **kwargs):
59
+ """ZeroGPU emulates bare .to('cuda'), but LTX-2 uses non_blocking/copy."""
60
+ if args and _is_cuda_target(args[0]):
61
+ # keep dtype if it was passed positionally after the device
62
+ new_args = ("cuda",) + args[1:]
63
+ new_kwargs = {k: v for k, v in kwargs.items() if k not in ("non_blocking", "copy")}
64
+ return _original_tensor_to(self, *new_args, **new_kwargs)
65
+
66
+ if kwargs.get("device") is not None and _is_cuda_target(kwargs["device"]):
67
+ new_kwargs = {k: v for k, v in kwargs.items() if k not in ("non_blocking", "copy")}
68
+ new_kwargs["device"] = "cuda"
69
+ return _original_tensor_to(self, *args, **new_kwargs)
70
+
71
  return _original_tensor_to(self, *args, **kwargs)
72
 
73
+
74
+ torch.Tensor.to = _spaces_safe_to
75
 
76
  import gradio as gr
77
  import numpy as np