Batch upload - merge_datasets.py
Browse files- scripts/merge_datasets.py +93 -0
scripts/merge_datasets.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import json
|
| 4 |
+
import argparse
|
| 5 |
+
|
| 6 |
+
sys.path.append(
|
| 7 |
+
os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
from megatron.core.datasets.indexed_dataset import (
|
| 11 |
+
IndexedDataset,
|
| 12 |
+
IndexedDatasetBuilder,
|
| 13 |
+
get_bin_path,
|
| 14 |
+
get_idx_path,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def get_args():
|
| 19 |
+
parser = argparse.ArgumentParser()
|
| 20 |
+
|
| 21 |
+
group = parser.add_argument_group(title="input data")
|
| 22 |
+
group.add_argument(
|
| 23 |
+
"--input",
|
| 24 |
+
type=str,
|
| 25 |
+
required=True,
|
| 26 |
+
help="Path to directory containing all document files to merge",
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
group = parser.add_argument_group(title="output data")
|
| 30 |
+
group.add_argument(
|
| 31 |
+
"--output-prefix",
|
| 32 |
+
type=str,
|
| 33 |
+
required=True,
|
| 34 |
+
help="Path to binary output file without suffix",
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
group = parser.add_argument_group(title="miscellaneous")
|
| 38 |
+
group.add_argument(
|
| 39 |
+
"--multimodal",
|
| 40 |
+
action="store_true",
|
| 41 |
+
help="Whether the datasets are assumed to be multimodal"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
args = parser.parse_args()
|
| 45 |
+
|
| 46 |
+
assert os.path.isdir(
|
| 47 |
+
args.input
|
| 48 |
+
), f"ERROR: {args.input} is not a directory or does not exist"
|
| 49 |
+
|
| 50 |
+
assert os.path.isdir(
|
| 51 |
+
os.path.dirname(args.output_prefix)
|
| 52 |
+
), f"ERROR: {os.path.dirname(args.output_prefix)} is not a directory or does not exist"
|
| 53 |
+
|
| 54 |
+
return args
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def main():
|
| 58 |
+
args = get_args()
|
| 59 |
+
|
| 60 |
+
prefixes = set()
|
| 61 |
+
for basename in os.listdir(args.input):
|
| 62 |
+
prefix, ext = os.path.splitext(basename)
|
| 63 |
+
|
| 64 |
+
if prefix in prefixes:
|
| 65 |
+
continue
|
| 66 |
+
|
| 67 |
+
if not os.path.isfile(os.path.join(args.input, basename)):
|
| 68 |
+
continue
|
| 69 |
+
|
| 70 |
+
ext_pair = ".bin" if ext == ".idx" else ".idx"
|
| 71 |
+
assert os.path.isfile(
|
| 72 |
+
os.path.join(args.input, prefix) + ext_pair
|
| 73 |
+
), f"ERROR: {ext_pair} file not provided for {os.path.join(args.input, prefix)}"
|
| 74 |
+
|
| 75 |
+
prefixes.add(prefix)
|
| 76 |
+
|
| 77 |
+
builder = None
|
| 78 |
+
for prefix in sorted(prefixes):
|
| 79 |
+
if builder is None:
|
| 80 |
+
dataset = IndexedDataset(os.path.join(args.input, prefix), multimodal=args.multimodal)
|
| 81 |
+
builder = IndexedDatasetBuilder(
|
| 82 |
+
get_bin_path(args.output_prefix), dtype=dataset.index.dtype, multimodal=args.multimodal
|
| 83 |
+
)
|
| 84 |
+
del dataset
|
| 85 |
+
|
| 86 |
+
builder.add_index(os.path.join(args.input, prefix))
|
| 87 |
+
|
| 88 |
+
builder.finalize(get_idx_path(args.output_prefix))
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
if __name__ == '__main__':
|
| 92 |
+
|
| 93 |
+
main()
|