-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpretokenize_dataset.py
More file actions
458 lines (375 loc) · 14.7 KB
/
pretokenize_dataset.py
File metadata and controls
458 lines (375 loc) · 14.7 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#!/usr/bin/env python3
"""
Pretokenize Dataset with Cosmos Tokenizer
Pre-encodes all video frames using the frozen Cosmos CV8x8x8 tokenizer
and saves the latents to disk. This provides 2-3× training speedup by
avoiding on-the-fly tokenization.
Output format:
data/pretokenized/
├── episode_0000/
│ ├── latents.pt # (T_lat, 16, 16) float16
│ ├── actions.pt # (T,) or dict
│ └── rewards.pt # (T,)
├── episode_0001/
│ └── ...
└── metadata.pt # {num_episodes, T_lat_formula, pool_tokens, ...}
Usage:
python pretokenize_dataset.py --data-path data/mineRL_extracted --output-path data/pretokenized
python pretokenize_dataset.py --data-path data/mineRL_extracted --output-path data/pretokenized --max-episodes 100
"""
import argparse
import math
import sys
from pathlib import Path
from typing import Dict, Optional, List
from tqdm import tqdm
import torch
import numpy as np
def compute_temporal_latent_steps(num_frames: int) -> int:
"""
Compute T_latent for causal Cosmos tokenizer.
Formula: T_latent = 1 + ceil((T_frames - 1) / 8)
"""
if num_frames <= 0:
raise ValueError(f"num_frames must be positive, got {num_frames}")
if num_frames == 1:
return 1
return 1 + math.ceil((num_frames - 1) / 8)
def load_cosmos_tokenizer(checkpoint_path: str, device: str = "cuda", pool_tokens: Optional[int] = None, input_resolution: int = 64):
"""Load the Cosmos tokenizer wrapper."""
# Add project root to path
project_root = Path(__file__).parent
if str(project_root) not in sys.path:
sys.path.insert(0, str(project_root))
from dreamer.models.cosmos_tokenizer_wrapper import create_cosmos_tokenizer
tokenizer = create_cosmos_tokenizer(
checkpoint_path=checkpoint_path,
pool_tokens=pool_tokens, # None = no pooling (best quality)
input_resolution=input_resolution, # 64 = native MineRL (no upsampling)
device=device,
dtype="bfloat16",
)
return tokenizer
def load_episode_data(episode_path: Path) -> Optional[Dict[str, torch.Tensor]]:
"""
Load episode data from various formats.
Returns:
Dict with 'frames', 'actions', 'rewards' or None if failed
"""
try:
# Format 1: Single .pt file with all data
if episode_path.suffix == ".pt":
data = torch.load(episode_path, map_location="cpu", weights_only=False)
if isinstance(data, dict):
return data
# Handle list of transitions
if isinstance(data, list):
frames = torch.stack([d.get("frame", d.get("obs", d.get("observation"))) for d in data])
actions = torch.stack([d.get("action", torch.tensor(0)) for d in data])
rewards = torch.stack([d.get("reward", torch.tensor(0.0)) for d in data])
return {"frames": frames, "actions": actions, "rewards": rewards}
# Format 2: Directory with separate files
if episode_path.is_dir():
result = {}
# Load frames
frames_npy = episode_path / "frames.npy"
frames_pt = episode_path / "frames.pt"
if frames_npy.exists():
frames = torch.from_numpy(np.load(frames_npy))
elif frames_pt.exists():
frames = torch.load(frames_pt, map_location="cpu", weights_only=False)
else:
# Try loading individual images
import cv2
png_files = sorted(episode_path.glob("*.png"))
jpg_files = sorted(episode_path.glob("*.jpg"))
img_files = png_files if png_files else jpg_files
if not img_files:
return None
frames_list = []
for img_file in img_files:
img = cv2.imread(str(img_file))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
frames_list.append(torch.from_numpy(img).permute(2, 0, 1))
frames = torch.stack(frames_list)
result["frames"] = frames
# Load actions
actions_npy = episode_path / "actions.npy"
actions_pt = episode_path / "actions.pt"
if actions_npy.exists():
result["actions"] = torch.from_numpy(np.load(actions_npy, allow_pickle=True))
elif actions_pt.exists():
result["actions"] = torch.load(actions_pt, map_location="cpu", weights_only=False)
else:
# Default: zeros
result["actions"] = torch.zeros(len(result["frames"]), dtype=torch.long)
# Load rewards
rewards_npy = episode_path / "rewards.npy"
rewards_pt = episode_path / "rewards.pt"
if rewards_npy.exists():
result["rewards"] = torch.from_numpy(np.load(rewards_npy))
elif rewards_pt.exists():
result["rewards"] = torch.load(rewards_pt, map_location="cpu", weights_only=False)
else:
# Default: zeros
result["rewards"] = torch.zeros(len(result["frames"]), dtype=torch.float32)
return result
return None
except Exception as e:
print(f"Warning: Failed to load {episode_path}: {e}")
return None
def find_episodes(data_path: Path) -> List[Path]:
"""Find all episode paths in the data directory."""
episodes = []
# .pt files at root
pt_files = sorted(data_path.glob("*.pt"))
episodes.extend(pt_files)
# Directories with frames.npy
for frames_file in sorted(data_path.rglob("frames.npy")):
episodes.append(frames_file.parent)
# Directories with images
for subdir in sorted(data_path.iterdir()):
if subdir.is_dir() and subdir not in episodes:
# Check if it has images
if list(subdir.glob("*.png")) or list(subdir.glob("*.jpg")):
episodes.append(subdir)
# Remove duplicates while preserving order
seen = set()
unique_episodes = []
for ep in episodes:
ep_str = str(ep.resolve())
if ep_str not in seen:
seen.add(ep_str)
unique_episodes.append(ep)
return unique_episodes
def preprocess_frames(frames: torch.Tensor) -> torch.Tensor:
"""
Preprocess frames for Cosmos tokenizer.
Args:
frames: (T, C, H, W) or (T, H, W, C) uint8 or float
Returns:
frames: (1, C, T, H, W) float32 in [0, 1]
"""
# Handle channel-last format
if frames.dim() == 4 and frames.shape[-1] in [1, 3]:
frames = frames.permute(0, 3, 1, 2)
# Convert to float [0, 1]
if frames.dtype == torch.uint8:
frames = frames.float() / 255.0
elif frames.max() > 1.0:
frames = frames.float() / 255.0
else:
frames = frames.float()
# Add batch dimension and rearrange: (T, C, H, W) -> (1, C, T, H, W)
frames = frames.permute(1, 0, 2, 3).unsqueeze(0)
return frames
@torch.no_grad()
def tokenize_episode(
tokenizer,
frames: torch.Tensor,
device: str = "cuda",
chunk_size: int = 64,
) -> torch.Tensor:
"""
Tokenize video frames with Cosmos.
Args:
tokenizer: CosmosTokenizerWrapper
frames: (1, C, T, H, W) float32 in [0, 1]
device: Device to use
chunk_size: Max frames per chunk (for memory)
Returns:
latents: (T_lat, 16, 16) float16
"""
frames = frames.to(device)
T = frames.shape[2]
if T <= chunk_size:
# Process all at once
output = tokenizer.encode(frames)
latents = output["latents"].squeeze(0) # (T_lat, 16, 16)
else:
# Process in chunks with overlap for continuity
latents_chunks = []
overlap = 8 # Overlap frames for smooth transitions
start = 0
while start < T:
end = min(start + chunk_size, T)
chunk = frames[:, :, start:end]
output = tokenizer.encode(chunk)
chunk_latents = output["latents"].squeeze(0) # (T_lat_chunk, 16, 16)
# Skip overlapping latents except for first chunk
if start > 0:
# Skip first latent steps (corresponds to overlap region)
overlap_latent_steps = compute_temporal_latent_steps(overlap) - 1
chunk_latents = chunk_latents[overlap_latent_steps:]
latents_chunks.append(chunk_latents)
start = end - overlap if end < T else T
latents = torch.cat(latents_chunks, dim=0)
# Convert to float16 for storage efficiency
return latents.half().cpu()
def pretokenize_dataset(
data_path: str,
output_path: str,
cosmos_checkpoint: str = "cosmos_tokenizer/CV8x8x8",
device: str = "cuda",
max_episodes: Optional[int] = None,
chunk_size: int = 64,
):
"""
Pretokenize entire dataset with Cosmos tokenizer.
Args:
data_path: Path to input dataset
output_path: Path to save pretokenized data
cosmos_checkpoint: Path to Cosmos tokenizer checkpoint
device: Device to use for encoding
max_episodes: Maximum episodes to process (None = all)
chunk_size: Max frames per encoding chunk
"""
data_path = Path(data_path)
output_path = Path(output_path)
output_path.mkdir(parents=True, exist_ok=True)
print(f"Loading Cosmos tokenizer from {cosmos_checkpoint}...")
tokenizer = load_cosmos_tokenizer(cosmos_checkpoint, device)
print(f" Pool tokens: {tokenizer.pool_tokens}")
print(f" Latent dim: {tokenizer.latent_dim}")
print(f"\nFinding episodes in {data_path}...")
episodes = find_episodes(data_path)
print(f" Found {len(episodes)} episodes")
if max_episodes:
episodes = episodes[:max_episodes]
print(f" Processing first {max_episodes} episodes")
# Track statistics
stats = {
"total_frames": 0,
"total_latent_steps": 0,
"num_episodes": 0,
"failed_episodes": 0,
}
print(f"\nPretokenizing to {output_path}...")
for idx, episode_path in enumerate(tqdm(episodes, desc="Tokenizing")):
episode_name = f"episode_{idx:05d}"
episode_output_dir = output_path / episode_name
# Skip if already processed
if (episode_output_dir / "latents.pt").exists():
continue
# Load episode data
data = load_episode_data(episode_path)
if data is None or "frames" not in data:
stats["failed_episodes"] += 1
continue
frames = data["frames"]
T = frames.shape[0]
# Preprocess frames
frames_preprocessed = preprocess_frames(frames)
# Tokenize
try:
latents = tokenize_episode(
tokenizer,
frames_preprocessed,
device=device,
chunk_size=chunk_size,
)
except Exception as e:
print(f"\nWarning: Failed to tokenize episode {idx}: {e}")
stats["failed_episodes"] += 1
continue
# Save outputs
episode_output_dir.mkdir(parents=True, exist_ok=True)
torch.save(latents, episode_output_dir / "latents.pt")
torch.save(data["actions"], episode_output_dir / "actions.pt")
torch.save(data["rewards"], episode_output_dir / "rewards.pt")
# Save original frame count for action alignment
torch.save({"num_frames": T, "num_latent_steps": latents.shape[0]},
episode_output_dir / "info.pt")
# Update stats
stats["total_frames"] += T
stats["total_latent_steps"] += latents.shape[0]
stats["num_episodes"] += 1
# Clear GPU cache periodically
if idx % 100 == 0:
torch.cuda.empty_cache()
# Save metadata
metadata = {
"num_episodes": stats["num_episodes"],
"total_frames": stats["total_frames"],
"total_latent_steps": stats["total_latent_steps"],
"pool_tokens": tokenizer.pool_tokens,
"latent_dim": tokenizer.latent_dim,
"temporal_formula": "T_lat = 1 + ceil((T - 1) / 8)",
"failed_episodes": stats["failed_episodes"],
"source_path": str(data_path),
}
torch.save(metadata, output_path / "metadata.pt")
# Print summary
print(f"\n{'='*60}")
print("Pretokenization Complete!")
print(f"{'='*60}")
print(f" Episodes processed: {stats['num_episodes']}")
print(f" Failed episodes: {stats['failed_episodes']}")
print(f" Total frames: {stats['total_frames']:,}")
print(f" Total latent steps: {stats['total_latent_steps']:,}")
print(f" Compression ratio: {stats['total_frames'] / max(1, stats['total_latent_steps']):.2f}x")
print(f" Output path: {output_path}")
# Estimate storage
latent_size_mb = stats["total_latent_steps"] * 16 * 16 * 2 / 1024 / 1024 # float16
print(f" Estimated latent storage: {latent_size_mb:.1f} MB")
def main():
parser = argparse.ArgumentParser(
description="Pretokenize dataset with Cosmos tokenizer",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Tokenize full dataset
python pretokenize_dataset.py --data-path data/mineRL_extracted --output-path data/pretokenized
# Tokenize subset for testing
python pretokenize_dataset.py --data-path data/mineRL_extracted --output-path data/pretokenized_test --max-episodes 10
# Use custom Cosmos checkpoint
python pretokenize_dataset.py --data-path data/mineRL_extracted --output-path data/pretokenized --cosmos-checkpoint /path/to/CV8x8x8
"""
)
parser.add_argument(
"--data-path",
type=str,
required=True,
help="Path to input dataset directory",
)
parser.add_argument(
"--output-path",
type=str,
required=True,
help="Path to save pretokenized data",
)
parser.add_argument(
"--cosmos-checkpoint",
type=str,
default="cosmos_tokenizer/CV8x8x8",
help="Path to Cosmos tokenizer checkpoint",
)
parser.add_argument(
"--device",
type=str,
default="cuda",
help="Device to use for encoding",
)
parser.add_argument(
"--max-episodes",
type=int,
default=None,
help="Maximum episodes to process (default: all)",
)
parser.add_argument(
"--chunk-size",
type=int,
default=64,
help="Max frames per encoding chunk (for memory)",
)
args = parser.parse_args()
pretokenize_dataset(
data_path=args.data_path,
output_path=args.output_path,
cosmos_checkpoint=args.cosmos_checkpoint,
device=args.device,
max_episodes=args.max_episodes,
chunk_size=args.chunk_size,
)
if __name__ == "__main__":
main()