https://github.com/amazon-science/contextualunderstanding-contrastivedecoding
Enhancing contextual understanding in large language models through contrastive decoding
https://github.com/amazon-science/contextualunderstanding-contrastivedecoding
Science Score: 13.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
○.zenodo.json file
-
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (14.0%) to scientific vocabulary
Repository
Enhancing contextual understanding in large language models through contrastive decoding
Basic Info
Statistics
- Stars: 17
- Watchers: 3
- Forks: 1
- Open Issues: 1
- Releases: 0
Metadata Files
README.md
Enhancing contextual understanding in large language models through contrastive decoding
Large language models (LLMs) tend to inadequately integrate input context during text generation, relying excessively on encoded prior knowledge in model parameters, potentially resulting in generated text with factual inconsistencies or contextually unfaithful content. LLMs utilize two primary knowledge sources: 1) prior (parametric) knowledge from pretraining, and 2) contextual (non-parametric) knowledge from input prompts. The study addresses the open question of how LLMs effectively balance these knowledge sources during the generation process, specifically in the context of open-domain question answering. To address this issue, we introduce a novel approach integrating contrastive decoding with adversarial irrelevant passages as negative samples to enhance robust context grounding during generation. Notably, our method operates at inference time without requiring further training. We conduct comprehensive experiments to demonstrate its applicability and effectiveness, providing empirical evidence showcasing its superiority over existing methodologies.
Development
First, to create an environment, run the following command using conda:
conda env create -f environment.yml
You will also need to make an editable install of Huggingface's transformers library since we will need to change the decoding function.
Once you have installed the library, you can simply need to swap the file src/contrastive_decoding/lib/transformers/utils.py in your local copy of the transformers' repository. The path of utils.py in the repository should be src/transformers/generation/
Then, you can start running experiments:
./scripts/run_nq.sh
Or alternatively, you can use the following code snippet:
CUDA_VISIBLE_DEVICES=0 python src/contrastive_decoding/run_qa_prompt.py \
--model_name /home/ec2-user/data/Llama-7b-hf \
--input_file ./data/nq_test.tsv \
--eval_method CD \
--n_examples 5 \
--ret_path ./data/retrieval/nq_contriever_results.jsonl \
--bf16 \
--alpha 0.5 \
--alias 'nq-alpha-0.5'
Here are a brief explanation of the args that can be used:
- --model_name: this should be the local path or huggingface's model name for the model that you would like to use
- --input_file: this should be the file of the QA benchmark in .tsv format
- --eval_method: this can be BM25, contriever, CD, CAD. CD is for our contrastive decoding, and CAD stands for context aware decoding, a baseline that we compare with.
- --n_examples: the number of examples (shots) for the prompt
- --ret_path: this is the path to the retrieval file
- --alpha: you should only set this value if you use either CD or CAD
- --use_gold_ctx: set this arg if you would like to use gold context instead of retrieved context
- --use_fixed_irr: set this arg if you would like to use one the proivided fixed irrelevant passage
- --use_random_irr: set this arg if you would like to randomly select one passage and use it as the irrelevant passage. If both fixed or random are not used, then the default behaviour is to use the most distant passage from the relevent passage as the irrelevant passage.
- --alias: use this arg to set the experiment name that will be used in the saved results (in csv format)
Security
See CONTRIBUTING for more information.
License
This library is licensed under the CC-BY-NC-4.0 License.
@Inproceedings{Zhao2024,
author = {Zheng Zhao and Emilio Monti and Jens Lehmann and Haytham Assem},
title = {Enhancing contextual understanding in large language models through contrastive decoding},
year = {2024},
url = {https://www.amazon.science/publications/enhancing-contextual-understanding-in-large-language-models-through-contrastive-decoding},
booktitle = {NAACL 2024},
}
Owner
- Name: Amazon Science
- Login: amazon-science
- Kind: organization
- Website: https://amazon.science
- Twitter: AmazonScience
- Repositories: 80
- Profile: https://github.com/amazon-science
GitHub Events
Total
- Issues event: 1
- Watch event: 4
- Fork event: 2
Last Year
- Issues event: 1
- Watch event: 4
- Fork event: 2
Issues and Pull Requests
Last synced: over 1 year ago
All Time
- Total issues: 1
- Total pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Total issue authors: 1
- Total pull request authors: 0
- Average comments per issue: 0.0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 1
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 1
- Pull request authors: 0
- Average comments per issue: 0.0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- fan2goa1 (1)
- lgduu (1)
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels
Dependencies
- absl-py ==2.0.0
- accelerate ==0.23.0
- aiohttp ==3.8.5
- aiosignal ==1.3.1
- aniso8601 ==9.0.1
- ansi2html ==1.8.0
- async-timeout ==4.0.3
- blinker ==1.6.2
- click ==8.1.7
- cloudpickle ==2.2.1
- coloredlogs ==15.0.1
- contextlib2 ==21.6.0
- datasets ==2.14.5
- dill ==0.3.7
- dparse ==0.6.3
- einops ==0.7.0rc2
- flash-attn ==2.3.0
- flask ==2.3.3
- flask-restful ==0.3.10
- frozenlist ==1.4.0
- fsspec ==2023.6.0
- google-pasta ==0.2.0
- gym ==0.26.2
- gym-notices ==0.0.8
- horovod ==0.28.1
- huggingface-hub ==0.16.4
- humanfriendly ==10.0
- itsdangerous ==2.1.2
- joblib ==1.3.2
- jsonlines ==4.0.0
- llvmlite ==0.40.1
- metrics ==0.3.3
- multidict ==6.0.4
- multiprocess ==0.70.15
- ninja ==1.11.1
- nltk ==3.8.1
- numba ==0.57.1
- numpy ==1.24.4
- nvgpu ==0.10.0
- openai ==0.28.0
- optimum ==1.13.2
- packaging ==21.3
- pathlib2 ==2.3.7.post1
- pathos ==0.3.1
- pathspec ==0.5.5
- pillow ==10.0.0
- pox ==0.3.3
- ppft ==1.7.6.7
- protobuf ==4.24.2
- pyarrow ==13.0.0
- pyfunctional ==1.4.3
- pygame ==2.5.1
- pygments ==2.2.0
- pynvml ==11.5.0
- regex ==2023.8.8
- rouge-score ==0.1.2
- ruamel-yaml ==0.17.32
- ruamel-yaml-clib ==0.2.7
- s3fs ==0.4.2
- safetensors ==0.3.3
- sagemaker ==2.180.0
- schema ==0.7.5
- scikit-learn ==1.3.0
- sentencepiece ==0.1.99
- shap ==0.42.1
- slicer ==0.0.7
- smclarify ==0.5
- smdebug-rulesconfig ==1.0.1
- tabulate ==0.9.0
- tblib ==1.7.0
- termcolor ==2.3.0
- threadpoolctl ==3.2.0
- tokenizers ==0.14.0
- transformers ==4.34.0.dev0
- werkzeug ==2.3.7
- xxhash ==3.3.0
- yarl ==1.9.2