forked from AkariAsai/OpenScholar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_alpaca.py
73 lines (61 loc) · 3.22 KB
/
_alpaca.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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
from functools import partial
from torchtune.datasets._instruct import instruct_dataset, InstructDataset
from torchtune.modules.tokenizers import ModelTokenizer
def alpaca_dataset(
tokenizer: ModelTokenizer,
*,
source: str = "tatsu-lab/alpaca",
train_on_input: bool = True,
max_seq_len: int = 512,
packed: bool = False,
split: str = "train",
) -> InstructDataset:
"""
Support for family of Alpaca-style datasets from Hugging Face Datasets using
the `data input format <https://huggingface.co/datasets/tatsu-lab/alpaca#data-instances>`_
and `prompt template <https://github.com/tatsu-lab/stanford_alpaca/blob/main/train.py#L31>`_
from the original alpaca codebase, where ``instruction``, ``input``, and ``output``
are fields from the dataset.
Masking of the prompt during training is controlled by the ``train_on_input`` flag, which is
set to ``True`` by `default <https://github.com/tloen/alpaca-lora/blob/main/finetune.py#L49>`_
- If ``train_on_input`` is True, the prompt is used during training and
contributes to the loss.
- If ``train_on_input`` is False, the prompt is masked out (tokens replaced with -100)
Args:
tokenizer (ModelTokenizer): Tokenizer used by the model that implements the ``tokenize_messages`` method.
source (str): path string of dataset, anything supported by Hugging Face's ``load_dataset``.
train_on_input (bool): Whether the model is trained on the prompt or not. Default is True.
max_seq_len (int): Maximum number of tokens in the returned input and label token id lists.
Default is 512, but we recommend setting this to the highest you can fit in memory and
is supported by the model. For example, llama2-7B supports up to 4096 for sequence length.
packed (bool): Whether or not to pack the dataset to ``max_seq_len`` prior to training. Default is False.
split (str): ``split`` argument for ``datasets.load_dataset``. You can use this argument to load a subset
of a given split, e.g. ``split="train[:10%]"``. Default is "train".
Returns:
InstructDataset: dataset configured with source data and template
Example:
>>> alpaca_ds = alpaca_dataset(tokenizer=tokenizer)
>>> for batch in Dataloader(alpaca_ds, batch_size=8):
>>> print(f"Batch size: {len(batch)}")
>>> Batch size: 8
"""
return instruct_dataset(
tokenizer=tokenizer,
source=source,
template="torchtune.data.AlpacaInstructTemplate",
train_on_input=train_on_input,
max_seq_len=max_seq_len,
packed=packed,
split=split,
)
alpaca_cleaned_dataset = partial(alpaca_dataset, source="yahma/alpaca-cleaned")
alpaca_cleaned_dataset.__doc__ = """
Builder for a variant of Alpaca-style datasets with the cleaned version of the
original Alpaca dataset, `yahma/alpaca-cleaned <https://huggingface.co/datasets/yahma/alpaca-cleaned>`_.
See the dataset page and :func:`~torchtune.datasets.alpaca_dataset` for more details.
"""