LLaMA-Factory-Mirror/scripts/length_cdf.py

68 lines
2.2 KiB
Python
Raw Permalink Normal View History

2024-02-19 02:09:13 +08:00
# coding=utf-8
2024-06-15 17:54:33 +08:00
# 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.
2024-02-19 02:09:13 +08:00
from collections import defaultdict
import fire
from tqdm import tqdm
2024-05-16 18:39:08 +08:00
from llamafactory.data import get_dataset
from llamafactory.hparams import get_train_args
from llamafactory.model import load_tokenizer
2024-02-19 02:09:13 +08:00
def length_cdf(
model_name_or_path: str,
2024-05-04 22:02:25 +08:00
dataset: str = "alpaca_en",
dataset_dir: str = "data",
template: str = "default",
interval: int = 1000,
2024-02-19 02:09:13 +08:00
):
2024-06-15 17:54:33 +08:00
r"""
Calculates the distribution of the input lengths in the dataset.
Usage: python length_cdf.py --model_name_or_path path_to_model --dataset alpaca_en --template default
"""
2024-04-03 18:14:24 +08:00
model_args, data_args, training_args, _, _ = get_train_args(
2024-02-19 02:09:13 +08:00
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,
2024-07-15 01:04:56 +08:00
do_train=True,
2024-02-19 02:09:13 +08:00
)
)
2024-04-26 05:44:30 +08:00
tokenizer_module = load_tokenizer(model_args)
2024-07-15 01:04:56 +08:00
trainset = get_dataset(model_args, data_args, training_args, stage="sft", **tokenizer_module)["train_dataset"]
total_num = len(trainset)
2024-02-19 02:09:13 +08:00
length_dict = defaultdict(int)
2024-07-15 01:04:56 +08:00
for sample in tqdm(trainset["input_ids"]):
2024-02-19 02:09:13 +08:00
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)