Update README.md
Browse files
README.md
CHANGED
|
@@ -1,199 +1,145 @@
|
|
| 1 |
---
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
---
|
| 5 |
|
| 6 |
-
#
|
| 7 |
|
| 8 |
-
|
|
|
|
| 9 |
|
|
|
|
|
|
|
| 10 |
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
##
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
- **Language(s) (NLP):** [More Information Needed]
|
| 25 |
-
- **License:** [More Information Needed]
|
| 26 |
-
- **Finetuned from model [optional]:** [More Information Needed]
|
| 27 |
|
| 28 |
-
#
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
#
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
|
| 52 |
-
###
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
| 61 |
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
|
|
|
| 75 |
|
| 76 |
-
## Training Details
|
| 77 |
|
| 78 |
-
##
|
| 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 |
-
- **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]
|
|
|
|
| 1 |
---
|
| 2 |
+
license: mit
|
| 3 |
+
base_model: microsoft/Phi-3-mini-4k-instruct
|
| 4 |
+
datasets:
|
| 5 |
+
- b-mc2/sql-create-context
|
| 6 |
+
tags:
|
| 7 |
+
- peft
|
| 8 |
+
- qlora
|
| 9 |
+
- text-to-sql
|
| 10 |
+
- phi-3
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Enhanced QLoRA Adapter for Phi-3-mini: A Technical SQL Assistant (2 Epochs)
|
| 14 |
|
| 15 |
+
This repository contains an improved, high-performance QLoRA adapter for the `microsoft/Phi-3-mini-4k-instruct` model.
|
| 16 |
+
This version has been fine-tuned for **two full epochs** on a Text-to-SQL task, resulting in enhanced performance and reliability compared to single-epoch versions.
|
| 17 |
|
| 18 |
+
The model is designed to function as a technical assistant, capable of generating accurate SQL queries from natural language questions based on a provided database schema.
|
| 19 |
+
This project was developed for an engineering and deployment course, with a focus on creating a robust, reproducible, and practical AI artifact.
|
| 20 |
|
| 21 |
+
## Key Improvements in This Version
|
| 22 |
|
| 23 |
+
- **Enhanced Reliability:** Training for two epochs has significantly improved the model's ability to consistently adhere to the required chat template format, reducing parsing errors in production.
|
| 24 |
+
- **Maintained Accuracy:** The model maintains its high accuracy in generating syntactically correct and logically sound SQL queries.
|
| 25 |
+
- **Robust Loading:** The usage instructions below follow best practices to ensure reliable loading across different environments.
|
| 26 |
|
| 27 |
+
## How to Use
|
| 28 |
|
| 29 |
+
First, ensure you have a compatible environment by installing these specific library versions:
|
| 30 |
+
```bash
|
| 31 |
+
pip install transformers==4.38.2 peft==0.10.0 accelerate==0.28.0 bitsandbytes==0.43.0 torch
|
| 32 |
+
```
|
| 33 |
|
| 34 |
+
The following code provides the most robust method for loading and running inference with this adapter.
|
| 35 |
|
| 36 |
+
```python
|
| 37 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 38 |
+
from peft import PeftModel
|
| 39 |
+
import torch
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# --- 1. Configuration ---
|
| 42 |
+
base_model_id = "microsoft/Phi-3-mini-4k-instruct"
|
| 43 |
+
# IMPORTANT: Replace with your new model's ID on the Hugging Face Hub
|
| 44 |
+
adapter_id = "YourUsername/YourNewModelName"
|
| 45 |
|
| 46 |
+
# --- 2. Load the Quantized Base Model ---
|
| 47 |
+
# This is required to fit the model in memory-constrained environments like Colab
|
| 48 |
+
bnb_config = BitsAndBytesConfig(
|
| 49 |
+
load_in_4bit=True,
|
| 50 |
+
bnb_4bit_quant_type="nf4",
|
| 51 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 52 |
+
)
|
| 53 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 54 |
+
base_model_id,
|
| 55 |
+
quantization_config=bnb_config,
|
| 56 |
+
device_map="auto",
|
| 57 |
+
trust_remote_code=True,
|
| 58 |
+
)
|
| 59 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True)
|
| 60 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 61 |
|
| 62 |
+
# --- 3. Load and Apply the LoRA Adapter ---
|
| 63 |
+
model = PeftModel.from_pretrained(base_model, adapter_id)
|
| 64 |
+
print("Successfully loaded quantized base model and applied adapter.")
|
| 65 |
|
| 66 |
+
# --- 4. Prepare for Inference ---
|
| 67 |
+
context = "CREATE TABLE employees (name VARCHAR, department VARCHAR, salary INTEGER)"
|
| 68 |
+
question = "What are the names of employees in the 'Engineering' department with a salary over 80000?"
|
| 69 |
|
| 70 |
+
prompt = f"""<|user|>
|
| 71 |
+
Given the database schema:
|
| 72 |
+
{context}
|
| 73 |
|
| 74 |
+
Generate the SQL query for the following request:
|
| 75 |
+
{question}<|end|>
|
| 76 |
+
<|assistant|>
|
| 77 |
+
"""
|
| 78 |
|
| 79 |
+
# --- 5. Generate the Response ---
|
| 80 |
+
input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(model.device)
|
| 81 |
+
outputs = model.generate(input_ids=input_ids, max_new_tokens=100, do_sample=False)
|
| 82 |
+
generated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 83 |
+
generated_sql = generated_text.split("<|assistant|>")[-1].strip()
|
| 84 |
|
| 85 |
+
print(f"\nGenerated SQL: {generated_sql}")
|
| 86 |
+
# Expected output: SELECT name FROM employees WHERE department = 'Engineering' AND salary > 80000
|
| 87 |
|
| 88 |
+
```
|
| 89 |
|
| 90 |
+
## Training Procedure
|
| 91 |
+
### Dataset
|
| 92 |
|
| 93 |
+
The model was fine-tuned on a 10,000-sample subset of the b-mc2/sql-create-context dataset, split 90/10 for training and validation.
|
| 94 |
|
| 95 |
+
### Fine-tuning Configuration (QLoRA)
|
| 96 |
|
| 97 |
+
* **Quantization:** 4-bit NormalFloat (NF4) with `bfloat16` compute dtype.
|
| 98 |
+
* **LoRA Rank (`r`):** 8
|
| 99 |
+
* **LoRA Alpha (`lora_alpha`):** 16
|
| 100 |
+
* **Target Modules:** All linear layers in the Phi-3 architecture (`q_proj`, `k_proj`, `v_proj`, `o_proj`, etc.).
|
| 101 |
|
| 102 |
+
### Training Hyperparameters
|
| 103 |
|
| 104 |
+
* **Learning Rate:** 2e-4
|
| 105 |
+
* **Epochs: 2**
|
| 106 |
+
* **Effective Batch Size:** 8
|
| 107 |
+
* **Optimizer:** Paged AdamW (32-bit)
|
| 108 |
+
* **LR Scheduler:** Cosine
|
| 109 |
|
|
|
|
| 110 |
|
| 111 |
+
## Evaluation and Results
|
| 112 |
|
| 113 |
+
Qualitative evaluation on a held-out test set confirms that the model consistently generates correct SQL queries.
|
| 114 |
+
The extended training to two epochs has successfully addressed the primary limitation of the single-epoch version: inconsistent formatting.
|
| 115 |
+
This model now reliably generates the `<|assistant|>` token, making it more suitable for automated parsing and deployment.
|
| 116 |
|
| 117 |
+
## Deployment & Optimization Considerations
|
| 118 |
|
| 119 |
+
For deployment in a production environment, consider the following optimizations:
|
| 120 |
|
| 121 |
+
1. **Merge Adapter Weights:** Before deploying, merge the adapter weights into the base model to create a single, solid model.
|
| 122 |
+
This eliminates the overhead of dynamically applying the adapter during inference and can improve performance.
|
| 123 |
|
| 124 |
+
```python
|
| 125 |
+
# After loading the model and adapter:
|
| 126 |
+
merged_model = model.merge_and_unload()
|
| 127 |
+
# Use 'merged_model' for all subsequent 'generate' calls.
|
| 128 |
+
```
|
| 129 |
|
| 130 |
+
2. **Further Quantization:** For CPU-based deployment or even more efficient GPU usage, the merged model can be further quantized into formats like **GGUF** (for use with `llama.cpp`) or **AWQ/GPTQ**.
|
| 131 |
+
3. **API Serving:** Wrap the model in a high-performance web server like FastAPI or use a dedicated LLM serving framework like vLLM for optimal throughput and batching.
|
| 132 |
|
|
|
|
| 133 |
|
| 134 |
+
## Limitations and Responsible AI
|
| 135 |
|
| 136 |
+
* **Generalization:** The model is specialized for the Text-to-SQL task and the schema styles seen in its training data. It may not perform well on highly complex or esoteric SQL dialects.
|
| 137 |
+
* **Security:** This model is a proof-of-concept and has not been hardened against SQL injection attacks. All generated SQL should be treated as untrusted input and must be sanitized or executed in a sandboxed, read-only environment.
|
| 138 |
+
* **Bias:** The training data is the source of the model's knowledge. Any biases present in the sql-create-context dataset may be reflected in the model's outputs.
|
| 139 |
|
|
|
|
| 140 |
|
|
|
|
| 141 |
|
|
|
|
| 142 |
|
|
|
|
| 143 |
|
|
|
|
| 144 |
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|