Update README.md
Browse files
README.md
CHANGED
@@ -1,202 +1,242 @@
|
|
1 |
---
|
2 |
base_model: occiglot/occiglot-7b-eu5
|
3 |
library_name: peft
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
---
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
<!-- Provide a quick summary of what the model is/does. -->
|
9 |
-
|
10 |
-
|
11 |
|
12 |
## Model Details
|
13 |
|
14 |
### Model Description
|
15 |
|
16 |
-
|
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 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
57 |
|
58 |
## Bias, Risks, and Limitations
|
59 |
|
60 |
-
|
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 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
### Training Data
|
79 |
|
80 |
-
|
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 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
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 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
## Environmental Impact
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
-
|
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 |
-
|
166 |
|
167 |
#### Software
|
|
|
|
|
168 |
|
169 |
-
|
170 |
-
|
171 |
-
|
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 |
-
-
|
|
|
|
|
|
|
|
1 |
---
|
2 |
base_model: occiglot/occiglot-7b-eu5
|
3 |
library_name: peft
|
4 |
+
license: mit
|
5 |
+
datasets:
|
6 |
+
- julioc-p/Question-Sparql
|
7 |
+
language:
|
8 |
+
- en
|
9 |
+
metrics:
|
10 |
+
- f1
|
11 |
+
- precision
|
12 |
+
- recall
|
13 |
+
tags:
|
14 |
+
- code
|
15 |
+
- text-to-sparql
|
16 |
+
- sparql
|
17 |
+
- wikidata
|
18 |
+
- qlora
|
19 |
---
|
20 |
|
21 |
+
This model is a fine-tuned version of `occiglot/occiglot-7b-eu5` for generating SPARQL queries from English natural language questions, specifically targeting the Wikidata knowledge graph.
|
|
|
|
|
|
|
|
|
22 |
|
23 |
## Model Details
|
24 |
|
25 |
### Model Description
|
26 |
|
27 |
+
It was fine-tuned using QLoRA with 4-bit quantization. It takes an English natural language question and corresponding entity/relationship context as input and aims to produce a SPARQL query for Wikidata. This model is part of experiments investigating continual multilingual pre-training.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
- **Developed by:** Julio Cesar Perez Duran
|
30 |
+
- **Funded by :** DFKI
|
31 |
+
- **Model type:** Decoder-only Transformer-based language model
|
32 |
+
- **Language(s) (NLP):** en (English)
|
33 |
+
- **License:** mit
|
34 |
+
- **Finetuned from model:** `occiglot/occiglot-7b-eu5`
|
35 |
|
36 |
## Bias, Risks, and Limitations
|
37 |
|
38 |
+
- **Context Reliant:** Performance heavily relies on the provided entity/relationship context mappings.
|
39 |
+
- **Output Format:** generates extraneous text after the SPARQL query, requiring post-processing (extraction of content within ` ```sparql ... ``` ` delimiters).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
## How to Get Started with the Model
|
42 |
|
43 |
+
The following Python script provides an example of how to load the model and tokenizer to generate a SPARQL query.
|
44 |
+
|
45 |
+
```python
|
46 |
+
import torch
|
47 |
+
from transformers import AutoTokenizer, BitsAndBytesConfig
|
48 |
+
from peft import AutoPeftModelForCausalLM
|
49 |
+
import re
|
50 |
+
import json
|
51 |
+
|
52 |
+
model_id = "julioc-p/occiglot_txt_sparql_en_v2"
|
53 |
+
|
54 |
+
# Configuration for 4-bit quantization
|
55 |
+
bnb_config = BitsAndBytesConfig(
|
56 |
+
load_in_4bit=True,
|
57 |
+
bnb_4bit_quant_type="nf4",
|
58 |
+
bnb_4bit_compute_dtype=torch.float16,
|
59 |
+
bnb_4bit_use_double_quant=True,
|
60 |
+
)
|
61 |
+
|
62 |
+
|
63 |
+
model = AutoPeftModelForCausalLM.from_pretrained(
|
64 |
+
model_id,
|
65 |
+
quantization_config=bnb_config,
|
66 |
+
device_map="auto"
|
67 |
+
)
|
68 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
69 |
+
|
70 |
+
if tokenizer.pad_token is None:
|
71 |
+
tokenizer.pad_token = tokenizer.eos_token
|
72 |
+
model.config.pad_token_id = tokenizer.pad_token_id
|
73 |
+
|
74 |
+
# SPARQL extraction function
|
75 |
+
def extract_sparql(text):
|
76 |
+
code_block_match = re.search(r"```(?:sparql)?\s*(.*?)\s*```", text, re.DOTALL | re.IGNORECASE)
|
77 |
+
if code_block_match:
|
78 |
+
text_to_search = code_block_match.group(1)
|
79 |
+
else:
|
80 |
+
text_to_search = text
|
81 |
+
|
82 |
+
match = re.search(r"(SELECT|ASK|CONSTRUCT|DESCRIBE).*?\}", text_to_search, re.DOTALL | re.IGNORECASE)
|
83 |
+
if match:
|
84 |
+
return match.group(0).strip()
|
85 |
+
return ""
|
86 |
+
|
87 |
+
# --- Example usage ---
|
88 |
+
question = "Who was Barnard College's American female employee?"
|
89 |
+
example_context_json_str = '''
|
90 |
+
{
|
91 |
+
"entities": {
|
92 |
+
"Barnard College": "Q167733",
|
93 |
+
"American": "Q30",
|
94 |
+
"female": "Q6581072",
|
95 |
+
"employee": "Q5"
|
96 |
+
},
|
97 |
+
"relationships": {
|
98 |
+
"instance of": "P31",
|
99 |
+
"employer": "P108",
|
100 |
+
"gender": "P21",
|
101 |
+
"country of citizenship": "P27"
|
102 |
+
}
|
103 |
+
}
|
104 |
+
'''
|
105 |
+
# System prompt template for v2 models (English)
|
106 |
+
system_message_template = """You are an expert text to SparQL query translator. Users will ask you questions in English and you will generate a SparQL query based on the provided context encloses in ```sparql <respose_query>```.
|
107 |
+
CONTEXT:
|
108 |
+
{context}"""
|
109 |
+
|
110 |
+
# Format the system message with the actual context
|
111 |
+
formatted_system_message = system_message_template.format(context=example_context_json_str)
|
112 |
+
|
113 |
+
chat_template = [
|
114 |
+
{"role": "system", "content": formatted_system_message},
|
115 |
+
{"role": "user", "content": question},
|
116 |
+
]
|
117 |
+
|
118 |
+
inputs = tokenizer.apply_chat_template(
|
119 |
+
chat_template,
|
120 |
+
tokenize=True,
|
121 |
+
add_generation_prompt=True,
|
122 |
+
return_tensors="pt"
|
123 |
+
).to(model.device)
|
124 |
+
|
125 |
+
# Generate the output
|
126 |
+
with torch.no_grad():
|
127 |
+
outputs = model.generate(
|
128 |
+
input_ids=inputs.input_ids,
|
129 |
+
attention_mask=inputs.attention_mask,
|
130 |
+
max_new_tokens=512,
|
131 |
+
do_sample=True,
|
132 |
+
temperature=0.7,
|
133 |
+
top_p=0.9,
|
134 |
+
pad_token_id=tokenizer.pad_token_id
|
135 |
+
)
|
136 |
+
|
137 |
+
# Decode only the generated part
|
138 |
+
generated_text_full = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
139 |
+
# Extract only assistant's response
|
140 |
+
assistant_response_part = generated_text_full.split("<|im_start|>assistant")[-1].split("<|im_end|>")[0].strip()
|
141 |
+
|
142 |
+
cleaned_sparql = extract_sparql(assistant_response_part)
|
143 |
+
|
144 |
+
print(f"Question: {question}")
|
145 |
+
print(f"Context: {example_context_json_str}")
|
146 |
+
print(f"Generated SPARQL: {cleaned_sparql}")
|
147 |
+
print(f"Assistant's Raw Response: {assistant_response_part}")
|
148 |
+
|
149 |
+
```
|
150 |
|
151 |
### Training Data
|
152 |
|
153 |
+
The model was fine-tuned on a subset of the `julioc-p/Question-Sparql` dataset. 80,000 English examples, which included a `context` field containing Wikidata entity and relationship ID mappings.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
#### Training Hyperparameters
|
156 |
|
157 |
+
The following hyperparameters were used for fine-tuning:
|
158 |
+
- **LoRA Configuration:**
|
159 |
+
- `r` (LoRA rank): 256
|
160 |
+
- `lora_alpha`: 128
|
161 |
+
- `lora_dropout`: 0.05
|
162 |
+
- `target_modules`: "all-linear"
|
163 |
+
- **Training Arguments:**
|
164 |
+
- `num_train_epochs`: 3
|
165 |
+
- Effective batch size: 6
|
166 |
+
- `optim`: "adamw_torch_fused"
|
167 |
+
- `learning_rate`: 2e-4
|
168 |
+
- `fp16`: True
|
169 |
+
- `max_grad_norm`: 0.3
|
170 |
+
- `warmup_ratio`: 0.03
|
171 |
+
- `lr_scheduler_type`: "constant"
|
172 |
+
- `packing`: True
|
173 |
+
- NEFTune `noise_alpha`: 5
|
174 |
+
- **BitsAndBytesConfig:**
|
175 |
+
- `load_in_4bit`: True
|
176 |
+
- `bnb_4bit_quant_type`: "nf4"
|
177 |
+
- `bnb_4bit_compute_dtype`: `torch.float16`
|
178 |
+
- `bnb_4bit_use_double_quant`: True
|
179 |
+
|
180 |
+
#### Speeds, Sizes, Times
|
181 |
+
- Training took approx. 8 hours on a single NVIDIA V100 GPU.
|
182 |
|
183 |
## Evaluation
|
184 |
|
|
|
|
|
185 |
### Testing Data, Factors & Metrics
|
186 |
|
187 |
#### Testing Data
|
188 |
+
1. **QALD-10 test set (English):** Standardized benchmark. 394 English questions were evaluated for this model.
|
189 |
+
2. **v2 Test Set (English):** 10,000 English held-out examples from the `julioc-p/Question-Sparql` dataset, including context.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
|
191 |
#### Metrics
|
192 |
+
QALD standard macro-averaged F1-score, Precision, and Recall. Non-executable queries result in P, R, F1 = 0.
|
|
|
|
|
|
|
193 |
|
194 |
### Results
|
195 |
|
196 |
+
**On QALD-10 (English, N=394):**
|
197 |
+
- **Macro F1-Score:** 0.1906
|
198 |
+
- **Macro Precision:** 0.5890
|
199 |
+
- **Macro Recall:** 0.1943
|
200 |
+
- **Executable Queries:** 99.24% (391/394)
|
201 |
+
- **Correctness (Exact Match + Both Empty):** 18.27% (72/394)
|
202 |
+
- Correct (Exact Match): 16.75% (66/394)
|
203 |
+
- Correct (Both Empty): 1.52% (6/394)
|
204 |
+
|
205 |
+
**On v2 Test Set (English, N=10000):**
|
206 |
+
- **Macro F1-Score:** 0.8051
|
207 |
+
- **Macro Precision:** 0.8906
|
208 |
+
- **Macro Recall:** 0.8057
|
209 |
+
- **Executable Queries:** 99.71% (9971/10000)
|
210 |
+
- **Correctness (Exact Match + Both Empty):** 80.39% (8039/10000)
|
211 |
+
- Correct (Exact Match): 72.29% (7229/10000)
|
212 |
+
- Correct (Both Empty): 8.10% (810/10000)
|
213 |
|
214 |
## Environmental Impact
|
215 |
+
- **Hardware Type:** 1 x NVIDIA V100 32GB GPU
|
216 |
+
- **Hours used:** Approx. 8 hours for fine-tuning.
|
217 |
+
- **Cloud Provider:** DFKI HPC Cluster
|
218 |
+
- **Compute Region:** Germany
|
219 |
+
- **Carbon Emitted:** Approx. 0.30 kg CO2eq.
|
220 |
|
221 |
+
## Technical Specifications
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
|
223 |
### Compute Infrastructure
|
224 |
|
|
|
|
|
225 |
#### Hardware
|
226 |
+
- NVIDIA V100 GPU (32 GB RAM)
|
227 |
+
- Approx. 60 GB system RAM
|
228 |
|
229 |
#### Software
|
230 |
+
- Slurm, NVIDIA Enroot, CUDA 11.8.0
|
231 |
+
- Python, Hugging Face `transformers`, `peft` (0.13.2), `bitsandbytes`, `trl`, PyTorch.
|
232 |
|
233 |
+
## More Information
|
234 |
+
- **Thesis GitHub:** [https://github.com/julioc-p/cross-lingual-transferability-thesis](https://github.com/julioc-p/cross-lingual-transferability-thesis)
|
235 |
+
- **Dataset:** [https://huggingface.co/datasets/julioc-p/Question-Sparql](https://huggingface.co/datasets/julioc-p/Question-Sparql)
|
236 |
+
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
237 |
### Framework versions
|
238 |
+
- PEFT 0.13.2
|
239 |
+
- Transformers (`4.39.3`)
|
240 |
+
- BitsAndBytes (`0.43.0`)
|
241 |
+
- trl (`0.8.6`)
|
242 |
+
- PyTorch (`torch==2.1.0`)
|