length_cdf.py 2.37 KB
Newer Older
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1
# coding=utf-8
chenych's avatar
chenych committed
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright 2024 the LlamaFactory team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
15
16
17
18
19
20

from collections import defaultdict

import fire
from tqdm import tqdm

luopl's avatar
luopl committed
21
from llamafactory.data import get_dataset, get_template_and_fix_tokenizer
chenych's avatar
chenych committed
22
23
from llamafactory.hparams import get_train_args
from llamafactory.model import load_tokenizer
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
24
25
26
27


def length_cdf(
    model_name_or_path: str,
luopl's avatar
luopl committed
28
    dataset: str = "alpaca_en_demo",
chenych's avatar
chenych committed
29
30
31
    dataset_dir: str = "data",
    template: str = "default",
    interval: int = 1000,
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
32
):
chenych's avatar
chenych committed
33
34
    r"""
    Calculates the distribution of the input lengths in the dataset.
luopl's avatar
luopl committed
35
    Usage: python length_cdf.py --model_name_or_path path_to_model --dataset alpaca_en_demo --template default
chenych's avatar
chenych committed
36
    """
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
37
38
39
40
41
42
43
44
45
46
    model_args, data_args, training_args, _, _ = get_train_args(
        dict(
            stage="sft",
            model_name_or_path=model_name_or_path,
            dataset=dataset,
            dataset_dir=dataset_dir,
            template=template,
            cutoff_len=1_000_000,
            output_dir="dummy_dir",
            overwrite_cache=True,
chenych's avatar
chenych committed
47
            do_train=True,
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
48
49
        )
    )
chenych's avatar
chenych committed
50
    tokenizer_module = load_tokenizer(model_args)
luopl's avatar
luopl committed
51
52
    template = get_template_and_fix_tokenizer(tokenizer_module["tokenizer"], data_args)
    trainset = get_dataset(template, model_args, data_args, training_args, "sft", **tokenizer_module)["train_dataset"]
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    total_num = len(trainset)
    length_dict = defaultdict(int)
    for sample in tqdm(trainset["input_ids"]):
        length_dict[len(sample) // interval * interval] += 1

    length_tuples = list(length_dict.items())
    length_tuples.sort()
    count_accu, prob_accu = 0, 0
    for length, count in length_tuples:
        count_accu += count
        prob_accu += count / total_num * 100
        print("{:d} ({:.2f}%) samples have length < {}.".format(count_accu, prob_accu, length + interval))


if __name__ == "__main__":
    fire.Fire(length_cdf)