File size: 2,323 Bytes
91ac3cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env bash
set -euo pipefail

PROJECT_DIR="${PROJECT_DIR:-/workspace/KAWK50M}"
CLEAN_DIR="${CLEAN_DIR:-data/clean-5b}"
TOKENIZED_DIR="${TOKENIZED_DIR:-data/tokenized-5b}"
TOKENIZER_DIR="${TOKENIZER_DIR:-artifacts/tokenizer-20k-pilot}"
WORKERS="${WORKERS:-16}"

cd "${PROJECT_DIR}"
source "${PROJECT_DIR}/.venv/bin/activate"

if [[ -e "${TOKENIZED_DIR}" ]]; then
  echo "Refusing to overwrite existing output: ${TOKENIZED_DIR}" >&2
  exit 1
fi
mkdir -p "${TOKENIZED_DIR}/parts"

tokenize_parts() {
  local split="$1"
  local split_dir="${TOKENIZED_DIR}/parts/${split}"
  mkdir -p "${split_dir}"
  find "${CLEAN_DIR}" -maxdepth 1 -type f -name "${split}-*.jsonl" -print0 \
    | sort -z \
    | xargs -0 -r -P "${WORKERS}" -I '{}' bash -c '
        input="$1"
        output_dir="$2"
        tokenizer_dir="$3"
        stem="$(basename "${input%.jsonl}")"
        python -u -m scripts.tokenize_shard \
          --input "${input}" \
          --tokenizer-dir "${tokenizer_dir}" \
          --output "${output_dir}/${stem}.bin" \
          --batch-size 64
      ' _ '{}' "${split_dir}" "${TOKENIZER_DIR}"
}

tokenize_parts train
tokenize_parts validation
tokenize_parts test

python -m scripts.combine_token_parts \
  --input-dir "${TOKENIZED_DIR}/parts/train" \
  --output "${TOKENIZED_DIR}/train.bin" \
  --max-tokens 5000000000
python -m scripts.combine_token_parts \
  --input-dir "${TOKENIZED_DIR}/parts/validation" \
  --output "${TOKENIZED_DIR}/validation.bin" \
  --max-tokens 2000000
python -m scripts.combine_token_parts \
  --input-dir "${TOKENIZED_DIR}/parts/test" \
  --output "${TOKENIZED_DIR}/test.bin" \
  --max-tokens 1000000

python - <<'PY'
from __future__ import annotations

import json
from pathlib import Path

root = Path("data/tokenized-5b")
splits = {}
for split in ("train", "validation", "test"):
    path = root / f"{split}.metadata.json"
    splits[split] = json.loads(path.read_text(encoding="utf-8"))
metadata = {
    "tokenizer": str(Path("artifacts/tokenizer-20k-pilot").resolve()),
    "vocab_size": 20000,
    "eos_token_id": 2,
    "splits": splits,
}
(root / "metadata.json").write_text(
    json.dumps(metadata, ensure_ascii=False, indent=2) + "\n",
    encoding="utf-8",
)
print(json.dumps(metadata, ensure_ascii=False, indent=2))
PY

echo "Parallel 5B tokenization complete."