Upload huge base dataset (36GB)
Browse files- BaseSFTPtData.py +117 -0
BaseSFTPtData.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =============================================================================
|
| 2 |
+
# COPYRIGHT © 2025 Konstantin Vladimirovich Grabko. ALL RIGHTS RESERVED.
|
| 3 |
+
# CMS Manhattan JiRack Technology — PATENT PENDING
|
| 4 |
+
#
|
| 5 |
+
# This code is proprietary.
|
| 6 |
+
# Personal and non-commercial research use is allowed.
|
| 7 |
+
# Any commercial use, derivative works for profit, or distribution
|
| 8 |
+
# requires a paid license and 5% royalty.
|
| 9 |
+
#
|
| 10 |
+
# Unauthorized commercial use is strictly prohibited.
|
| 11 |
+
# Contact: grabko@cmsmanhattan.com
|
| 12 |
+
# =============================================================================
|
| 13 |
+
|
| 14 |
+
import torch
|
| 15 |
+
import os
|
| 16 |
+
from transformers import AutoTokenizer
|
| 17 |
+
from tqdm import tqdm
|
| 18 |
+
|
| 19 |
+
def stream_docs(file_path, delimiter="<|end_of_text|>"):
|
| 20 |
+
buffer = ""
|
| 21 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 22 |
+
while True:
|
| 23 |
+
chunk = f.read(1024 * 1024) # 1MB
|
| 24 |
+
if not chunk:
|
| 25 |
+
if buffer.strip(): yield buffer
|
| 26 |
+
break
|
| 27 |
+
buffer += chunk
|
| 28 |
+
while delimiter in buffer:
|
| 29 |
+
doc, buffer = buffer.split(delimiter, 1)
|
| 30 |
+
if doc.strip(): yield doc
|
| 31 |
+
|
| 32 |
+
def tokenize_with_overlap(
|
| 33 |
+
input_file="jirack_base_dataset.txt",
|
| 34 |
+
#model_id="meta-llama/Llama-3.1-8B-Instruct",
|
| 35 |
+
model_id=".",
|
| 36 |
+
chunk_size=2000,
|
| 37 |
+
max_length=8192,
|
| 38 |
+
overlap_size=512,
|
| 39 |
+
output_prefix="jirack_overlap_data"
|
| 40 |
+
):
|
| 41 |
+
print(f"📥 Загрузка токенизатора: {model_id}")
|
| 42 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 43 |
+
|
| 44 |
+
# КРИТИЧЕСКИЙ ФИКС: Проверяем pad_token_id
|
| 45 |
+
if tokenizer.pad_token_id is None:
|
| 46 |
+
if tokenizer.eos_token_id is not None:
|
| 47 |
+
tokenizer.pad_token_id = tokenizer.eos_token_id
|
| 48 |
+
else:
|
| 49 |
+
tokenizer.pad_token_id = 128004 # Дефолт для Llama 3
|
| 50 |
+
|
| 51 |
+
pad_id = tokenizer.pad_token_id
|
| 52 |
+
print(f"🛠 Используемый Pad Token ID: {pad_id}")
|
| 53 |
+
|
| 54 |
+
stride = max_length - overlap_size
|
| 55 |
+
input_ids_buffer = []
|
| 56 |
+
labels_buffer = []
|
| 57 |
+
chunk_idx = 0
|
| 58 |
+
|
| 59 |
+
def save_chunk(ids, labels, idx):
|
| 60 |
+
if not ids: return
|
| 61 |
+
filename = f"{output_prefix}_{idx}.pt"
|
| 62 |
+
torch.save({
|
| 63 |
+
"input_ids": torch.stack(ids).to(torch.int64),
|
| 64 |
+
"labels": torch.stack(labels).to(torch.int64)
|
| 65 |
+
}, filename)
|
| 66 |
+
print(f"\n💾 Сохранен чанк {idx}: {filename} ({len(ids)} строк)")
|
| 67 |
+
|
| 68 |
+
print(f"🔄 Нарезка 36GB файла. Окно: {max_length}, Нахлест: {overlap_size}")
|
| 69 |
+
|
| 70 |
+
for doc in tqdm(stream_docs(input_file), desc="Processing"):
|
| 71 |
+
try:
|
| 72 |
+
text = doc.strip()
|
| 73 |
+
if not text: continue
|
| 74 |
+
|
| 75 |
+
full_text = f"<|begin_of_text|>{text}<|end_of_text|>"
|
| 76 |
+
full_ids = tokenizer.encode(full_text, add_special_tokens=False)
|
| 77 |
+
|
| 78 |
+
if not full_ids: continue
|
| 79 |
+
|
| 80 |
+
# Нарезаем на окна
|
| 81 |
+
windows = []
|
| 82 |
+
if len(full_ids) <= max_length:
|
| 83 |
+
windows.append(full_ids)
|
| 84 |
+
else:
|
| 85 |
+
for i in range(0, len(full_ids), stride):
|
| 86 |
+
w = full_ids[i : i + max_length]
|
| 87 |
+
if len(w) > 10:
|
| 88 |
+
windows.append(w)
|
| 89 |
+
|
| 90 |
+
for w in windows:
|
| 91 |
+
ids = list(w)
|
| 92 |
+
lbs = list(w)
|
| 93 |
+
|
| 94 |
+
if len(ids) < max_length:
|
| 95 |
+
pad_len = max_length - len(ids)
|
| 96 |
+
# Используем проверенный pad_id
|
| 97 |
+
ids += [pad_id] * pad_len
|
| 98 |
+
lbs += [-100] * pad_len
|
| 99 |
+
|
| 100 |
+
input_ids_buffer.append(torch.tensor(ids, dtype=torch.int64))
|
| 101 |
+
labels_buffer.append(torch.tensor(lbs, dtype=torch.int64))
|
| 102 |
+
|
| 103 |
+
if len(input_ids_buffer) >= chunk_size:
|
| 104 |
+
save_chunk(input_ids_buffer, labels_buffer, chunk_idx)
|
| 105 |
+
chunk_idx += 1
|
| 106 |
+
input_ids_buffer, labels_buffer = [], []
|
| 107 |
+
|
| 108 |
+
except Exception as e:
|
| 109 |
+
# Теперь мы будем видеть реальную ошибку, если она осталась
|
| 110 |
+
print(f"\n⚠️ Ошибка: {e}")
|
| 111 |
+
continue
|
| 112 |
+
|
| 113 |
+
if input_ids_buffer:
|
| 114 |
+
save_chunk(input_ids_buffer, labels_buffer, chunk_idx)
|
| 115 |
+
|
| 116 |
+
if __name__ == "__main__":
|
| 117 |
+
tokenize_with_overlap()
|