File size: 14,156 Bytes
fe2e77b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
"""
get_llm_calib_data.py

Script to extract LLM input embeddings from OpenVLA-OFT forward pass for use as calibration data in quantization.
This script captures the multimodal embeddings (vision + text + proprio) that are fed to the LLM.

The script randomly samples episodes from the dataset and captures ALL frames within each selected episode.

Run with:
    python vla-scripts/get_llm_calib_data.py \
        --vla_path <PATH/TO/CHECKPOINT> \
        --dataset_name libero_spatial_no_noops \
        --output_path ./calib_data/libero_spatial.bin \
        --num_episodes 10
"""

import json
import os
import random
import struct
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, List

import draccus
import numpy as np
import torch
import tqdm
from transformers import AutoConfig, AutoImageProcessor, AutoModelForVision2Seq, AutoProcessor

from prismatic.extern.hf.configuration_prismatic import OpenVLAConfig
from prismatic.extern.hf.modeling_prismatic import OpenVLAForActionPrediction
from prismatic.extern.hf.processing_prismatic import PrismaticImageProcessor, PrismaticProcessor
from prismatic.models.backbones.llm.prompting import PurePromptBuilder
from prismatic.util.data_utils import PaddedCollatorForActionPrediction
from prismatic.vla.action_tokenizer import ActionTokenizer
from prismatic.vla.datasets import EpisodicRLDSDataset, RLDSBatchTransform

os.environ["TOKENIZERS_PARALLELISM"] = "false"

# Binary format constants
CALIB_MAGIC = b"OPENVLA_CALIB\0\0\0"  # 16 bytes
CALIB_VERSION = 2


@dataclass
class CalibrationConfig:
    # fmt: off
    vla_path: str = "openvla/openvla-7b"                   # HuggingFace Hub ID or local path
    data_root_dir: Path = Path("modified_libero_rlds_data") # Path to RLDS dataset directory
    dataset_name: str = "libero_spatial_no_noops"          # Name of dataset (task suite)
    output_path: Path = Path("calibration_data.bin")       # Output binary file path
    num_episodes: int = -1                                 # Number of episodes to sample (-1 = all)
    num_images_in_input: int = 2                           # Number of images (1 or 2)
    use_proprio: bool = True                               # Use proprioception
    batch_size: int = 1                                    # Batch size for processing
    seed: int = 42                                         # Random seed
    targets_only: bool = False                             # Only extract action targets (no model needed)
    # fmt: on


class EpisodeTaskTransform:
    """Minimal transform to extract task language per episode."""

    def __call__(self, rlds_batch: Dict) -> Dict[str, str]:
        lang = rlds_batch["task"]["language_instruction"].decode().lower()
        return {"language_instruction": lang}


def select_episode_indices_stratified(
    cfg: CalibrationConfig,
    image_sizes,
) -> set:
    """Select episode indices with equal per-task sampling."""
    # Build a lightweight episodic dataset to map episode index -> task description.
    # We keep the same dataset construction so episode indexing matches the main pass.
    index_dataset = EpisodicRLDSDataset(
        cfg.data_root_dir,
        cfg.dataset_name,
        EpisodeTaskTransform(),
        resize_resolution=image_sizes,
        shuffle_buffer_size=1,
        image_aug=False,
    )

    task_to_indices: Dict[str, List[int]] = {}
    num_total = len(index_dataset)
    for ep_idx, episode_frames in enumerate(tqdm.tqdm(index_dataset, total=num_total, desc="Indexing episodes by task")):
        if len(episode_frames) == 0:
            continue
        task = episode_frames[0]["language_instruction"]
        task_to_indices.setdefault(task, []).append(ep_idx)

    if cfg.num_episodes == -1 or cfg.num_episodes >= num_total:
        selected = set(range(num_total))
        print(f"[*] Collecting all episodes: {len(selected)}")
        return selected

    num_tasks = len(task_to_indices)
    if num_tasks == 0:
        raise ValueError("No tasks found while indexing episodes.")
    if cfg.num_episodes % num_tasks != 0:
        raise ValueError(
            f"num_episodes={cfg.num_episodes} must be divisible by number of tasks={num_tasks} "
            f"for balanced per-task sampling."
        )

    per_task = cfg.num_episodes // num_tasks
    selected = set()
    print(f"[*] Stratified sampling: {per_task} episode(s) per task across {num_tasks} tasks")

    for task in sorted(task_to_indices.keys()):
        indices = task_to_indices[task]
        if per_task > len(indices):
            raise ValueError(
                f"Requested {per_task} episodes for task '{task}', but only {len(indices)} available."
            )
        chosen = random.sample(indices, per_task)
        selected.update(chosen)
        print(f"    - {task}: selected {len(chosen)} / {len(indices)}")

    print(f"[*] Selected {len(selected)} episodes total (balanced)")
    return selected


