File size: 1,747 Bytes
e2b1e36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import glob
import datasets


_DESCRIPTION = """\
Precomputed hidden state activations from layer 23 of Gemma-3-1B-IT
for the OpenWebText dataset, tokenized with sequence length 1024.
Designed for training a Titans memory layer.
"""

_SHARD_BASE = "shard_[0-9]*.npy"


class OpenwebtextGemma3Tokenized1024ActivationsLayer23(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features({
                "activations": datasets.Array3D(shape=(1024, 3072), dtype="float32"),
                "mask": datasets.Array2D(shape=(1024,), dtype="int32"),
                "tokens": datasets.Array2D(shape=(1024,), dtype="int32"),
            }),
        )

    def _split_generators(self, dl_manager):
        all_npy = sorted(glob.glob(_SHARD_BASE))
        shards = [f for f in all_npy
                  if not f.endswith("_masks.npy") and not f.endswith("_tokens.npy")]
        return [datasets.SplitGenerator(
            name=datasets.Split.TRAIN,
            gen_kwargs={"shard_files": shards},
        )]

    def _generate_examples(self, shard_files):
        for shard_path in shard_files:
            shard_id = shard_path.split("shard_")[1].split(".npy")[0]
            act = np.load(shard_path).astype(np.float32)
            mask = np.load(shard_path.replace(".npy", "_masks.npy"))
            tokens = np.load(shard_path.replace(".npy", "_tokens.npy"))
            for i in range(act.shape[0]):
                yield f"{shard_id}_{i}", {
                    "activations": act[i],
                    "mask": mask[i],
                    "tokens": tokens[i],
                }