LOTD - Lord Of The Datasets
Full API overview.
lotd
LOTD - Lord of the Datasets Efficient NLP dataset preprocessing library.
ChatTokenizer
Applies chat template and tokenizes messages in a dataset.
__call__
__call__(prompts: Union[List[str], List[List[str]]], responses: Union[List[str], List[List[str]]] = [], system: Union[List[str], None] = None) -> Dict[str, List[List[int]]]
Tokenizes chats.
| Parameters: |
|
|---|
| Returns: |
|
|---|
__init__
__init__(tokenizer: Union[PreTrainedTokenizer, PreTrainedTokenizerFast], max_length: Union[int, None] = None) -> None
Initializes the dataset chat tokenizer.
| Parameters: |
|
|---|
LengthFilter
Filters datasets by max and min length of input_ids.
__call__
__call__(input_ids: List[List[int]]) -> List[bool]
Processes a batch of input_ids.
__init__
__init__(min_length: int = 0, max_length: Union[int, None] = None) -> None
Initializes the collator.
| Parameters: |
|
|---|
PadCollator
Collator which converts inputs into pytorch tensors and applies padding.
__call__
__call__(batch: List[Dict[str, Any]]) -> Dict[str, torch.Tensor]
Collates inputs.
| Parameters: |
|
|---|
| Returns: |
|
|---|
__init__
__init__(pad_id: int, pre: Union[Callable, None] = None, post: Union[Callable, None] = None, padding_side: Literal['right', 'left'] = 'right', ignore_columns: List[str] = []) -> None
Initializes the collator.
| Parameters: |
|
|---|
TextTokenizer
Applies template and tokenizes texts in a dataset.
__call__
__call__(texts: List[str]) -> Dict[str, List[List[int]]]
Tokenizes a batch of text samples.
| Parameters: |
|
|---|
| Returns: |
|
|---|
__init__
__init__(tokenizer: Union[PreTrainedTokenizer, PreTrainedTokenizerFast], template: str = '[CLS]{{text}}[SEP]', max_length: Union[int, None] = None) -> None
Initializes the dataset tokenizer.
| Parameters: |
|
|---|
format_chat
format_chat(prompts: Union[str, List[str]] = [], responses: Union[str, List[str]] = [], system: Union[str, None] = None) -> List[Dict[str, str]]
Format list of prompts, responses and system messages into a chat.
| Parameters: |
|
|---|
| Returns: |
|
|---|
generate_chat_template
generate_chat_template(last_only: bool = False, wrapper: str = '{{template}}', system_tags: Tuple[str, str] = ('<|system|>', '<|end|>'), user_tags: Tuple[str, str] = ('<|user|>', '<|end|>'), assistant_tags: Tuple[str, str] = ('<|assistant|>', '<|end|>')) -> str
Generate chat template for instruction tuning.
| Parameters: |
|
|---|
| Returns: |
|
|---|
get_loaders
get_loaders(dataset: Dataset, collate_fn: Callable = lambda x: x, batch_size: int = 16, train_size: float = 0.8, val_size: float = 0.1, num_workers: int = 15, seed: int = 42) -> Tuple[DataLoader, DataLoader, DataLoader]
Shortcut to generate pytorch dataloaders (train/val/test) from hf dataset.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Splits dataset and assigns collators automatically.
load_cached
load_cached(cache_path: str, process_fn: Callable) -> Dataset
Try loading processed dataset from cache. Processes dataset and saves it to cache if pre-cached dataset is not found.
| Parameters: |
|
|---|
| Returns: |
|
|---|
split_dataset
split_dataset(dataset: Dataset, train_size: float = 0.8, val_size: float = 0.1, seed: int = 42) -> Tuple[Dataset, Dataset, Dataset]
Split HF dataset into train, validation and test.
| Parameters: |
|
|---|
| Returns: |
|
|---|
train_size and val_size are taken from total and their sum should not be more than 1.
Test size would be equal to 1 - train_size - val_size.
strip_features
strip_features(dataset: Dataset, keep_features: List[str] = ['input_ids', 'prompt_mask']) -> Dataset
Remove all features from dataset except specified ones.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Useful for reducing memory usage and cleaning up datasets.
Datasets
lotd.datasets
alpaca
alpaca(tokenizer: Union[PreTrainedTokenizer, PreTrainedTokenizerFast], cache_path: Union[str, None] = None, max_length: int = 512, batch_size: int = 16, seed: int = 42, pre: Union[Callable, None] = None, post: Union[Callable, None] = None) -> Tuple[DataLoader, DataLoader, DataLoader]
Downloads and Pre-processes Alpaca dataset for instruction fine-tuning.
| Parameters: |
|
|---|
| Returns: |
|
|---|
build_dataset
build_dataset(preprocess_func)
Decorator for building dataloaders. Processes or tries loading the dataset if cache_path is specified, creates new PadCollator, and returns dataloaders.
tinystories
tinystories(tokenizer: Union[PreTrainedTokenizer, PreTrainedTokenizerFast], template: str = '[CLS]{{tex}}[SEP]', cache_path: Union[str, None] = None, max_length: int = 512, batch_size: int = 16, seed: int = 42, pre: Union[Callable, None] = None, post: Union[Callable, None] = None) -> Tuple[DataLoader, DataLoader, DataLoader]
Downloads and Pre-processes TinyStories dataset for language modeling. train and validation are merged, shuffled and re-split with 80%/10%/10% ratio.
| Parameters: |
|
|---|
| Returns: |
|
|---|