kgreenewald commited on
Commit
9222de2
·
verified ·
1 Parent(s): 72333e4

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +188 -3
README.md CHANGED
@@ -1,3 +1,188 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ language:
4
+ - en
5
+ pipeline_tag: text-generation
6
+ library_name: transformers
7
+ ---
8
+
9
+ # Granite 3.2 8B Instruct - Uncertainty LoRA
10
+
11
+ Welcome to Granite Experiments!
12
+
13
+ Think of Experiments as a preview of what's to come. These projects are still under development, but we wanted to let the open-source community take them for spin! Use them, break them, and help us build what's next for Granite – we'll keep an eye out for feedback and questions in the [Community section](https://huggingface.co/ibm-granite/granite-uncertainty-3.0-8b-lora/discussions). Happy exploring!
14
+
15
+
16
+ ## Model Summary
17
+
18
+ **Granite 3.2 8b Instruct - Uncertainty** is a LoRA adapter for [ibm-granite/granite-3.2-8b-instruct](https://huggingface.co/ibm-granite/granite-3.2-8b-instruct),
19
+ adding the capability to provide calibrated certainty scores when answering questions when prompted, in addition to retaining the full abilities of the [ibm-granite/granite-3.2-8b-instruct](https://huggingface.co/ibm-granite/granite-3.2-8b-instruct) model.
20
+
21
+ - **Developer:** IBM Research
22
+ - **Model type:** LoRA adapter for [ibm-granite/granite-3.2-8b-instruct](https://huggingface.co/ibm-granite/granite-3.2-8b-instruct)
23
+ - **License:** [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)
24
+
25
+
26
+ ### Model Sources
27
+
28
+ <!-- Provide the basic links for the model. -->
29
+
30
+
31
+ - **Paper:** The **Granite Uncertainty 3.2 8b** model is finetuned to provide certainty scores mimicking the output of a calibrator trained via the method in [[Shen et al. ICML 2024] Thermometer: Towards Universal Calibration for Large Language Models](https://arxiv.org/abs/2403.08819)
32
+
33
+
34
+ ## Usage
35
+
36
+ <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
37
+
38
+ ### Intended use
39
+
40
+ This is an experimental LoRA testing new functionality being developed for IBM's Granite LLM family. We are welcoming the community to test it out and give us feedback, but we are NOT recommending this model be used for real deployments at this time. Stay tuned for more updates on the Granite roadmap.
41
+
42
+
43
+ **Certainty score definition** The model will respond with a certainty percentage, quantized to 10 possible values (i.e. 5%, 15%, 25%,...95%).
44
+ This percentage is *calibrated* in the following sense: given a set of answers assigned a certainty score of X%, approximately X% of these answers should be correct. See the eval experiment below for out-of-distribution verification of this behavior.
45
+
46
+ **Certainty score interpretation** Certainty scores calibrated as defined above may at times seem biased towards moderate certainty scores for the following reasons. Firstly, as humans we tend to be overconfident in
47
+ our evaluation of what we know and don't know - in contrast, a calibrated model is less likely to output very high or very low confidence scores, as these imply certainty of correctness or incorrectness.
48
+ Examples where you might see very low confidence scores might be on answers where the model's response was something to the effect of "I don't know", which is easy to evaluate as not
49
+ being the correct answer to the question (though it is the appropriate one). Secondly, remember that the model
50
+ is evaluating itself - correctness/incorrectness that may be obvious to us or to larger models may be less obvious to an 8b model. Finally, teaching a model every fact it knows
51
+ and doesn't know is not possible, hence it must generalize to questions of wildly varying difficulty (some of which may be trick questions!) and to settings where it has not had its outputs judged.
52
+ Intuitively, it does this by extrapolating based on related questions
53
+ it has been evaluated on in training - this is an inherently inexact process and leads to some hedging.
54
+
55
+ **Possible downstream use cases (not implemented)**
56
+ * Human usage: Certainty scores give human users an indication of when to trust answers from the model (which should be augmented by their own knowledge).
57
+ * Model routing/guards: If the model has low certainty (below a chosen threshold), it may be worth sending the request to a larger, more capable model or simply choosing not to show the response to the user.
58
+ * RAG: **Granite Uncertainty 3.2 8b** is calibrated on document-based question answering datasets, hence it can be applied to giving certainty scores for answers created using RAG. This certainty will be a prediction of overall correctness based on both the documents given and the model's own knowledge (e.g. if the model is correct but the answer is not in the documents, the certainty can still be high).
59
+
60
+ **Important note** Certainty is inherently an intrinsic property of a model and its abilitities. **Granite Uncertainty 3.2 8b** is not intended to predict the certainty of responses generated by any other models besides itself or [ibm-granite/granite-3.2-8b-instruct](https://huggingface.co/ibm-granite/granite-3.2-8b-instruct).
61
+ Additionally, certainty scores are *distributional* quantities, and so will do well on realistic questions in aggregate, but in principle may have surprising scores on individual
62
+ red-teamed examples.
63
+
64
+ **Usage steps** There are two supported usage scenarios.
65
+
66
+ Scenario 1. Answering a question and obtaining a certainty score proceeds as follows. Given a user query written in the `user` role:
67
+
68
+ 1. Use the base model to generate a response as normal (via the `assistant` role).
69
+ 2. Prompt the model to generate a certainty score by generating in the `certainty` role (use "certainty" as the role in the chat template, or simply append `<|start_of_role|>certainty<|end_of_role|>` and continue generating), see examples below.
70
+ 3. The model will respond with a certainty percentage, quantized with steps of 10% (i.e. 05%, 15%, 25%,...95%). Note, any additional text after the score and % can be ignored. You can curb additional generation by setting "max token length" = 3 when using this role.
71
+
72
+ Scenario 2. Predicting the certainty score from the question only, *prior* to generating an answer. Given a user query written in the `user` role:
73
+
74
+ 1. Prompt the model to generate a certainty score by generating in the `certainty` role (use "certainty" as the role in the chat template, or simply append `<|start_of_role|>certainty<|end_of_role|>` and continue generating), see examples below.
75
+ 2. The model will respond with a certainty percentage, quantized with steps of 10% (i.e. 05%, 15%, 25%,...95%). Note, any additional text after the score and % can be ignored. You can curb additional generation by setting "max token length" = 3 when using this role.
76
+ 3. Remove the generated certainty string, and if desired, use the base model to generate a response as normal (via the `assistant` role).
77
+
78
+ ### Quickstart Example
79
+
80
+ The following code describes how to use the Granite Uncertainty model to answer questions and obtain intrinsic calibrated certainty scores. Note that a generic system prompt is included, this is not necessary and can be modified as needed.
81
+
82
+ ```python
83
+ import torch,os
84
+ from transformers import AutoTokenizer, AutoModelForCausalLM
85
+ from peft import PeftModel, PeftConfig
86
+
87
+ token = os.getenv("HF_MISTRAL_TOKEN")
88
+ BASE_NAME = "ibm-granite/granite-3.2-8b-instruct"
89
+ LORA_NAME = "ibm-granite/granite-uncertainty-3.2-8b-lora"
90
+ device=torch.device('cuda' if torch.cuda.is_available() else 'cpu')
91
+
92
+ # Load model
93
+ tokenizer = AutoTokenizer.from_pretrained(BASE_NAME,padding_side='left',trust_remote_code=True, token=token)
94
+ model_base = AutoModelForCausalLM.from_pretrained(BASE_NAME,device_map="auto")
95
+ model_UQ = PeftModel.from_pretrained(model_base, LORA_NAME)
96
+
97
+
98
+ question = "What is IBM?"
99
+ print("Question:" + question)
100
+ question_chat = [
101
+ {
102
+ "role": "system",
103
+ "content": ""
104
+ },
105
+ {
106
+ "role": "user",
107
+ "content": question
108
+ },
109
+ ]
110
+
111
+ # Generate answer
112
+ input_text = tokenizer.apply_chat_template(question_chat,tokenize=False,add_generation_prompt=True)
113
+ # remove automatic system prompt
114
+ string_to_remove = tokenizer.apply_chat_template(question_chat[0:1], tokenize=False,add_generation_prompt=False)
115
+ input_text = input_text[len(string_to_remove):]
116
+ #tokenize
117
+ inputs = tokenizer(input_text, return_tensors="pt")
118
+ output = model_UQ.generate(inputs["input_ids"].to(device), attention_mask=inputs["attention_mask"].to(device), max_new_tokens=80)
119
+ output_text = tokenizer.decode(output[0])
120
+ answer = output_text.split("assistant<|end_of_role|>")[1]
121
+ print("Answer: " + answer)
122
+
123
+ # Generate certainty score
124
+ uq_generation_prompt = "<|start_of_role|>certainty<|end_of_role|>"
125
+ uq_chat = question_chat + [
126
+ {
127
+ "role": "assistant",
128
+ "content": answer
129
+ },
130
+ ]
131
+
132
+ uq_text = tokenizer.apply_chat_template(uq_chat,tokenize=False) + uq_generation_prompt
133
+ # remove automatic system prompt
134
+ uq_text = uq_text[len(string_to_remove):]
135
+ inputs = tokenizer(uq_text, return_tensors="pt")
136
+ output = model_UQ.generate(inputs["input_ids"].to(device), attention_mask=inputs["attention_mask"].to(device), max_new_tokens=1)
137
+ output_text = tokenizer.decode(output[0])
138
+ uq_score = int(output_text[-1])
139
+ print("Certainty: " + str(5 + uq_score * 10) + "%")
140
+ ```
141
+
142
+
143
+ ## Evaluation
144
+
145
+ The model was evaluated on the [MMLU](https://huggingface.co/datasets/cais/mmlu) datasets (not used in training). Shown are the [Expected Calibration Error (ECE)](https://towardsdatascience.com/expected-calibration-error-ece-a-step-by-step-visual-explanation-with-python-code-c3e9aa12937d) for each task, for the base model (Granite-3.2-8b-instruct) and Granite-Uncertainty-3.2-8b.
146
+ The average ECE across tasks for our method is 0.064 (out of 1) and is consistently low across tasks (maximum task ECE 0.10), compared to the base model average ECE of 0.20 and maximum task ECE of 0.60. Note that our ECE of 0.064 is smaller than the gap between the quantized certainty outputs (10% quantization steps). Additionally, the zero-shot performance on the MMLU tasks does not degrade, averaging at 89%.
147
+ <!-- This section describes the evaluation protocols and provides the results. -->
148
+
149
+
150
+
151
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/6602ffd971410cf02bf42c06/2MwP7DRZlNBtWSKWFvXOI.png)
152
+
153
+
154
+
155
+ ## Training Details
156
+ The **Granite Uncertainty 3.2 8b** model is a LoRA adapter finetuned to provide certainty scores mimicking the output of a calibrator trained via the method in [[Shen et al. ICML 2024] Thermometer: Towards Universal Calibration for Large Language Models](https://arxiv.org/abs/2403.08819).
157
+
158
+
159
+
160
+ ### Training Data
161
+ The following datasets were used for calibration and/or finetuning.
162
+
163
+ * [BigBench](https://huggingface.co/datasets/tasksource/bigbench)
164
+ * [MRQA](https://huggingface.co/datasets/mrqa-workshop/mrqa)
165
+ * [newsqa](https://huggingface.co/datasets/lucadiliello/newsqa)
166
+ * [trivia_qa](https://huggingface.co/datasets/mandarjoshi/trivia_qa)
167
+ * [search_qa](https://huggingface.co/datasets/lucadiliello/searchqa)
168
+ * [openbookqa](https://huggingface.co/datasets/allenai/openbookqa)
169
+ * [web_questions](https://huggingface.co/datasets/Stanford/web_questions)
170
+ * [smiles-qa](https://huggingface.co/datasets/alxfgh/ChEMBL_Drug_Instruction_Tuning)
171
+ * [orca-math](https://huggingface.co/datasets/microsoft/orca-math-word-problems-200k)
172
+ * [ARC-Easy](https://huggingface.co/datasets/allenai/ai2_arc)
173
+ * [commonsense_qa](https://huggingface.co/datasets/tau/commonsense_qa)
174
+ * [social_i_qa](https://huggingface.co/datasets/allenai/social_i_qa)
175
+ * [super_glue](https://huggingface.co/datasets/aps/super_glue)
176
+ * [figqa](https://huggingface.co/datasets/nightingal3/fig-qa)
177
+ * [riddle_sense](https://huggingface.co/datasets/INK-USC/riddle_sense)
178
+ * [ag_news](https://huggingface.co/datasets/fancyzhx/ag_news)
179
+ * [medmcqa](https://huggingface.co/datasets/openlifescienceai/medmcqa)
180
+ * [dream](https://huggingface.co/datasets/dataset-org/dream)
181
+ * [codah](https://huggingface.co/datasets/jaredfern/codah)
182
+ * [piqa](https://huggingface.co/datasets/ybisk/piqa)
183
+
184
+
185
+
186
+ ## Model Card Authors
187
+
188
+ Kristjan Greenewald