Giter Site home page Giter Site logo

zjukg / knowpat Goto Github PK

View Code? Open in Web Editor NEW
154.0 4.0 13.0 9.24 MB

[Paper][ACL 2024 Findings] Knowledgeable Preference Alignment for LLMs in Domain-specific Question Answering

Home Page: https://arxiv.org/abs/2311.06503

Python 92.55% Shell 7.45%
knowledge-graph large-language-models question-answering domain-specific-qa preference-alignment instruction-tuning

knowpat's Introduction

Knowledgeable Preference Alignment for LLMs in Domain-specific Question Answering

license Preprint Pytorch

For domain-specific application of large language models (LLMs), external knowledge and LLMs should work together to achieve best user experience. LLMs should acquire an ability to make the right choices about retrieved external knowledge to meet the human needs. Knowledgeable Preference AlignmenT (KnowPAT) is a new pipeline to align LLMs with human's knowledge preference. KnowPAT incorporates domain knowledge graphs to construct preference set and design new alignment objective to fine-tune the LLMs.

🌈 Model Architecture

Model_architecture

💻 Data preparation

For reasons of commercial confidentiality, the dataset in our paper will not be made public. However, we provide examples of the data in data/ so that you can construct your own preference dataset and try out our alignment method according to the given format.

For each data instance, you should prepare a question and several answers, and the human preference score of each answer. A higher score means a better and preferred answer. Note that the answers should be sorted in the score descending order, which means the better answer and its score should be in the front.

  • News: 🎉🎉🥰🎉🎉 Our paper has been accepted by Findings of ACL 2024.
  • News: We now add a new Chinese QA datasets RJUA and it is open-sourced. You can try to conduct experiments on this dataset.

🔬 Dependencies

Our code is developed based on RRHF. Please build the Python environment following the instruction like RRHF.

To set up, you can use the following command lines to set up python3.8 and pytorch requirements:

conda create -n alignment python=3.8
pip install torch==1.13.0+cu116 torchvision==0.14.0+cu116 torchaudio==0.13.0 --extra-index-url https://download.pytorch.org/whl/cu116

Install other packages:

pip install -r requirements.txt

📕 Training & Test

  • run KnowPAT training on RJUA datasets
export MODEL_PATH="YOUR LLM PATH"
export SAVE_PATH="YOUR SAVE PATH"
export DATA_PATH="data/RJUA_train.json"
export WANDB_DISABLED=true
wandb offline

CUDA_VISIBLE_DEVICES=1 nohup python train.py \
    --model_name_or_path $MODEL_PATH \
    --data_path $DATA_PATH \
    --bf16 True \
    --output_dir $SAVE_PATH \
    --num_train_epochs 3 \
    --per_device_train_batch_size 1 \
    --per_device_eval_batch_size 1 \
    --gradient_accumulation_steps 8 \
    --evaluation_strategy "no" \
    --save_strategy "steps" \
    --save_steps 300 \
    --save_total_limit 40 \
    --learning_rate 1e-4 \
    --weight_decay 0. \
    --warmup_ratio 0.03 \
    --lr_scheduler_type "cosine" \
    --logging_steps 1 \
    --model_max_length 512 --rrhf_weight 0.01 > log_rjua.txt &

You may need to fill your model/save/data path before running. The model path should be a llama-architecture LLM.

  • run inference
CUDA_VISIBLE_DEVICES=0 python inference.py

🤝 Cite:

Please consider citing this paper if you use the code from our work. Thanks a lot :)


@article{DBLP:journals/corr/abs-2311-06503,
  author       = {Yichi Zhang and
                  Zhuo Chen and
                  Yin Fang and
                  Lei Cheng and
                  Yanxi Lu and
                  Fangming Li and
                  Wen Zhang and
                  Huajun Chen},
  title        = {Knowledgeable Preference Alignment for LLMs in Domain-specific Question
                  Answering},
  journal      = {CoRR},
  volume       = {abs/2311.06503},
  year         = {2023}
}

knowpat's People

Contributors

eltociear avatar wencolani avatar zhang-each avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

knowpat's Issues

huggingface_hub.utils._validators.HFValidationError: Repo id must use alphanumeric chars or '-', '_', '.', '--' and '..' are forbidden, '-' and '.' cannot start or end the name, max length is 96: ''.

您好,我想请问,我在运行inference.py文件时报了这个错误:huggingface_hub.utils.validators.HFValidationError: Repo id must use alphanumeric chars or '-', '', '.', '--' and '..' are forbidden, '-' and '.' cannot start or end the name, max length is 96: ''.
请问 checkpoint_name=YOUR SAVE PATH是一个什么样的路径,base_path是LLAMA的路径吗? 为什么这里lora_weights = "".format(checkpoint_name)格式化字符串前面是一个空串呀?
能否指点一下,谢谢啦!
1

score

