|
| 1 | +"""Tests for vision-safe loss computation in the standalone GRPO trainer. |
| 2 | +
|
| 3 | +These tests verify the fix for the Qwen3 vision merge attention mask crash. |
| 4 | +The root cause: manually concatenating action_ids onto prompt input_ids |
| 5 | +created inconsistent input that the model's vision merge couldn't handle. |
| 6 | +The fix: process prompt + action as a single string through the processor. |
| 7 | +
|
| 8 | +No GPU, no model weights, no API keys required. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +from unittest.mock import MagicMock, patch |
| 14 | +import io |
| 15 | + |
| 16 | +import pytest |
| 17 | +import torch |
| 18 | + |
| 19 | + |
| 20 | +# --------------------------------------------------------------------------- |
| 21 | +# Fixtures |
| 22 | +# --------------------------------------------------------------------------- |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def tiny_png() -> bytes: |
| 27 | + """A minimal valid PNG image (10x10 red).""" |
| 28 | + from PIL import Image |
| 29 | + img = Image.new("RGB", (10, 10), color=(255, 0, 0)) |
| 30 | + buf = io.BytesIO() |
| 31 | + img.save(buf, format="PNG") |
| 32 | + return buf.getvalue() |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture |
| 36 | +def mock_processor(): |
| 37 | + """A mock processor that behaves like a Qwen VLM processor. |
| 38 | +
|
| 39 | + Returns input_ids of predictable lengths so we can verify |
| 40 | + the action slicing math. |
| 41 | + """ |
| 42 | + processor = MagicMock() |
| 43 | + |
| 44 | + # Tokenizer: every 4 characters = 1 token (deterministic, length-based) |
| 45 | + # This approximates BPE behavior better than splitting on spaces |
| 46 | + tokenizer = MagicMock() |
| 47 | + |
| 48 | + def _to_ids(text): |
| 49 | + n = max(1, len(text) // 4) if text else 0 |
| 50 | + return list(range(100, 100 + n)) |
| 51 | + |
| 52 | + def encode(text, add_special_tokens=False, return_tensors=None): |
| 53 | + ids = _to_ids(text) |
| 54 | + if return_tensors == "pt": |
| 55 | + return {"input_ids": torch.tensor([ids]) if ids else torch.zeros(1, 0, dtype=torch.long)} |
| 56 | + return ids |
| 57 | + |
| 58 | + tokenizer.side_effect = encode |
| 59 | + tokenizer.encode = lambda text, **kw: encode(text, **kw) |
| 60 | + processor.tokenizer = tokenizer |
| 61 | + |
| 62 | + def process(text=None, images=None, return_tensors=None): |
| 63 | + """Simulate processor: tokenize text, add vision tensors.""" |
| 64 | + t = text[0] if isinstance(text, list) else text |
| 65 | + ids_list = _to_ids(t) |
| 66 | + ids = torch.tensor([ids_list]) if ids_list else torch.zeros(1, 0, dtype=torch.long) |
| 67 | + result = { |
| 68 | + "input_ids": ids, |
| 69 | + "attention_mask": torch.ones_like(ids), |
| 70 | + } |
| 71 | + if images is not None: |
| 72 | + # Simulate vision tensors — their exact shape doesn't matter, |
| 73 | + # what matters is they're CONSISTENT with the input_ids |
| 74 | + result["pixel_values"] = torch.randn(1, 3, 10, 10) |
| 75 | + result["image_grid_thw"] = torch.tensor([[1, 10, 10]]) |
| 76 | + return result |
| 77 | + |
| 78 | + processor.side_effect = process |
| 79 | + processor.apply_chat_template = MagicMock( |
| 80 | + side_effect=lambda msgs, **kw: "prompt tokens here" |
| 81 | + ) |
| 82 | + |
| 83 | + return processor |
| 84 | + |
| 85 | + |
| 86 | +# --------------------------------------------------------------------------- |
| 87 | +# Test 1: Processor consistency (unified vs manual concat) |
| 88 | +# --------------------------------------------------------------------------- |
| 89 | + |
| 90 | + |
| 91 | +class TestProcessorConsistency: |
| 92 | + """Verify processor(prompt+action) produces consistent inputs.""" |
| 93 | + |
| 94 | + def test_unified_includes_action_tokens(self, mock_processor): |
| 95 | + """Full text through processor includes both prompt and action.""" |
| 96 | + prompt = "You are a GUI automation agent. Given a screenshot and a goal, predict the next action." |
| 97 | + action = "Thought: I need to click the button.\nAction: CLICK(x=0.50, y=0.30)" |
| 98 | + full_text = prompt + action |
| 99 | + |
| 100 | + prompt_inputs = mock_processor(text=[prompt], images=["img"]) |
| 101 | + full_inputs = mock_processor(text=[full_text], images=["img"]) |
| 102 | + |
| 103 | + prompt_len = prompt_inputs["input_ids"].shape[1] |
| 104 | + full_len = full_inputs["input_ids"].shape[1] |
| 105 | + |
| 106 | + # Full text should be longer than prompt alone |
| 107 | + assert full_len > prompt_len, ( |
| 108 | + f"Full input ({full_len}) should be longer than prompt ({prompt_len})" |
| 109 | + ) |
| 110 | + |
| 111 | + def test_unified_has_consistent_vision_tensors(self, mock_processor): |
| 112 | + """Processor output has vision tensors consistent with input_ids.""" |
| 113 | + full_text = "prompt tokens here CLICK(x=0.5,y=0.3)" |
| 114 | + inputs = mock_processor(text=[full_text], images=["img"]) |
| 115 | + |
| 116 | + assert "input_ids" in inputs |
| 117 | + assert "pixel_values" in inputs |
| 118 | + assert "attention_mask" in inputs |
| 119 | + # Attention mask matches input_ids length |
| 120 | + assert inputs["attention_mask"].shape == inputs["input_ids"].shape |
| 121 | + |
| 122 | + def test_manual_concat_would_be_inconsistent(self, mock_processor): |
| 123 | + """Prove the old approach creates inconsistent inputs. |
| 124 | +
|
| 125 | + The old code did: |
| 126 | + prompt_inputs = processor(prompt, image) |
| 127 | + full_ids = cat(prompt_inputs["input_ids"], action_ids) |
| 128 | + full_inputs = {**prompt_inputs, "input_ids": full_ids} |
| 129 | +
|
| 130 | + This makes input_ids longer but pixel_values stay prompt-sized. |
| 131 | + The model's vision merge sees the mismatch. |
| 132 | + """ |
| 133 | + prompt = "prompt tokens here" |
| 134 | + action_text = "CLICK(x=0.5,y=0.3)" |
| 135 | + |
| 136 | + # Old approach: process prompt only |
| 137 | + prompt_inputs = mock_processor(text=[prompt], images=["img"]) |
| 138 | + prompt_ids = prompt_inputs["input_ids"] |
| 139 | + |
| 140 | + # Manually add action tokens |
| 141 | + action_ids = torch.tensor([[200]]) # one action token |
| 142 | + old_full_ids = torch.cat([prompt_ids, action_ids], dim=1) |
| 143 | + |
| 144 | + # The inconsistency: input_ids is now longer than what the |
| 145 | + # processor produced pixel_values/attention_mask for |
| 146 | + assert old_full_ids.shape[1] > prompt_inputs["attention_mask"].shape[1], ( |
| 147 | + "Manual concat makes input_ids longer than attention_mask — " |
| 148 | + "this is the root cause of the vision merge crash" |
| 149 | + ) |
| 150 | + |
| 151 | + |
| 152 | +# --------------------------------------------------------------------------- |
| 153 | +# Test 2: Action logit slicing math |
| 154 | +# --------------------------------------------------------------------------- |
| 155 | + |
| 156 | + |
| 157 | +class TestActionLogitSlicing: |
| 158 | + """Verify the math for extracting action log-probs from output logits.""" |
| 159 | + |
| 160 | + def test_slice_last_n_action_tokens(self): |
| 161 | + """Action logits are the last n_action positions in output.""" |
| 162 | + vocab_size = 100 |
| 163 | + seq_len = 20 |
| 164 | + n_action = 3 |
| 165 | + |
| 166 | + # Synthetic logits: shape (1, seq_len, vocab_size) |
| 167 | + logits = torch.randn(1, seq_len, vocab_size) |
| 168 | + |
| 169 | + # The trainer slices: logits[:, seq_len - n_action - 1 : seq_len - 1, :] |
| 170 | + al = logits[:, seq_len - n_action - 1: seq_len - 1, :] |
| 171 | + |
| 172 | + assert al.shape == (1, n_action, vocab_size), ( |
| 173 | + f"Expected (1, {n_action}, {vocab_size}), got {al.shape}" |
| 174 | + ) |
| 175 | + |
| 176 | + def test_gather_correct_token_logprobs(self): |
| 177 | + """Gathering log-probs for specific token IDs works correctly.""" |
| 178 | + vocab_size = 10 |
| 179 | + n_action = 3 |
| 180 | + |
| 181 | + # Logits where token 5 has the highest score at each position |
| 182 | + logits = torch.zeros(1, n_action, vocab_size) |
| 183 | + logits[0, :, 5] = 10.0 # token 5 is strongly preferred |
| 184 | + |
| 185 | + lp = torch.nn.functional.log_softmax(logits, dim=-1) |
| 186 | + action_ids = torch.tensor([[5, 5, 5]]) # all token 5 |
| 187 | + |
| 188 | + tlp = lp.gather(2, action_ids.unsqueeze(-1)).squeeze(-1) |
| 189 | + |
| 190 | + # Log-prob of the most likely token should be close to 0 |
| 191 | + assert tlp.sum().item() > -1.0, ( |
| 192 | + f"Log-prob sum should be near 0 for the most likely tokens, " |
| 193 | + f"got {tlp.sum().item()}" |
| 194 | + ) |
| 195 | + |
| 196 | + def test_different_sequence_lengths_same_result(self): |
| 197 | + """Slicing from the end works regardless of total sequence length. |
| 198 | +
|
| 199 | + This is the key property: after vision merge, seq_len may differ |
| 200 | + from input_ids length. Slicing from the END (not from prompt_len) |
| 201 | + always gets the right tokens. |
| 202 | + """ |
| 203 | + vocab_size = 50 |
| 204 | + n_action = 2 |
| 205 | + |
| 206 | + # Same action logits at the end, different total lengths |
| 207 | + for seq_len in [10, 15, 20, 50]: |
| 208 | + logits = torch.randn(1, seq_len, vocab_size) |
| 209 | + # Put a known pattern at the end |
| 210 | + logits[0, -3, :] = 0.0 # position before action |
| 211 | + logits[0, -3, 42] = 99.0 # token 42 at this position |
| 212 | + |
| 213 | + al = logits[:, seq_len - n_action - 1: seq_len - 1, :] |
| 214 | + assert al.shape == (1, n_action, vocab_size) |
| 215 | + # First action position should strongly prefer token 42 |
| 216 | + assert al[0, 0, 42].item() == 99.0 |
| 217 | + |
| 218 | + |
| 219 | +# --------------------------------------------------------------------------- |
| 220 | +# Test 3: _compute_rollout_loss integration |
| 221 | +# --------------------------------------------------------------------------- |
| 222 | + |
| 223 | + |
| 224 | +class TestComputeRolloutLossIntegration: |
| 225 | + """Test _compute_rollout_loss with a real tiny model (no mocks).""" |
| 226 | + |
| 227 | + @staticmethod |
| 228 | + def _make_tiny_model(vocab_size=200): |
| 229 | + """Real nn.Module — avoids MagicMock leaking into torch ops.""" |
| 230 | + import torch.nn as nn |
| 231 | + |
| 232 | + class TinyVLM(nn.Module): |
| 233 | + def __init__(self): |
| 234 | + super().__init__() |
| 235 | + self.embed = nn.Embedding(vocab_size, 16) |
| 236 | + self.head = nn.Linear(16, vocab_size) |
| 237 | + |
| 238 | + def forward(self, input_ids, **kwargs): |
| 239 | + h = self.embed(input_ids) |
| 240 | + |
| 241 | + class Out: |
| 242 | + pass |
| 243 | + |
| 244 | + out = Out() |
| 245 | + out.logits = self.head(h) |
| 246 | + return out |
| 247 | + |
| 248 | + return TinyVLM() |
| 249 | + |
| 250 | + def test_runs_without_crash(self, mock_processor, tiny_png): |
| 251 | + """The full loss computation runs end-to-end without error.""" |
| 252 | + from openadapt_evals.training.standalone.trainer import GRPOTrainer |
| 253 | + from openadapt_evals.training.standalone.config import TrainingConfig |
| 254 | + from openadapt_evals.training.standalone.waa_direct import Rollout, RolloutStep |
| 255 | + |
| 256 | + config = TrainingConfig(vision_loss_mode="include") |
| 257 | + trainer = GRPOTrainer(config) |
| 258 | + trainer._processor = mock_processor |
| 259 | + trainer._config = config |
| 260 | + trainer._model = self._make_tiny_model() |
| 261 | + |
| 262 | + step = RolloutStep( |
| 263 | + screenshot=tiny_png, |
| 264 | + action=MagicMock(type="click", x=0.5, y=0.3), |
| 265 | + raw_text="CLICK(x=0.50, y=0.30)", |
| 266 | + reward=0.0, |
| 267 | + ) |
| 268 | + rollout = Rollout( |
| 269 | + task_id="test", instruction="Click the button", |
| 270 | + steps=[step], reward=1.0, |
| 271 | + ) |
| 272 | + |
| 273 | + loss = trainer._compute_rollout_loss(rollout, advantage=1.0, scale=1.0) |
| 274 | + assert isinstance(loss, float) |
| 275 | + assert loss != 0.0, "Loss should be non-zero with advantage=1.0" |
| 276 | + |
| 277 | + def test_exclude_mode_strips_vision_keys(self, mock_processor, tiny_png): |
| 278 | + """In exclude mode, vision tensors are not passed to the model.""" |
| 279 | + from openadapt_evals.training.standalone.trainer import GRPOTrainer |
| 280 | + from openadapt_evals.training.standalone.config import TrainingConfig |
| 281 | + from openadapt_evals.training.standalone.waa_direct import Rollout, RolloutStep |
| 282 | + |
| 283 | + config = TrainingConfig(vision_loss_mode="exclude") |
| 284 | + trainer = GRPOTrainer(config) |
| 285 | + trainer._processor = mock_processor |
| 286 | + trainer._config = config |
| 287 | + |
| 288 | + model = self._make_tiny_model() |
| 289 | + captured = {} |
| 290 | + orig_forward = model.forward |
| 291 | + |
| 292 | + def spy_forward(input_ids, **kwargs): |
| 293 | + captured.update(kwargs) |
| 294 | + captured["input_ids_shape"] = input_ids.shape |
| 295 | + return orig_forward(input_ids, **kwargs) |
| 296 | + |
| 297 | + model.forward = spy_forward |
| 298 | + trainer._model = model |
| 299 | + |
| 300 | + step = RolloutStep( |
| 301 | + screenshot=tiny_png, |
| 302 | + action=MagicMock(type="click", x=0.5, y=0.3), |
| 303 | + raw_text="CLICK(x=0.50, y=0.30)", reward=0.0, |
| 304 | + ) |
| 305 | + rollout = Rollout( |
| 306 | + task_id="test", instruction="Click the button", |
| 307 | + steps=[step], reward=1.0, |
| 308 | + ) |
| 309 | + |
| 310 | + trainer._compute_rollout_loss(rollout, advantage=1.0, scale=1.0) |
| 311 | + |
| 312 | + assert "pixel_values" not in captured, "exclude mode should strip pixel_values" |
| 313 | + assert "image_grid_thw" not in captured, "exclude mode should strip image_grid_thw" |
0 commit comments