def save_embeddings_to_binary(embeddings_list: List[np.ndarray], output_path: Path, config: dict) -> None:
    """Save embeddings to binary format for llama.cpp imatrix calibration."""
    num_samples = len(embeddings_list)
    if num_samples == 0:
        raise ValueError("No embeddings to save!")

    hidden_dim = embeddings_list[0].shape[1]
    print(f"\nSaving {num_samples} samples to {output_path}")
    print(f"  Hidden dim: {hidden_dim}")

    # Compute sequence lengths and offsets
    sequence_lengths = [emb.shape[0] for emb in embeddings_list]
    offsets = []
    current_offset = 0
    for emb in embeddings_list:
        offsets.append(current_offset)
        current_offset += emb.shape[0] * hidden_dim * 4  # float32

    seq_lens_array = np.array(sequence_lengths)
    print(f"  Seq lengths: min={seq_lens_array.min()}, max={seq_lens_array.max()}, mean={seq_lens_array.mean():.1f}")
    print(f"  Total size: {current_offset / (1024**2):.2f} MB")

    # Create output directory
    output_path.parent.mkdir(parents=True, exist_ok=True)

    # Write binary file
    with open(output_path, 'wb') as f:
        f.write(CALIB_MAGIC)
        f.write(struct.pack('<I', CALIB_VERSION))
        f.write(struct.pack('<I', num_samples))
        f.write(struct.pack('<I', hidden_dim))
        f.write(struct.pack('<IIII', 0, 0, 0, 0))  # reserved
        f.write(struct.pack('<I', 0))  # padding to 48 bytes

        for seq_len in sequence_lengths:
            f.write(struct.pack('<I', seq_len))
        for offset in offsets:
            f.write(struct.pack('<Q', offset))
        for emb in embeddings_list:
            f.write(emb.astype(np.float32).tobytes())

    # Write metadata JSON
    metadata = {
        "format": "openvla_oft_calibration",
        "version": CALIB_VERSION,
        "num_frames": num_samples,
        "hidden_dim": hidden_dim,
        "dataset": config['dataset_name'],
        "model": config['vla_path'],
        "num_episodes": config['num_episodes'],
        "sequence_length_stats": {
            "min": int(seq_lens_array.min()),
            "max": int(seq_lens_array.max()),
            "mean": float(seq_lens_array.mean()),
        },
    }
    with open(output_path.with_suffix('.json'), 'w') as f:
        json.dump(metadata, f, indent=2)

    print(f"Saved to {output_path}")


