Skip to content

KTO使用自定义数据集报错 #4062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
waywooKwong opened this issue May 1, 2025 · 1 comment
Closed

KTO使用自定义数据集报错 #4062

waywooKwong opened this issue May 1, 2025 · 1 comment

Comments

@waywooKwong
Copy link

waywooKwong commented May 1, 2025

问题已解决:自己构造的数据集 label 使用成了 str 的 "False"/"True",导致错误

Describe the bug

  1. 使用文档中的KTO示例代码可以跑通
  2. 仅更换数据集为本地路径构造的json,
    出现报错:RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

Image

我的脚本代码:

nproc_per_node=1

CUDA_VISIBLE_DEVICES=6 \
NPROC_PER_NODE=$nproc_per_node \
MASTER_PORT=29501 \
swift rlhf \
    --rlhf_type kto \
    --model Qwen/Qwen2.5-0.5B-Instruct \
    --train_type lora \
    --dataset '/my_path/swift_kto_dataset_v3.json' \
    --num_train_epochs 1 \
    --per_device_train_batch_size 1 \
    --per_device_eval_batch_size 1 \
    --learning_rate 1e-4 \
    --lora_rank 4 \
    --lora_alpha 16 \
    --target_modules all-linear \
    --gradient_accumulation_steps $(expr 16 / $nproc_per_node) \
    --eval_steps 100 \
    --save_steps 100 \
    --save_total_limit 2 \
    --logging_steps 5 \
    --max_length 2048 \
    --output_dir output \
    --warmup_ratio 0.05 \
    --dataloader_num_workers 4 \
    --deepspeed zero2 \
    --dataset_num_proc 4 \
    --torch_dtype float16 

我的json数据格式:
其中label=0,label=1的数量相等,考虑有关数据集的隐患是:content内容重复度较高

Image

Your hardware and system info

python 3.10
cuda 12.4
datasets 3.0.0
deepspeed 0.16.7
ms-swift 3.4.0
trl 0.17.0

@waywooKwong
Copy link
Author

waywooKwong commented May 1, 2025

我在huggingface trl的Issues中看到类似问题的解决方案:

  1. Can you call model.enable_input_require_grads() before get_peft_model ? You have to use the main branch of transformers though (pip install --upgrade git+https://github.com/huggingface/transformers)

  2. Hello, is_trainable=True is required to load the pretrained adapter and have them in trainable mode.
    Please pass PeftModel.from_pretrained(model, peft_model_id, is_trainable=True).to(device) and let us know if that solves the issue

现在我直接使用脚本运行,怎么进行上述提到的特定函数的参数指定呢?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant