File size: 4,302 Bytes
6f8c75b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# =============================================================================
# COPYRIGHT © 2025 Konstantin Vladimirovich Grabko. ALL RIGHTS RESERVED.
# CMS Manhattan JiRack Technology — PATENT PENDING
#
# This code is proprietary. 
# Personal and non-commercial research use is allowed.
# Any commercial use, derivative works for profit, or distribution 
# requires a paid license and 5% royalty.
#
# Unauthorized commercial use is strictly prohibited.
# Contact: grabko@cmsmanhattan.com
# =============================================================================

import torch
import os
from transformers import AutoTokenizer
from tqdm import tqdm

def stream_docs(file_path, delimiter="<|end_of_text|>"):
    buffer = ""
    with open(file_path, 'r', encoding='utf-8') as f:
        while True:
            chunk = f.read(1024 * 1024) # 1MB
            if not chunk:
                if buffer.strip(): yield buffer
                break
            buffer += chunk
            while delimiter in buffer:
                doc, buffer = buffer.split(delimiter, 1)
                if doc.strip(): yield doc

def tokenize_with_overlap(
    input_file="jirack_base_dataset.txt",
    #model_id="meta-llama/Llama-3.1-8B-Instruct",
    model_id=".",
    chunk_size=2000,
    max_length=8192,
    overlap_size=512,
    output_prefix="jirack_overlap_data"
):
    print(f"📥 Загрузка токенизатора: {model_id}")
    tokenizer = AutoTokenizer.from_pretrained(model_id)
    
    # КРИТИЧЕСКИЙ ФИКС: Проверяем pad_token_id
    if tokenizer.pad_token_id is None:
        if tokenizer.eos_token_id is not None:
            tokenizer.pad_token_id = tokenizer.eos_token_id
        else:
            tokenizer.pad_token_id = 128004 # Дефолт для Llama 3
    
    pad_id = tokenizer.pad_token_id
    print(f"🛠 Используемый Pad Token ID: {pad_id}")

    stride = max_length - overlap_size
    input_ids_buffer = []
    labels_buffer = []
    chunk_idx = 0

    def save_chunk(ids, labels, idx):
        if not ids: return
        filename = f"{output_prefix}_{idx}.pt"
        torch.save({
            "input_ids": torch.stack(ids).to(torch.int64),
            "labels": torch.stack(labels).to(torch.int64)
        }, filename)
        print(f"\n💾 Сохранен чанк {idx}: {filename} ({len(ids)} строк)")

    print(f"🔄 Нарезка 36GB файла. Окно: {max_length}, Нахлест: {overlap_size}")

    for doc in tqdm(stream_docs(input_file), desc="Processing"):
        try:
            text = doc.strip()
            if not text: continue

            full_text = f"<|begin_of_text|>{text}<|end_of_text|>"
            full_ids = tokenizer.encode(full_text, add_special_tokens=False)
            
            if not full_ids: continue

            # Нарезаем на окна
            windows = []
            if len(full_ids) <= max_length:
                windows.append(full_ids)
            else:
                for i in range(0, len(full_ids), stride):
                    w = full_ids[i : i + max_length]
                    if len(w) > 10:
                        windows.append(w)

            for w in windows:
                ids = list(w)
                lbs = list(w)

                if len(ids) < max_length:
                    pad_len = max_length - len(ids)
                    # Используем проверенный pad_id
                    ids += [pad_id] * pad_len
                    lbs += [-100] * pad_len
                
                input_ids_buffer.append(torch.tensor(ids, dtype=torch.int64))
                labels_buffer.append(torch.tensor(lbs, dtype=torch.int64))

                if len(input_ids_buffer) >= chunk_size:
                    save_chunk(input_ids_buffer, labels_buffer, chunk_idx)
                    chunk_idx += 1
                    input_ids_buffer, labels_buffer = [], []

        except Exception as e:
            # Теперь мы будем видеть реальную ошибку, если она осталась
            print(f"\n⚠️ Ошибка: {e}")
            continue

    if input_ids_buffer:
        save_chunk(input_ids_buffer, labels_buffer, chunk_idx)

if __name__ == "__main__":
    tokenize_with_overlap()