kgreenewald commited on
Commit
ab79fae
·
verified ·
1 Parent(s): 93c8acb

Update certainty_lora/README.md

Browse files
Files changed (1) hide show
  1. certainty_lora/README.md +183 -165
certainty_lora/README.md CHANGED
@@ -1,202 +1,220 @@
1
  ---
2
- base_model: ibm-granite/granite-3.3-8b-instruct
3
- library_name: peft
 
 
 
4
  ---
5
 
6
- # Model Card for Model ID
7
 
8
- <!-- Provide a quick summary of what the model is/does. -->
9
 
 
10
 
11
 
12
- ## Model Details
13
 
14
- ### Model Description
 
15
 
16
- <!-- Provide a longer summary of what this model is. -->
 
 
17
 
 
 
18
 
 
19
 
20
- - **Developed by:** [More Information Needed]
21
- - **Funded by [optional]:** [More Information Needed]
22
- - **Shared by [optional]:** [More Information Needed]
23
- - **Model type:** [More Information Needed]
24
- - **Language(s) (NLP):** [More Information Needed]
25
- - **License:** [More Information Needed]
26
- - **Finetuned from model [optional]:** [More Information Needed]
27
 
28
- ### Model Sources [optional]
29
 
30
- <!-- Provide the basic links for the model. -->
31
-
32
- - **Repository:** [More Information Needed]
33
- - **Paper [optional]:** [More Information Needed]
34
- - **Demo [optional]:** [More Information Needed]
35
-
36
- ## Uses
37
-
38
- <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
39
-
40
- ### Direct Use
41
-
42
- <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
43
-
44
- [More Information Needed]
45
-
46
- ### Downstream Use [optional]
47
-
48
- <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
49
-
50
- [More Information Needed]
51
-
52
- ### Out-of-Scope Use
53
-
54
- <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
55
-
56
- [More Information Needed]
57
-
58
- ## Bias, Risks, and Limitations
59
-
60
- <!-- This section is meant to convey both technical and sociotechnical limitations. -->
61
-
62
- [More Information Needed]
63
-
64
- ### Recommendations
65
 
66
- <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
67
 
68
- Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
69
-
70
- ## How to Get Started with the Model
71
-
72
- Use the code below to get started with the model.
73
-
74
- [More Information Needed]
75
-
76
- ## Training Details
77
-
78
- ### Training Data
79
-
80
- <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
81
-
82
- [More Information Needed]
83
-
84
- ### Training Procedure
85
-
86
- <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
87
 
88
- #### Preprocessing [optional]
89
 
90
- [More Information Needed]
91
 
92
 
93
- #### Training Hyperparameters
94
 
95
- - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
96
 
97
- #### Speeds, Sizes, Times [optional]
98
 
99
- <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
- [More Information Needed]
102
 
103
  ## Evaluation
104
 
 
 
105
  <!-- This section describes the evaluation protocols and provides the results. -->
106
 
107
- ### Testing Data, Factors & Metrics
108
-
109
- #### Testing Data
110
-
111
- <!-- This should link to a Dataset Card if possible. -->
112
-
113
- [More Information Needed]
114
-
115
- #### Factors
116
-
117
- <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
118
 
119
- [More Information Needed]
120
 
121
- #### Metrics
122
 
123
- <!-- These are the evaluation metrics being used, ideally with a description of why. -->
124
 
125
- [More Information Needed]
126
 
