-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilders.py
976 lines (853 loc) · 32 KB
/
builders.py
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
import os
import random
from enum import Enum
from typing import Any, Callable, Dict, List, Optional
import numpy as np
import torch
from datasets import load_dataset
from lightning.pytorch.core.datamodule import LightningDataModule
from torch.utils.data import DataLoader, IterableDataset
from transformers import PreTrainedTokenizer
class DataFormat(Enum):
SIMPLE = "simple"
INSTRUCTION = "instruction"
CONVERSATION = "conversation"
PERSONACHAT = "persona_chat"
CUSTOM = "custom"
SMOLTALK = "smoltalk"
SODA = "soda"
WIKI = "wiki"
HUGGINGFACE_DATASETS = {
"textbooks": dict(
path="open-phi/textbooks",
keys=["markdown"],
format=DataFormat.SIMPLE,
weight=0.001,
),
"smollm-corpus": dict(
path="HuggingFaceTB/smollm-corpus",
name="cosmopedia-v2",
keys=["prompt", "text"],
format=DataFormat.INSTRUCTION,
weight=0.001,
),
"natural-instructions": dict(
path="Muennighoff/natural-instructions",
name="default",
keys=["definition", "inputs", "targets"],
format=DataFormat.CONVERSATION,
weight=0.001,
),
"persona-chat": dict(
path="google/Synthetic-Persona-Chat",
keys=["user 1 personas", "user 2 personas", "Best Generated Conversation"],
format=DataFormat.PERSONACHAT,
weight=0.001,
),
"smoltalk": dict(
path="HuggingFaceTB/smoltalk",
name="all",
keys=["messages"],
format=DataFormat.SMOLTALK,
weight=0.001,
),
"soda": dict(
path="allenai/soda",
keys=[
"speakers",
"narrative",
"literal",
"dialogue",
"head",
"relation",
"tail",
],
format=DataFormat.SODA,
weight=0.005,
),
"github-code": dict(
path="codeparrot/github-code",
name="all-all",
keys=["code"],
format=DataFormat.SIMPLE,
weight=0.001,
),
"tinystories": dict(
path="roneneldan/TinyStories",
name="default",
keys=["text"],
format=DataFormat.SIMPLE,
weight=0.001,
),
"legal": dict(
path="pile-of-law/pile-of-law",
name="all",
keys=["text"],
format=DataFormat.SIMPLE,
weight=0.001,
),
"wikipedia": dict(
path="wikimedia/wikipedia",
name="20231101.en",
keys=["title", "text"],
format=DataFormat.WIKI,
weight=0.001,
),
"redpajama": dict(
path="togethercomputer/RedPajama-Data-V2",
name="sample-10B",
snapshots=["2023-14"],
keys=["raw_content"],
format=DataFormat.SIMPLE,
weight=1.0,
),
"fineweb-edu-10bt": dict(
path="HuggingFaceFW/fineweb-edu",
name="sample-10BT",
keys=["text"],
format=DataFormat.SIMPLE,
weight=1.0,
),
"fineweb-edu-100bt": dict(
path="HuggingFaceFW/fineweb-edu",
name="sample-100BT",
keys=["text"],
format=DataFormat.SIMPLE,
weight=1.0,
),
"fineweb-edu-350bt": dict(
path="HuggingFaceFW/fineweb-edu",
name="sample-350BT",
keys=["text"],
format=DataFormat.SIMPLE,
weight=1.0,
),
"fineweb": dict(
path="HuggingFaceFW/fineweb",
name="default",
keys=["text"],
format=DataFormat.SIMPLE,
weight=0.25,
),
}
def format_simple(
document: Dict, keys: List[str], bos_token: str, eos_token: str
) -> str:
"""Just concatenate content with spaces"""
return (
bos_token + " ".join(document.get(key, "") for key in keys) + eos_token + "\n"
)
def format_instruction(
document: Dict, keys: List[str], bos_token: str, eos_token: str
) -> str:
"""Format as instruction/output pairs in ChatML format."""
assert len(keys) == 2, "Instruction format requires exactly 2 keys"
instruction = document.get(keys[0], "")
output = document.get(keys[1], "")
return (
f"{bos_token}user\n{instruction}\n{eos_token}\n"
f"{bos_token}assistant\n{output}\n{eos_token}\n"
)
def format_conversation(
document: Dict, keys: List[str], bos_token: str, eos_token: str
) -> str:
"""Format as a conversation in ChatML format."""
assert len(keys) == 3, "Conversation format requires exactly 3 keys"
parts = []
for i, key in enumerate(keys):
if i == 0:
role = "system"
elif i == 1:
role = "user"
elif i == 2:
role = "assistant"
message = document.get(key, "")
parts.append(f"{bos_token}{role}\n{message}\n{eos_token}\n")
return "".join(parts)
def format_personachat(
document: Dict, keys: List[str], bos_token: str, eos_token: str
) -> str:
"""Format persona chat conversations into ChatML format."""
# Extract personas
user_personas = document.get("user 1 personas", "").split("\n")
assistant_personas = document.get("user 2 personas", "").split("\n")
conversation = document.get("Best Generated Conversation", "").split("\n")
# Include personas in system message
system_message = ""
if user_personas:
system_message += "".join(
f"- {p.strip()}\n" for p in user_personas if p.strip()
)
if assistant_personas:
system_message += "".join(
f"- {p.strip()}\n" for p in assistant_personas if p.strip()
)
# Initialize the formatted text with system message
formatted = f"{bos_token}system\n{system_message.strip()}\n{eos_token}\n"
# Map speaker labels to ChatML roles
speaker_map = {
"A": "user",
"B": "assistant",
"USER 1": "user",
"USER1": "user",
"USER 2": "assistant",
"USER2": "assistant",
}
# Format the conversation using "user" and "assistant"
for i, utterance in enumerate(conversation):
if ": " in utterance:
speaker_label, text = utterance.split(": ", 1)
role = speaker_map.get(speaker_label.strip().upper(), "user")
else:
# Alternate speakers if no prefix is present
role = "user" if i % 2 == 0 else "assistant"
text = utterance
formatted += f"{bos_token}{role}\n{text.strip()}\n{eos_token}\n"
return formatted
def format_smoltalk(
document: Dict, keys: List[str], bos_token: str, eos_token: str
) -> str:
"""Format Smoltalk-style message arrays into ChatML format."""
assert (
len(keys) == 1 and keys[0] == "messages"
), "Smoltalk format requires 'messages' key"
# Get messages array
messages = document.get(keys[0], [])
# Format each message in original order
formatted_messages = []
for message in messages:
role = message.get("role", "user") # Default to user if role missing
content = message.get("content", "").strip()
if content: # Only add non-empty messages
formatted_messages.append(f"{bos_token}{role}\n{content}\n{eos_token}\n")
# Join all messages together
return "".join(formatted_messages)
def format_wiki(document: Dict, keys: List[str], bos_token: str, eos_token: str) -> str:
"""Format wiki text."""
assert len(keys) == 2, "Wiki format requires exactly 2 keys"
title = document.get(keys[0], "")
body = document.get(keys[1], "")
return f"{bos_token}{title}\n{body}{eos_token}\n"
def format_soda(document: Dict, keys: List[str], bos_token: str, eos_token: str) -> str:
"""Formats a single example into ChatML format."""
speakers = document[keys[0]]
narrative = document[keys[1]]
literal = document[keys[2]]
dialogue = document[keys[3]]
head = document[keys[4]]
relation = document[keys[5]]
tail = document[keys[6]]
# Create person mapping first
person_mapping = create_person_mapping(document)
# Get speaker roles
unique_speakers = list(dict.fromkeys(speakers)) # preserve order, remove duplicates
speaker_roles = {}
# Always map first two speakers to user/assistant
if len(unique_speakers) >= 1:
speaker_roles[unique_speakers[0]] = "user"
if len(unique_speakers) >= 2:
speaker_roles[unique_speakers[1]] = "assistant"
# Map any additional speakers to "other"
for speaker in unique_speakers[2:]:
speaker_roles[speaker] = "other"
# Start with system message
chatml = f"{bos_token}system\n"
# Add role mappings to system context
for speaker, role in speaker_roles.items():
chatml += f"{role}: {speaker}\n"
# Add knowledge structure
chatml += f"Cause: {replace_person_references(head, person_mapping)}\n"
chatml += f"Relation: {relation[1:]}\n"
chatml += f"Effect: {replace_person_references(tail, person_mapping)}\n"
# Add context from literal and narrative
chatml += f"Context: {narrative}\n"
chatml += f"Thought: ({literal})\n" # <|thinking|>
chatml += f"{eos_token}\n"
# Add conversation turns
for speaker, message in zip(speakers, dialogue):
role = speaker_roles[speaker]
chatml += f"{bos_token}{role}\n"
chatml += f"{message}\n"
chatml += f"{eos_token}\n"
return chatml
def create_person_mapping(example: Dict) -> Dict[str, str]:
"""Creates a mapping from PersonX/Y/Z to actual names."""
mapping = {}
# Only add non-empty mappings
if example["PersonX"]:
mapping["PersonX"] = example["PersonX"]
if example["PersonY"]:
mapping["PersonY"] = example["PersonY"]
if example["PersonZ"]:
mapping["PersonZ"] = example["PersonZ"]
return mapping
def replace_person_references(text: str, mapping: Dict[str, str]) -> str:
"""Replaces PersonX/Y/Z references with actual names."""
if not text:
return text
result = text
for person, name in mapping.items():
if name: # Only replace if we have a name
result = result.replace(person, name)
return result
FORMAT_HANDLERS = {
DataFormat.SIMPLE: format_simple,
DataFormat.INSTRUCTION: format_instruction,
DataFormat.CONVERSATION: format_conversation,
DataFormat.PERSONACHAT: format_personachat,
DataFormat.SMOLTALK: format_smoltalk,
DataFormat.SODA: format_soda,
DataFormat.WIKI: format_wiki,
}
def get_datamodules(
seed: int,
dev: bool,
phi: bool,
gun: bool,
source: bool,
tokenizer,
hparams,
data_path,
*args,
):
train_data = []
config = get_dataset_configs(dev, phi)
for c in config["primary"]:
train_data.append(
get_dataset("huggingface", tokenizer, hparams["block_size"], seed, c, *args)
)
if data_path:
train_data.append(
get_dataset(
"directory",
tokenizer,
hparams["block_size"],
seed,
data_path=data_path,
*args,
)
)
if source:
train_data.append(
get_dataset(
"self",
tokenizer,
hparams["block_size"],
seed,
*args,
)
)
if gun:
train_data.append(get_dataset("gun", tokenizer, hparams["block_size"], seed))
validation_data = []
if len(config["validation"]) > 0:
for c in config["validation"]:
validation_data.append(
get_dataset(
"huggingface", tokenizer, hparams["block_size"], seed, c, *args
)
)
train_dataloader = PraxisDataModule(
train_data,
validation_data,
tokenizer,
hparams["batch_size"],
hparams["block_size"],
hparams["oversample_chance"],
hparams["supersample_chance"],
)
return train_dataloader
def get_dataset(format, tokenizer, block_size, seed, *args, **kwargs):
if format == "huggingface":
dataset = HuggingfaceDataset(tokenizer, block_size, seed, *args)
# First arg is config dict for huggingface
dataset.weight = args[0].get("weight", 1.0)
return dataset
elif format == "directory":
dataset = MultiDirectoryDataset(
tokenizer, block_size, directories=kwargs.get("data_path")
)
dataset.weight = 0.1
return dataset
elif format == "self":
dataset = MultiDirectoryDataset(
tokenizer,
block_size,
directories="./",
allowed_extensions=[
".py",
".js",
".mjs",
".gd",
".tscn",
".cfg",
".godot",
".html",
".css",
".txt",
".md",
".sh",
],
)
dataset.weight = 0.001
return dataset
elif format == "gun":
dataset = GunChatDataset(tokenizer, block_size)
dataset.weight = 0.001
return dataset
def get_dataset_configs(dev: bool, phi: bool):
config = {"primary": [], "validation": []}
config["primary"].append(HUGGINGFACE_DATASETS.get("fineweb-edu-10bt"))
config["primary"].append(HUGGINGFACE_DATASETS.get("fineweb"))
if phi:
config["primary"].append(HUGGINGFACE_DATASETS.get("textbooks"))
config["primary"].append(HUGGINGFACE_DATASETS.get("smollm-corpus"))
config["primary"].append(HUGGINGFACE_DATASETS.get("natural-instructions"))
config["primary"].append(HUGGINGFACE_DATASETS.get("persona-chat"))
config["primary"].append(HUGGINGFACE_DATASETS.get("smoltalk"))
config["primary"].append(HUGGINGFACE_DATASETS.get("soda"))
config["primary"].append(HUGGINGFACE_DATASETS.get("github-code"))
config["primary"].append(HUGGINGFACE_DATASETS.get("tinystories"))
config["primary"].append(HUGGINGFACE_DATASETS.get("wikipedia"))
config["primary"].append(HUGGINGFACE_DATASETS.get("legal"))
if dev:
# Overwrite with simpler dataset
config["primary"] = [HUGGINGFACE_DATASETS.get("textbooks")]
else:
config["validation"].append(HUGGINGFACE_DATASETS.get("redpajama"))
print("training on:")
[
print(f"dataset: {entry['path']}, weight: {entry['weight']}")
for entry in config["primary"]
]
return config
class PraxisSampler:
def __init__(self, tokenizer: PreTrainedTokenizer, block_size: int):
self.tokenizer = tokenizer
self.block_size = block_size
self.sequence_cache = [] # Store raw text sequences
self.token_cache = [] # Store tokenized batches
self.weight = 1.0
@property
def can_sample(self):
return True
def fill_sequence_cache(self):
"""Each dataset implementation should override this"""
raise NotImplementedError
def get_sequences(self, count: int = 1) -> List[str]:
"""Get raw sequences from this dataset"""
while len(self.sequence_cache) < count:
self.fill_sequence_cache()
return [self.sequence_cache.pop(0) for _ in range(count)]
def get_batch(
self, oversample: bool = False, supersample: bool = False
) -> torch.Tensor:
if supersample and oversample:
raise ValueError("Cannot both oversample and supersample simultaneously.")
seq_factor = 4 if supersample else (2 if oversample else 1)
while len(self.token_cache) < seq_factor:
self.fill_token_cache()
batch = torch.cat([self.token_cache.pop(0) for _ in range(seq_factor)], dim=0)
return batch
class InterleaveDataManager:
def __init__(
self, samplers, weights, tokenizer, block_size, text_cache_size=100_000
):
self.samplers = samplers
self.weights = weights
self.tokenizer = tokenizer
self.block_size = block_size
self.text_cache_size = text_cache_size
self.token_stream = torch.tensor(
[], dtype=torch.long
) # Single continuous stream
def extend_token_stream(self):
"""Add more tokens to our stream when needed"""
interleaved = self.create_interleaved_sequence()
tokens = self.tokenizer(
text=interleaved,
padding=False, # No padding needed since we're building a stream
return_tensors="pt",
)["input_ids"].squeeze(
0
) # Get flat token sequence
self.token_stream = torch.cat([self.token_stream, tokens])
def get_batch(
self, batch_size: int, oversample: bool = False, supersample: bool = False
) -> List[torch.Tensor]:
sequence_length = self.block_size
current_batch_size = batch_size
# Check if batch size supports the requested sampling mode
if supersample and batch_size >= 16:
current_batch_size = batch_size // 16
sequence_length = self.block_size * 4
elif oversample and batch_size >= 4:
current_batch_size = batch_size // 4
sequence_length = self.block_size * 2
else:
# If batch size isn't sufficient, fall back to normal sampling
oversample = False
supersample = False
# Calculate how many total tokens we need
tokens_needed = current_batch_size * sequence_length
# Make sure we have enough tokens
while len(self.token_stream) < tokens_needed:
self.extend_token_stream()
# Extract batch
batch = []
for i in range(current_batch_size):
start = i * sequence_length
end = start + sequence_length
batch.append(self.token_stream[start:end])
# Remove used tokens from the stream
self.token_stream = self.token_stream[tokens_needed:]
return batch
def create_interleaved_sequence(self) -> str:
"""Create a single interleaved sequence from multiple samplers"""
sequence = ""
while len(sequence) < self.text_cache_size:
# Pick a sampler based on weights
sampler = random.choices(self.samplers, weights=self.weights, k=1)[0]
# Get a sequence from that sampler
new_sequences = sampler.get_sequences(1)
sequence += new_sequences[0]
return sequence
def fill_token_cache(self):
"""Fill token cache with interleaved sequences"""
interleaved = self.create_interleaved_sequence()
tokens = self.tokenizer(
text=interleaved,
max_length=self.block_size,
stride=0,
padding=True,
truncation=True,
return_overflowing_tokens=True,
return_tensors="pt",
)["input_ids"]
self.token_cache.extend(
[batch for batch in tokens if len(batch) == self.block_size]
)
class HuggingfaceDataset(PraxisSampler):
def __init__(
self, tokenizer: PreTrainedTokenizer, block_size: int, seed: int, config: Dict
):
super().__init__(tokenizer, block_size)
self.keys = config.get("keys", ["text"])
self.format = config.get("format", DataFormat.SIMPLE)
if isinstance(self.format, str):
self.format = DataFormat(self.format)
# For custom formats, config should provide format_handler
self.format_handler = (
config.get("format_handler")
if self.format == DataFormat.CUSTOM
else FORMAT_HANDLERS[self.format]
)
dataset_args = dict(
path=config.get("path", "HuggingFaceFW/fineweb"),
split="train",
streaming=True,
cache_dir=os.path.join(config.get("cache_dir", "data"), "datasets"),
trust_remote_code=True,
)
if "name" in config:
dataset_args["name"] = config["name"]
self.dataset = load_dataset(**dataset_args)
self.buffer_size = 1_000
self.shuffled_dataset = self.dataset.shuffle(
seed=seed, buffer_size=self.buffer_size
)
self.dataset_iterator = iter(self.shuffled_dataset)
def fill_sequence_cache(self):
try:
document = next(self.dataset_iterator)
formatted = self._format_document(document)
self.sequence_cache.append(formatted)
except StopIteration:
self.dataset_iterator = iter(self.shuffled_dataset)
self.fill_sequence_cache()
def _format_document(self, document):
return self.format_handler(
document, self.keys, self.tokenizer.bos_token, self.tokenizer.eos_token
)
def state_dict(self):
# Get the internal state of the shuffled dataset
return self.shuffled_dataset.state_dict()
def load_state_dict(self, state_dict):
# Restore the internal state so iteration picks up where we left off
self.shuffled_dataset.load_state_dict(state_dict)
# Recreate the iterator from the restored state
self.dataset_iterator = iter(self.shuffled_dataset)
class MultiDirectoryDataset(PraxisSampler):
def __init__(
self,
tokenizer: PreTrainedTokenizer,
block_size: int,
directories: List[str],
allowed_extensions: Optional[List[str]] = [],
excluded_dirs: Optional[List[str]] = None,
):
super().__init__(tokenizer, block_size)
# Normalize and resolve all directory paths relative to current working directory
self.cwd = os.getcwd()
self.directories = [
os.path.normpath(os.path.join(self.cwd, d)) for d in directories
]
self.allowed_extensions = [ext.lower() for ext in allowed_extensions]
# Default exclusions for common development directories
default_exclusions = {
".git",
".venv",
"venv",
"__pycache__",
"node_modules",
"build",
"dist",
".pytest_cache",
".mypy_cache",
".tox",
}
user_exclusions = set(excluded_dirs) if excluded_dirs else set()
self.excluded_dirs = default_exclusions.union(user_exclusions)
print(f"Working directory: {self.cwd}")
print(f"Scanning directories: {self.directories}")
print(f"File extensions filter: {self.allowed_extensions}")
print(f"Excluding directories: {self.excluded_dirs}")
self.file_list = self._get_file_list()
print(f"Found {len(self.file_list)} files")
random.shuffle(self.file_list)
self.file_iterator = iter(self.file_list)
def _should_skip_directory(self, dirpath: str) -> bool:
"""
Check if directory should be skipped based on exclusion rules
and ensure we don't leave the working directory context.
"""
dir_name = os.path.basename(dirpath)
# Check if directory is in excluded list
if dir_name in self.excluded_dirs:
return True
# Ensure directory is within working directory context
try:
# Resolve the real path, following symlinks
real_path = os.path.realpath(dirpath)
# Check if this path is within our working directory
return not real_path.startswith(self.cwd)
except:
# If there's any error resolving the path, skip it to be safe
return True
def _get_file_list(self) -> List[str]:
"""
Recursively traverse directories and return a flat list of fully-qualified file paths,
staying within the working directory context.
"""
all_files = []
for directory in self.directories:
if not os.path.exists(directory):
print(f"Warning: Directory {directory} does not exist, skipping...")
continue
try:
# Walk through directory recursively
for root, dirs, files in os.walk(
directory, topdown=True, followlinks=False
):
# Modify dirs in-place to prevent walking into excluded directories
dirs[:] = [
d
for d in dirs
if not self._should_skip_directory(os.path.join(root, d))
]
for filename in files:
# Get full path
full_path = os.path.join(root, filename)
# Verify file is within working directory
real_path = os.path.realpath(full_path)
if not real_path.startswith(self.cwd):
continue
# Check if file extension is allowed
file_ext = os.path.splitext(filename)[1].lower()
if len(self.allowed_extensions) > 0:
if file_ext in self.allowed_extensions:
all_files.append(full_path)
else:
all_files.append(full_path)
except Exception as e:
print(f"Error scanning directory {directory}: {str(e)}")
continue
return all_files
def _read_file(self, file_path: str) -> str:
"""Read and return the contents of a file."""
try:
with open(file_path, "r", encoding="utf-8") as f:
return f.read()
except UnicodeDecodeError:
# Fallback to latin-1 if UTF-8 fails
try:
with open(file_path, "r", encoding="latin-1") as f:
return f.read()
except Exception as e:
print(f"Error reading file {file_path}: {str(e)}")
return ""
except Exception as e:
print(f"Error reading file {file_path}: {str(e)}")
return ""
def fill_sequence_cache(self):
try:
file_path = next(self.file_iterator)
content = self._read_file(file_path)
self.sequence_cache.append(content)
except StopIteration:
random.shuffle(self.file_list)
self.file_iterator = iter(self.file_list)
self.fill_sequence_cache()
class GunChatDataset(PraxisSampler):
def __init__(self, tokenizer: PreTrainedTokenizer, block_size: int):
super().__init__(tokenizer, block_size)
from adapters import GunAdapter as Gun
self.gun = Gun()
def fill_sequence_cache(self):
# Get a list of text samples
text_list = self.gun.get_sample(250)
# Prepare the system prompt with arbitrary descriptions
user_description = random.choice(
[
"The user is interested in technology and gadgets.",
"The user loves discussing philosophy and life.",
"The user is curious about the latest news.",
"The user enjoys learning about history.",
"The user is seeking advice on personal development.",
"The user is passionate about art and creativity.",
"The user is looking for travel recommendations.",
"The user is studying computer science.",
"The user wants to learn new cooking recipes.",
"The user is enthusiastic about sports and fitness.",
]
)
assistant_description = random.choice(
[
"The assistant is a knowledgeable and helpful AI.",
"The assistant provides clear and concise answers.",
"The assistant is friendly and supportive.",
"The assistant offers detailed explanations.",
"The assistant helps users understand complex topics.",
"The assistant is skilled in problem-solving.",
"The assistant is patient and understanding.",
"The assistant excels in educational guidance.",
"The assistant is adept at providing creative ideas.",
"The assistant is resourceful and informative.",
]
)
system_prompt = f"{user_description}\n{assistant_description}"
# Initialize the formatted text with system message
formatted = f"{self.tokenizer.bos_token}system\n{system_prompt}\n{self.tokenizer.eos_token}\n"
# Build the conversation by randomly assigning text to user or assistant
for text in text_list:
role = random.choice(["user", "assistant"])
formatted += f"{self.tokenizer.bos_token}{role}\n{text.strip()}\n{self.tokenizer.eos_token}\n"
# Add the conversation to the sequence cache
self.sequence_cache.append(formatted)
class WeightedIterableDataset(IterableDataset):
def __init__(
self,
datasets: List[PraxisSampler],
weights: List[float],
tokenizer: PreTrainedTokenizer,
block_size: int,
batch_size: int,
oversample_chance: float = 0,
supersample_chance: float = 0,
):
self.data_manager = InterleaveDataManager(
datasets, weights, tokenizer, block_size
)
self.batch_size = batch_size
self.oversample_chance = oversample_chance
self.supersample_chance = supersample_chance
def __iter__(self):
while True:
oversample = random.random() < self.oversample_chance
supersample = random.random() < self.supersample_chance
batch = self.data_manager.get_batch(
self.batch_size, oversample, supersample
)
yield torch.stack(batch)
class PraxisDataModule(LightningDataModule):
def __init__(
self,
train_datasets: List[Dict],
val_datasets: List[Dict],
tokenizer: PreTrainedTokenizer,
batch_size: int = 16,
block_size: int = 512,
oversample_chance: float = 0,
supersample_chance: float = 0,
):
super().__init__()
self.train_datasets = self.create_datasets(
train_datasets,
tokenizer,
block_size,
batch_size,
oversample_chance,
supersample_chance,
)
self.val_datasets = False
if len(val_datasets) > 0:
self.val_datasets = self.create_datasets(
val_datasets, tokenizer, block_size, batch_size, 0, 0
)
def create_datasets(
self,
datasets,
tokenizer,
block_size,
batch_size,
oversample_chance=0,
supersample_chance=0,
):
# Get weights and normalize them while preserving relative magnitudes
raw_weights = [dataset.weight for dataset in datasets]
weights = [w / sum(raw_weights) for w in raw_weights]
return WeightedIterableDataset(
datasets,
weights,
tokenizer,
block_size,
batch_size,
oversample_chance,
supersample_chance,
)
def train_dataloader(self):
return DataLoader(
dataset=self.train_datasets,
batch_size=None,
num_workers=1,
pin_memory=False,
)
def val_dataloader(self):
if self.val_datasets:
return DataLoader(
dataset=self.val_datasets,
batch_size=None,
num_workers=1,
pin_memory=False,
)
else:
return []
def state_dict(self) -> Dict[str, Any]:
sampler_states = []
for sampler in self.train_datasets.data_manager.samplers:
if hasattr(sampler, "state_dict") and callable(sampler.state_dict):
sampler_states.append(sampler.state_dict())
else:
# If sampler doesn't support state, append None
sampler_states.append(None)
return {"sampler_states": sampler_states}
def load_state_dict(self, state_dict: Dict[str, Any]) -> None:
sampler_states = state_dict.get("sampler_states", [])
samplers = self.train_datasets.data_manager.samplers
for sampler, s_state in zip(samplers, sampler_states):
if (
s_state is not None
and hasattr(sampler, "load_state_dict")
and callable(sampler.load_state_dict)
):
sampler.load_state_dict(s_state)