lastgeyan commited on
Commit
e32d736
·
verified ·
1 Parent(s): 52a795e

README: correct prompt template for cache key + unpack script

Browse files
Files changed (1) hide show
  1. README.md +79 -49
README.md CHANGED
@@ -16,85 +16,115 @@ size_categories:
16
  Precomputed [UMT5-XXL](https://huggingface.co/google/umt5-xxl) text embeddings for the
17
  **1,039,891 unique task prompts** of the RoboTwin 2.0 3D dataset
18
  ([`flex-pi/robotwin_3d`](https://huggingface.co/datasets/flex-pi/robotwin_3d)), as consumed by
19
- Wan2.2-TI2V-5B / FastWAM.
20
 
21
- Precomputing these takes substantial GPU time; this cache lets you skip it.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  ## Contents
24
 
25
  | Path | Description |
26
  |---|---|
27
- | `shards/shard_NNNNN.safetensors` | 520 shards, 2,000 prompts each (last one 1,891), ~2.10 GB per shard |
28
- | `manifest.txt` | The 1,039,891 prompt hashes, **sorted**, one per line — line `i` is row `i % 2000` of shard `i // 2000` |
29
 
30
- Each shard holds two tensors, row-aligned:
 
31
 
32
  | Tensor | Shape | Dtype |
33
  |---|---|---|
34
  | `contexts` | `[N, 128, 4096]` | `bfloat16` |
35
  | `masks` | `[N, 128]` | `bool` |
36
 
37
- The shard's `__metadata__["keys"]` is a JSON list of that shard's N hashes, in row order.
 
 
38
 
39
- The key for a prompt is `sha256(prompt.encode("utf-8")).hexdigest()`. Prompts themselves live in
40
- `meta/tasks.jsonl` of the main dataset.
41
 
42
- `masks` marks the valid tokens (mean 40.9 of 128). Values beyond the mask are **not** zero — they are
43
- the raw T5 outputs. Zero them yourself if your model does not apply the mask.
44
-
45
- ## Usage
46
-
47
- Random access without downloading everything — resolve one prompt to its shard, fetch only that shard:
48
 
49
  ```python
50
- import bisect, hashlib, json
 
 
 
 
 
51
  from huggingface_hub import hf_hub_download
52
  from safetensors import safe_open
53
 
54
- REPO = "flex-pi/robotwin_3d_text_embeds_cache"
55
- SHARD = 2000
 
56
 
57
- manifest = hf_hub_download(REPO, "manifest.txt", repo_type="dataset")
58
- keys = open(manifest).read().split() # sorted
59
-
60
- def get(prompt):
61
- h = hashlib.sha256(prompt.encode("utf-8")).hexdigest()
62
- i = bisect.bisect_left(keys, h)
63
- if i == len(keys) or keys[i] != h:
64
- raise KeyError(prompt)
65
- path = hf_hub_download(REPO, f"shards/shard_{i // SHARD:05d}.safetensors", repo_type="dataset")
66
- with safe_open(path, framework="pt") as f:
67
- row = i % SHARD
68
- return f.get_slice("contexts")[row], f.get_slice("masks")[row]
69
-
70
- context, mask = get("Lift the medium-sized green bottle ensuring it remains upright.")
71
- print(context.shape, context.dtype, int(mask.sum())) # (128, 4096) torch.bfloat16 49
 
 
72
  ```
73
 
74
- `safe_open` + `get_slice` reads only the requested row, so this does not load the whole 2 GB shard
75
- into memory.
 
76
 
77
- ### Rebuilding the original per-prompt `.pt` layout
78
 
79
- Some code expects `{cache_dir}/{sha256}.t5_len128.wan22ti2v5b.pt` holding `{"context", "mask"}`:
80
 
81
  ```python
82
- import json, os, torch
 
83
  from safetensors import safe_open
84
 
85
- def unpack(shard_path, out_dir):
86
- os.makedirs(out_dir, exist_ok=True)
87
- with safe_open(shard_path, framework="pt") as f:
88
- ks = json.loads(f.metadata()["keys"])
89
- C, M = f.get_slice("contexts"), f.get_slice("masks")
90
- for r, k in enumerate(ks):
91
- torch.save({"context": C[r], "mask": M[r]},
92
- os.path.join(out_dir, f"{k}.t5_len128.wan22ti2v5b.pt"))
93
- ```
94
 
95
- Note this expands to ~1 TB across 1,039,891 files.
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
  ## Provenance
98
 
99
- Repacked byte-exactly from the original per-prompt `.pt` cache tensors are bit-identical, verified
100
- by round-trip comparison against the source files. Encoder: UMT5-XXL, context length 128.
 
16
  Precomputed [UMT5-XXL](https://huggingface.co/google/umt5-xxl) text embeddings for the
17
  **1,039,891 unique task prompts** of the RoboTwin 2.0 3D dataset
18
  ([`flex-pi/robotwin_3d`](https://huggingface.co/datasets/flex-pi/robotwin_3d)), as consumed by
19
+ Wan2.2-TI2V-5B / FastWAM. Precomputing these costs substantial GPU time; this cache skips it.
20
 
21
+ ## ⚠️ The cache key is the *templated* prompt, not the raw task string
22
+
23
+ Task strings from `meta/tasks.jsonl` are wrapped in a fixed template before encoding. Hashing the
24
+ raw task string will **not** find anything.
25
+
26
+ ```python
27
+ DEFAULT_PROMPT = "A video recorded from a robot's point of view executing the following instruction: {task}"
28
+ key = hashlib.sha256(DEFAULT_PROMPT.format(task=task).encode("utf-8")).hexdigest()
29
+ ```
30
+
31
+ Worked example:
32
+
33
+ | | |
34
+ |---|---|
35
+ | task | `Lift the medium-sized green bottle ensuring it remains upright.` |
36
+ | prompt | `A video recorded from a robot's point of view executing the following instruction: Lift the medium-sized green bottle ensuring it remains upright.` |
37
+ | key | `83eacc02ca58bc93ecd638b3d2318494f442778e62a71a13d7987dfdb649fe18` |
38
 
39
  ## Contents
40
 
41
  | Path | Description |
42
  |---|---|
43
+ | `shards/shard_NNNNN.safetensors` | 520 shards × 2,000 prompts (last one 1,891), ~2.10 GB each |
44
+ | `manifest.txt` | All 1,039,891 keys, **sorted**, one per line — line `i` is row `i % 2000` of shard `i // 2000` |
45
 
46
+ Each shard holds two row-aligned tensors, plus `__metadata__["keys"]` (JSON list of that shard's
47
+ keys in row order):
48
 
49
  | Tensor | Shape | Dtype |
50
  |---|---|---|
51
  | `contexts` | `[N, 128, 4096]` | `bfloat16` |
52
  | `masks` | `[N, 128]` | `bool` |
53
 
54
+ `masks` marks valid tokens (mean 40.9 of 128). Values past the mask are **not** zero — they are raw
55
+ T5 outputs. FastWAM zeroes them on load (`context[~mask] = 0`); do the same if your model does not
56
+ apply the mask.
57
 
58
+ ## Converting back to the per-file `.pt` layout
 
59
 
60
+ FastWAM reads a flat directory of `{key}.t5_len128.wan22ti2v5b.pt` files, each a `{"context", "mask"}`
61
+ dict **not** shards. This script reconstructs exactly that layout:
 
 
 
 
62
 
63
  ```python
64
+ # unpack_to_pt.py -- rebuild the flat .pt cache from the sharded release.
65
+ # python unpack_to_pt.py /path/to/text_embeds_cache/robotwin2.0_3d
66
+ import json, os, sys
67
+ from concurrent.futures import ThreadPoolExecutor
68
+
69
+ import torch
70
  from huggingface_hub import hf_hub_download
71
  from safetensors import safe_open
72
 
73
+ REPO, NSHARD = "flex-pi/robotwin_3d_text_embeds_cache", 520
74
+ out, tmp = sys.argv[1], "/tmp/tec_shards"
75
+ os.makedirs(out, exist_ok=True)
76
 
77
+ def do(i):
78
+ # local_dir= gives a real file we can delete; the default cache would only
79
+ # yield a symlink, so removing it frees nothing.
80
+ p = hf_hub_download(REPO, f"shards/shard_{i:05d}.safetensors",
81
+ repo_type="dataset", local_dir=tmp)
82
+ with safe_open(p, framework="pt") as f:
83
+ ks = json.loads(f.metadata()["keys"])
84
+ C, M = f.get_slice("contexts"), f.get_slice("masks")
85
+ for r, k in enumerate(ks):
86
+ dst = os.path.join(out, f"{k}.t5_len128.wan22ti2v5b.pt")
87
+ if not os.path.exists(dst):
88
+ torch.save({"context": C[r], "mask": M[r]}, dst)
89
+ os.remove(p) # drop the 2 GB shard once expanded
90
+ print(f"shard {i:05d} -> {len(ks)} files", flush=True)
91
+
92
+ with ThreadPoolExecutor(4) as ex:
93
+ list(ex.map(do, range(NSHARD)))
94
  ```
95
 
96
+ Expands to ~1 TB across 1,039,891 files. Point `text_embedding_cache_dir` at `out` afterwards.
97
+
98
+ To pull only part of it, pass a subset of shard indices — each shard is self-describing.
99
 
100
+ ## Random access without unpacking
101
 
102
+ Resolve one prompt to its shard and read only that row; `get_slice` avoids loading the 2 GB shard:
103
 
104
  ```python
105
+ import bisect, hashlib, json
106
+ from huggingface_hub import hf_hub_download
107
  from safetensors import safe_open
108
 
109
+ REPO, SHARD = "flex-pi/robotwin_3d_text_embeds_cache", 2000
110
+ TPL = "A video recorded from a robot's point of view executing the following instruction: {task}"
111
+ keys = open(hf_hub_download(REPO, "manifest.txt", repo_type="dataset")).read().split()
 
 
 
 
 
 
112
 
113
+ def get(task):
114
+ h = hashlib.sha256(TPL.format(task=task).encode("utf-8")).hexdigest()
115
+ i = bisect.bisect_left(keys, h)
116
+ if i == len(keys) or keys[i] != h:
117
+ raise KeyError(task)
118
+ p = hf_hub_download(REPO, f"shards/shard_{i // SHARD:05d}.safetensors", repo_type="dataset")
119
+ with safe_open(p, framework="pt") as f:
120
+ r = i % SHARD
121
+ return f.get_slice("contexts")[r], f.get_slice("masks")[r]
122
+
123
+ ctx, mask = get("Lift the medium-sized green bottle ensuring it remains upright.")
124
+ print(ctx.shape, ctx.dtype, int(mask.sum())) # torch.Size([128, 4096]) torch.bfloat16 49
125
+ ```
126
 
127
  ## Provenance
128
 
129
+ Repacked byte-exactly from the original per-prompt `.pt` cache; tensors are bit-identical, verified
130
+ by round-trip comparison against the source files. Encoder UMT5-XXL, context length 128, bfloat16.