@draccus.wrap()
def collect_calibration_data(cfg: CalibrationConfig) -> None:
    print(f"Collecting calibration data from `{cfg.vla_path}` using `{cfg.dataset_name}`")
    if cfg.targets_only:
        print("[*] targets_only mode: skipping model loading, only extracting action labels")

    random.seed(cfg.seed)

    # Register model classes
    AutoConfig.register("openvla", OpenVLAConfig)
    AutoImageProcessor.register(OpenVLAConfig, PrismaticImageProcessor)
    AutoProcessor.register(OpenVLAConfig, PrismaticProcessor)
    AutoModelForVision2Seq.register(OpenVLAConfig, OpenVLAForActionPrediction)

    # Load processor (always needed for tokenizer/dataset)
    print("[*] Loading processor...")
    processor = AutoProcessor.from_pretrained(cfg.vla_path, trust_remote_code=True)

    # Load model only if we need embeddings
    vla = None
    image_sizes = None
    if not cfg.targets_only:
        device_id = 0
        torch.cuda.set_device(device_id)
        print("[*] Loading model...")
        vla = AutoModelForVision2Seq.from_pretrained(
            cfg.vla_path,
            torch_dtype=torch.bfloat16,
            low_cpu_mem_usage=True,
            trust_remote_code=True,
        ).to(device_id)
        vla.vision_backbone.set_num_images_in_input(cfg.num_images_in_input)
        vla.eval()

        # Monkey-patch forward method to use our local version with calibration_mode support
        from prismatic.extern.hf.modeling_prismatic import PrismaticForConditionalGeneration
        vla.forward = PrismaticForConditionalGeneration.forward.__get__(vla, type(vla))

        print(f"  Hidden dim: {vla.llm_dim}")
        print(f"  Num patches: {vla.vision_backbone.get_num_patches()}")
        image_sizes = tuple(vla.config.image_sizes)
    else:
        # Load config to get image_sizes without loading the full model
        model_config = AutoConfig.from_pretrained(cfg.vla_path, trust_remote_code=True)
        image_sizes = tuple(model_config.image_sizes)

    # Create dataset
    print(f"[*] Loading dataset: {cfg.dataset_name}")
    action_tokenizer = ActionTokenizer(processor.tokenizer)
    batch_transform = RLDSBatchTransform(
        action_tokenizer,
        processor.tokenizer,
        image_transform=processor.image_processor.apply_transform,
        prompt_builder_fn=PurePromptBuilder,
        use_wrist_image=(cfg.num_images_in_input > 1),
        use_proprio=cfg.use_proprio,
    )

    dataset = EpisodicRLDSDataset(
        cfg.data_root_dir,
        cfg.dataset_name,
        batch_transform,
        resize_resolution=image_sizes,
        shuffle_buffer_size=1,
        image_aug=False,
    )
    print(f"  Total episodes: {len(dataset)}")

    collator = PaddedCollatorForActionPrediction(
        processor.tokenizer.model_max_length,
        processor.tokenizer.pad_token_id,
        padding_side="right"
    )

    # Stratified episode sampling: equal number from each task description.
    selected = select_episode_indices_stratified(cfg, image_sizes)
    num_total = len(dataset)
    print(f"[*] Collecting {len(selected)} episodes (all frames per episode)")

    # Collect embeddings, token-ID labels, and continuous OFT actions
    embeddings_list: List[np.ndarray] = []
    labels_list: List[np.ndarray] = []
    oft_actions_list: List[np.ndarray] = []  # continuous (8, 7) actions for AMF
    episodes_done = 0
    IGNORE_INDEX = -100

    with torch.no_grad():
        for ep_idx, episode_frames in enumerate(tqdm.tqdm(dataset, total=num_total)):
            if ep_idx not in selected:
                continue

            # Process ALL frames in this episode
            for i in range(0, len(episode_frames), cfg.batch_size):
                batch_frames = episode_frames[i:i + cfg.batch_size]
                batch = collator(batch_frames)

                if not cfg.targets_only:
                    # Forward pass with calibration_mode=True to get multimodal embeddings
                    with torch.autocast("cuda", dtype=torch.bfloat16):
                        output = vla(
                            input_ids=batch["input_ids"].to(device_id),
                            attention_mask=batch["attention_mask"].to(device_id),
                            pixel_values=batch["pixel_values"].to(torch.bfloat16).to(device_id),
                            labels=batch["labels"].to(device_id),
                            calibration_mode=True,
                        )

                    # Extract multimodal embeddings
                    mm_embeds = output["multimodal_embeddings"]  # [B, seq_len, hidden_dim]
                    for j in range(mm_embeds.shape[0]):
                        embeddings_list.append(mm_embeds[j].float().cpu().numpy())

                # Extract action token ID labels (for NLL Fisher)
                frame_labels = batch["labels"]  # [B, seq_len]
                for j in range(frame_labels.shape[0]):
                    lbl = frame_labels[j].cpu().numpy()
                    labels_list.append(lbl[lbl != IGNORE_INDEX])

                # Extract continuous OFT actions (for Action-Mahalanobis Fisher / AMF)
                # shape: (B, 8, 7) — normalized actions in approximately [-1, 1]
                if "actions" in batch:
                    actions = batch["actions"].float().numpy()  # (B, 8, 7)
                    for j in range(actions.shape[0]):
                        oft_actions_list.append(actions[j])  # (8, 7)

            episodes_done += 1
            if episodes_done >= len(selected):
                break

    print(f"\n[*] Collected {len(labels_list)} frames from {episodes_done} episodes")

    # Save embeddings (skip in targets_only mode)
    if not cfg.targets_only:
        if embeddings_list:
            print(f"  Sample shape: {embeddings_list[0].shape}")
        config_dict = {
            "dataset_name": cfg.dataset_name,
            "vla_path": cfg.vla_path,
            "num_episodes": episodes_done,
        }
        save_embeddings_to_binary(embeddings_list, cfg.output_path, config_dict)

    # Save action token ID targets (for NLL Fisher / imatrix)
    targets_array = np.stack(labels_list, axis=0)
    targets_path = cfg.output_path.with_name(cfg.output_path.stem + "_targets.npy")
    np.save(targets_path, targets_array)
    print(f"Saved token-ID targets: shape={targets_array.shape} to {targets_path}")

    # Save continuous OFT action targets (for AMF — Action-Mahalanobis Fisher)
    if oft_actions_list:
        oft_targets_array = np.stack(oft_actions_list, axis=0)  # (N, 8, 7)
        oft_targets_path = cfg.output_path.with_name(cfg.output_path.stem + "_oft_targets.npy")
        np.save(oft_targets_path, oft_targets_array)
        print(f"Saved OFT action targets: shape={oft_targets_array.shape} to {oft_targets_path}")

    print("\nDone!")


if __name__ == "__main__":
    collect_calibration_data()