(
self, input_ids, attention_mask, mllm_output, action_cond_features, current_image_index=None, **kwargs
)
| 440 | |
| 441 | |
| 442 | def encode_condition( |
| 443 | self, input_ids, attention_mask, mllm_output, action_cond_features, current_image_index=None, **kwargs |
| 444 | ): |
| 445 | prompt_embeds = mllm_output.hidden_states |
| 446 | embeddings = mllm_output.hidden_states[0] |
| 447 | |
| 448 | repeats = 1 if action_cond_features is None else action_cond_features.shape[0] // input_ids.shape[0] |
| 449 | if current_image_index is None: |
| 450 | current_image_index = torch.full( |
| 451 | (input_ids.shape[0],), |
| 452 | 1 if self.config.use_history_obs else 0, |
| 453 | dtype=torch.long, |
| 454 | device=input_ids.device, |
| 455 | ) |
| 456 | elif not isinstance(current_image_index, torch.Tensor): |
| 457 | current_image_index = torch.tensor(current_image_index, dtype=torch.long, device=input_ids.device) |
| 458 | else: |
| 459 | current_image_index = current_image_index.to(device=input_ids.device, dtype=torch.long) |
| 460 | |
| 461 | if repeats > 1: |
| 462 | input_ids = input_ids.repeat_interleave(repeats, dim=0) |
| 463 | attention_mask = attention_mask.repeat_interleave(repeats, dim=0) |
| 464 | embeddings = embeddings.repeat_interleave(repeats, dim=0) |
| 465 | prompt_embeds =[p.repeat_interleave(repeats, dim=0) for p in prompt_embeds] |
| 466 | current_image_index = current_image_index.repeat_interleave(repeats, dim=0) |
| 467 | |
| 468 | if self.tokenizer.num_metaqueries > 0: |
| 469 | # Get positions for all sequences in batch at once |
| 470 | boi_pos = torch.where(input_ids == self.boi_token_id)[1] |
| 471 | eoi_pos = torch.where(input_ids == self.eoi_token_id)[1] |
| 472 | |
| 473 | def get_vision_positions(input_ids, token_id, image_indices): |
| 474 | positions = torch.full((input_ids.size(0),), -1, dtype=torch.long, device=input_ids.device) |
| 475 | rows, cols = torch.where(input_ids == token_id) |
| 476 | for r in rows.unique(): |
| 477 | cols_r = cols[rows == r].sort().values |
| 478 | image_idx = min(int(image_indices[r].item()), cols_r.numel() - 1) |
| 479 | positions[r] = cols_r[image_idx] |
| 480 | return positions |
| 481 | |
| 482 | vision_start = get_vision_positions(input_ids, self.vision_start_token_id, current_image_index) |
| 483 | vision_end = get_vision_positions(input_ids, self.vision_end_token_id, current_image_index) |
| 484 | |
| 485 | |
| 486 | # Create mask for selecting tokens between BOI and EOI |
| 487 | batch_size, seq_len = input_ids.shape |
| 488 | indices = torch.arange(seq_len, device=input_ids.device)[None, :].expand( |
| 489 | batch_size, -1 |
| 490 | ) |
| 491 | |
| 492 | prompt_embeds_mask = (indices > boi_pos[:, None]) & (indices < eoi_pos[:, None]) |
| 493 | embeddings_mask = (indices > vision_start[:, None]) & (indices < vision_end[:, None]) |
| 494 | |
| 495 | embeddings = embeddings[embeddings_mask].view( |
| 496 | batch_size, -1, embeddings.size(-1) |
| 497 | ) |
| 498 | |
| 499 | prompt_embeds_all_layer = [] |
no outgoing calls
no test coverage detected