SaniaE commited on
Commit
bbd7b9a
·
verified ·
1 Parent(s): 66938b9

optimized 4 captions

Browse files
Files changed (1) hide show
  1. app.py +14 -17
app.py CHANGED
@@ -83,37 +83,36 @@ async def startup_event():
83
 
84
  def _generate_balanced_4_track(image, temp, max_len=15):
85
  """
86
- Generates exactly 4 captions (2 from BLIP, 2 from ViT)
87
- using single-pass batching for both models.
88
  """
89
  captions = []
90
 
91
  with torch.inference_mode():
92
- # Track A: Batched BLIP Pass (Generates 2 samples simultaneously)
93
  b_data = MODELS["blip"]
94
  b_inputs = b_data["processor"](images=image, return_tensors="pt")
95
  b_pixels = b_inputs.pixel_values.to(DEVICE)
96
- batched_b_pixels = b_pixels.repeat(2, 1, 1, 1) # Parallel batch dimension
97
 
98
  b_ids = b_data["model"].generate(
99
  pixel_values=batched_b_pixels,
100
  max_new_tokens=max_len,
101
- do_sample=True,
102
- temperature=temp,
103
- top_k=20, # Restrict search space
104
- top_p=0.8, # Clip probability tail
105
- early_stopping=True, # Instant termination
106
- use_cache=True # Fast KV-caching
107
  )
108
  b_caps = b_data["processor"].batch_decode(b_ids, skip_special_tokens=True)
109
  captions.extend([cap.strip() for cap in b_caps])
110
 
111
- # Track B: Batched ViT Pass (Generates 2 samples simultaneously)
112
  v_data = MODELS["vit"]
113
  i_proc, t_proc = v_data["processor"]
114
  v_inputs = i_proc(images=image, return_tensors="pt")
115
  v_pixels = v_inputs.pixel_values.to(DEVICE)
116
- batched_v_pixels = v_pixels.repeat(2, 1, 1, 1) # Parallel batch dimension
117
 
118
  if hasattr(v_inputs, "attention_mask") and v_inputs.attention_mask is not None:
119
  batched_mask = v_inputs.attention_mask.to(DEVICE).repeat(2, 1)
@@ -124,10 +123,9 @@ def _generate_balanced_4_track(image, temp, max_len=15):
124
  pixel_values=batched_v_pixels,
125
  attention_mask=batched_mask,
126
  max_new_tokens=max_len,
127
- do_sample=True,
128
- temperature=temp,
129
- top_k=20,
130
- top_p=0.8,
131
  early_stopping=True,
132
  use_cache=True
133
  )
@@ -135,7 +133,6 @@ def _generate_balanced_4_track(image, temp, max_len=15):
135
  captions.extend([cap.strip() for cap in v_caps])
136
 
137
  return captions
138
-
139
  # --- Endpoints ---
140
 
141
  @app.post("/generate")
 
83
 
84
  def _generate_balanced_4_track(image, temp, max_len=15):
85
  """
86
+ Symmetrical 4-Caption Engine optimized with Contrastive Search
87
+ to eliminate padding-token calculation latency.
88
  """
89
  captions = []
90
 
91
  with torch.inference_mode():
92
+ # Track A: Contrastive BLIP Pass (2 Parallel Variations)
93
  b_data = MODELS["blip"]
94
  b_inputs = b_data["processor"](images=image, return_tensors="pt")
95
  b_pixels = b_inputs.pixel_values.to(DEVICE)
96
+ batched_b_pixels = b_pixels.repeat(2, 1, 1, 1)
97
 
98
  b_ids = b_data["model"].generate(
99
  pixel_values=batched_b_pixels,
100
  max_new_tokens=max_len,
101
+ do_sample=False, # Disable random sampling overhead
102
+ penalty_alpha=0.6, # Contrastive penalty anchor
103
+ top_k=4, # Narrow contrastive candidate pool
104
+ early_stopping=True,
105
+ use_cache=True
 
106
  )
107
  b_caps = b_data["processor"].batch_decode(b_ids, skip_special_tokens=True)
108
  captions.extend([cap.strip() for cap in b_caps])
109
 
110
+ # Track B: Contrastive ViT Pass (2 Parallel Variations)
111
  v_data = MODELS["vit"]
112
  i_proc, t_proc = v_data["processor"]
113
  v_inputs = i_proc(images=image, return_tensors="pt")
114
  v_pixels = v_inputs.pixel_values.to(DEVICE)
115
+ batched_v_pixels = v_pixels.repeat(2, 1, 1, 1)
116
 
117
  if hasattr(v_inputs, "attention_mask") and v_inputs.attention_mask is not None:
118
  batched_mask = v_inputs.attention_mask.to(DEVICE).repeat(2, 1)
 
123
  pixel_values=batched_v_pixels,
124
  attention_mask=batched_mask,
125
  max_new_tokens=max_len,
126
+ do_sample=False, # Disable random sampling overhead
127
+ penalty_alpha=0.6, # Contrastive penalty anchor
128
+ top_k=4, # Narrow contrastive candidate pool
 
129
  early_stopping=True,
130
  use_cache=True
131
  )
 
133
  captions.extend([cap.strip() for cap in v_caps])
134
 
135
  return captions
 
136
  # --- Endpoints ---
137
 
138
  @app.post("/generate")