May I ask how your answers are ranked and scored? Is it just 4, 3, 2, 1?

推理报错

image
我训练完的checkpoint_name如下
image
base_path结构如下
image

推理报错

训练后加载模型推理时报错,请问这是什么原因

Traceback (most recent call last):
File "/cpfs01/shared/KnowPAT/inference.py", line 68, in
model = LlamaForCausalLM.from_pretrained(
File "/opt/conda/envs/llama_factory/lib/python3.10/site-packages/transformers/modeling_utils.py", line 3787, in from_pretrained
model.load_adapter(
File "/opt/conda/envs/llama_factory/lib/python3.10/site-packages/transformers/integrations/peft.py", line 193, in load_adapter
adapter_state_dict = load_peft_weights(peft_model_id, token=token, **adapter_kwargs)
File "/opt/conda/envs/llama_factory/lib/python3.10/site-packages/peft/utils/save_and_load.py", line 244, in load_peft_weights
has_remote_safetensors_file = file_exists(
File "/opt/conda/envs/llama_factory/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 110, in _inner_fn
validate_repo_id(arg_value)
File "/opt/conda/envs/llama_factory/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 158, in validate_repo_id
raise HFValidationError(
huggingface_hub.utils._validators.HFValidationError: Repo id must be in the form 'repo_name' or 'namespace/repo_name': '/KnowPAT/checkpoint/nq_5k/checkpoint-1800/'. Use repo_type argument if needed.

加载的checkpoint如下:
image

torch.cuda.OutOfMemoryError: CUDA out of memory.

Traceback (most recent call last):
File "train.py", line 357, in
train()
File "train.py", line 353, in train
trainer.train()
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/transformers/trainer.py", line 1885, in train
return inner_training_loop(
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/transformers/trainer.py", line 2216, in _inner_training_loop
tr_loss_step = self.training_step(model, inputs)
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/transformers/trainer.py", line 3216, in training_step
loss = self.compute_loss(model, inputs)
File "train.py", line 288, in compute_loss
logits = model(
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1190, in _call_impl
return forward_call(*input, **kwargs)
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/accelerate/utils/operations.py", line 822, in forward
return model_forward(*args, **kwargs)
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/accelerate/utils/operations.py", line 810, in call
return convert_to_fp32(self.model_forward(*args, **kwargs))
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/torch/amp/autocast_mode.py", line 14, in decorate_autocast
return func(*args, **kwargs)
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/peft/peft_model.py", line 1395, in forward
return self.base_model(
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1190, in _call_impl
return forward_call(*input, **kwargs)
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/peft/tuners/tuners_utils.py", line 179, in forward
return self.model.forward(*args, **kwargs)
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/accelerate/hooks.py", line 166, in new_forward
output = module._old_forward(*args, **kwargs)
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/transformers/models/llama/modeling_llama.py", line 1205, in forward
outputs = self.model(
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1190, in _call_impl
return forward_call(*input, **kwargs)
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/transformers/models/llama/modeling_llama.py", line 1000, in forward
layer_outputs = decoder_layer(
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1190, in _call_impl
return forward_call(*input, **kwargs)
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/accelerate/hooks.py", line 166, in new_forward
output = module._old_forward(*args, **kwargs)
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/transformers/models/llama/modeling_llama.py", line 749, in forward
hidden_states, self_attn_weights, present_key_value = self.self_attn(
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1190, in _call_impl
return forward_call(*input, **kwargs)
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/accelerate/hooks.py", line 166, in new_forward
output = module._old_forward(*args, **kwargs)
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/transformers/models/llama/modeling_llama.py", line 351, in forward
query_states = self.q_proj(hidden_states)
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1190, in _call_impl
return forward_call(*input, **kwargs)
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/peft/tuners/lora/layer.py", line 540, in forward
result = result + lora_B(lora_A(dropout(x))) * scaling
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1190, in _call_impl
return forward_call(*input, **kwargs)
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/torch/nn/modules/dropout.py", line 59, in forward
return F.dropout(input, self.p, self.training, self.inplace)
File "/home/ymr/anaconda3/envs/alignment/lib/python3.8/site-packages/torch/nn/functional.py", line 1252, in dropout
return VF.dropout(input, p, training) if inplace else _VF.dropout(input, p, training)
torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 32.00 MiB (GPU 0; 23.65 GiB total capacity; 22.31 GiB already allocated; 28.56 MiB free; 22.37 GiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

hello,i have questions above when I use 2 4090.I'd like to ask for the device requirement.Looking forward to your reply.
您好,可以问一下以上问题的解决方案吗?可以提供一些帮助吗?

train.py需要多大内存?

我用的llama2-7b-hf,服务器用的48GB显存的A40,微调数据集只用了5条数据,为啥就爆显存了?lora方法微调llama2-7b不是不需要那么多显存吗?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.