127
- ### Results
128
-
129
- [More Information Needed]
130
-
131
- #### Summary
132
-
133
-
134
-
135
- ## Model Examination [optional]
136
-
137
- <!-- Relevant interpretability work for the model goes here -->
138
-
139
- [More Information Needed]
140
-
141
- ## Environmental Impact
142
-
143
- <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
144
-
145
- Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
146
-
147
- - **Hardware Type:** [More Information Needed]
148
- - **Hours used:** [More Information Needed]
149
- - **Cloud Provider:** [More Information Needed]
150
- - **Compute Region:** [More Information Needed]
151
- - **Carbon Emitted:** [More Information Needed]
152
-
153
- ## Technical Specifications [optional]
154
-
155
- ### Model Architecture and Objective
156
-
157
- [More Information Needed]
158
-
159
- ### Compute Infrastructure
160
-
161
- [More Information Needed]
162
-
163
- #### Hardware
164
-
165
- [More Information Needed]
166
-
167
- #### Software
168
-
169
- [More Information Needed]
170
-
171
- ## Citation [optional]
172
-
173
- <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
174
-
175
- **BibTeX:**
176
-
177
- [More Information Needed]
178
-
179
- **APA:**
180
-
181
- [More Information Needed]
182
-
183
- ## Glossary [optional]
184
-
185
- <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
186
-
187
- [More Information Needed]
188
-
189
- ## More Information [optional]
190
-
191
- [More Information Needed]
192
-
193
- ## Model Card Authors [optional]
194
-
195
- [More Information Needed]
196
 
197
- ## Model Card Contact
198
 
199
- [More Information Needed]
200
- ### Framework versions
201
 
202
- - PEFT 0.15.2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ license: apache-2.0
3
+ language:
4
+ - en
5
+ pipeline_tag: text-generation
6
+ library_name: transformers
7
  ---
8
 
