-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathtensor.py
More file actions
34 lines (25 loc) · 865 Bytes
/
Copy pathtensor.py
File metadata and controls
34 lines (25 loc) · 865 Bytes
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
"""Tensor: store a torch / NumPy array (2-D+).
1-D token ids for LLM training belong with Text + TokensLoader (see text.py).
"""
import torch
from litdata import StreamingDataLoader, StreamingDataset, Tensor, optimize
def make_sample(index: int) -> dict:
return {
"index": index,
"feat": Tensor(array=torch.randn(3, 4, 4)),
}
if __name__ == "__main__":
optimize(
fn=make_sample,
inputs=list(range(8)),
output_dir="example_optimize_dataset/tensor",
num_workers=2,
chunk_bytes="64MB",
mode="overwrite",
)
dataset = StreamingDataset("example_optimize_dataset/tensor")
sample = dataset[0]
feat = sample["feat"] # Tensor
print(feat.shape, feat.dtype)
batch = next(iter(StreamingDataLoader(dataset, batch_size=4, num_workers=0)))
print(batch["feat"].shape)