LovnishVerma commited on
Commit
8417e5e
·
verified ·
1 Parent(s): 93a00cf

Upload 43 files

Browse files
Files changed (1) hide show
  1. src/vision/florence.py +16 -7
src/vision/florence.py CHANGED
@@ -21,16 +21,14 @@ except ImportError:
21
 
22
  def get_device() -> str:
23
  """Select best available device."""
24
- if IS_SPACES:
25
- return "cuda"
26
  if torch.cuda.is_available():
27
  return "cuda"
28
  elif torch.backends.mps.is_available():
29
  return "mps"
30
  return "cpu"
31
 
32
- DEVICE: str = get_device()
33
- DTYPE: torch.dtype = torch.float16 if DEVICE == "cuda" else torch.float32
34
 
35
  class FlorenceVisionEngine(VisionEngine):
36
  def __init__(self):
@@ -44,7 +42,7 @@ class FlorenceVisionEngine(VisionEngine):
44
  return
45
 
46
  try:
47
- print(f"Loading Florence-2 on {DEVICE.upper()}...")
48
 
49
  # Hotfix for Florence-2 in newer transformers versions
50
  import transformers
@@ -60,7 +58,13 @@ class FlorenceVisionEngine(VisionEngine):
60
  trust_remote_code=True,
61
  torch_dtype=DTYPE,
62
  attn_implementation="eager"
63
- ).to(DEVICE).eval()
 
 
 
 
 
 
64
 
65
  self.processor = AutoProcessor.from_pretrained(
66
  CONFIG.MODEL_NAME,
@@ -99,6 +103,11 @@ class FlorenceVisionEngine(VisionEngine):
99
  if self.model is None or self.processor is None:
100
  raise RuntimeError("Model not loaded. Call load() first.")
101
 
 
 
 
 
 
102
  image = preprocess_image(image)
103
  image = auto_enhance(image)
104
  max_tokens = CONFIG.MAX_NEW_TOKENS.get(task_token, 64)
@@ -107,7 +116,7 @@ class FlorenceVisionEngine(VisionEngine):
107
  text=task_token,
108
  images=image,
109
  return_tensors="pt",
110
- ).to(DEVICE)
111
 
112
  if "pixel_values" in inputs:
113
  inputs["pixel_values"] = inputs["pixel_values"].to(DTYPE)
 
21
 
22
  def get_device() -> str:
23
  """Select best available device."""
 
 
24
  if torch.cuda.is_available():
25
  return "cuda"
26
  elif torch.backends.mps.is_available():
27
  return "mps"
28
  return "cpu"
29
 
30
+ DEVICE: str = "cpu" if IS_SPACES else get_device()
31
+ DTYPE: torch.dtype = torch.float16 if IS_SPACES or DEVICE == "cuda" else torch.float32
32
 
33
  class FlorenceVisionEngine(VisionEngine):
34
  def __init__(self):
 
42
  return
43
 
44
  try:
45
+ print(f"Loading Florence-2 on {DEVICE.upper()} (will move to GPU during inference if on Spaces)...")
46
 
47
  # Hotfix for Florence-2 in newer transformers versions
48
  import transformers
 
58
  trust_remote_code=True,
59
  torch_dtype=DTYPE,
60
  attn_implementation="eager"
61
+ ).eval()
62
+
63
+ # Hotfix for Florence-2 processor tokenizer compatibility
64
+ if not hasattr(transformers.PreTrainedTokenizerBase, "additional_special_tokens"):
65
+ transformers.PreTrainedTokenizerBase.additional_special_tokens = property(
66
+ lambda self: getattr(self, "_additional_special_tokens", [])
67
+ )
68
 
69
  self.processor = AutoProcessor.from_pretrained(
70
  CONFIG.MODEL_NAME,
 
103
  if self.model is None or self.processor is None:
104
  raise RuntimeError("Model not loaded. Call load() first.")
105
 
106
+ # Ensure model is on the right device when inference runs
107
+ target_device = "cuda" if IS_SPACES else DEVICE
108
+ if next(self.model.parameters()).device.type != target_device:
109
+ self.model.to(target_device)
110
+
111
  image = preprocess_image(image)
112
  image = auto_enhance(image)
113
  max_tokens = CONFIG.MAX_NEW_TOKENS.get(task_token, 64)
 
116
  text=task_token,
117
  images=image,
118
  return_tensors="pt",
119
+ ).to(target_device)
120
 
121
  if "pixel_values" in inputs:
122
  inputs["pixel_values"] = inputs["pixel_values"].to(DTYPE)