9
+ # Granite 3.3 8B Instruct - Uncertainty aLoRA
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.3 8b Instruct - Uncertainty** is an Activated LoRA (aLoRA) adapter for [ibm-granite/granite-3.3-8b-instruct](https://huggingface.co/ibm-granite/granite-3.3-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.3-8b-instruct](https://huggingface.co/ibm-granite/granite-3.2-8b-instruct) model.
20
 
21
+ - **Developer:** IBM Research
22
+ - **Model type:** Activated LoRA adapter for [ibm-granite/granite-3.3-8b-instruct](https://huggingface.co/ibm-granite/granite-3.3-8b-instruct)
23
+ - **License:** [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)
24
 
25
+ ## Activated LoRA
26
+ Activated LoRA (aLoRA) is a new low rank adapter architecture that allows for reusing existing base model KV cache for more efficient inference.
27
 
28
+ [Paper](https://arxiv.org/abs/2504.12397)
29
 
30
+ [IBM Research Blogpost](https://research.ibm.com/blog/inference-friendly-aloras)
 
 
 
 
 
 
31
 
32
+ [Github - needed to run inference](https://github.com/IBM/activated-lora)
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
+ ### Model Sources
36
 
37
+ <!-- Provide the basic links for the model. -->
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
 
39
 
40
+ - **UQ method** The **Granite Uncertainty 3.3 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)
41
 
42
 
43
+ ## Usage
44
 
45
+ <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
46
 
47
+ ### Intended use
48
 
49
+ 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.
50
+
51
+
52
+ **Certainty score definition** The model will respond with a certainty percentage, quantized to 10 possible values (i.e. 5%, 15%, 25%,...95%).
53
+ 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.
54
+
55
+ **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
56
+ 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.
57
+ 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
58
+ being the correct answer to the question (though it is the appropriate one). Secondly, remember that the model
59
+ 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
60
+ 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.
61
+ Intuitively, it does this by extrapolating based on related questions
62
+ it has been evaluated on in training - this is an inherently inexact process and leads to some hedging.
63
+
64
+ **Possible downstream use cases (not implemented)**
65
+ * 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).
66
+ * 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.
67
+ * RAG: **Granite Uncertainty 3.3 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).
68
+
69
+ **Important note** Certainty is inherently an intrinsic property of a model and its abilitities. **Granite Uncertainty 3.3 8b** is not intended to predict the certainty of responses generated by any other models besides itself or [ibm-granite/granite-3.3-8b-instruct](https://huggingface.co/ibm-granite/granite-3.3-8b-instruct).
70
+ 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
71
+ red-teamed examples.
72
+
73
+ **Usage steps** There are two supported usage scenarios.
74
+
75
+ Scenario 1. Answering a question and obtaining a certainty score proceeds as follows. Given a user query written in the `user` role:
76
+
77
+ 1. Use the base model to generate a response as normal (via the `assistant` role).
78
+ 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.
79
+ 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.
80
+
81
+ Scenario 2. Predicting the certainty score from the question only, *prior* to generating an answer. Given a user query written in the `user` role:
82
+
83
+ 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.
84
+ 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.
85
+ 3. Remove the generated certainty string, and if desired, use the base model to generate a response as normal (via the `assistant` role).
86
+
87
+ ### Quickstart Example
88
+
89
+ The following code describes how to use the Granite Uncertainty model to answer questions and obtain intrinsic calibrated certainty scores. Note that no system prompt is used.
90
+
91
+ The code required for Activated LoRA is on [Github](https://github.com/IBM/activated-lora)
92
+
93
+ Prior to running the code below, either clone the repo or install as
94
+
95
+ ```
96
+ pip install git+ssh://[email protected]:IBM/activated-lora.git
97
+ ```
98
+
99
+ Note that two generation options are shown - one illustrating the KV cache reuse ability of aLoRA (faster), and another showing the simplest generation call (slower).
100
+ ```python
101
+ import torch,os
102
+ from transformers import AutoTokenizer, AutoModelForCausalLM, DynamicCache
103
+ from alora.peft_model_alora import aLoRAPeftModelForCausalLM
104
+ from alora.config import aLoraConfig
105
+ from alora.tokenize_alora import tokenize_alora
106
+
107
+ REUSE_CACHE = False
108
+
109
+ token = os.getenv("HF_MISTRAL_TOKEN")
110
+ BASE_NAME = "ibm-granite/granite-3.3-8b-instruct"
111
+ LORA_NAME = "ibm-granite/granite-3.3-8b-alora-uncertainty"
112
+ device=torch.device('cuda' if torch.cuda.is_available() else 'cpu')
113
+
114
+ # Load model
115
+ tokenizer = AutoTokenizer.from_pretrained(BASE_NAME,padding_side='left',trust_remote_code=True, token=token)
116
+ model_base = AutoModelForCausalLM.from_pretrained(BASE_NAME,device_map="auto")
117
+ model_UQ = aLoRAPeftModelForCausalLM.from_pretrained(model_base, LORA_NAME)
118
+
119
+ question = "What is IBM Research?"
120
+ print("Question:" + question)
121
+ question_chat = [
122
+ {
123
+ "role": "user",
124
+ "content": question
125
+ },
126
+ ]
127
+
128
+ # Generate answer with base model
129
+ input_text = tokenizer.apply_chat_template(question_chat,tokenize=False,add_generation_prompt=True)
130
+ # Remove default system prompt
131
+ len_sys = len(input_text.split("<|start_of_role|>user")[0])
132
+ input_text = input_text[len_sys:]
133
+
134
+ #tokenize
135
+ inputs = tokenizer(input_text, return_tensors="pt")
136
+ if REUSE_CACHE: #save KV cache for future aLoRA call
137
+ prompt_cache = DynamicCache()
138
+ with model_UQ.disable_adapter():
139
+ output_dict = model_base.generate(inputs["input_ids"].to(device), attention_mask=inputs["attention_mask"].to(device), max_new_tokens=600,past_key_values = prompt_cache, return_dict_in_generate=True)
140
+ answer_cache = output_dict.past_key_values
141
+ output = output_dict.sequences
142
+ else: #simplest call
143
+ with model_UQ.disable_adapter():
144
+ output = model_UQ.generate(inputs["input_ids"].to(device), attention_mask=inputs["attention_mask"].to(device), max_new_tokens=600)
145
+ output_text = tokenizer.decode(output[0])
146
+ answer = output_text.split("assistant<|end_of_role|>")[1]
147
+ print("Answer: " + answer)
148
+
149
+ # Generate certainty score
150
+ uq_generation_prompt = "<|start_of_role|>certainty<|end_of_role|>"
151
+ uq_chat = question_chat + [
152
+ {
153
+ "role": "assistant",
154
+ "content": answer
155
+ },
156
+ ]
157
+
158
+ uq_text = tokenizer.apply_chat_template(uq_chat,tokenize=False)
159
+ uq_text = uq_text[len_sys:]
160
+ # tokenize and generate
161
+ inputs, alora_offsets = tokenize_alora(tokenizer,uq_text, uq_generation_prompt)
162
+
163
+ if REUSE_CACHE: #reuse KV cache from earlier answer generation
164
+ output = model_UQ.generate(inputs["input_ids"].to(device), attention_mask=inputs["attention_mask"].to(device), max_new_tokens=1,alora_offsets=alora_offsets,past_key_values=answer_cache)
165
+ else: #simplest call
166
+ output = model_UQ.generate(inputs["input_ids"].to(device), attention_mask=inputs["attention_mask"].to(device), max_new_tokens=1,alora_offsets=alora_offsets)
167
+ output_text = tokenizer.decode(output[0])
168
+
169
+ # Extract score
170
+ uq_score = int(output_text[-1])
171
+ print("Certainty: " + str(5 + uq_score * 10) + "%")
172
+ ```
173
 
 
174
 
175
  ## Evaluation
176
 
177
+ 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.
178
+ 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%.
179
  <!-- This section describes the evaluation protocols and provides the results. -->
180
 
 
 
 
 
 
 
 
 
 
 
 
181
 
 
182
 
183
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/6602ffd971410cf02bf42c06/2MwP7DRZlNBtWSKWFvXOI.png)
184
 
 
185
 
 
186
 
187
+ ## Training Details
188
+ The **Granite Uncertainty 3.3 8b** model is an aLoRA 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).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
 
 
190
 
 
 
191
 
192
+ ### Training Data
193
+ The following datasets were used for calibration and/or finetuning.
194
+
195
+ * [BigBench](https://huggingface.co/datasets/tasksource/bigbench)
196
+ * [MRQA](https://huggingface.co/datasets/mrqa-workshop/mrqa)
197
+ * [newsqa](https://huggingface.co/datasets/lucadiliello/newsqa)
198
+ * [trivia_qa](https://huggingface.co/datasets/mandarjoshi/trivia_qa)
199
+ * [search_qa](https://huggingface.co/datasets/lucadiliello/searchqa)
200
+ * [openbookqa](https://huggingface.co/datasets/allenai/openbookqa)
201
+ * [web_questions](https://huggingface.co/datasets/Stanford/web_questions)
202
+ * [smiles-qa](https://huggingface.co/datasets/alxfgh/ChEMBL_Drug_Instruction_Tuning)
203
+ * [orca-math](https://huggingface.co/datasets/microsoft/orca-math-word-problems-200k)
204
+ * [ARC-Easy](https://huggingface.co/datasets/allenai/ai2_arc)
205
+ * [commonsense_qa](https://huggingface.co/datasets/tau/commonsense_qa)
206
+ * [social_i_qa](https://huggingface.co/datasets/allenai/social_i_qa)
207
+ * [super_glue](https://huggingface.co/datasets/aps/super_glue)
208
+ * [figqa](https://huggingface.co/datasets/nightingal3/fig-qa)
209
+ * [riddle_sense](https://huggingface.co/datasets/INK-USC/riddle_sense)
210
+ * [ag_news](https://huggingface.co/datasets/fancyzhx/ag_news)
211
+ * [medmcqa](https://huggingface.co/datasets/openlifescienceai/medmcqa)
212
+ * [dream](https://huggingface.co/datasets/dataset-org/dream)
213
+ * [codah](https://huggingface.co/datasets/jaredfern/codah)
214
+ * [piqa](https://huggingface.co/datasets/ybisk/piqa)
215
+
216
+
217
+
218
+ ## Model Card Authors
219
+
220
+ Kristjan Greenewald