Text Generation
English
code
File size: 10,634 Bytes
dae52e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""
=====================================
 Shulker Code β€” Dataset Module
 Code dataset loaders for training
 Developed by @kopeedev / CyeroX
=====================================

Supported formats:
 - JSONL: {"text": "...", "lang": "python"} or {"prompt": "...", "completion": "..."}
 - TXT: Plain code files
 - HuggingFace datasets (GitHub Code, CodeSearchNet, etc.)
"""

import os
import json
import glob
import random
from typing import Optional, List, Dict, Iterator, Union
from pathlib import Path

import torch
from torch.utils.data import Dataset, DataLoader, IterableDataset


# ─────────────────────────────────────────────
# Core Token Dataset
# ─────────────────────────────────────────────
class CodeTokenDataset(Dataset):
    """
    Dataset that loads pre-tokenized code sequences from JSONL files.

    Each sample in the JSONL can have:
    - {"text": "..."} β†’ raw code/text to tokenize
    - {"input_ids": [1, 2, ...]} β†’ pre-tokenized (faster)
    - {"prompt": "...", "completion": "..."} β†’ instruction format

    Uses sliding window to handle long files.
    """

    def __init__(
        self,
        file_paths: Union[str, List[str]],
        tokenizer,
        max_seq_len: int = 1024,
        stride: int = 512,        # Sliding window stride
        task: str = "gen",
        language: str = "python",
        cache_tokenized: bool = True,
    ):
        self.tokenizer = tokenizer
        self.max_seq_len = max_seq_len
        self.stride = stride
        self.task = task
        self.language = language

        # Normalize to list
        if isinstance(file_paths, str):
            file_paths = [file_paths]

        # Load all samples
        self.samples: List[List[int]] = []
        self._load_files(file_paths, cache_tokenized)

        print(f"πŸ“¦ Dataset loaded: {len(self.samples):,} sequences from {len(file_paths)} files")

    def _load_files(self, file_paths: List[str], cache_tokenized: bool):
        """Load and tokenize all files."""
        for path in file_paths:
            if not os.path.exists(path):
                print(f"⚠️  File not found: {path}")
                continue

            ext = Path(path).suffix.lower()

            if ext in (".jsonl", ".json"):
                self._load_jsonl(path)
            elif ext in (".txt", ".py", ".js", ".ts", ".cpp", ".java", ".go", ".rs"):
                self._load_text_file(path)
            else:
                # Try as text
                self._load_text_file(path)

    def _load_jsonl(self, path: str):
        """Load a JSONL file."""
        with open(path, "r", encoding="utf-8") as f:
            for line in f:
                line = line.strip()
                if not line:
                    continue
                try:
                    item = json.loads(line)
                    self._add_item(item)
                except json.JSONDecodeError:
                    continue

    def _load_text_file(self, path: str):
        """Load a plain text/code file."""
        with open(path, "r", encoding="utf-8", errors="replace") as f:
            text = f.read()

        # Detect language from extension
        ext_to_lang = {".py": "python", ".js": "javascript", ".ts": "typescript",
                       ".cpp": "cpp", ".java": "java", ".go": "go", ".rs": "rust"}
        lang = ext_to_lang.get(Path(path).suffix.lower(), "text")

        self._add_text(text, lang)

    def _add_item(self, item: Dict):
        """Process a single dataset item."""
        if "input_ids" in item:
            # Already tokenized
            ids = item["input_ids"]
            self._add_ids(ids)
        elif "text" in item:
            lang = item.get("lang", self.language)
            self._add_text(item["text"], lang)
        elif "prompt" in item and "completion" in item:
            # Instruction-following format
            text = item["prompt"] + item["completion"]
            lang = item.get("lang", self.language)
            self._add_text(text, lang)
        elif "content" in item:
            # GitHub Code format
            lang = item.get("programming_language", self.language)
            self._add_text(item["content"], lang)

    def _add_text(self, text: str, lang: str = "python"):
        """Tokenize text and add as sliding window chunks."""
        try:
            ids = self.tokenizer.encode_code(text, language=lang, task=self.task)
            self._add_ids(ids)
        except Exception:
            # Fallback: basic encode
            try:
                ids = self.tokenizer.encode(text)
                self._add_ids(ids)
            except Exception:
                pass

    def _add_ids(self, ids: List[int]):
        """Split token sequence into overlapping windows."""
        if len(ids) < 2:
            return

        # Slide a window over the sequence
        for start in range(0, max(1, len(ids) - self.max_seq_len + 1), self.stride):
            chunk = ids[start : start + self.max_seq_len]
            if len(chunk) >= 2:  # Need at least 2 tokens for input/label pairs
                self.samples.append(chunk)

    def __len__(self) -> int:
        return len(self.samples)

    def __getitem__(self, idx: int) -> Dict[str, torch.Tensor]:
        ids = self.samples[idx]

        # Pad to max_seq_len
        pad_len = self.max_seq_len - len(ids)
        attention_mask = [1] * len(ids) + [0] * pad_len
        ids = ids + [self.tokenizer.pad_token_id] * pad_len

        return {
            "input_ids": torch.tensor(ids, dtype=torch.long),
            "attention_mask": torch.tensor(attention_mask, dtype=torch.long),
            "labels": torch.tensor(ids, dtype=torch.long),  # Same as input for CLM
        }


# ─────────────────────────────────────────────
# Streaming Dataset (for huge datasets)
# ─────────────────────────────────────────────
class StreamingCodeDataset(IterableDataset):
    """
    Streaming dataset for training on massive code corpora.
    Reads from disk on-the-fly without loading everything into RAM.
    Perfect for datasets like GitHub Code (hundreds of GB).
    """

    def __init__(
        self,
        file_paths: List[str],
        tokenizer,
        max_seq_len: int = 1024,
        shuffle: bool = True,
        buffer_size: int = 1000,
    ):
        self.file_paths = file_paths
        self.tokenizer = tokenizer
        self.max_seq_len = max_seq_len
        self.shuffle = shuffle
        self.buffer_size = buffer_size

    def __iter__(self) -> Iterator[Dict[str, torch.Tensor]]:
        files = self.file_paths.copy()
        if self.shuffle:
            random.shuffle(files)

        buffer = []

        for path in files:
            with open(path, "r", encoding="utf-8", errors="replace") as f:
                for line in f:
                    line = line.strip()
                    if not line:
                        continue
                    try:
                        item = json.loads(line)
                        text = item.get("text") or item.get("content") or item.get("code", "")
                        lang = item.get("lang", "python")
                    except Exception:
                        text = line
                        lang = "python"

                    if not text:
                        continue

                    try:
                        ids = self.tokenizer.encode_code(text, language=lang)
                    except Exception:
                        continue

                    # Yield chunks
                    for start in range(0, max(1, len(ids) - self.max_seq_len + 1), self.max_seq_len // 2):
                        chunk = ids[start : start + self.max_seq_len]
                        if len(chunk) < 2:
                            continue

                        pad_len = self.max_seq_len - len(chunk)
                        attention_mask = [1] * len(chunk) + [0] * pad_len
                        chunk = chunk + [self.tokenizer.pad_token_id] * pad_len

                        sample = {
                            "input_ids": torch.tensor(chunk, dtype=torch.long),
                            "attention_mask": torch.tensor(attention_mask, dtype=torch.long),
                            "labels": torch.tensor(chunk, dtype=torch.long),
                        }

                        buffer.append(sample)

                        if self.shuffle and len(buffer) >= self.buffer_size:
                            random.shuffle(buffer)
                            yield from buffer
                            buffer = []

        # Yield remaining
        if self.shuffle:
            random.shuffle(buffer)
        yield from buffer


# ─────────────────────────────────────────────
# DataLoader Factory
# ─────────────────────────────────────────────
def create_dataloader(
    dataset: Dataset,
    batch_size: int = 8,
    num_workers: int = 4,
    shuffle: bool = True,
    pin_memory: bool = True,
    drop_last: bool = True,
) -> DataLoader:
    """Create an optimized DataLoader for training."""
    return DataLoader(
        dataset,
        batch_size=batch_size,
        shuffle=shuffle and not isinstance(dataset, IterableDataset),
        num_workers=num_workers,
        pin_memory=pin_memory and torch.cuda.is_available(),
        drop_last=drop_last,
        persistent_workers=num_workers > 0,
        prefetch_factor=2 if num_workers > 0 else None,
    )


def discover_code_files(directory: str, extensions: Optional[List[str]] = None) -> List[str]:
    """
    Recursively find all code files in a directory.

    Args:
        directory: Root directory to search
        extensions: List of extensions, e.g. [".py", ".js"]

    Returns:
        List of file paths
    """
    if extensions is None:
        extensions = [".py", ".js", ".ts", ".cpp", ".java", ".go", ".rs", ".cs", ".txt", ".jsonl"]

    files = []
    for ext in extensions:
        pattern = os.path.join(directory, "**", f"*{ext}")
        files.extend(glob.glob(pattern, recursive=True))

    print(f"πŸ” Found {len(files):,} code files in {directory}")
    return files