forked from EleutherAI/pythia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shard_memmap.py
61 lines (45 loc) · 1.66 KB
/
shard_memmap.py
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
import os
import argparse
import numpy as np
from tqdm import tqdm
def shard(
input_file: str,
output_dir: str,
):
"""Shard a Megatron .bin file into ~ 4.5 GB chunks"""
SHARD_SIZE = 5_000_000_000 # bytes ~= 4.5 GB
# load in memmapped .bin file
full_idx_map = np.memmap(input_file, mode="r", order="C")
# get number of chunks (rounds down bc start counting from shard number 0)
num_chunks = full_idx_map.shape[0] // SHARD_SIZE
# chunk by iterating over file
for i in tqdm(range(num_chunks + 1)): # while still have file contents remaining to chunk:
start_idx = i * SHARD_SIZE
end_idx = (i + 1) * SHARD_SIZE
if end_idx > full_idx_map.shape[0]:
chunk = full_idx_map[start_idx:]
else:
chunk = full_idx_map[start_idx:end_idx]
shard_filename = os.path.join(output_dir, os.path.basename(input_file)[:-4]) + f"-{i:05}-of-{num_chunks:05}.bin"
with open(shard_filename, "wb+") as out_shard_file:
print(f"Dumping shard {i:05} to {shard_filename} ...")
chunk.tofile(out_shard_file)
del chunk
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Shard a single Megatron data .bin file"
)
## CLI args
parser.add_argument(
"--input_file",
type=str,
help="Path to .bin file e.g. /path/to/pile_0.87_deduped_text_document.bin",
)
parser.add_argument(
"--output_dir",
type=str,
help="Folder to save shards into",
)
args = parser.parse_args()
os.makedirs(args.output_dir, exist_ok=True)
shard(args.input_file, args.output_dir)